szecsit
szecsit

Reputation: 21

How to set the parameters of OpenCV3 calcHist() using vectors?

I am calculating a histogram from a greyscale image using the above code; it is working fine.

cv::Mat imgSrc = cv::imread("Image.jpg", cv::IMREAD_UNCHANGED);
cv::Mat histogram; //Array for the histogram
int channels[] = {0}; 
int binNumArray[] = {256}; 
float intensityRanges[] = { 0, 256 }; 
const float* ranges[] = { intensityRanges };
cv::calcHist( &imgSrc, 1, channels, cv::noArray(), histogram, 1,     binNumArray, ranges, true, false) ;    

In the book of Kaehler & Bradski they refer to this as the "old-fashioned C-style arrays" and they say the new style would use STL vector templates, and the arrays of images from which to calculate the histogram are to be given using cv::InputArrayOfArrays. However, if I try to replace for example the channel array by:

std::vector channels {0};

Gives compilation error.So my questions are these: 1. How can I define the arrays of the 'channels', 'binNumArray', 'intensityRanges' using vectors? 2. How can I define the array of input images using cv::InputArrayOfArrays?

Upvotes: 1

Views: 7981

Answers (1)

Miki
Miki

Reputation: 41775

This example show both the "old" approach and the "new" approach, so you can appreciate the difference. It's based on the example found in the OpenCV documentation.

The "new" approach is just a convenience wrapper that internally calls the "old" one.

#include <opencv2\opencv.hpp>
#include <vector>
using namespace cv;
using namespace std;


int main()
{
    Mat3b src = imread("path_to_image");
    Mat3b hsv;
    cvtColor(src, hsv, CV_BGR2HSV);

    Mat hist;
    Mat hist2;
    {
        // Quantize the hue to 30 levels
        // and the saturation to 32 levels
        int hbins = 30, sbins = 32;
        int histSize[] = { hbins, sbins };
        // hue varies from 0 to 179, see cvtColor
        float hranges[] = { 0, 180 };
        // saturation varies from 0 (black-gray-white) to
        // 255 (pure spectrum color)
        float sranges[] = { 0, 256 };
        const float* ranges[] = { hranges, sranges };

        // we compute the histogram from the 0-th and 1-st channels
        int channels[] = { 0, 1 };
        calcHist(&hsv, 1, channels, Mat(), // do not use mask
            hist, 2, histSize, ranges,
            true, // the histogram is uniform
            false);
    }

    {
        // Quantize the hue to 30 levels
        // and the saturation to 32 levels
        vector<int> histSize = { 30, 32 };
        // hue varies from 0 to 179, see cvtColor
        // saturation varies from 0 (black-gray-white) to
        // 255 (pure spectrum color)
        vector<float> ranges = { 0, 180, 0, 256 };
        // we compute the histogram from the 0-th and 1-st channels
        vector<int> channels = { 0, 1 };

        vector<Mat> mats = { hsv };
        calcHist(mats, channels, Mat(), // do not use mask
            hist2, histSize, ranges,
            /*true, // the histogram is uniform, this is ALWAYS true*/
            false);
    }
    return 0;
}

Upvotes: 4

Related Questions