opencv brg 颜色直方图不起作用

opencv brg color histogram not work

我正在阅读 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;
    }
  1. 上面的代码确实 提供了 3D 直方图。我不太明白你为什么认为没有。
  2. 为什么是三维的?因为方法 calcHist() 中的参数 int dim 具有值 3。如果你想要二维直方图,那么它将是 2.
  3. 您想使用 cout << hist.at<int>(x, y, z) << endl; 打印 3D 直方图的值,其中 x、y 和 z 是 3D 直方图的坐标。