1/13/2014

(OpenCV, data type change, copy) vector to Mat, Mat to vector


This post is about how to copy Mat data to vector and copy vector data to Mat.
Reference this example source code.


printf("/////////////////////////////////////////////////////////////\n");
printf("//vector to Mat\n");
int r=3;
int c=4;

vector< float> Vf;

//insert value
int cnt=0;
for(int i=0; i< c; ++i)
for(int j=0; j< r; ++j)
Vf.push_back(cnt++);
//create Mat
Mat M=Mat(r,c,CV_32FC1);
//copy vector to mat
memcpy(M.data,Vf.data(),Vf.size()*sizeof(float));

//print Mat
cout < < M < < endl;


printf("/////////////////////////////////////////////////////////////\n");
printf("//Mat to vector\n");
vector< float> Vf2;

//copy mat to vector
Vf2.assign((float*)M.datastart, (float*)M.dataend);
//confirm
cnt=0;
for(int i=0; i< c; ++i)
{
for(int j=0; j< r; ++j)
printf("%lf ", Vf2[cnt++]);
printf("\n");
}


--

You want to copy image buffer to Mat example source code.
Reference on this page -> http://feelmare.blogspot.kr/2014/01/opencv-mat-class-image-bufferpoint-copy.html