Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mat to unsigned char*

Tags:

c++

opencv

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.

like image 739
Abhishek Thakur Avatar asked Dec 19 '12 07:12

Abhishek Thakur


1 Answers

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;
    }
}
like image 179
ArtemStorozhuk Avatar answered Oct 31 '22 04:10

ArtemStorozhuk