很多时候,我们对图像处理需要对图片进行分析,要分析图像数据分布情况,使用直方图分析是一个很好的选择。
下面是本人使用opencv自己写的图片数据直方图,没有使用opencv提供的hist函数,这里数据是显示图片像素值概率分布情况,代码如下:
void imgproc::showHistogarm(IplImage* src,IplImage *dst)
/*****************************
author:chengtianqing123
showHistogarm
打印图片直方图数据
src
源图片
dst
输出图片(256*256)
*****************************/
{
assert(src!=NULL);
int num[256]={0};//数据统计
double p[256]={0};//数据概率分布
int max=0;
int h=src->height;
int w=src->width;
for(int i=0;i<h;i++)
{
for(int j=0;j<w;j++)
{
int piexl=((uchar*)(src->imageData+src->widthStep*i))[j];
num[piexl]++;
if(num[piexl]>max)//获取最高概率像素值
{
max=num[piexl];
}
}
}
for(int i=0;i<256;i++)//归一化处理
{
//cout<<num[i]<<" ";
p[i]=num[i]*1.0/max;
}
cvSet(dst,cvScalar(255,255,255),0);
for(int i=0;i<256;i++)
{
CvPoint pt1=cvPoint(i,0);
CvPoint pt2=cvPoint((i+1),p[i]*256);//打印图片高为256
cvRectangle(dst,pt1,pt2,cvScalar(0,255,0));
}
}