直方图均衡化源码
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;
}
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;
}
相关文章推荐
- rpm包管理以及前端工具yum
- 蓝鲸python第一题个人理解运行版
- 让数组中奇数位于偶数前面
- 码农小汪之-线程的基本概念
- 想成为更优秀的程序员?这8个方法帮你忙
- 5-2 然后是几点 (15分)
- 直方图均衡化
- leetcode 283. Move Zeroes
- HDU 1875 畅通工程再续 最小生成树
- 【JQuery】使用wrap()和wrapInner()方法包裹元素和内容
- HDU 1875 畅通工程再续 最小生成树
- C语言18个经典问题答录
- fragment的使用
- 前端代码的一些基本规范使用
- Android 消息循环机制源码分析
- Nagle算法
- [Android实例] 推荐给你们一个好用的ListView、RecyclerView适配器
- Android 6.0 wif的变动
- 【笔试/面试】数组及其内存结构
- linux中的热插拔和mdev机制