【问题标题】:opencv calcHist erroropencv calcHist 错误
【发布时间】:2011-10-11 06:55:49
【问题描述】:

当我尝试计算直方图并想知道我是否遗漏了什么时,我在 OpenCV 中遇到了模棱两可的错误。我看了一下函数参数,发现它取了以下之一:

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

在我的代码中,我有以下内容:

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.create(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 );
        }
    }
}

我试图弄清楚我可以更改哪些内容以编译此代码。另请注意,这段代码位于类的函数中。我正在尝试将大部分直方图示例代码分成它自己的单独函数,该函数仅在某些点调用,所以我不确定它是否与 phranges 设置在不正确的位置或我的变量是否有任何关系设置不当。

非常感谢任何建议。

谢谢

【问题讨论】:

    标签: c image-processing opencv histogram


    【解决方案1】:

    我看了一下文档,发现有以下函数,和你的有点不一样:

    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)
    

    无论如何,如果您遇到编译时歧义错误,则问题与 openCV 无关,而是与 C++ 相关。该函数的 2 个版本是相同的,除了 hist 参数会导致歧义并且编译器不知道该调用哪一个。

    在您的代码中,您将 hist 变量声明如下:

    cv::Mat hist;
    

    我认为您必须将其声明为:

    cv::MatND hist;
    

    或:

    cv::SparseMat hist;
    

    或将 cv::Mat 转换为适当的类型,以指示编译器应该调用哪个版本的函数。

    【讨论】:

    • 我试图将其声明为 cv:SparseMat 并且我也尝试将其转换为它。我仍然得到错误。
    • @Seb:您能否复制并粘贴您收到的确切错误消息?
    • 我修好了。我必须将变量放在函数调用的范围内。变量的位置对函数有不同的影响,这似乎很奇怪。不过感谢您的帮助。
    【解决方案2】:

    正如我在上面的评论中提到的,我必须将所有变量放在函数范围内,而不是放在 if 检查本身中。我不知道为什么这会有所不同,但也许它在编译期间有一些事情要做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-05
      • 1970-01-01
      • 2016-03-17
      • 2017-08-02
      • 2013-02-19
      • 2018-10-31
      相关资源
      最近更新 更多