上下文
我目前正在研究 https://github.com/piercus/node-opencv (从 https://github.com/peterbraden/node-opencv fork ),我正在为 calcHist 函数实现一个 Binder 。
问题
- C++ 函数将
const float** ranges
作为输入 (参见 http://docs.opencv.org/2.4/modules/imgproc/doc/histograms.html#calchist) - 我想设置输入的大小考虑
- 我不知道如何在不破坏编译器的情况下有条件地设置此
const float**
的大小
解决方法
考虑到最大维度数为 3,我制定了一个解决方法(参见 full source )
// Wrap Javascript input which is like [[0, 256], [0, 256]]
Local<Array> nodeRanges = Local<Array>::Cast(info[3]->ToObject());
// create a first table
float histRanges[dims][2];
for (unsigned int i = 0; i < dims; i++) {
Local<Array> nodeRange = Local<Array>::Cast(nodeRanges->Get(i)->ToObject());
float lower = nodeRange->Get(0)->NumberValue();
float higher = nodeRange->Get(1)->NumberValue();
histRanges[i][0] = lower;
histRanges[i][1] = higher;
}
// minimum length is 1 so i can fullfill first range without issue
float first_range[] = { histRanges[0][0], histRanges[0][1] };
float second_range[] = { 0, 0}; // here is my problem, do i really need to do this
float third_range[] = { 0, 0};// same problem here
if(dims >= 2){
second_range[0] = histRanges[1][0];
second_range[1] = histRanges[1][1];
}
if(dims >= 3){
third_range[0] = histRanges[2][0];
third_range[1] = histRanges[2][1];
}
// now i can create a const float** compatible type
const float* histRanges1[] = {first_range, second_range, third_range};
[... other stuffs ...]
// const float** is needed here
cv::calcHist(&inputImage, 1, channels, cv::Mat(), outputHist, dims, histSize, histRanges1, uniform);
问题
是否可以在不创建“零填充”对象的情况下以优雅的方式进行操作? 我希望输入的最大大小为 32(而不是 3)。
最佳答案
您不需要复制 histRanges
的内容,因为其中的数字已经布局为 float
数组,就像 cv::calcHist
要求。您只需要创建一个指向这些数组的指针数组。
float histRanges[dims][2];
const float* ranges[dims];
for (unsigned int i = 0; i < dims; i++) {
Local<Array> nodeRange = Local<Array>::Cast(nodeRanges->Get(i)->ToObject());
float lower = nodeRange->Get(0)->NumberValue();
float higher = nodeRange->Get(1)->NumberValue();
histRanges[i][0] = lower;
histRanges[i][1] = higher;
ranges[i] = histRanges[i];
}
cv::calcHist(&inputImage, 1, channels, cv::Mat(), outputHist, dims, histSize, ranges, uniform);
关于c++ - 将节点数组(可变长度)转换为 const float** 以调用 opencv.calcHist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45868943/