Merging cv::Mat horizontally
22,475
Solution 1
Take a look at hconcat and vconcat.
usage:
Mat M1,M2,HM,VM;
// M1 and M2 - source matrices
// HM,VM - results matrices
...
hconcat(M1,M2,HM); // horizontal concatenation
vconcat(M1,M2,VM); // vertical concatenation
Take care, these methods are not documented.
Solution 2
There is a very simple way of displaying two images side by side. The following function can be used which is provided by opencv.
Mat image1, image2;
hconcat(image1,image2,image1);//Syntax->
hconcat(source1,source2,destination);
This function can also be used to copy a set of columns from an image to another image.
Mat image;
Mat columns=image.colRange(20,30);
hconcat(image,columns,image);
Author by
Tomer
Updated on August 15, 2022Comments
-
Tomer almost 2 years
I want to merge a few
cv::Mat
, when I usemat1.push_back(mat2)
it addmat2
to the end ofmat1
vertically , is there a way to do this horizontally? The only other option I can think of is making everycv::Mat
into acv::RotatedRect
, rotate it, creating a newMat
, merging, rotating everything in the end in the same way, but it sound pointlessly long if there is another way -
Ruchir over 8 yearsMinor typo in the comment on line 3 (HM and VM instead of MH and MV) but doesn't affect the results :)
-
beaker over 7 years
-
Andrey Smorodov over 7 yearsIt mentioned in answer.
-
Teh Sunn Liu over 7 yearsI tried hconcat with different number of rows it crashes. Works only if number of rows are same. but works fine if number of cols are different
-
Andrey Smorodov over 7 yearsYes that's correct and can't be else. You can't combine horizontally matrices with different number of rows. As you can't combine vertically matrices with different number of cols.