Search code examples
c++opencvpointersconstantsnode.js-addon

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


Context

I'm working currently on https://github.com/piercus/node-opencv (forked from https://github.com/peterbraden/node-opencv), i'm implementing a binder for calcHist function.

Problem

Workaround

Considering that maximum number of dimensions is 3, i have made a workaround (see 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);

Question

Is it possible to do it in an elegant way without creating a "zero-filled" object ? I would like to have maximum size of input 32 (instead of 3).


Solution

  • You don't need to copy the contents of histRanges, as the numbers in it are already laid out as float arrays, just like cv::calcHist requires. You only need to create an array of pointers to those arrays.

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