9

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

Frincu Alexandru
  • 113
  • 1
  • 1
  • 8
  • 5
    what do you mean by "merging"? blending? interpolation? taking objects from one image and place them into the other? You need to explain what you want to achieve since it isn't really clear. – Micka Oct 20 '15 at 14:52
  • Like in this example " http://stackoverflow.com/questions/10256802/how-to-merge-two-images-in-opencv " , but if you see, he has an error , in the right part of the images there is an dark area. I was hoping that someone can explain me better . By " merging " i mean i want to connect 2 images into one, or print two images one close to the other one. – Frincu Alexandru Oct 21 '15 at 09:49
  • this task consists of 2 steps: 1. compute the information where inside the 1st image the 2nd one has to be placed (non-trivial) and 2. decide which pixels of the 1st image should be used and which pixels of the 2nd image should be used, or how to combine (blend) the used pixels. Do you need information about both steps or just the second step? – Micka Oct 21 '15 at 09:53
  • If you could explain me both steps that would be great :D. As i said i am new to opencv so there are a few things that there not clear to me. As i read from the link above there are some functions wich can do the merging, and i am asking if there is an easier way – Frincu Alexandru Oct 21 '15 at 09:57
  • Just to say again, maybe i explained bad, 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. – Frincu Alexandru Oct 21 '15 at 10:00
  • 1. step: one way is to find keypoints that can be found in both images. Use Sift/SURF/ORB, followed by a matching. From the matching you can compute a transformation (typically homography) that describes how the position of both images is related to each other. After that you can create a new images that is big enough to hold both images and you maybe have to adjust the transformation to transform the images to legal panorama positions (no negative pixel positions etc). – Micka Oct 21 '15 at 10:09
  • 2. you copy one of the images to a temporary buffer of panorama size. you warp the other image to another temporary buffer. At that moment both images have their final positions in the their temporary buffers. Now you could just copy both temporary buffers pixels to the final panorama and decide which pixel to use (e.g. the brighter one of both temporary buffers, or the one that is closer to it's original image's center) or you use linear blending or sth. – Micka Oct 21 '15 at 10:11
  • Ok, thank you very much. – Frincu Alexandru Oct 21 '15 at 10:12
  • you should try to implement it step by step, use imshow on intermediate results and ask another question if there is an observed problem. – Micka Oct 21 '15 at 10:30

2 Answers2

18

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;
}
Miki
  • 40,887
  • 13
  • 123
  • 202
1

I have concatenated vertically two image in C# (OpenCvSharp) : (images can be different size)

    private Mat VerticalConcat(Mat image1, Mat image2)
    {
        var smallImage = image1.Cols < image2.Cols ? image1 : image2;
        var bigImage = image1.Cols > image2.Cols ? image1 : image2;
        Mat combine = Mat.Zeros(new OpenCvSharp.CPlusPlus.Size(Math.Abs(image2.Cols - image1.Cols), smallImage.Height), image2.Type());
        Cv2.HConcat(smallImage, combine, combine);
        Cv2.VConcat(bigImage, combine, combine);
        return combine;
    }
Adem Aygun
  • 550
  • 2
  • 6
  • 25