Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opencv - how to merge two images

Tags:

c++

image

opencv

I'm new to opencv and i searched on the internet if there is an example of how to merge two images, but didin't found anything good to help me. Can someone help me with some indications or a small code to understand ? thanks in advance

like image 602
Frincu Alexandru Avatar asked Oct 20 '15 14:10

Frincu Alexandru


People also ask

How do I merge two images in python?

Merging two imagesCreate an empty image using the Image. new() function. Paste the images using the paste() function. Save and display the resultant image using the save() and show() functions.


1 Answers

From the comments to the question, you said:

I dont want to blend half from the first picture with the other half from the second. I just waint to print both images, one near the other one

So, starting from these images:

enter image description here

enter image description here

You want this result?

enter image description here

Note that if both images have the same height, you won't see the black background.

Code:

#include <opencv2\opencv.hpp>
using namespace cv;

int main()
{
    // Load images
    Mat3b img1 = imread("path_to_image_1");
    Mat3b img2 = imread("path_to_image_2");

    // Get dimension of final image
    int rows = max(img1.rows, img2.rows);
    int cols = img1.cols + img2.cols;

    // Create a black image
    Mat3b res(rows, cols, Vec3b(0,0,0));

    // Copy images in correct position
    img1.copyTo(res(Rect(0, 0, img1.cols, img1.rows)));
    img2.copyTo(res(Rect(img1.cols, 0, img2.cols, img2.rows)));

    // Show result
    imshow("Img 1", img1);
    imshow("Img 2", img2);
    imshow("Result", res);
    waitKey();

    return 0;
}
like image 53
Miki Avatar answered Oct 25 '22 09:10

Miki