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]);
In OpenCV the main matrix class is called Mat and is contained in the OpenCV-namespace cv. This matrix is not templated but nevertheless can contain different data types. These are indicated by a certain type-number. Additionally, OpenCV provides a templated class called Mat_, which is derived from Mat.
float array[4]; You can define the array without the size. but it should be in this way: float array[] = {3.544, 5.544, 6.544, 6.544};
The Mat class of OpenCV library is used to store the values of an image. It represents an n-dimensional array and is used to store image data of grayscale or color images, voxel volumes, vector fields, point clouds, tensors, histograms, etc. This class comprises of two data parts: the header and a pointer.
CV_32F defines the depth of each element of the matrix, while. CV_32FC1 defines both the depth of each element and the number of channels.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With