Stitching input images (panorama) using OpenCV with C++
Last Updated :
26 Apr, 2022
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 opencv.org image for details.
Implementation
CPP
#include <iostream>
#include <fstream>
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/stitching.hpp"
using namespace std;
using namespace cv;
Stitcher::Mode mode = Stitcher::PANORAMA;
vector<Mat> imgs;
int main( int argc, char * argv[])
{
for ( int i = 1; i < argc; ++i)
{
Mat img = imread(argv[i]);
if (img.empty())
{
cout << "Can 't read image ' " << argv[i] << "'\n";
return -1;
}
imgs.push_back(img);
}
Mat pano;
Ptr<Stitcher> stitcher = Stitcher::create(mode, false );
Stitcher::Status status = stitcher->stitch(imgs, pano);
if (status != Stitcher::OK)
{
cout << "Can't stitch images\n";
return -1;
}
imwrite("result.jpg", pano);
imshow("Result", pano);
waitKey(0);
return 0;
}
|
Input Images:
Output:
References:
1) http://docs.opencv.org/2.4/modules/stitching/doc/stitching.html
2) http://docs.opencv.org/2.4/modules/stitching/doc/introduction.html
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...