Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can refer this c++ code which will plot histogram of source image before and after OpenCV histogram equalization

#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

Mat create_histogram_image(Mat);


int main()
{
   Mat src,out;
   Mat hist1(640,480,CV_8UC1,Scalar(255));
   Mat hist2(640,480,CV_8UC1,Scalar(255));

    src = imread("src.jpg",0);

       Mat src_hist=create_histogram_image(src);
       equalizeHist( src, out );// Perform histogram equalization
       Mat out_hist=create_histogram_image(out);

    namedWindow( "Original", 1) ;// Show original
    imshow( "Original", src );


    namedWindow("Result", 1) ; // Show histogram equalized
    imshow("Result", out );

    namedWindow( "Histogram before equalization", 1) ;//Show Histograms
    imshow( "Histogram before equalization", src_hist );
    namedWindow( "Histogram after equalization", 1) ;
    imshow( "Histogram after equalization", out_hist );

    waitKey(0);

    return 0;
}
Mat create_histogram_image(Mat src){

    /// Establish the number of bins
  int histSize = 256;

  /// Set the ranges ( for B,G,R) )
  float range[] = { 0, 256 } ;
  const float* histRange = { range };

  bool uniform = true; bool accumulate = false;

  Mat hist;

  /// Compute the histograms:
  calcHist( &src, 1, 0, Mat(), hist, 1, &histSize, &histRange, uniform, accumulate );

  // Draw the histograms for B, G and R
  int hist_w = src.cols;//512;
  int hist_h = src.rows;//400;
  int bin_w = cvRound( (double) hist_w/histSize );

  Mat histImage( hist_h, hist_w, CV_8UC1, Scalar( 0) );


  normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );

  /// Draw for each channel
  for( int i = 1; i < histSize; i++ )
  {
      line( histImage, Point( bin_w*(i-1), hist_h - cvRound(hist.at<float>(i-1)) ) ,
                       Point( bin_w*(i), hist_h - cvRound(hist.at<float>(i)) ),
                       Scalar( 255), 2, 8, 0  );

  }
return histImage;

}