gpt4 book ai didi

c++ - 如何使用 vector 设置 OpenCV3 calcHist() 的参数?

转载 作者:行者123 更新时间:2023-11-30 05:15:55 27 4
gpt4 key购买 nike

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

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 vector 模板,计算直方图的图像数组是使用 cv::InputArrayOfArrays 给出。但是,如果我尝试通过以下方式替换 channel 数组:

std::vector channel {0};

给出编译错误。所以我的问题是:1. 如何使用 vector 定义“channels”、“binNumArray”、“intensityRanges”的数组?2. 如何使用 cv::InputArrayOfArrays 定义输入图像数组?

最佳答案

这个例子同时显示了 "old" approach"new" approach ,所以你可以体会到差异。它基于在 OpenCV documentation 中找到的示例.

"new" 方法只是一个方便的包装器,在内部调用“旧” 方法。

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

关于c++ - 如何使用 vector 设置 OpenCV3 calcHist() 的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42902527/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com