Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

template initialization of opencv Mat from 2dVector

Hi,

I'm writing a function with some lines to convert from a 2d STL vector to OpenCV Mat. Since, OpenCV supports Mat initialization from vector with Mat(vector). But this time, I try a 2D vector and not successful.

the function is simple like:

template <class NumType>
Mat Vect2Mat(vector<vector<NumType>> vect)
{
    Mat mtx = Mat(vect.size(), vect[0].size(), CV_64F, 0);  // don't need to init??
    //Mat mtx;

    // copy data
    for (int i=0; i<vect.size(); i++)
        for (int j=0; j<vect[i].size(); j++)
        {
            mtx.at<NumType>(i,j) = vect[i][j];
            //cout << vect[i][j] << " ";
        }   

    return mtx;
}

So is there a way to initalize Mat mtx accordingly with NumType?? the syntax is always fixed with CV_32F, CV_64F, .... and therefore, very restricted

Thank you!