c++opencvdata-conversionmatalglib

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.


Solution

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

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