如何使用向量设置 OpenCV3 calcHist() 的参数
How to set the parameters of OpenCV3 calcHist() using vectors?
我正在使用上面的代码从灰度图像计算直方图;它工作正常。
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;
}
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 函数调用中参数的顺序重要吗
- 部分定义/别名模板模板参数
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 使用不带参数的函数访问结构元素
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 如何使用Luacneneneba API正确读取字符串和表参数
- 在派生函数中指定void*参数
- 视图中的参数推导失败:take_while
- static_assert在宏中,但也可以扩展到可以用作函数参数的东西
- 使用指向成员的指针将成员函数作为参数传递
- 没有名称的C++模板参数
- 如何使用向量设置 OpenCV3 calcHist() 的参数