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

一世执手 提交于 2019-12-22 08:28:59

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/17227588/what-is-the-best-way-to-use-the-opencv-library-in-conjunction-with-the-armadillo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!