[OpenCV] Draw 1D Histogram Graph


/ Published in: C++
Save to your folder(s)

繪製 1D vector 成 1D Histogram


Copy this code and paste it in your HTML
  1. #include <vector>
  2. #include <algorithm>
  3. #include <numeric>
  4. #include <iterator>
  5.  
  6. vector<int> hist(256, 0);
  7.  
  8. // fill data values into hist
  9. ...
  10.  
  11. // Plot 1D Histogram
  12. IplImage* imgHistogram = cvCreateImage(cvSize(256, 50), 8, 1);
  13.  
  14. cvRectangle(imgHistogram, cvPoint(0,0),cvPoint(256,50),CV_RGB(255,255,255),-1);
  15. int max_value = *(max_element(hist.begin(), hist.end()));
  16.  
  17. for (int i = 0; i < 256; ++i) {
  18. int val = hist[i];
  19. int nor = cvRound(val * 50 / max_value);
  20. cvLine(imgHistogram, cvPoint(i, 50), cvPoint(i, 50-nor), CV_RGB(0,0,0));
  21. }
  22.  
  23. cvShowImage("hist", imgHistogram);
  24.  
  25. cvReleaseImage(&imgHistogram);

URL: http://blog.isaias.com.mx/2005/12/1d-histogram-with-opencv-c.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.