将OpenCV库与Armadillo库结合使用的最佳方法是什么? [英] What is the best way to use the OpenCV library in conjunction with the Armadillo library?
问题描述
我正在使用OpenCV构建图像处理应用程序.我还使用Armadillo库,因为它具有一些非常简洁的与矩阵相关的功能.事实是,为了在cv :: Mat上使用Armadillo函数,我需要从cv :: Mat到arma :: Mat的频繁转换. 为此,我使用这样的函数将cv :: Mat转换为arma :: Mat
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
}
是否有更有效的方法?
推荐答案
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();
使用上面的方法时要小心,因为您不能自己释放内存(Armadillo仍将管理内存).
Be careful when using the above, as you're not allowed to free the memory yourself (Armadillo will still manage the memory).
或者,您可以从现有内存直接构造Armadillo矩阵 .例如:
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.
还要记住,犰狳以列主要顺序,即首先存储第0列,然后存储第1列,第2列,依此类推.这与MATLAB,LAPACK和BLAS所使用的相同.
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.
这篇关于将OpenCV库与Armadillo库结合使用的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!