OPENCV Realization Panorama _1

Source: Internet
Author: User

Introduction
This is the use of OPENCV to achieve image stitching, which is often said in the framing mode. In this article: http://blog.sina.com.cn/s/blog_4b27c38d01019xlv.html, there is a basic introduction to the pattern. In OPENCV official website: http://docs.opencv.org/modules/stitching/doc/stitching.html, there is a comprehensive basic introduction to this part of the API. Opencv_source_code/samples/cpp/stitching.cpp: OpenCV is a simple version of the website to provide examples of image stitching. Opencv_source_code/samples/cpp/stitching_detailed.cpp: OpenCV is a complex and comprehensive version of image stitching available on the website.

Simple example
First look at the OPENCV implementation of the simplest example of image stitching, which is to cut stitching.cpp to the simplest code, and performance effects.

Specific Code
#include <iostream> #include <fstream> #include "opencv2/highgui/highgui.hpp" #include "opencv2/stitching /stitcher.hpp "using namespace std;using namespace CV; bool Try_use_gpu = true; false;vector<mat> imgs;string result_name = "Result.jpg"; int Parsecmdargs (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); Imshow (Argv[i], IMG);} return 0;} int main (int argc, char* argv[]) {int retval = Parsecmdargs (argc, argv), if (retval) return-1; Mat Pano; Stitcher Stitcher = Stitcher::createdefault (TRY_USE_GPU); Stitcher::status Status = Stitcher.stitch (IMGs, Pano); if (Status! = Stitcher::ok) {cout << "Can ' t stitch images, error code =" << int (status) << Endl;return- 1;} Imwrite (Result_name, Pano); Imshow ("Show", Pano); Cv::waitkey (0); return 0;}


Effect Demo
Run this example:./tmp 1.jpg 2.jpg 3.jpg
The implementation effect is as follows:                                                              input image                    
                                                    Output image                                   

Panorama Example 2
In the previous example, simply use the function: Stitcher.stitch to generate a panorama of the default settings, where you continue to use new ways to generate various types of panoramas.

Implementation Code
The specific code is as follows:
#include <iostream> #include <fstream> #include "opencv2/highgui/highgui.hpp" #include "opencv2/stitching /stitcher.hpp "using namespace std;using namespace CV; BOOL Try_use_gpu = false;vector<mat> imgs;string result_name = "Result.jpg"; int Parsecmdargs (int argc, char** argv) {for (int i = 1; i < argc-1; ++i) {Mat img = imread (argv[i]); if (Img.empty ()) {Cou T << "Can ' t read image '" << argv[i] << "' \ n"; return-1;} Imgs.push_back (IMG); Imshow (Argv[i], IMG);} return 0;} int main (int argc, char* argv[]) {int retval = Parsecmdargs (argc, argv), if (retval) return-1; Mat Pano; /* Create a Stitcher object */stitcher stitcher = Stitcher::createdefault (TRY_USE_GPU); /* Set Build result graph is: 1: Plane, 2: Cylinder, 3: Stereoscopic picture */if (argv[4][0] = = ' 1 ') {planewarper* cw = new Planewarper (); Stitcher.setwarper (CW);} else if (argv[4][0] = = ' 2 ') {sphericalwarper* cw = new Sphericalwarper (); Stitcher.setwarper (CW);} else if (argv[4][0] = = ' 3 ') {stereographicwarper *CW = new Cv::stereographicwarper (); Stitcher.setwarper (CW);}/* Use surf algorithm to find feature points */detail::surffeaturesfinder *featurefinder = new Detail::surffeaturesfinder (); Stitcher.setfeaturesfinder (Featurefinder); /* matches the given image and estimates the camera's rotation */stitcher::status Status = Stitcher.estimatetransform (IMGs); if (Status! = Stitcher::ok) {cout < < "Can ' t stitch images, error code =" << int (status) << endl;return-1;} /* Generate panorama image */status = Stitcher.composepanorama (Pano); if (status! = Stitcher::ok) {cout << "Can ' t stitch images, error C Ode = "<< int (status) << endl;return-1;} Imwrite (Result_name, Pano); Imshow ("Show", Pano); Cv::waitkey (0); return 0;}



Code explanation
1, fill IMGs, fill in the input picture all into the container imgs, and the input picture, show it all.
int Parsecmdargs (int argc, char** argv) {for (int i = 1; i < argc-1; ++i) {Mat img = imread (argv[i]); if (Img.empty ()) {Cou T << "Can ' t read image '" << argv[i] << "' \ n"; return-1;} Imgs.push_back (IMG); Imshow (Argv[i], IMG);} return 0;}
2. Create a Stitcher object.
Stitcher Stitcher = Stitcher::createdefault (TRY_USE_GPU);
 3, set the resulting graph is: 1: Plane, 2: Cylinder, 3: Stereoscopic picture. There are many types of panoramas that can be generated in OPENCV. In the case of the complex version it provides: Stitching_detailed.cpp, there are a variety of options available: plane|cylindrical|spherical|fisheye|stereographic|  compressedplanea2b1|  compressedplanea1.5b1|compressedplaneportraita2b1|compressedplaneportraita1.5b1|paninia2b1| Paninia1.5b1|paniniportraita2b1|paniniportraita1.5b1|mercator|transversemercator in this example, only 3 are used as the selection: 
/* Set Build result graph is: 1: Plane, 2: Cylinder, 3: Stereoscopic picture */if (argv[4][0] = = ' 1 ') {planewarper* cw = new Planewarper (); Stitcher.setwarper (CW);} else if (argv[4][0] = = ' 2 ') {sphericalwarper* cw = new Sphericalwarper (); Stitcher.setwarper (CW);} else if (argv[4][0] = = ' 3 ') {stereographicwarper *CW = new Cv::stereographicwarper (); Stitcher.setwarper (CW);}
4, choose to find the algorithm of feature points, OPENCV provides surf and orb two ways to choose, in this case, the use of surf.
Detail::surffeaturesfinder *featurefinder = new Detail::surffeaturesfinder (); Stitcher.setfeaturesfinder ( Featurefinder);
5, generate the output panorama, here use another way to achieve.
     /* matches the given image and estimates the camera's rotation */        stitcher::status Status = Stitcher.estimatetransform (IMGs);/* Generates a panoramic image */status = Stitcher.composepanorama (Pano); Imwrite (Result_name, Pano); Imshow ("Show", Pano);

Effect Demo
The three types of this example are shown below:

1. Plane

                                   

2. Spherical

                                   

3, three-dimensional

                                   

OPENCV Realization Panorama _1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.