如何将 C++ 数组转换为 opencv Mat

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44453088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:36:46  来源:igfitidea点击:

How to convert C++ array to opencv Mat

c++opencv

提问by mcExchange

What is the fastest way to convert a C++ array into an 2D opencv Mat object? Brute force method would be to loop over all entries of the 2D mat and fill them with values of the array.
Is there a better / faster way to do that? Could I somehow just give pointer of the array to the 2D matrix?
(I'm using opencv 2.4.8)

将 C++ 数组转换为 2D opencv Mat 对象的最快方法是什么?蛮力方法是遍历 2D mat 的所有条目并用数组的值填充它们。
有没有更好/更快的方法来做到这一点?我可以以某种方式将数组的指针指向二维矩阵吗?
(我使用的是 opencv 2.4.8)

回答by api55

Yes there is, in the documentationof cv::Matyou can see how it can be achieved.

是的,在您的文档cv::Mat可以看到它是如何实现的。

Specifically in this line

具体在这一行

C++: Mat::Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP)

C++: Mat::Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP)

This means that you can do something like

这意味着你可以做类似的事情

double x[100][100];

cv::Mat A(100, 100, CV_64F, x);

This will not copy the data to it. You have to also remember that OpenCV data is row major, which means that it has all the columns of one rows and then the next row and so on. So your array has to match this style for it to work.

这不会将数据复制到它。您还必须记住,OpenCV 数据是行主要的,这意味着它具有一行的所有列,然后是下一行,依此类推。所以你的数组必须匹配这种风格才能工作。

About how fast it is, the documentation also talks about it:

关于它的速度有多快,文档也谈到了它:

data– Pointer to the user data. Matrix constructors that take data and step parameters do not allocate matrix data. Instead, they just initialize the matrix header that points to the specified data, which means that no data is copied. This operation is very efficient and can be used to process external data using OpenCV functions. The external data is not automatically deallocated, so you should take care of it.

data– 指向用户数据的指针。接受数据和步骤参数的矩阵构造函数不分配矩阵数据。相反,它们只是初始化指向指定数据的矩阵头,这意味着没有数据被复制。此操作非常高效,可用于使用 OpenCV 函数处理外部数据。外部数据不会自动解除分配,因此您应该注意它。

It will work quite fast, but it is the same array, if you modified it outside of the cv::Mat, it will be modified in the cv::Mat, and if it is destroyed at any point, the datamember of cv::Matwill point to a non-existant place.

它会工作得很快,但它是同一个数组,如果你在 之外修改它cv::Mat,它会在 中被修改cv::Mat,如果它在任何时候被销毁,data成员cv::Mat将指向一个不存在的地方。

UPDATE:

更新:

I forgot to also say, that you can create the cv::Matand do std::memcpy. This way it will copy the data, which may be slower, but the data will be owned by the cv::Matobject and destroyed upon with the cv::Matdestroyer.

我也忘了说,您可以创建cv::Mat并执行std::memcpy。这样它会复制数据,这可能会更慢,但数据将被cv::Mat对象拥有并被销毁器cv::Mat销毁。

double x[100][100];
cv::Mat A(100,100,CV_64F);
std::memcpy(A.data, x, 100*100*sizeof(double));