如何使用浮点数组中的数据初始化cv :: Mat [英] How can I initialize a cv::Mat with data from a float array
问题描述
我需要创建一个cv::Mat
变量,该变量用来自float *
数组的数据初始化.
这应该是基本的,但是我很难弄清楚.
我有代码:
float *matrixAB = <120 floating point array created elsewhere>;
cv::Mat cv_matrixAB = cv::Mat(12, 10, CV_32F, &matrixAB);
但cv_matrixAB
从不包含float
值,更重要的是与matrixAB
中包含的数据不匹配.
如果我将行更改为:
cv::Mat cv_matrixAB = cv::Mat(12, 10, CV_32F, matrixAB);
则cv_matrixAB.data
均为0
.我也尝试过使用CV_64F
作为类型,但是我看到了相同的行为.
谁能帮助我确定我要去哪里?根据cv::Mat
构造函数文档,我应该能够以float *
数组的形式提供数据.>
更新:此处的更多信息:
甚至以下代码也不起作用. printf
显示63
,这当然不是dummy_query_data
中的值.
float dummy_query_data[10] = { 1, 2, 3, 4,
5, 6, 7, 8 };
cv::Mat dummy_query = cv::Mat(2, 4, CV_32F, dummy_query_data);
printf("%f\n", (float)dummy_query.data[3]);
您还好.但是,您应该使用at<float>()
而不是 .data
(这会给你uchar *
).或者直接使用cout << mat;
打印其所有元素.它将为您带来预期的结果.
float dummy_query_data[10] = { 1, 2, 3, 4, 5, 6, 7, 8 };
cv::Mat dummy_query = cv::Mat(2, 4, CV_32F, dummy_query_data);
cout << dummy_query.at<float>(0,2) << endl;
cout << dummy_query << endl;
它将输出:
3
[1, 2, 3, 4;
5, 6, 7, 8]
I need to create a cv::Mat
variable that is initialized with my data from a float *
array.
This should be basic, but I'm having trouble figuring it out.
I have the code:
float *matrixAB = <120 floating point array created elsewhere>;
cv::Mat cv_matrixAB = cv::Mat(12, 10, CV_32F, &matrixAB);
but cv_matrixAB
never contains float
values, and more importantly doesn't match the data contained in matrixAB
.
If I change the line to:
cv::Mat cv_matrixAB = cv::Mat(12, 10, CV_32F, matrixAB);
then the cv_matrixAB.data
are all 0
. I have also tried using CV_64F
as the type, but I see the same behaivor.
Can anyone help me identify where I am going wrong? According to the cv::Mat
constructor documentation, I should be able to provide my data in the form of a float *
array.
Update: a little more info here:
Even the following code does not work. The printf
displays 63
, which of course is not a value in dummy_query_data
.
float dummy_query_data[10] = { 1, 2, 3, 4,
5, 6, 7, 8 };
cv::Mat dummy_query = cv::Mat(2, 4, CV_32F, dummy_query_data);
printf("%f\n", (float)dummy_query.data[3]);
You're doing fine. But you should access the mat element by using at<float>()
instead of .data
(which will give you uchar *
). Or simply use cout << mat;
to print all its elements. It will give you the expected result.
float dummy_query_data[10] = { 1, 2, 3, 4, 5, 6, 7, 8 };
cv::Mat dummy_query = cv::Mat(2, 4, CV_32F, dummy_query_data);
cout << dummy_query.at<float>(0,2) << endl;
cout << dummy_query << endl;
It will output:
3
[1, 2, 3, 4;
5, 6, 7, 8]
这篇关于如何使用浮点数组中的数据初始化cv :: Mat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!