Concatenate Mat in OpenCV

Moataz Elmasry picture Moataz Elmasry · Jun 13, 2012 · Viewed 20.7k times · Source

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

Answer

Angie Quijano picture Angie Quijano · Dec 16, 2015

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.