Thursday, 14 April 2016

How to Put/Overlay/Place/Copy Small Image over the Bigger One

This Opencv C++ tutorial is about placing a smaller image over the Bigger one.
Now, In order to overlay one image over the other we need to get the point where we should place the Smaller Image.
And also we need to create a Region of Interest(ROI) on the bigger image.
Thus the Logic of the Code would be:
1. Read the Bigger Image
2. Read the smaller Image
3. Create a Region of Interest over the bigger Image
    cv::Mat small_image;
    cv::Mat big_image;
    ...
   //Somehow fill small_image and big_image with your data
   ...
  small_image.copyTo(big_image(cv::Rect(x,y,small_image.cols, small_image.rows)));
  Refer the Opencv C++ Code below:

//Copying One Image/Overlaying one image over the other
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
 Mat src1,src2;
 src1=imread("C:\\Users\\arjun\\Desktop\\image1.jpg",1);
 src2=imread("C:\\Users\\arjun\\Desktop\\opencv-logo.png",1);
 if(!src1.data)
 { cout<<"Error loading src1"<<endl; return -1;}
 if(!src2.data)
 { cout<<"Error loading src2"<<endl; return -1;}
 if(src1.size <= src2.size)
 { cout<<"Error First Image should be larger than Second Image"<<endl;}
 src2.copyTo(src1(cv::Rect(10,10,src2.cols, src2.rows)));

 namedWindow("Image Window src1",CV_WINDOW_AUTOSIZE);
 imshow("Image Window src1",src1);

 waitKey(0);
 return(0);
}

Input Image1(Bigger Image):

Input Image2(Smaller Image):

Output Image(Overlayed Image):

Note:-
Here we have copied Smaller Image (opencv-logo) over Bigger Image (image1).

1 comment: