(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 valueint cnt=0;for(int i=0; i< c; ++i)for(int j=0; j< r; ++j)Vf.push_back(cnt++);//create MatMat M=Mat(r,c,CV_32FC1);//copy vector to matmemcpy(M.data,Vf.data(),Vf.size()*sizeof(float));//print Matcout < < M < < endl;printf("/////////////////////////////////////////////////////////////\n");printf("//Mat to vector\n");vector< float> Vf2;//copy mat to vectorVf2.assign((float*)M.datastart, (float*)M.dataend);//confirmcnt=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