OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Sun, 13 Jan 2013 02:49:30 -0600template initialization of opencv Mat from 2dVectorhttp://answers.opencv.org/question/5944/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!
Sat, 12 Jan 2013 03:13:54 -0600http://answers.opencv.org/question/5944/template-initialization-of-opencv-mat-from-2dvector/Answer by Michael Burdinov for <p>Hi, </p>
<p>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.</p>
<p>the function is simple like:</p>
<pre><code>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;
}
</code></pre>
<p>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</p>
<p>Thank you!</p>
http://answers.opencv.org/question/5944/template-initialization-of-opencv-mat-from-2dvector/?answer=5977#post-id-5977It is not possible to initialize Mat with vector of vectors. Main reason is that by default Mat(vector) does not perform copy of data. This is just a header that points to contents of the vector itself. It can be done because content of vector is continious in memory. This can't be done with vector of vectors because content of each sub-vector may be in different place in memory and so it must be copied in order to create Mat.
It is possible to initailize Mat with NumType as long as it is legal type for Mat:
Mat_<NumType> mtx(vect.size(),vect[0].size());
Also more effective way of coping data to mtx is:
for(int i=0; i<vect.size(); i++)
{
if (vect[0].size() != vect[i].size())
// throw some error
memcpy(mtx.ptr(i), &(vect[i][0]), vect[i].size()*sizeof(NumType));
}
Sun, 13 Jan 2013 02:49:30 -0600http://answers.opencv.org/question/5944/template-initialization-of-opencv-mat-from-2dvector/?answer=5977#post-id-5977