Can anyone tell me how can I convert Mat
to unsigned char*
in OpenCV and also whether the data will be an array? Also, I want to know how can the same thing be done for vector<vector<double>>
to float*
so as to make it a pointer of array? thanks.
As was already mentioned you should use a data
member of cv::Mat
:
cv::Mat m;
...
uchar *data = m.data;
About your second question:
first of all, when you convert from double
to float
you lose some data. And there's no ready solution to do that so just use simple cycle and copy vector to the array-pointer:
float* toArray(vector<vector<double> >& arr)
{
if (arr.empty())
{
return NULL;
}
else
{
//I assume that each vector (element of arr) has the same size
int m = arr.size();
int n = arr[0].size();
float *res = new float[m * n];
int count = 0;
for (int i=0; i<m; i++)
{
for (int j=0; j<n; j++)
{
res[count++] = (float) arr[i][j];
}
}
return res;
}
}
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