直方图均衡化,有缺陷:
http://blog.csdn.net/guo8113/article/details/25193611
只有灰度图,不是三个通道
http://blog.csdn.net/wormtree/article/details/44151015
int main(int argc, char** argv)
{
//载入图片
int i=0, j=0, temp=0;
IplImage * img = cvLoadImage("image4.jpg", CV_LOAD_IMAGE_UNCHANGED);//图片路径
int height = img->height;
int width = img->width;
int step = img->widthStep;
printf("width:%d setp:%d\n",width,step);
uchar *data = (uchar*)img->imageData;
float size = height*width;
//直方图
unsigned int histr[256] = {0};
unsigned int histg[256] = {0};
unsigned int histb[256] = {0};
for (i=0; ii++)
{
for (j=0; jj+=3)
{
temp = data[i*step+j];
histr[temp]++;
temp = data[i*step+j+1];
histg[temp]++;
temp = data[i*step+j+2];
histb[temp]++;
}
}
//归一化直方图
float histPDFr[256] = {0};
float histPDFg[256] = {0};
float histPDFb[256] = {0};
for (i=0; i<255; i++)
{
histPDFr[i]=(float)histr[i]/size;
histPDFg[i]=(float)histg[i]/size;
histPDFb[i]=(float)histb[i]/size;
}
//累积直方图
float histCDF[256] = {0};
int histEQUr[256] = {0};
int histEQUg[256] = {0};
int histEQUb[256] = {0};
for (i=0; i<256; i++)
{
if (0==i) histCDF[i] = histPDFr[i];
else histCDF[i] = histCDF[i-1] + histPDFr[i];
//直方图均衡化,映射
histEQUr[i] = (int)(255.0 * histCDF[i] + 0.5);
}
for (i=0; i<256; i++)
{
if (0==i) histCDF[i] = histPDFg[i];
else histCDF[i] = histCDF[i-1] + histPDFg[i];
histEQUg[i] = (int)(255.0 * histCDF[i] + 0.5);
}
for (i=0; i<256; i++)
{
if (0==i) histCDF[i] = histPDFb[i];
else histCDF[i] = histCDF[i-1] + histPDFb[i];
histEQUb[i] = (int)(255.0 * histCDF[i] + 0.5);
}
for (i=0; ii++)
{
for (j=0; jj+=3)
{
temp = data[i*step+j];
data[i*step+j] = histEQUr[temp];
temp = data[i*step+j+1];
data[i*step+j+1] = histEQUg[temp];
temp = data[i*step+j+2];
data[i*step+j+2] = histEQUb[temp];
}
}
cvNamedWindow("demo", CV_WINDOW_AUTOSIZE);
cvShowImage("demo", img);
cvWaitKey(0);
cvDestroyWindow("demo");
cvReleaseImage(&img);
return 0;
}