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

I am creating an image processing application using OpenCV. I also use the Armadillo library because it has some very neat matrix related features. The fact is that to use the Armadillo functions on cv :: Mat I need frequent conversions from cv :: Mat to arma :: Mat. To do this, I convert cv :: Mat to arma :: Mat using such a function

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

Is there a more efficient way to do this?

+4
source share
1 answer

To avoid or reduce copying, you can access the memory used by Armadillo matrices through a function . memptr () . For instance:

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

Be careful when using the above, as you are not allowed to free memory on your own (Armadillo will still manage the memory).

Alternatively, you can directly build the Armadillo matrix from existing memory. For instance:

 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 keep in mind that Armadillo stores and accesses matrices in the column ordinal , i.e. column 0 is stored first, then column 1, column 2, etc. This is the same as used by MATLAB, LAPACK and BLAS.

+2
source

Source: https://habr.com/ru/post/1487405/


All Articles