Archive for June, 2014

It is assumed that OpenCV is already installed.

The goal:
To make a perspective transformation on an image using homography and overlay it onto the other image.

Input :
Two image files – “main image” and “logo image”.


Overlayed images




The “logo image” is overlayed onto the main image. We need a homography matrix to transform the image points of “logo image” before it is overlayed. To calculate a homography matrix we need 4 correspomding pair of points from “logo image” and “main image”. The 4 points for “logo image” are taken as the four corners of the image where as the 4 points for “main image” are chosen by the user. Remember each of the 4 points in each image is of the form (x,y).

Once homography matrix is calculated the “logo image” is perspectively projected onto the “main image”. In this implementation the pixels of “logo image” replace the pixels of “main image”. Users can change this to do any other kind of blending.

Running the code:

Github code
Assume that the executable generated is “homography”. The images files(main.jpg,logo.jpg) are passed as two arguments. Note that the image files are present in the Github repository.

$ ./homography main.jpg logo.jpg
The video link with demo :


#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include <iostream>
#include <limits>
#include <numeric>
using namespace cv;
using namespace std;

// We need 4 corresponding 2D points(x,y) to calculate homography.
vector<Point2f> left_image;      // Stores 4 points(x,y) of the logo image. Here the four points are 4 corners of image.
vector<Point2f> right_image;    // stores 4 points that the user clicks(mouse left click) in the main image.

// Image containers for main and logo image
Mat imageMain;
Mat imageLogo;

// Function to add main image and transformed logo image and show final output.
// Icon image replaces the pixels of main image in this implementation.
void showFinal(Mat src1,Mat src2)

    Mat gray,gray_inv,src1final,src2final;
    bitwise_not ( gray, gray_inv );
    Mat finalImage = src1final+src2final;
    namedWindow( "output", WINDOW_AUTOSIZE );


// Here we get four points from the user with left mouse clicks.
// On 5th click we output the overlayed image.
void on_mouse( int e, int x, int y, int d, void *ptr )
    if (e == EVENT_LBUTTONDOWN )
        if(right_image.size() < 4 )

            cout << x << " "<< y <<endl;
            cout << " Calculating Homography " <<endl;
            // Deactivate callback
            cv::setMouseCallback("Display window", NULL, NULL);
            // once we get 4 corresponding points in both images calculate homography matrix
            Mat H = findHomography(  left_image,right_image,0 );
            Mat logoWarped;
            // Warp the logo image to change its perspective
            warpPerspective(imageLogo,logoWarped,H,imageMain.size() );



int main( int argc, char** argv )
//  We need tow argumemts. "Main image" and "logo image"
    if( argc != 3)
        cout <<" Usage: error" << endl;
        return -1;

// Load images from arguments passed.
    imageMain = imread(argv[1], CV_LOAD_IMAGE_COLOR);
    imageLogo = imread(argv[2], CV_LOAD_IMAGE_COLOR);
// Push the 4 corners of the logo image as the 4 points for correspondence to calculate homography.

    namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", imageMain );

    setMouseCallback("Display window",on_mouse, NULL );

//  Press "Escape button" to exit
        int key=cvWaitKey(10);
        if(key==27) break;

    return 0;