用二维数组初始化 OpenCV Mat [英] initialize an OpenCV Mat with an 2D array

查看:41
本文介绍了用二维数组初始化 OpenCV Mat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想创建一个具有一些值的 OpenCV Mat A(二维),然后使用 A 作为输入将其传递给另一个 OpenCV 函数.

In my application, I want to create a OpenCV Mat A (2-Dimensions) having some values and then pass it to another OpenCV function using A as input.

目前,我正在尝试:

// float data[2][5] = {{1,2,3,4,5},{7,8,9,10,11}}; 
// OR
float data[10] = {1,2,3,4,5,7,8,9,10,11};

// and then
//  A = Mat(1, 5, CV_32FC1, &data, 2);   // init from float 1D - array
// OR 
    A = Mat(2, 5, CV_32FC1, &data, 2);  

对于一维数组,值传递是可以的.但这不适用于二维数组,这是更常见的情况.我如何在 OpenCV 中解决这个问题?

in case of 1D array, the value passing is OK. But this does not work for 2D array, which is even more a common case. How can I solve this in OpenCV??

推荐答案

最初,我使用了 OpenCV 在线指南中的助记符:

Originally, I used the mnemonic from OpenCV online guides:

Mat::Mat(Size size, int type, void* data, size_t step=AUTO_STEP)

但我不明白文档中size_t step=AUTO_STEP 的含义.这意味着我可以省略 step 参数,OpenCV 将使用该参数自动选择 AUTO_STEP

But I didn't understand what the document meant by size_t step=AUTO_STEP. And this means that I can omit the step argument with which OpenCV will automatically choose AUTO_STEP

我试过了,这很有效:

A = Mat(2, 5, CV_32FC1, &data);

用于从数组初始化的 2D Mat

for the 2D Mat initialized from array

这篇关于用二维数组初始化 OpenCV Mat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆