verdery
verdery

Reputation: 531

opencv brg color histogram not work

I am reading OpenCV 2 Computer Vision Application Programming Cookbook and implementing examples in it.

In chapter 4, color histogram example doesn't work unfortunately.

The code is below. But this code does not give me histogram or any error. Also, it says that color images histogram is three dimensional. I don't understand why it is thee and it is not two.

    #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;
    }

Upvotes: 0

Views: 803

Answers (1)

OpenMinded
OpenMinded

Reputation: 1175

  1. This code above DOES give you a 3D histogram. I don't quite understand why you think it does not.
  2. Why is it three-dimensional? Because argument int dim in method calcHist() has value 3. If you want 2D histogram then it would be 2.
  3. You want to print values of 3D histogram with cout << hist.at<int>(x, y, z) << endl; where x, y and z are coordinates for 3D histogram.

Upvotes: 1

Related Questions