How to merge 3 matrices into 1 in opencv?

只谈情不闲聊 提交于 2019-12-30 07:18:07

问题


I have three matrices, each of size 4x1. I want to copy all of these matrices to another matrix of size 4x3 and call it R. Is there a smart way to do it?


回答1:


You can just use hconcat for horizontal concatenation. You can use it per matrix, e.g. hconcat( mat1, mat2, R ), or apply it directly on a vector or array of matrices.

Here's a sample code:

vector<Mat> matrices = {
    Mat(4, 1, CV_8UC1, Scalar(1)),
    Mat(4, 1, CV_8UC1, Scalar(2)),
    Mat(4, 1, CV_8UC1, Scalar(3)),
};
Mat R;
hconcat( matrices, R );
cout << R << endl;

Here's the result:

[1, 2, 3;
  1, 2, 3;
  1, 2, 3;
  1, 2, 3]
Program ended with exit code: 1

Similarly, if you want to do it vertically (stack by rows), use vconcat.




回答2:


You can use

Mat R(3, 4, CV_32F); // [3 rows x 4 cols] with float values
mat1.copyTo(R.row(0));
mat2.copyTo(R.row(1));
mat3.copyTo(R.row(2));

or

Mat R(4, 3, CV_32F); // [4 rows x 3 cols] with float values
mat1.copyTo(R.col(0));
mat2.copyTo(R.col(1));
mat3.copyTo(R.col(2));

Alternatively, as @sub_o suggested, you can also use hconcat()/vconcat() to concatenate matrices.




回答3:


For those using OpenCv in Python, if you have arrays A, B, and C, and want array D that is horizontal concatenation of the others:

D = cv2.hconcat((A, B, C))

There is also a vconcat method.



来源:https://stackoverflow.com/questions/23042798/how-to-merge-3-matrices-into-1-in-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!