简体   繁体   English

如何使用float数组中的数据初始化cv :: Mat

[英]How can I initialize a cv::Mat with data from a float array

I need to create a cv::Mat variable that is initialized with my data from a float * array. 我需要创建一个cv::Mat变量,该变量使用float *数组中的数据进行初始化。 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 . 但是cv_matrixAB从不包含float值,更重要的是与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 . 那么cv_matrixAB.data都是0 I have also tried using CV_64F as the type, but I see the same behaivor. 我也尝试使用CV_64F作为类型,但我看到相同的行为。

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. 根据cv::Mat构造函数文档,我应该能够以float *数组的形式提供我的数据。

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 . 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]);

You're doing fine. 你做的不错。 But you should access the mat element by using at<float>() instead of .data (which will give you uchar * ). 但是您应该使用at<float>()而不是.data (它将为您提供uchar * )来访问mat元素。 Or simply use cout << mat; 或者只是使用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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM