kkdirvi
kkdirvi

Reputation: 59

OpenCV Mat to alglib real 2d Array conversion

How to convert Opencv Mat to Alglib real 2D array?

Here is an example where I am stucked

Mat Col(28539,97,CV_32F);

I want to convert this Mat to alglib real_2d_array for training a classifier.

Upvotes: 1

Views: 1188

Answers (1)

manlio
manlio

Reputation: 18912

Mat Col(28539, 97, CV_32F);

is a OpenCV bi-dimensional (28539 rows, 97 columns) dense floating-point (CV_32F = float) array.

The alglib almost-equivalent datatype is

// bi-dimensional real (double precision) array
real_2d_array matrix;

The data layout in Mat is compatible with real_2d_array (and the majority of dense array types from other toolkits and SDKs).

A simple way to convert is:

const int rows(28539);
const int columns(97);

matrix.setlength(rows, columns);

for (int i(0); i < rows; ++i)
  for (int j(0); j < columns; ++j)
    matrix(i, j) = Col.at<float>(i, j);

Mat::at returns a reference to the specified array element.

EDIT

From the reference manual:

void alglib::dfbuildrandomdecisionforest(
    real_2d_array xy,
    ae_int_t npoints,
    ae_int_t nvars,
    ae_int_t nclasses,
    ae_int_t ntrees,
    double r,
    ae_int_t& info,
    decisionforest& df,
    dfreport& rep);
  • xy is the training set (lines corresponding to sample components and columns corresponding to variables).

    For a classification task the first nvars of the columns contain independent variables. The last column will contain the class number (from 0 to nclasses-1). Fractional values are rounded to the nearest integer.

  • npoints is the training set size (>=1).
  • nvars is the number of independent variables (>=1).
  • nclasses must be >1 for classification.
  • ntrees is the number of trees in a forest (>=1).
  • r is the percent of a training set used to build individual trees (0 < R <= 1).

The remaining parameters are output parameters. In case of problems you should check info:

  • info return code:
    • -2, if there is a point with class number outside of [0..nclasses-1].
    • -1, if incorrect parameters was passed (npoints<1, nvars<1, nclasses<1, ntrees<1, r<=0 or r>1).
    • 1, if task has been solved.

Upvotes: 1

Related Questions