Create cv::Mat header for const data

Typically if my data is non-const, I can initialize a cv::Mat header on top of it for algebraic manipulation.

float awesome_data[24] = {0};
cv::Mat awesome_mat = cv::Mat(6, 4, CV_32F, awesome_data);

But, if my data is const

const float awesome_data[24] = {0};
cv::Mat awesome_mat = cv::Mat(6, 4, CV_32F, awesome_data);

will have an error: unable to convert from const void * to void *. I know that I won't be changing awesome_mat, what is the best way to do this?

Currently, I have to do a const cast

const float awesome_data[24] = {0};
cv::Mat awesome_mat = cv::Mat(6, 4, CV_32F, const_cast<float *>(awesome_data));

Answers


The data in cv::Mat can always be modified. To be more safe you should copy the data:

const float awesome_data[24] = {0};
cv::Mat awesome_mat = cv::Mat(6, 4, CV_32F, const_cast<float *>(awesome_data)).clone();

With const cv::Mat only the matrix header is readonly but not the data (Differences of using "const cv::Mat &", "cv::Mat &", "cv::Mat" or "const cv::Mat" as function parameters?):

const cv::Mat mat = cv::Mat::zeros(6, 4, CV_32F);
mat += 1; // compiles and runs fine!

As far as I know, OpenCV does not have a const_Mat class. However, by using

const float awesome_data[24] = {0};
const cv::Mat awesome_mat = cv::Mat(6, 4, CV_32F, const_cast<float *>(awesome_data));

you preserve constness.


The only way to do this completely safely is to make a copy of awesome_data (one way or another). The solution by @R1tschY makes a copy of the data using an OpenCV method, which itself requires a const_cast. To avoid the const_cast altogether, the memory should be copied outside of OpenCV (since OpenCV does not respect const data). This would be one way:

const float awesome_data[24] = {0};
cv::Mat awesome_mat = cv::Mat(6, 4, CV_32F); //Allocates its own memory
memcpy(awesome_mat.data, awesome_data, 24*sizeof(float)); //Copy into the allocated memory.

I qould type it without const cast as:

const float awesome_data[24] = {0}; 
Mat awesome_mat = Mat(6, 4, CV_32F, (void*)awesome_data,0); 

Need Your Help

How do I combine two media queries?

css media-queries

How do I combine two media queries? One is for smaller devices and one for bigger devices, but only in portrait mode!

How to pass a parcelable object that contains a list of objects?

android android-activity android-intent

I have created a Parcelable object below, my object contains a List of Products. In my constructor how do I handle re-creating my Parcelable for the List?