Chinaunix首页 | 论坛 | 博客
  • 博客访问: 142525
  • 博文数量: 35
  • 博客积分: 245
  • 博客等级: 二等列兵
  • 技术积分: 320
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-06 14:31
文章分类

全部博文(35)

文章存档

2017年(8)

2015年(1)

2014年(7)

2013年(11)

2012年(1)

2011年(7)

我的朋友

分类: C/C++

2014-07-30 09:27:35

OpenCV 直方图程序

开发环境

  • VS2008
  • OpenCV2.4.3

详细代码



点击(此处)折叠或打开

  1. #include "opencv2/highgui/highgui.hpp"
  2. #include "opencv2/imgproc/imgproc.hpp"
  3. #include <iostream>
  4. #include <stdio.h>

  5. using namespace cv;
  6. using namespace std;

  7. int histSize[1];            // number of bins
  8. float hranges[2];            // min and max pixel value
  9. const float* ranges[1];        //
  10. int channels[1];            // only 1 channel used here

  11. void initial()
  12. {
  13.     // Initial arguments for 1D histogram
  14.     histSize[0] = 256;
  15.     hranges[0] = 0.0;
  16.     hranges[1] = 255.0;
  17.     ranges[0] = hranges;
  18.     channels[0] = 0;        // by default, we look at channel 0
  19. }
  20. // Create the 1D histogram
  21. MatND getHistogram(const Mat &image)
  22. {
  23.     MatND hist;
  24.     // Compute histogram
  25.     calcHist(&image, //
  26.         1,             // histogram from 1 image only
  27.         channels,     // the channel used
  28.         Mat(),         // no mask is used
  29.         hist,         // the resulting histogram
  30.         1,             // it is a 1D histogram
  31.         histSize,     // number of bins
  32.         ranges);     // pixel value range
  33.     return hist;
  34. }
  35. Mat getHistogramImage(const Mat &image)
  36. {
  37.     // Compute histogram first
  38.     MatND hist = getHistogram(image);
  39.     // Get min and max values
  40.     double maxVal = 0;
  41.     double minVal = 0;
  42.     minMaxLoc(hist,&minVal,&maxVal,0,0);
  43.     // Image on which to display histogram
  44.     Mat histImg(histSize[0],histSize[0],CV_8U,Scalar(255));
  45.     // Set highest point at 90% of nbins
  46.     int hpt = static_cast<int>(0.9*histSize[0]);
  47.     // Draw a vertical line for each bin
  48.     for (int h=0;h<histSize[0];h++)
  49.     {
  50.         float binVal = hist.at<float>(h);
  51.         int intensity = static_cast<int>(binVal*hpt/maxVal);
  52.         // This function draws a line between 2 points
  53.         line(histImg,Point(h,histSize[0]),Point(h,histSize[0]-intensity),Scalar::all(128));
  54.     }
  55.     return histImg;
  56. }
  57. /** @function main */
  58. int main( int argc, char** argv )
  59. {
  60.     Mat src, dst;
  61.     initial();

  62.     char* source_window = "Source image";
  63.     char* equalized_window = "Equalized Image";

  64.     /// Load image
  65.     src = imread( "lena.jpg", 1 );

  66.     if( !src.data )
  67.     { cout<<"Usage: ./Histogram_Demo "<<endl;
  68.     return -1;}

  69.     /// Convert to grayscale
  70.     cvtColor( src, src, CV_BGR2GRAY );

  71.     /// Apply Histogram Equalization
  72.     equalizeHist( src, dst );


  73.     Mat srcHist = getHistogramImage(src);
  74.     Mat dstHist = getHistogramImage(dst);


  75.     /// Display results
  76.     namedWindow( source_window, CV_WINDOW_AUTOSIZE );
  77.     namedWindow( equalized_window, CV_WINDOW_AUTOSIZE );

  78.     imshow( source_window, src );
  79.     imshow( equalized_window, dst );

  80.     imshow( "Src_Hist", srcHist );
  81.     imshow( "Dst_Hist", dstHist );


  82.     /// Wait until user exits the program
  83.     waitKey(0);

  84.     return 0;
  85. }

程序运行结果





阅读(1576) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~