如何使用向量设置 OpenCV3 calcHist() 的参数



我正在使用上面的代码从灰度图像计算直方图;它工作正常。

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

在Kaehler&Bradski的书中,他们将其称为"老式的C式数组",他们说新样式将使用STL矢量模板,并且计算直方图的图像数组将使用cv::InputArrayOfArrays给出。但是,如果我尝试将通道数组替换为:

标准::矢量通道{0};

给出编译错误。所以我的问题是这些:1. 如何使用向量定义"通道"、"binNumArray"、"强度范围"的数组?2. 如何使用 cv::InputArrayOfArray 定义输入图像的数组?

此示例同时显示了"旧">方法和"新">方法,因此您可以体会到其中的区别。它基于 OpenCV 文档中的示例。

">

"方法只是一个方便的包装器,内部称为"旧">方法。

#include <opencv2opencv.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;
}

最新更新