首页 > 其他 > 详细

画出直方图

时间:2021-06-04 22:47:21      阅读:31      评论:0      收藏:0      [点我收藏+]
 1 // 画出直方图分布
 2 void saveHistogram(CvHistogram* hist)
 3 {
 4     if(!hist) return;
 5     if(hist->mat.dim->size<=0) return;
 6     int scale = 2;
 7     int hist_height = 300;
 8     IplImage* hist_image = cvCreateImage(cvSize(hist->mat.dim->size*scale,hist_height),8,3);
 9     cvZero(hist_image);
10  
11     float max_value = 0;
12     cvGetMinMaxHistValue(hist, 0,&max_value,0,0);
13     for(int i=0;i<hist->mat.dim->size;i++)
14     {
15         float bin_val = cvQueryHistValue_1D(hist,i);
16         int intensity = cvRound(bin_val*(hist_height-50)/max_value);
17         // cvRectangle  cvLine
18         cvRectangle(
19             hist_image,
20             cvPoint(i*scale,hist_height-1),
21             cvPoint((i+1)*scale - 1, hist_height - intensity),
22             CV_RGB(255,255,255));
23     }
24     cvNamedWindow("hist_image");
25     cvShowImage("hist_image",hist_image);
26     cvWaitKey(0);
27 }
28 
29 
30 // 直方图获取与显示
31 CvHistogram* Histogram_Image(IplImage *image)
32 {
33     int hist_size=256;
34     float range[]={0,255};
35     float * ranges[]={range};
36     IplImage * gray_plane = cvCreateImage(cvGetSize(image),8,1);
37     cvCvtColor(image,gray_plane,CV_BGR2GRAY);
38     CvHistogram* gray_hist = cvCreateHist(1,&hist_size,CV_HIST_ARRAY,ranges,1);
39     cvCalcHist(&gray_plane,gray_hist,0,0);
40     return gray_hist;
41 }

技术分享图片

技术分享图片

2021-06-04

画出直方图

原文:https://www.cnblogs.com/2015-16/p/14851171.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!