# calcHist() using vectors

I am calculating a histogram from a greyscale image using the above code; it is working fine.

cv::Mat imgSrc = cv::imread("Image.jpg", cv::IMREAD_UNCHANGED); cv::Mat histogram; //Array for the histogram int channels[] = {0}; int binNumArray[] = {256}; float intensityRanges[] = { 0, 256 }; const float* ranges[] = { intensityRanges }; cv::calcHist( &imgSrc, 1, channels, cv::noArray(), histogram, 1, binNumArray, ranges, true, false) ;

In the book of Kaehler & Bradski they refer to this as the "old-fashioned C-style arrays" and they say the new style would use STL vector templates, and the arrays of images from which to calculate the histogram are to be given using cv::InputArrayOfArrays. However, if I try to replace for example the channel array by:

std::vector<int> channels {0};

Gives compilation error.So my questions are these: 1. How can I define the arrays of the 'channels', 'binNumArray', 'intensityRanges' using vectors? 2. How can I define the array of input images using cv::InputArrayOfArrays?

edit retag close merge delete

"and they say the new style would use STL vector templates" -- yea, it didn't happen ;(

( 2017-03-20 07:10:58 -0500 )edit

I figured it out in the meantime. The vector format does exist and does work, but the parameter list is different. With the vector format I must not provide the number of images (parameter 2 in the 'old' style.

( 2017-03-21 05:17:36 -0500 )edit

oh, indeed

( 2017-03-21 05:24:17 -0500 )edit

Sort by ยป oldest newest most voted

for the reference, here's what i came up with:

Mat img(8,8,CV_8U);
randu(img,0,255);
Mat hist;

calcHist(vector<Mat>{img}, vector<int>{0}, Mat(), hist, vector<int>{16}, vector<float>{0,255});

more

1

Yes, I had thought you could change any of the parameters to vector format but you have to change ALL of them and use a differrent parameter list (with reduced number of parameters). Anyway, it is sorted now.

( 2017-03-21 11:54:14 -0500 )edit

Official site

GitHub

Wiki

Documentation