灰度直方图均衡化

 #include"iostream" using namespace std; #include<cv.h> #include<cxcore.h> #include<highgui.h> int main() { IplImage *cvimage,*pgray,*temp_pgray; cvimage = cvLoadImage(".//photo//6.jpg"); pgray = cvCreateImage(cvGetSize(cvimage),IPL_DEPTH_8U,1); //强制转化到灰度图 temp_pgray = cvCreateImage(cvGetSize(cvimage),IPL_DEPTH_8U,1); cvCvtColor(cvimage,pgray,CV_RGB2GRAY); cvCvtColor(cvimage,temp_pgray,CV_RGB2GRAY); /*整体增加亮度的实验*/ /*cout<<pgray->width<<endl; cout<<pgray->height<<endl; cout<<pgray->widthStep<<endl; for(int y=0;y<pgray->height;y++) { for(int x=0;x<pgray->width;x++) { int temp = (int) ((uchar*)(pgray->imageData + pgray->widthStep*y))[x]; temp += 30; if(temp>255) temp = 255; ((uchar*)(pgray->imageData + pgray->widthStep*y))[x] = temp; //cout<<(int) ((uchar*)(pgray->imageData + pgray->widthStep*y))[x]<<" "; } cout<<endl; }*/ /*************************************************/ /*直方图均衡化*/ int rec[256] = {0}; /*记录每个灰度的个数*/ for(int y=0;y<pgray->height;y++) { for(int x=0;x<pgray->width;x++) { int temp = (int) ((uchar*)(pgray->imageData + pgray->widthStep*y))[x]; rec[temp]++; } } for(int i=1;i<255;i++) //记下统计分布函数的值 rec[i] += rec[i-1]; int least = 0; while( rec[least] == 0 ) //最小分布函数对应的灰度 least++; double allpoint; allpoint = (pgray->width) * (pgray->height); allpoint -= rec[least]; for(int y=0;y<pgray->height;y++) { for(int x=0;x<pgray->width;x++) { int temp = (int) ((uchar*)(pgray->imageData + pgray->widthStep*y))[x]; temp = (rec[temp] - rec[least]) * 255/allpoint; ((uchar*)(pgray->imageData + pgray->widthStep*y))[x] = temp; } } cvNamedWindow("source",1); cvNamedWindow("gray",1); cvNamedWindow("temp_pgray",1); cvShowImage("source",cvimage); cvShowImage("gray",pgray); cvShowImage("temp_pgray",temp_pgray); cvWaitKey(); cvDestroyWindow("source"); cvDestroyWindow("gray"); cvDestroyWindow("temp_pgray"); cvReleaseImage(&cvimage); cvReleaseImage(&pgray); cvReleaseImage(&temp_pgray); return 1; }

 

直方图均衡化认识:

1.公式见维基:

http://en.wikipedia.org/wiki/Histogram_equalization

2.完成的目的:

每一个灰度级的像素的数目基本相同。

3.方法思路:

根据当前不大于这个灰度级的像素点的个数来决定平衡化以后这个点相应的灰度级。

你可能感兴趣的:(include,iostream)