Stitching input images (panorama) using OpenCV with C++

This program is intended to create a panorama from a set of images by stitching them together using OpenCV library stitching.hpp and the implementation for the same is done in C++. The program saves the resultant stitched image in the same directory as the program file. If the set of images are not stitched then it exits the program with an error. This error comes due to the fact that the input images don’t have common areas or they don’t share a common patch of pixels.

Steps to implement the code:

1) Make sure OpenCV is installed on the local machine
2) Place the input images in the same directory as the program.
3) Compile the code from the command prompt as usual.
4) While running the code give all the input images as arguments.
5) Check the resultant image by the name “result.jpg”

Stitching Pipeline:

This image shows the basic architecture of how the stitching algorithm works.It is based on the research paper with title “Automatic Panoramic Image Stitching using Invariant Features” by M. Brown and D. Lowe. Refer to the second link in references.

Please refer this image for details.






// CPP program to Stitch
// input images (panorama) using OpenCV 
#include <iostream>
#include <fstream>
// Include header files from OpenCV directory
// required to stitch images.
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/stitching.hpp"
using namespace std;
using namespace cv;
// Define mode for stitching as panoroma 
// (One out of many functions of Stitcher)
Stitcher::Mode mode = Stitcher::PANORAMA;
// Array for pictures
vector<Mat> imgs;
int main(int argc, char* argv[])
    // Get all the images that need to be 
    // stitched as arguments from command line 
    for (int i = 1; i < argc; ++i)
            // Read the ith argument or image 
            // and push into the image array
            Mat img = imread(argv[i]);
            if (img.empty())
                // Exit if image is not present
                cout << "Can't read image '" << argv[i] << "'\n";
                return -1;
    // Define object to store the stitched image
    Mat pano;
    // Create a Stitcher class object with mode panoroma
    Ptr<Stitcher> stitcher = Stitcher::create(mode, false);
    // Command to stitch all the images present in the image array
    Stitcher::Status status = stitcher->stitch(imgs, pano);
    if (status != Stitcher::OK)
        // Check if images could not be stiched
        // status is OK if images are stiched successfully
        cout << "Can't stitch images\n";
        return -1;
    // Store a new image stiched from the given 
    //set of images as "result.jpg"
    imwrite("result.jpg", pano);
    // Show the result
    imshow("Result", pano);
    return 0;


Input Images:




This article is contributed by Shashwat Jain. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up
Article Tags :
Practice Tags :


Please write to us at to report any issue with the above content.