Ask Your Question

Make a collage with other images

asked 2013-06-21 14:18:53 -0500

yes123 gravatar image

updated 2013-06-21 14:20:52 -0500

I have a vector<Mat> with images with different size.

Then I have a bigger image (let's say 640x480) I want to make a grid collage like this:

image description

With variable numbers of cols. How would you do that?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2013-06-22 03:09:00 -0500

berak gravatar image

here's my idea:

void tile(const vector<Mat> &src, Mat &dst, int grid_x, int grid_y) {
    // patch size
    int width  = dst.cols/grid_x;
    int height = dst.rows/grid_y;
    // iterate through grid
    int k = 0;
    for(int i = 0; i < grid_y; i++) {
        for(int j = 0; j < grid_x; j++) {
            Mat s = src[k++];

int main(int argc, char **argv)

    Mat img;

    namedWindow("image", WINDOW_NORMAL);
    int gridx=4, gridy=4;
    vector<Mat> vec;
    VideoCapture cap(0);
        cap >> img;
        imshow("image", img); 
        char k = (char) waitKey(10);
        if ( vec.size() == gridx*gridy ) 
    Mat res = Mat(480,640,CV_8UC3);
    return 0;
edit flag offensive delete link more

answered 2013-06-21 14:58:23 -0500

Guanta gravatar image

Think, this: with some slight modifications is exactly what you need. You only need to resize your images beforehand. Good luck!

edit flag offensive delete link more


If I resize the image in images I don't need to check max_height and max_width like you do in your loop... But nyway I don't understand your second loop. Why do you parse every pixels (y<rows and x<cols). Also the given number of cols should be used to calculate the width to resize the inner images

yes123 gravatar imageyes123 ( 2013-06-21 15:07:18 -0500 )edit

you are right, in your case you don't need to find out the proper dimensions but to resize them. The other two loops are not a parsing of pixels but going through the rows/cols of your grid and copying the image to the appropriate location.

Guanta gravatar imageGuanta ( 2013-06-21 15:29:22 -0500 )edit

Question Tools


Asked: 2013-06-21 14:18:53 -0500

Seen: 6,585 times

Last updated: Jun 23 '13