您的位置:首页 > 其它

直方图均衡化源码

2016-04-02 10:50 316 查看
#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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: