【问题标题】:opencv brg color histogram not workopencv brg颜色直方图不起作用
【发布时间】:2015-02-26 14:31:29
【问题描述】:

我正在阅读 OpenCV 2 Computer Vision Application Programming Cookbook 并在其中实施示例。

不幸的是,在第 4 章中,颜色直方图示例不起作用。

代码如下。但是这段代码没有给我直方图或任何错误。此外,它说彩色图像直方图是三维的。我不明白为什么是你而不是两个。

    #include <opencv2\imgproc\imgproc.hpp>
    #include <opencv2\highgui\highgui.hpp>
    #include <opencv2\core\core.hpp>
    #include <iostream>

    using namespace cv;
    using namespace std;

    int main(){
        Mat image = imread("waves.jpg");
        int histSize[3];
        float hranges[2];
        const float* ranges[3];
        int channels[3];
        // Prepare arguments for a color histogram
        histSize[0] = histSize[1] = histSize[2] = 256;
        hranges[0] = 0.0; // BRG range
        hranges[1] = 255.0;
        ranges[0] = hranges; // all channels have the same range
        ranges[1] = hranges;
        ranges[2] = hranges;
        channels[0] = 0; // the three channels
        channels[1] = 1;
        channels[2] = 2;

        Mat hist;
        // Compute histogram
        calcHist(&image,
            1, // histogram of 1 image only
            channels, // the channel used
            cv::Mat(), // no mask is used
            hist, // the resulting histogram
            3, // it is a 3D histogram
            histSize, // number of bins
            ranges // pixel value range
            );

        cout << hist.at<int>(100, 100, 0) << endl;
        cout << hist.at<int>(100, 100, 1) << endl;
        cout << hist.at<int>(100, 100, 2) << endl;

        return 0;
    }

【问题讨论】:

    标签: c++ opencv image-processing computer-vision histogram


    【解决方案1】:
    1. 上面的这段代码确实为您提供了一个 3D 直方图。我不太明白你为什么认为它没有。
    2. 为什么是三维的?因为方法 calcHist() 中的参数 int dim 具有值 3。如果您想要 2D 直方图,那么它将是 2
    3. 您想使用cout &lt;&lt; hist.at&lt;int&gt;(x, y, z) &lt;&lt; endl; 打印 3D 直方图的值,其中 x、y 和 z 是 3D 直方图的坐标。

    【讨论】:

    • 不幸的是它没有给我 3d 直方图。这是我的问题。在 calcHist 函数之后,dim 等于 3,但 row 和 col 等于 -1。 cout 只在屏幕上打印零。调试我从 3Dhistrogram 矩阵中获得以下相关信息:维度:3,行:-1,列:-1,大小:256
    • 请看here。对于 cols 和 rows 它说 "当数组有超过 2 个维度时,行数和列数或 (-1, -1)" 。对于您的零,这是因为该元素的图像中没有颜色。尝试使用这样的迭代器打印完整的直方图:for (MatConstIterator_&lt;int&gt; it = hist.begin&lt;int&gt;(); it != hist.end&lt;int&gt;(); it++) { cout &lt;&lt; *it &lt;&lt; " "; },我很确定它会为某些元素打印一些值。请注意,范围为 256 的 3D 直方图将包含 16777216 个元素。
    • 感谢您的帮助。在某些元素的 3d 矩阵中,它只给了我超过 10 亿的疯狂数字。大多数 3d 矩阵只是零。其实我不明白为什么书会给出这样的例子。 256x256x256数组是什么意思?
    • 我找到了疯狂数字的原因。我使用了 at 模板函数,但它不正确。使用 at 打印数字会给出正确的输出。我也意识到为什么我们使用 256x256x256 3d 矩阵。 A Guide to Utilizing Color Histograms for Computer Vision and Image Search Engines
    猜你喜欢
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    • 2014-03-06
    • 2017-08-27
    • 2023-03-17
    • 2013-01-12
    相关资源
    最近更新 更多