Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to use the OpenCV library in conjunction with the Armadillo library?

I am building an image processing application using OpenCV. I am also using the Armadillo library because it has some very neat matrix related functions. The thing is though, in order to use Armadillo functions on cv::Mat I need frequent conversions from cv::Mat to arma::Mat . To accomplish this I convert the cv::Mat to an arma::Mat using a function like this

arma::Mat cvMat2armaMat(cv::Mat M)
{
    copy cv::Mat data to a arma::Mat
    return arma::Mat
}

Is there a more efficient way of doing this?

like image 599
Vishy Avatar asked Nov 03 '22 19:11

Vishy


1 Answers

To avoid or reduce copying, you can access the memory used by Armadillo matrices via the .memptr() member function. For example:

mat X(5,6);
double* mem = X.memptr();

Be careful when using the above, as you're not allowed to free the memory yourself (Armadillo will still manage the memory).

Alternatively, you can construct an Armadillo matrix directly from existing memory. For example:

double* data = new double[4*5];
// ... fill data ...
mat X(data, 4, 5, false);  // 'false' indicates that no copying is to be done; see docs

In this case you will be responsible for manually managing the memory.

Also bear in mind that Armadillo stores and accesses matrices in column-major order, ie. column 0 is first stored, then column 1, column 2, etc. This is the same as used by MATLAB, LAPACK and BLAS.

like image 128
mtall Avatar answered Jan 04 '23 14:01

mtall