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

13 comments:

  1. thanks a lot! :)

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. I tryed to use this solution in my code but it crash, I tryed the same code shown above and it crash too, I think it is because the memcpy. Any idea?

    ReplyDelete
    Replies
    1. Does the length of the data was entered correctly in memcpy param 3rd?
      on line 15, CV_32FC1 means 32bit float and 1 channel.
      Thank you.

      Delete
  4. The data are the same that you used. I copy&paste your code.

    ReplyDelete
  5. Anonymous5/4/16 02:55

    For people who have problems with this code to copy a vector to a mat, you can also use this:

    vector oOutput; // fill this with data
    int iHeight = 5;
    int iWidth = 4;

    Mat oRealOutput;

    oRealOutput = Mat(iHeight, iWidth, CV_64F);

    int k = 0;
    for (int i = 0; i< oRealOutput.rows; ++i)
    {
    for (int j = 0; j< oRealOutput.cols; ++j)
    {
    oRealOutput.at< double >(i, j) = oOutput[k];
    k = k + 1;
    }
    }

    ReplyDelete
    Replies
    1. Thank you for answer.
      May be, this problem is 64 and 32bit problem.
      If your build option is 64bit, than you use CV_64.
      otherwise your build option is 32bit, than you use like sample code.

      I think that.

      Thank you

      Delete
  6. The Vector doesnt have any attributes "assign"

    ReplyDelete
  7. Great things you’ve always shared with us. Just keep writing this kind of posts.The time which was wasted in traveling for tuition now it can be used for studies.Thanks convert into vector

    ReplyDelete
  8. But a copy machine is not always cheap. photocopy machine

    ReplyDelete
  9. We are elite in our services because we provide the best Embroidery Digitizing Services. We have introduced a variety of deals Embroidery designs and packages for our clients. Embroidery Digitizing

    ReplyDelete
  10. Bitmap workmanship is related to document names finishing off with .gif, .bmp, .jpeg, .jpg, .png, and .pcx. Vector workmanship records are ordinarily spared as .eps, .ai, or .cdr documents. Vector workmanship is numerical calculations made utilizing programming programs, for example, Adobe Illustrator, Freehand, Corel Draw, Quark and a couple of others. These projects use mathematic conditions and mathematical natives (focuses, lines, and shapes) to make craftsmanship that is spotless, camera prepared, and can be scaled limitlessly, without losing any quality or lucidity. A similar workmanship might be utilized for a business card, to a banner, to a board. Professional graphic design

    ReplyDelete