Ask Your Question
1

Understanding parameters of calcHist and encapsulate this api

asked 2013-09-03 12:24:09 -0600

stereomatching gravatar image

updated 2013-09-04 10:40:05 -0600

Try to encapsulate the api of calcHist because it is too complicated for me. I've read the documentation for calcHist() many times, but I think my inexperience precluding me from understanding all of the parameters of this api.

void calcHist(const Mat* images, int nimages, const int* channels, InputArray mask, OutputArray hist, int dims, const int* histSize, const float** ranges, bool uniform=true, bool accumulate=false )

(1)nimages : Number of source images-->is it always same as the number of images point to? ex :

cv::Mat a, b, c;
cv::Mat images[] = {a, b, c};
int nimages = sizeof(images) / sizeof(cv::Mat);

(2)channels : The channels used to compute the histogram-->is it always same as the number of dims? ex : int channels[] = {0, 1}; int dims = sizeof(channels)/ sizeof(int);

(3)histSizes && ranges: Are they always have the same elements numbers as channels?

(4)channels, histSize and ranges always have same number of elements? ex :

int channels[] = {0, 1}; //two elements
int histSize[] = {180, 256}; //two elements
float hue[] = {0, 180};
float sat[] = {0, 255};
float const *ranges[] = {hue, sat}; //two elements

(5)if (1)~(4) are correct, that means we could deduce nimages from images;deduce dims from channels, histSize or ranges?

encapsulation base on the assumption from (1)~(4)

void calc_histogram(std::initializer_list<cv::Mat> images, cv::OutputArray &output, std::initializer_list<int> channels,
                    std::initializer_list<int> hist_sizes, std::initializer_list<float[2]> ranges,
                    cv::InputArray const &mask = cv::Mat(), bool uniform = true, bool accumulate = false)
{        
    size_t const sizes = ranges.size();
    std::vector<float const*> d_ranges(sizes);
    for(size_t i = 0; i != sizes; ++i){
        d_ranges[i] = *(std::begin(ranges) + i);
    }

    cv::calcHist(std::begin(images), images.size(), std::begin(channels), mask, output,
                 channels.size(), std::begin(hist_sizes), &d_ranges[0], uniform ,accumulate);
}

I call it like this(make it close to the api like python).

cv::Mat hsv;
cv::MatND results; 
//.....do somthing
calc_histogram({hsv}, results, {0, 1}, {180, 256}, { {0, 180}, {0, 256} });

I don't know my assumptions correct or not.If you know another way to encapsulate it, please shed me some lights(I try to use std::array<float[2], n=""> to replace ranges, but this api need to specify nontype template parameter, although it could save the price of memory allocation).

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-09-04 12:32:44 -0600

berak gravatar image

updated 2013-09-04 16:34:01 -0600

i think, your assumptions are right.

here's my try at handling the ranges, unfortunately i got no opencv version compiled against c++11, so a dummy must do for now ( also, my gcc can only do c++0x , so i had to use x.begin() instead of begin(x) ) :

// dummy calcHist replacement
void calcHist(const int *a1, const float**ranges) {}

void calcHist_wrap(initializer_list<int> a1, initializer_list<initializer_list<float>> a2) {
    const initializer_list<float>*i2 = a2.begin(); 
    const float *b = i2->begin();
    calcHist(a1.begin(),&b);
}

int main(int argc, char **argv)
{
    calcHist_wrap({1,2},{{3,4},{5,6}});
    return 0;
}

also i think, you can skip the '&' symbols from cv::OutputArray &output , as OutputArray is already a typedef _OutputArray& OutputArray

edit flag offensive delete link more

Comments

Thanks, I did a big mistake--haven't checked out what is OutputArray and InputArray before I use them.About your suggestion, It can not work, because the cv::caclHist need an array of "float const" The contiguous address of initializer_list is base on the type "initializer_list<float>" but not "float const*".

stereomatching gravatar imagestereomatching ( 2013-09-05 09:07:15 -0600 )edit

shame, it even compiled on my machine. awesome sportive challenge ;)

berak gravatar imageberak ( 2013-09-05 09:36:55 -0600 )edit

Question Tools

Stats

Asked: 2013-09-03 12:24:09 -0600

Seen: 878 times

Last updated: Sep 04 '13