1

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.

manlio
  • 18,345
  • 14
  • 76
  • 126
kkdirvi
  • 59
  • 11

1 Answers1

1
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.
manlio
  • 18,345
  • 14
  • 76
  • 126
  • Thanks manlio for your reply. I already did the same way. While other parameters i set are as alglib::dfbuildrandomdecisionforest(matrix, 28539, 96,7, 80, 0.66, info, df, rep); but still the model is not built. I also checked data contained in matrix which is also correct. Can you plz suggest what I am doing wrong(Like passing parameter to the random forest) – kkdirvi Sep 27 '14 at 19:21
  • Have you checked the value of the `info` output parameter? The return code should be 1. Also the last column of `matrix` must contain the class number (from 0 to 6 in your case). – manlio Sep 27 '14 at 22:44
  • I checked output of info and it is -1 which means there is some problem with parameters. Let me tell you some detail....matrix contains training data and last column is class label. Other parameters are as alglib::dfbuildrandomdecisionforest(matrix, 28539, 96,7, 80, 0.66, info, df, rep); – kkdirvi Sep 28 '14 at 10:48
  • total classes I am using are 7(7 parts of face i.e.nose,mouth,hair etc) – kkdirvi Sep 28 '14 at 10:49