Possible to use calcHist with a float as bin

Hi,

I am trying to make an histogram of an cv::Mat (type: 32FC1), which contains values between 0 - 1. I am trying to create 2 bins, the first one from 0 - 0,5 and the second one from 0,5 - 1, but I am only getting one bin from 0 - 1. Is it possible to make bins which do not start or finish with an whole number? Or am I doing it the wrong way?

This is the code I tried:

int histSize = 1.F;

float range[] = {0.F, 0.5F}; //the upper boundary is exclusive

float range2[] = {0.5F, 1.F}; //the upper boundary is exclusive

const float* histRange[] = {range, range2};

bool uniform = false , accumulate = false;

cv::Mat hist;

calcHist(&src, 1, 0, cv::Mat(), hist, 1, &histSize, histRange, uniform, accumulate);

for ( int i = histSize-1; i >= 0; i--) {

  std::cout << i << ": " << hist.at< float >(i) << std::endl;

}

Thanks in advance

have a look at the tutorial again,
your range should be {0,1} (it’s for all the bins), and the histSize (number of bins) should be 2

Thank you, that did the trick.

1 Like