Ask Your Question
0

Can't get simple histogram generation to work.

asked 2013-11-12 21:25:52 -0500

Cambertix gravatar image

updated 2013-11-12 21:31:52 -0500

Greetings everyone !.

So, I've been trying to get this simple function to work for almost a day now. I'm just trying to show a RGB histogram of an image (an area thereof) and show the bins as rectangles on the screen.

I've failed, i've tried a million tutorials, but somehow the answer escapes me. All I am getting from cv.calcHist() at the moment is a bunch of 0 and a couple of "meaningful" values in the first and last positions.

I hope you can give me a hand with this one.

current_main_img = cv.imread("TestPattern.jpeg")

def show_histogram(img):

scale = 32
bins_number = 256/scale
bin_length = 256/bins_number
bins = np.arange(bins_number).reshape(bins_number,1)
print(bins)
finalHist = np.zeros((300,240,3))

iInfo = [ (255,0,0),(0,255,0),(0,0,255) ]

for iChannel, iColor in enumerate(iInfo):

    histElement = cv.calcHist([img],[iChannel],None,[bins_number],[0,255])

    cv.normalize(histElement,histElement,0,255,cv.NORM_MINMAX)

    tmpHist=np.int32(np.around(histElement))
    points = np.int32(np.column_stack((bins,tmpHist)))

    print(points)

    for posx, intensity in points:
        subbin_length=bin_length/3
        displacement = posx*bin_length+iChannel*subbin_length
        cv.rectangle(finalHist,(displacement+posx*subbin_length, 0),
                               (displacement+(posx+1)*subbin_length, 
                                intensity), iColor, 2)

flippedHist = np.flipud(finalHist)

cv.namedWindow("Histogram", cv.CV_WINDOW_AUTOSIZE )
cv.imshow("Histogram", flippedHist )

And the following is the result:

Result:

Result

Thanks in advance !.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2013-11-12 23:03:33 -0500

Haris gravatar image

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;

}
edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-11-12 21:25:52 -0500

Seen: 268 times

Last updated: Nov 12 '13