简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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