歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

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<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;
}

實驗結果:

更多詳情見請繼續閱讀下一頁的精彩內容: http://www.linuxidc.com/Linux/2013-10/91706p2.htm

Copyright © Linux教程網 All Rights Reserved