Unsigned mother char *

Can someone tell me how I can convert Mat to unsigned char* in OpenCV, and also, will the data be arrays? Also, I want to know how you can do the same for vector<vector<double>> before float* to make it a pointer to an array? thanks.

+7
source share
1 answer

As already mentioned, you should use the data cv::Mat member:

 cv::Mat m; ... uchar *data = m.data; 

About your second question: first of all, when converting from double to float you lose some data. And there is no ready-made solution for this, so just use a simple loop and copy the vector into 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; } } 
+6
source

All Articles