Ask Your Question

calcHist() using vectors

asked 2017-03-20 06:16:01 -0500

szecsit gravatar image

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 flag offensive close merge delete


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

berak gravatar imageberak ( 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.

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

oh, indeed

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

1 answer

Sort by ยป oldest newest most voted

answered 2017-03-21 05:46:02 -0500

berak gravatar image

updated 2017-03-21 05:50:52 -0500

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

Mat img(8,8,CV_8U);
Mat hist;

calcHist(vector<Mat>{img}, vector<int>{0}, Mat(), hist, vector<int>{16}, vector<float>{0,255});
edit flag offensive delete link more



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.

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

Question Tools

1 follower


Asked: 2017-03-20 06:16:01 -0500

Seen: 942 times

Last updated: Mar 21 '17