用C语言编写灰度图像直方图变换增强
发布网友
发布时间:2022-05-16 13:28
我来回答
共1个回答
热心网友
时间:2023-10-23 19:28
参考一下这个程序:
#define IMAGEWIDTH 80
#define IMAGEHEIGHT 80
void Histogram(unsigned char *pImage,int nWidth,int nHeight,float fHisto[256]);
void Enhance(unsigned char *pImage,unsigned char *pImage1,int nWidth,int nHeight,float fHisto[256],float fHisto1[256]);
unsigned char dbImage[IMAGEWIDTH*IMAGEHEIGHT];
unsigned char dbTargetImage[IMAGEWIDTH*IMAGEHEIGHT];
float fHistogram[256],fHistogram1[256];
unsigned char lut[256];
/* 直方图统计程序 */
int main()
{
... //准备一幅灰度图片到dbImage
Histogram(dbImage,IMAGEWIDTH,IMAGEHEIGHT,fHistogram); //做直方图统计
Enhance(dbImage,dbTargetImage,IMAGEWIDTH,IMAGEHEIGHT,fHistogram,fHistogram1); //直方图增强输出到dbTargetImage
...//完成后显示
}
void Histogram(unsigned char *pImage,int nWidth,int nHeight,float fHisto[256])
{
int i,j;
unsigned int uWork;
unsigned char *pWork;
for ( i=0;i<256;i++ ) fHisto[i]=0.0f;
pWork=pImage;
for ( i=0;i<nHeight;i++ )
{
for ( j=0;j<nWidth;j++,pWork++ )
{
uWork=(unsigned int)(*pWork);
fHisto[uWork]++;
}
}
uWork=nWidth*nHeight;
for ( i=0;i<256;i++ )
{
fHisto[i]/=uWork;
fHisto[i]*=100;
}
}
void Enhance(unsigned char *pImage,unsigned char *pImage1,int nWidth,int nHeight,float fHisto[256],float fHisto1[256])
{
int i,j;
unsigned int uWork;
unsigned char *pWork,*pWork1;
for ( i=0;i<256;i++ )
fHisto1[i]=fHisto[i]/100;
for ( i=1;i<256;i++ )
fHisto1[i]+=fHisto1[i-1];
for ( i=0;i<256;i++ )
lut[i]=fHisto1[i]*256;
for ( i=0;i<256;i++ )
if ( lut[i]>=256 )
lut[i]=255;
pWork=pImage; pWork1=pImage1;
for ( i=0;i<nHeight;i++ )
for ( j=0;j<nWidth;j++,pWork++,pWork1++ )
(*pWork1)=lut[(*pWork)];
}来自:求助得到的回答