Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV displaying 2 images adjacently in the same window

I am trying to display 2 images horizontally adjacent to each other in the same window using OpenCV.

I have tried using adjustROI function for this.Image 1 has 1088 pixels width and 2208 pixels height while Image 2 has 1280 pixels width and 2208 pixels height.Please suggest what could be wrong in the code below.All I am getting is an image of size Image2 with content from Image2 as well.

Mat img_matches=Mat(2208,2368,imgorig.type());//set size as combination of img1 and img2
img_matches.adjustROI(0,0,0,-1280); 
imgorig.copyTo(img_matches);
img_matches.adjustROI(0,0,1088,1280);
imgorig2.copyTo(img_matches);
like image 832
code4fun Avatar asked Oct 30 '12 05:10

code4fun


People also ask

How do I put 2 photos on cv2?

You can add two images with the OpenCV function, cv. add(), or simply by the numpy operation res = img1 + img2. Both images should be of same depth and type, or the second image can just be a scalar value.

How do I view multiple images?

The easiest way to display multiple images in one figure is use figure(), add_subplot(), and imshow() methods of Matplotlib. The approach which is used to follow is first initiating fig object by calling fig=plt. figure() and then add an axes object to the fig by calling add_subplot() method.

How do I put one image on top of another in OpenCV?

The image pasting is a process of overlaying one image on top of the other. To successfully apply this process in OpenCV we need to select Region of Interest (ROI) in the first image, and then apply masking and some logical operations to overlay second image over the first image.


2 Answers

Here's a solution inspired in @misha's answer.

#include <cv.h>
#include <highgui.h>

using namespace cv;

int
main(int argc, char **argv)
{
    Mat im1 = imread(argv[1]);
    Mat im2 = imread(argv[2]);
    Size sz1 = im1.size();
    Size sz2 = im2.size();
    Mat im3(sz1.height, sz1.width+sz2.width, CV_8UC3);
    im1.copyTo(im3(Rect(0, 0, sz1.width, sz1.height)));
    im2.copyTo(im3(Rect(sz1.width, 0, sz2.width, sz2.height)));
    imshow("im3", im3);
    waitKey(0);
    return 0;
}

Instead of using the copy constructor, this solution uses Mat::operator()(const Rect& roi). While both solutions are O(1), this solution seems cleaner.

like image 135
Fábio Perez Avatar answered Dec 06 '22 21:12

Fábio Perez


As the height (rows of Mat) of the images are same, function hconcat maybe used to horizontally concatenate two images (Mat) and thus can be used to display them side-by-side in the same window. OpenCV doc.
It works with both grayscale and color images. The number of color channels in the source matrices must be same.

Mat im1, im2; // source images im1 and im2

Mat newImage;
hconcat(im1, im2, newImage);  // <---- place image side by side

imshow("Display side by side", newImage);
waitKey(0);

For the sake of completeness, vconcat can be similarly used for vertical concatenation.

like image 34
Sumit Dey Avatar answered Dec 06 '22 22:12

Sumit Dey