Concatenate Mat in OpenCV
Asked Answered
M

3

9

I have a couple of images in Mat objects all with same dimensions I'd like to create one bix cv::Mat object to hold them all

So the dimension of the new matrix is: widthNew = widthOld x number of matrices, height remains unchanged.

I found that such a copy could be done using:

void cvCopy(const CvArr* src, CvArr* dst, const CvArr* mask=NULL)

but then, how could the mask be defined three different times for the three matrices?.

Regards, Moataz

Montenegro answered 13/6, 2012 at 16:27 Comment(3)
Be careful that the max size for an IplImage is somewhere around 4000*4000. You might also have to reduce resolution of your input imageBerberine
Mask is optional, you do not need to define one.Backstop
alright. I'll put that in mind, thnxMontenegro
A
6

You use an roi to define an image which is actually a region of the destination image and then copy to that. see Copy an cv::Mat inside a ROI of another one

Allanadale answered 13/6, 2012 at 16:42 Comment(0)
B
15

I think there is an easy way to do this. OpenCV has a not documented methods called hconcat() and vconcat(). The first one is for horizontal concatenation and the second one for vertical concatenation.

You can use them in this way:

Mat A, B;
... //In this part you initialize the Mat A and Mat B.

Mat H, V; //These are the destination matrices
hconcat(A, B, H);
vconcat(A, B, V);

I hope this can help.

Because answered 16/12, 2015 at 15:58 Comment(1)
The destination can be the same as one of the sources so vconcat(out, out1, out); vconcat(out, out2, out); worked well for me in a multithreaded context (for combining output from individual threads).Arson
A
6

You use an roi to define an image which is actually a region of the destination image and then copy to that. see Copy an cv::Mat inside a ROI of another one

Allanadale answered 13/6, 2012 at 16:42 Comment(0)
T
1

You probably want to take look at the source code of cvShowManyImages(), which is a function that takes several images as input and concatenates them into a single 3-channel image to be displayed:

The method used is to set the ROIs of a Single Big image and then resizing and copying the input images on to the Single Big Image.

If you create a destination image big enough to hold your other images, you won't need to resize them.

If you want to know how to convert between IplImage <-> cv::Mat, check this thread.

Think answered 13/6, 2012 at 21:14 Comment(1)
sounds interesting, thnx. I think I'll copy the idea to implement something similar using pure cv::Mat instead of IpImageMontenegro

© 2022 - 2024 — McMap. All rights reserved.