OpenCV 绘制直方图

和这一篇《OpenCV 数字图像直方图》 http://www.linuxidc.com/Linux/2013-10/91707.htm 内容是一样的,只是使用Mat格式实现~

绘制灰色直方图

//绘制灰度直方图
int main(  )
{
 Mat src,gray;
 src=imread(“baboon.jpg”);
 cvtColor(src,gray,CV_RGB2GRAY);
 int bins = 256;
 int hist_size[] = {bins};
 float range[] = { 0, 256 };
 const float* ranges[] = { range};
 MatND hist;
 int channels[] = {0};

 calcHist( &gray, 1, channels, Mat(), // do not use mask
  hist, 1, hist_size, ranges,
  true, // the histogram is uniform
  false );

 double max_val;
 minMaxLoc(hist, 0, &max_val, 0, 0);
 int scale = 2;
 int hist_height=256;
 Mat hist_img = Mat::zeros(hist_height,bins*scale, CV_8UC3);
 for(int i=0;i {
  float bin_val = hist.at(i);
  int intensity = cvRound(bin_val*hist_height/max_val);  //要绘制的高度
  rectangle(hist_img,Point(i*scale,hist_height-1),
   Point((i+1)*scale – 1, hist_height – intensity),
   CV_RGB(255,255,255));
 }
 imshow( “Source”, src );
 imshow( “Gray Histogram”, hist_img );
 waitKey(10000000000);
 return 0;
}

实验结果:

更多详情见请继续阅读下一页的精彩内容http://www.linuxidc.com/Linux/2013-10/91706p2.htm

推荐阅读

Ubuntu 12.04 安装 OpenCV2.4.2 http://www.linuxidc.com/Linux/2012-09/70158.htm

CentOS下OpenCV无法读取视频文件 http://www.linuxidc.com/Linux/2011-07/39295.htm

Ubuntu 12.04下安装OpenCV 2.4.5总结 http://www.linuxidc.com/Linux/2013-06/86704.htm

Ubuntu 10.04中安装OpenCv2.1九步曲 http://www.linuxidc.com/Linux/2

Add a Comment

电子邮件地址不会被公开。 必填项已用*标注