Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate Mat in OpenCV

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

like image 463
Moataz Elmasry Avatar asked Jun 13 '12 16:06

Moataz Elmasry


3 Answers

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.

like image 175
Angie Quijano Avatar answered Nov 06 '22 00:11

Angie Quijano


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

like image 23
Martin Beckett Avatar answered Nov 06 '22 01:11

Martin Beckett


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.

like image 37
karlphillip Avatar answered Nov 06 '22 01:11

karlphillip