直方图均衡化源码

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值