#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <windows.h>
#include <memory.h>
#include <iostream.h>
BYTE *RmwRead8BitBmpFile2Img(const char * filename,int *width,int *height)
{ FILE * BinFile;
BITMAPFILEHEADER FileHeader;
BITMAPINFOHEADER BmpHeader;
BYTE *img;
int size;
int Suc=1;
// Open File
*width=*height=0;
if((BinFile=fopen(filename,"rb"))==NULL) return NULL;
// Read Struct Info
if (fread((void *)&FileHeader,1,sizeof(FileHeader),BinFile)!=sizeof(FileHeader)) Suc=-1;
if (fread((void *)&BmpHeader,1,sizeof(BmpHeader),BinFile)!=sizeof(BmpHeader)) Suc=-1;
if (Suc==-1) { fclose(BinFile); return NULL; }
// Read Image Data
*width=(BmpHeader.biWidth+3)/4*4;
*height=BmpHeader.biHeight;
size=(BmpHeader.biWidth+3)/4*4*BmpHeader.biHeight;
fseek(BinFile,FileHeader.bfOffBits,SEEK_SET);
if ( (img=new BYTE[size])!=NULL)
{ if(fread(img,sizeof(BYTE),size,BinFile)!=size)
{ fclose(BinFile);
delete img;
img=NULL;
return NULL;
}
}
fclose(BinFile);
return img;
}
int RmwWrite8BitImg2BmpFile(BYTE *img,int width,int height,const char * filename)
{ FILE * BinFile;
BITMAPFILEHEADER FileHeader;
BITMAPINFOHEADER BmpHeader;
BYTE p[4];
int i,Suc=1;
// Open File
if((BinFile=fopen(filename,"w+b"))==NULL) { return -1; }
// Fill the FileHeader
FileHeader.bfType= ((WORD) ('M' << 8) | 'B');
FileHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BmpHeader)+256*4L;
FileHeader.bfSize=FileHeader.bfOffBits+width*height ;
FileHeader.bfReserved1=0;
FileHeader.bfReserved2=0;
if (fwrite((void *)&FileHeader,1,sizeof(FileHeader),BinFile)!=sizeof(FileHeader)) Suc=-1;
// Fill the ImgHeader
BmpHeader.biSize = 40;
BmpHeader.biWidth = width;
BmpHeader.biHeight = height;
BmpHeader.biPlanes = 1 ;
BmpHeader.biBitCount = 8 ;
BmpHeader.biCompression = 0 ;
BmpHeader.biSizeImage = 0 ;
BmpHeader.biXPelsPerMeter = 0;
BmpHeader.biYPelsPerMeter = 0;
BmpHeader.biClrUsed = 0;
BmpHeader.biClrImportant = 0;
if (fwrite((void *)&BmpHeader,1,sizeof(BmpHeader),BinFile)!=sizeof(BmpHeader)) Suc=-1;
// write Pallete
for (i=0,p[3]=0;i<256;i++)
{ p[0]=p[1]=p[2]=i; // blue,green,red;
if (fwrite((void *)p,1,4,BinFile)!=4) { Suc=-1; break; }
}
// write image data
if (fwrite((void *)img,1,width*height,BinFile)!=width*height) Suc=-1;
// return;
fclose(BinFile);
return Suc;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
void TestHistogramEqualize(BYTE *pImg,int width,int height) //文件名pImg
{
BYTE *pCur,*pEnd=pImg+width*height;
double *temp = new double [width*height];
double *r = new double [width*height];
double *relation = new double [width*height];
double *final = new double [width*height] ;
int i,j,h;
double min=255,max=0;
for(i=0;i<height;i++){
for(j=0;j<width;j++){
temp[i*width+j]=128;
r[i*width+j]=128;
relation[i*width+j]=0;
}
}
pCur=pImg;
for(i=0;i<height;i++){
for(j=0;j<width;j++){
temp[i*width+j]=*pCur; //导入原始图像信息
pCur++;
}
}
for(h=height/2;h>0;){
for(i=0;i<height;i++){
for(j=0;j<width;j++){
if(i+h<=height){
relation[i*width+j]=log(temp[(i+h)*width+j])-log(temp[i*width+j]);
if(relation[i*width+j]>-9&&relation[i*width+j]<9){
r[i*width+j]=r[i*width+j]-relation[i*width+j];
r[(i+h)*width+j]=r[(i+h)*width+j]+relation[i*width+j];
}
}
}
}
h/=2;
}
for(int l=width/2;l>0;){
for(i=0;i<height;i++){
for(j=0;j<width;j++){
if(j+l<=width){
relation[i*width+j]=log(temp[i*width+j+l])-log(temp[i*width+j]);
if(relation[i*width+j]>-9&&relation[i*width+j]<9){
r[i*width+j]=r[i*width+j]-relation[i*width+j];
r[i*width+j+l]=r[i*width+j+l]+relation[i*width+j];
}
}
}
}
l/=2;
}
for(i=0;i<height;i++){
for(j=0;j<width;j++){
if(min>r[i*width+j])
min=r[i*width+j];
if(max<r[i*width+j])
max=r[i*width+j];
}
}
for(i=0;i<height;i++){
for(j=0;j<width;j++){
final[i*width+j]=((r[i*width+j]-min)/(max-min))*255;
}
}
pCur=pImg;
for(i=0;i<height;i++){
for(j=0;j<width;j++){
*pCur=final[i*width+j];
pCur++;
}
}
delete final;
delete r;
delete relation;
delete temp;
return;
}
void main()
{
BYTE *pOrgImg;
int width,height;
pOrgImg=RmwRead8BitBmpFile2Img("test.bmp",&width,&height);
if (pOrgImg==NULL) { printf("*fopen err!\n"); return; }
TestHistogramEqualize(pOrgImg,width,height);
RmwWrite8BitImg2BmpFile(pOrgImg,width,height,"222.bmp");
delete pOrgImg;
return;
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页