簡體   English   中英

將節點數組(可變長度)轉換為const float **以調用opencv.calcHist

[英]Convert Node Array (variable lenght) to a const float** to call opencv.calcHist

上下文

我目前正在使用https://github.com/piercus/node-opencv (從https://github.com/peterbraden/node-opencv分叉)上工作,我正在實現calcHist函數的綁定器。

問題

解決方法

考慮到最大尺寸為3,我做出了解決方法(請參閱完整資料

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM