开发者

opencv calcHist error

I am getting an ambiguity error in OpenCV when I am trying to calculate a histogram and was wondering if I was missing something. I took a look at the function parameters and saw that it took one of the following:

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 );

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

And in my code I have the following:

if(histMat.size > 0)
{
    float hranges[2] = {0, 180};
    float* phranges = hranges;

    cv::Mat hist;
    cv::Mat hsv;
    cv::Mat hue;
    cv::Mat mask;

    cvtColor(histMat, hsv, CV_BGR2HSV);

    int _vmin = vmin, _vmax = vmax;

    inRange(hsv, cv::Scalar(0, smin, MIN(_vmin,_vmax)),
        cv::Scalar(180, 256, MAX(_vmin, _vmax)), mask);
    int ch[] = {0, 0};

    hue.c开发者_JAVA百科reate(hsv.size(), hsv.depth());
    mixChannels(&hsv, 1, &hue, 1, ch, 1);

    if(eType == MODEL)
    {
        cv::Mat roi(hue, selection), maskroi(mask, selection);
        cv::calcHist(&roi, 1, 0, maskroi, hist, 1, &hsize, &phranges);
        cv::normalize(hist, hist, 0, 255, CV_MINMAX);

        trackWindow = selection;

        histimg = cv::Scalar::all(0);
        int binW = histimg.cols / hsize;
        cv::Mat buf(1, hsize, CV_8UC3);
        for( int i = 0; i < hsize; i++ )
            buf.at<cv::Vec3b>(i) = cv::Vec3b(cv::saturate_cast<uchar>(i*180./hsize), 255, 255);
        cvtColor(buf, buf, CV_HSV2BGR);

        for( int i = 0; i < hsize; i++ )
        {
            int val = cv::saturate_cast<int>(hist.at<float>(i)*histimg.rows/255);
            cv::rectangle( histimg, cv::Point(i*binW,histimg.rows),
                cv::Point((i+1)*binW,histimg.rows - val),
                cv::Scalar(buf.at<cv::Vec3b>(i)), -1, 8 );
        }
    }
}

I am trying to figure out what I can change in order to get this code to compile. Also please note that this chunk of code is in a function for a class. I'm trying to separate most of the Histogram sample code into it's own separate function that is called only at certain points so I am not sure if it has anything to do with the fact that phranges is set at an improper place or if my variables are set improperly.

Any advice is greatly appreciated.

Thanks


I took a look at the documentation and I found the following functions, which are a bit different than yours:

void calcHist(const Mat* arrays, int narrays, const int* channels, const Mat& mask, 
              MatND& hist, int dims, const int* histSize, const float** ranges,
              bool uniform=true, bool accumulate=false)

void calcHist(const Mat* arrays, int narrays, const int* channels, const Mat& mask, 
              SparseMat& hist, int dims, const int* histSize, const float** ranges,
              bool uniform=true, bool accumulate=false)

Anyway, if you are getting a compile-time ambiguity error, the problem is not openCV related but C++ related. The 2 versions of the function are identical, except of the hist parameter which is responsible for the ambiguity and the compiler doesn't know which one to call.

In your code, you declared the hist variable as follows:

cv::Mat hist;

I think you must declare it either as:

cv::MatND hist;

or:

cv::SparseMat hist;

or to cast the cv::Mat to an appropriate type, to instruct the compiler which version of the function should call.


As mentioned in my comment above I had to place all variables in the function scope and not in the if check itself. I am not sure why that made a difference, but maybe it had something to do during compile time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜