#include<stdio.h>
#include<iostream.h>
#include<windows.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
BYTE * SSLRead8BitBmpFile2Img(const char *filename,int *width,int *height)
{
//读8Bit灰度图像
FILE *BinFile;
BITMAPFILEHEADER FileHeader;
BITMAPINFOHEADER BmpHeader;
BYTE *pImg;
unsigned int size;
int suc=1,w,h;
//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)||(FileHeader.bfOffBits<sizeof(FileHeader)+sizeof(BmpHeader)))
{
fclose(BinFile);
return NULL;
}
//read image data
* width=w=(BmpHeader.biWidth+3)/4*4;
* height=h=BmpHeader.biHeight;
size=(BmpHeader.biWidth+3)/4*4*BmpHeader.biHeight;
fseek(BinFile,FileHeader.bfOffBits,SEEK_SET);
if((pImg=new BYTE[size])!=NULL)
{
for(int i=0;i<h;i++)
{
if(fread(pImg+(h-1-i)*w,sizeof(BYTE),w,BinFile)!=w)
{
fclose(BinFile);
delete pImg;
pImg=NULL;
return NULL;
}
}
}
fclose(BinFile);
return pImg;
}
bool SSLWrite8BitImg2BmpFile(BYTE *pImg,int width,int height,const char * filename)
{
FILE *BinFile;
BITMAPFILEHEADER FileHeader;
BITMAPINFOHEADER BmpHeader;
int i,extend;
bool suc=true;
BYTE p[4],*pCur;
//creat file
if((BinFile=fopen(filename,"w+b"))==NULL)
{
return false;
}
//fill the fileheader
FileHeader.bfType=((WORD)('M'<<8)|'B');
FileHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+256*4L;
FileHeader.bfReserved1=0;
FileHeader.bfReserved2=0;
if(fwrite((void *)&FileHeader,1,sizeof(FileHeader),BinFile)!=sizeof(FileHeader))
suc=false;
//fill the imgheader
BmpHeader.biSize=40;
BmpHeader.biHeight=height;
BmpHeader.biWidth=width;
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=false;
//write pallete
for(i=0,p[3]=0;i<256;i++)
{
p[3]=0;
p[0]=p[1]=p[2]=i;
if(fwrite((void *)p,1,4,BinFile)!=4)
{
suc=false;
break;
}
}
//write image data
extend=(width+3)/4*4-width;
if(extend==0)
{
for(pCur=pImg+(height-1)*width;pCur>=pImg;pCur-=width)
{
if(fwrite((void *)pCur,1,width,BinFile)!=(unsigned int)width)
suc=false;
}
}
else
{
for(pCur=pImg+(height-1)*width;pCur>=pImg;pCur-=width)
{
if(fwrite((void *)pCur,1,width,BinFile)!=(unsigned int )width)
suc=false;
for(i=0;i<extend;i++)
{
if(fwrite((void *)(pCur+width-1),1,1,BinFile)!=1)
suc=false;
}
}
}
fclose(BinFile);
return suc;
}
//彩色图像到灰度图像转换
void SSLRGBBImg0(BYTE *pRGBImg,int width,int height,BYTE *pGryImg)
{
BYTE *pRGB,*pGry,*pEnd=pRGBImg+3*width*height;
double gry;
for(pRGB=pRGBImg,pGry=pGryImg;pRGB<pEnd;)
{
gry=*(pRGB++)*0.114;
gry+=*(pRGB++)*0.587;
gry+=*(pRGB++)*0.229;
*(pGry++)=(int)(gry);
}
return;
}
//直方图的构造
void SSLGetHistogram(BYTE *pImg,int width,int height,int *histogram)
{
BYTE *pCur,*pEnd;
for(int i=0;i<256;i++)
{
for(pCur=pImg,pEnd=pImg+width*height;pCur<pEnd;)
{
histogram[(*pCur++)]++;
return;
}
}
}
//图像的亮度和对比度
void SSLCalculateImageBrightnessAndContrast(int *histogram,double *brightness,double *contrast)
{
int g,sumGry,imgsize,mean;
for(sumGry=imgsize=0,g=0;g<256;g++)
{
sumGry+=histogram[g]*g;
imgsize+=histogram[g];
}
*brightness=1.0*(sumGry/imgsize);
mean=int(*brightness);
for(sumGry=0,g=0;g<256;g++)
{
sumGry+=histogram[g]*(g-mean)*(g-mean);
}
*contrast=sqrt(sumGry/imgsize);
return;
}
//图像的最大亮度
void SSLImageMaxGry(int *histogram,int *maxGry)
{
for(*maxGry=255;*maxGry>=0;(*maxGry)--)
{
if(histogram[*maxGry])
break;
}
return;
}
//图像的最小亮度
void SSLImageMinGry(int *histogram,int *minGry)
{
for(*minGry=0;*minGry<256;(*minGry)++)
{
if(histogram[*minGry])
break;
}
return;
}
//图像的亮度中值
void SSLImageMinGry(int *histogram,int width,int height,int *medGry)
{
int sum;
for(sum=0,*medGry=0;*medGry<256;*medGry++)
{
sum+=histogram[*medGry];
if(sum*2>width*height)
break;
}
return;
}
//直方图均衡化
void SSLHistogramEqualize(BYTE *pImg,int width,int height)
{
BYTE *pCur,*pEnd=pImg+width*height;
unsigned int hist[256];
int LUT[256],i,sum;
memset(hist,0,sizeof(int)*256);
for(pCur=pImg;pCur<pEnd;)
hist[*(pCur++)]++;
for(sum=hist[0],LUT[0]=0,i=1;i<256;i++)
{
sum=sum+hist[i];
LUT[i]=255*sum/(width*height);
}
for(pCur=pImg;pCur<pEnd;)
*(pCur++)=LUT[*pCur];
return;
}
//图像边界处理
void SSLImageBoundary(BYTE *pImg,int width,int height,BYTE Color)
{
BYTE *pCur1,*pCur2;
int x,y;
for(x=0,pCur1=pImg,pCur2=pImg+(height-1L)*width;x<width;x++)
{
*(pCur1++)=*(pCur2++)=Color;
}
for(y=0,pCur2=pImg,pCur2=pImg+width-1;y<height;y++,pCur1+=width,pCur2+=width)
{
*pCur1=*pCur2=Color;
}
}
#ifdef test1
//Gradient算子
void SSLGradientGrayImage(BYTE *pGryImg,int width,int height,BYTE *pGrImg)
{
int i,j,G1,x,y;
BYTE *pGry,*pGr,*pCurGry,*pCurGr;
x=width-1;
y=height-1;
// SSLSetImageBoundary(pGrImg,width,height,0);
}
#endif
//Robert算子
//Sobel算子
void main()
{
BYTE *pRGBImg,*pImg,*pGryImg;
int width,height;
pRGBImg=SSLRead8BitBmpFile2Img("sun.bmp",&width,&height);
pGryImg = new BYTE[width*height];
SSLRGBBImg0(pRGBImg,width,height,pGryImg);
SSLWrite8BitImg2BmpFile(pGryImg,width,height,"sun1.bmp");
return;
}
评论0