【OpenCV】绘制直方图

申明,本文非笔者原创,原文转载自:http://blog.csdn.net/xiaowei_cqu/article/details/8833799


和这一篇《数字图像直方图》内容是一样的,只是使用Mat格式实现~


绘制灰色直方图

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //绘制灰度直方图  
  2. int main(  )  
  3. {  
  4.     Mat src,gray;  
  5.     src=imread("baboon.jpg");  
  6.     cvtColor(src,gray,CV_RGB2GRAY);  
  7.     int bins = 256;  
  8.     int hist_size[] = {bins};  
  9.     float range[] = { 0, 256 };  
  10.     const float* ranges[] = { range};  
  11.     MatND hist;  
  12.     int channels[] = {0};  
  13.   
  14.     calcHist( &gray, 1, channels, Mat(), // do not use mask  
  15.         hist, 1, hist_size, ranges,  
  16.         true// the histogram is uniform  
  17.         false );  
  18.   
  19.     double max_val;  
  20.     minMaxLoc(hist, 0, &max_val, 0, 0);  
  21.     int scale = 2;  
  22.     int hist_height=256;  
  23.     Mat hist_img = Mat::zeros(hist_height,bins*scale, CV_8UC3);  
  24.     for(int i=0;i<bins;i++)  
  25.     {  
  26.         float bin_val = hist.at<float>(i);   
  27.         int intensity = cvRound(bin_val*hist_height/max_val);  //要绘制的高度  
  28.         rectangle(hist_img,Point(i*scale,hist_height-1),  
  29.             Point((i+1)*scale - 1, hist_height - intensity),  
  30.             CV_RGB(255,255,255));  
  31.     }  
  32.     imshow( "Source", src );  
  33.     imshow( "Gray Histogram", hist_img );  
  34.     waitKey(10000000000);  
  35.     return 0;  
  36. }  

实验结果:


绘制RGB三色直方图

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //绘制RGB三色分量直方图  
  2. int main(  )  
  3. {  
  4.     Mat src;  
  5.     src=imread("baboon.jpg");  
  6.     int bins = 256;  
  7.     int hist_size[] = {bins};  
  8.     float range[] = { 0, 256 };  
  9.     const float* ranges[] = { range};  
  10.     MatND hist_r,hist_g,hist_b;  
  11.     int channels_r[] = {0};  
  12.   
  13.     calcHist( &src, 1, channels_r, Mat(), // do not use mask  
  14.         hist_r, 1, hist_size, ranges,  
  15.         true// the histogram is uniform  
  16.         false );  
  17.   
  18.     int channels_g[] = {1};  
  19.     calcHist( &src, 1, channels_g, Mat(), // do not use mask  
  20.         hist_g, 1, hist_size, ranges,  
  21.         true// the histogram is uniform  
  22.         false );  
  23.   
  24.     int channels_b[] = {2};  
  25.     calcHist( &src, 1, channels_b, Mat(), // do not use mask  
  26.         hist_b, 1, hist_size, ranges,  
  27.         true// the histogram is uniform  
  28.         false );  
  29.     double max_val_r,max_val_g,max_val_b;  
  30.     minMaxLoc(hist_r, 0, &max_val_r, 0, 0);  
  31.     minMaxLoc(hist_g, 0, &max_val_g, 0, 0);  
  32.     minMaxLoc(hist_b, 0, &max_val_b, 0, 0);  
  33.     int scale = 1;  
  34.     int hist_height=256;  
  35.     Mat hist_img = Mat::zeros(hist_height,bins*3, CV_8UC3);  
  36.     for(int i=0;i<bins;i++)  
  37.     {  
  38.         float bin_val_r = hist_r.at<float>(i);   
  39.         float bin_val_g = hist_g.at<float>(i);  
  40.         float bin_val_b = hist_b.at<float>(i);  
  41.         int intensity_r = cvRound(bin_val_r*hist_height/max_val_r);  //要绘制的高度  
  42.         int intensity_g = cvRound(bin_val_g*hist_height/max_val_g);  //要绘制的高度  
  43.         int intensity_b = cvRound(bin_val_b*hist_height/max_val_b);  //要绘制的高度  
  44.         rectangle(hist_img,Point(i*scale,hist_height-1),  
  45.             Point((i+1)*scale - 1, hist_height - intensity_r),  
  46.             CV_RGB(255,0,0));  
  47.   
  48.         rectangle(hist_img,Point((i+bins)*scale,hist_height-1),  
  49.             Point((i+bins+1)*scale - 1, hist_height - intensity_g),  
  50.             CV_RGB(0,255,0));  
  51.   
  52.         rectangle(hist_img,Point((i+bins*2)*scale,hist_height-1),  
  53.             Point((i+bins*2+1)*scale - 1, hist_height - intensity_b),  
  54.             CV_RGB(0,0,255));  
  55.   
  56.     }  
  57.     imshow( "Source", src );  
  58.     imshow( "RGB Histogram", hist_img );  
  59.     waitKey(10000000000);  
  60.     return 0;  
  61. }  

实验结果:


绘制二维直方图

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //绘制H-S二维直方图  
  2. int main( )  
  3. {  
  4.     Mat src,hsv;  
  5.     src=imread("baboon.jpg");  
  6.     cvtColor(src, hsv, CV_BGR2HSV);  
  7.     // Quantize the hue to 30 levels  
  8.     // and the saturation to 32 levels  
  9.     int hbins = 256, sbins = 180;  
  10.     int histSize[] = {hbins, sbins};  
  11.     // hue varies from 0 to 179, see cvtColor  
  12.     float hranges[] = { 0, 180 };  
  13.     // saturation varies from 0 (black-gray-white) to  
  14.     // 255 (pure spectrum color)  
  15.     float sranges[] = { 0, 256 };  
  16.     const float* ranges[] = { hranges, sranges };  
  17.     MatND hist;  
  18.     // we compute the histogram from the 0-th and 1-st channels  
  19.     int channels[] = {0, 1};  
  20.     calcHist( &hsv, 1, channels, Mat(), // do not use mask  
  21.         hist, 2, histSize, ranges,  
  22.         true// the histogram is uniform  
  23.         false );  
  24.     double maxVal=0;  
  25.     minMaxLoc(hist, 0, &maxVal, 0, 0);  
  26.     int scale = 2;  
  27.     Mat histImg = Mat::zeros(sbins*scale, hbins*scale, CV_8UC3);  
  28.     forint h = 0; h < hbins; h++ )  
  29.         forint s = 0; s < sbins; s++ )  
  30.         {  
  31.             float binVal = hist.at<float>(h, s);  
  32.             int intensity = cvRound(binVal*255/maxVal);  
  33.             rectangle( histImg, Point(h*scale, s*scale),  
  34.                 Point( (h+1)*scale - 1, (s+1)*scale - 1),  
  35.                 Scalar::all(intensity),  
  36.                 CV_FILLED );  
  37.         }  
  38.     namedWindow( "Source", 1 );  
  39.     imshow( "Source", src );  
  40.     namedWindow( "H-S Histogram", 1 );  
  41.     imshow( "H-S Histogram", histImg );  
  42.     waitKey(10000000000);  
  43.     return 0;  
  44. }  

实验结果:



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值