Opencv Panorama _ 1 and opencv Panorama _ 1
Introduction
In this article, we will use opencv to achieve image stitching, that is, the common scene capturing mode. In this article: http://blog.sina.com.cn/s/blog_4b27c38d01019xlv.html, there is a basic introduction to this mode. On the official website of opencv: 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: A simple version of Image stitching instance provided by opencv. Opencv_source_code/samples/cpp/stitching_detailed.cpp: An Example of complex and comprehensive image stitching provided by the opencv official website.
Simple instance
First, let's take a look at the simplest example of using opencv to splice images. This is to crop stitching. cpp to the simplest code and display results.
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 demonstration
Run this example:./tmp 1.jpg 2.jpg 3.jpg
The implementation result is as follows: Input Image
Output Image
Panorama instance 2
In the previous example, we used the simple function stitcher. stitch to generate the default panorama. Here we continue to use the new method to generate various types of panorama.
Implementation Code
The Code is as follows:
# Include <iostream> # include <fstream> # include "opencv2/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 () {cout <"Can't read image'" <argv [I] <"'\ n"; re Turn-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 the result graph to: 1: plane, 2: cylindrical, 3: Stereoscopic Image */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 the Surf algorithm to find feature points */detail: SurfFeaturesFinder * featureFinder = new detail: SurfFeaturesFinder (); stitcher. setFeaturesFinder (featureFinder);/* match the given image and estimated camera rotation */Stitcher: Status status = stitcher. estimateTransform (imgs); if (status! = Stitcher: OK) {cout <"Can't stitch images, error code =" <int (status) <endl; return-1 ;} /* generate a panoramic image */status = stitcher. composePanorama (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 ;}
Code Description
1. Fill in imgs, fill all input images in the container imgs, and display the input images one by one.
int parseCmdArgs(int argc, char** argv){for (int i = 1; i < argc-1; ++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;}
2. Create a stitcher object.
Stitcher stitcher = Stitcher::createDefault(try_use_gpu);
3. Set the result graph to 1: plane, 2: cylinder, and 3: Stereo screen. Opencv provides many types of panorama that can be generated. In the complex version of the instance it provides: stitching_detailed.cpp, you can choose from the following types: plane | cylindrical | spherical | fisheye | stereographic | kernel | compressedPlaneA1.5B1 | kernel | paniniA2B1 | paniniA1.5B1 | kernel | mercator | transverseMercator in this example, only three options are used:
/* Set the result graph to: 1: plane, 2: cylindrical, 3: Stereoscopic Image */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. Select the algorithm for finding feature points. opencv provides two methods: Surf and Orb. In this example, we use Surf.
detail::SurfFeaturesFinder *featureFinder = new detail::SurfFeaturesFinder();stitcher.setFeaturesFinder(featureFinder);
5. generate an output panorama, which is implemented in another way.
/* Match the given image and the estimated camera rotation */Stitcher: Status status = stitcher. estimateTransform (imgs);/* generate a panoramic image */status = stitcher. composePanorama (pano); imwrite (result_name, pano); imshow ("show", pano );
Effect demonstration
The three types in this example are shown as follows:
1. Plane
2. Sphere
3. Stereo