Home > Software engineering >  OpenCV: Stack Vectors to Mat
OpenCV: Stack Vectors to Mat

Time:02-24

I got 3 Vec3f and want to stack them to a 3x3 Matrix (C ). Is there a nice way to to so? In python its easy with numpy, however I dont know if there is a better way than assigin every single value from Vector to the corresponding Mat entry?

Cheers

CodePudding user response:

Yes you can. It depends on the precise packing arrangement that you want, but the simplest way is to simply copy their bytes into a properly sized Mat.

You access the bytes of a single Vec3f instance Vec3f v by using &v[0]. You access the bytes of a matrix Mat m by using m.data (not a function).

Here's an example:

cv::Mat m(3, 3, CV_32FC3);
cv::Vec3f vecs[3];
memcpy((void*)m.data, (const void*)&vecs[0][0], m.width * m.height * 3 * sizeof(float));
  • Related