OpenCV 绘制直方图


和这一篇《OpenCV 数字图像直方图》  内容是一样的,只是使用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<bins;i++)
 {
  float bin_val = hist.at<float>(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;
}

实验结果:

更多详情见请继续阅读下一页的精彩内容

推荐阅读:

Ubuntu 12.04 安装 OpenCV2.4.2

CentOS下OpenCV无法读取视频文件

Ubuntu 12.04下安装OpenCV 2.4.5总结

Ubuntu 10.04中安装OpenCv2.1九步曲

基于QT和OpenCV的人脸识别系统

  • 1
  • 2
  • 3
  • 下一页

相关内容