1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                          License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
23 //   * Redistribution's in binary form must reproduce the above copyright notice,
24 //     this list of conditions and the following disclaimer in the documentation
25 //     and/or other materials provided with the distribution.
26 //
27 //   * The name of the copyright holders may not be used to endorse or promote products
28 //     derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42 
43 #ifndef __OPENCV_STITCHING_STITCHER_HPP__
44 #define __OPENCV_STITCHING_STITCHER_HPP__
45 
46 #include "opencv2/core.hpp"
47 #include "opencv2/features2d.hpp"
48 #include "opencv2/stitching/warpers.hpp"
49 #include "opencv2/stitching/detail/matchers.hpp"
50 #include "opencv2/stitching/detail/motion_estimators.hpp"
51 #include "opencv2/stitching/detail/exposure_compensate.hpp"
52 #include "opencv2/stitching/detail/seam_finders.hpp"
53 #include "opencv2/stitching/detail/blenders.hpp"
54 #include "opencv2/stitching/detail/camera.hpp"
55 
56 /**
57 @defgroup stitching Images stitching
58 
59 This figure illustrates the stitching module pipeline implemented in the Stitcher class. Using that
60 class it's possible to configure/remove some steps, i.e. adjust the stitching pipeline according to
61 the particular needs. All building blocks from the pipeline are available in the detail namespace,
62 one can combine and use them separately.
63 
64 The implemented stitching pipeline is very similar to the one proposed in @cite BL07 .
65 
66 ![image](StitchingPipeline.jpg)
67 
68 @{
69     @defgroup stitching_match Features Finding and Images Matching
70     @defgroup stitching_rotation Rotation Estimation
71     @defgroup stitching_autocalib Autocalibration
72     @defgroup stitching_warp Images Warping
73     @defgroup stitching_seam Seam Estimation
74     @defgroup stitching_exposure Exposure Compensation
75     @defgroup stitching_blend Image Blenders
76 @}
77   */
78 
79 namespace cv {
80 
81 //! @addtogroup stitching
82 //! @{
83 
84 /** @brief High level image stitcher.
85 
86 It's possible to use this class without being aware of the entire stitching pipeline. However, to
87 be able to achieve higher stitching stability and quality of the final images at least being
88 familiar with the theory is recommended.
89 
90 @note
91    -   A basic example on image stitching can be found at
92         opencv_source_code/samples/cpp/stitching.cpp
93     -   A detailed example on image stitching can be found at
94         opencv_source_code/samples/cpp/stitching_detailed.cpp
95  */
96 class CV_EXPORTS_W Stitcher
97 {
98 public:
99     enum { ORIG_RESOL = -1 };
100     enum Status
101     {
102         OK = 0,
103         ERR_NEED_MORE_IMGS = 1,
104         ERR_HOMOGRAPHY_EST_FAIL = 2,
105         ERR_CAMERA_PARAMS_ADJUST_FAIL = 3
106     };
107 
108    // Stitcher() {}
109     /** @brief Creates a stitcher with the default parameters.
110 
111     @param try_use_gpu Flag indicating whether GPU should be used whenever it's possible.
112     @return Stitcher class instance.
113      */
114     static Stitcher createDefault(bool try_use_gpu = false);
115 
registrationResol() const116     CV_WRAP double registrationResol() const { return registr_resol_; }
setRegistrationResol(double resol_mpx)117     CV_WRAP void setRegistrationResol(double resol_mpx) { registr_resol_ = resol_mpx; }
118 
seamEstimationResol() const119     CV_WRAP double seamEstimationResol() const { return seam_est_resol_; }
setSeamEstimationResol(double resol_mpx)120     CV_WRAP void setSeamEstimationResol(double resol_mpx) { seam_est_resol_ = resol_mpx; }
121 
compositingResol() const122     CV_WRAP double compositingResol() const { return compose_resol_; }
setCompositingResol(double resol_mpx)123     CV_WRAP void setCompositingResol(double resol_mpx) { compose_resol_ = resol_mpx; }
124 
panoConfidenceThresh() const125     CV_WRAP double panoConfidenceThresh() const { return conf_thresh_; }
setPanoConfidenceThresh(double conf_thresh)126     CV_WRAP void setPanoConfidenceThresh(double conf_thresh) { conf_thresh_ = conf_thresh; }
127 
waveCorrection() const128     CV_WRAP bool waveCorrection() const { return do_wave_correct_; }
setWaveCorrection(bool flag)129     CV_WRAP void setWaveCorrection(bool flag) { do_wave_correct_ = flag; }
130 
waveCorrectKind() const131     detail::WaveCorrectKind waveCorrectKind() const { return wave_correct_kind_; }
setWaveCorrectKind(detail::WaveCorrectKind kind)132     void setWaveCorrectKind(detail::WaveCorrectKind kind) { wave_correct_kind_ = kind; }
133 
featuresFinder()134     Ptr<detail::FeaturesFinder> featuresFinder() { return features_finder_; }
featuresFinder() const135     const Ptr<detail::FeaturesFinder> featuresFinder() const { return features_finder_; }
setFeaturesFinder(Ptr<detail::FeaturesFinder> features_finder)136     void setFeaturesFinder(Ptr<detail::FeaturesFinder> features_finder)
137         { features_finder_ = features_finder; }
138 
featuresMatcher()139     Ptr<detail::FeaturesMatcher> featuresMatcher() { return features_matcher_; }
featuresMatcher() const140     const Ptr<detail::FeaturesMatcher> featuresMatcher() const { return features_matcher_; }
setFeaturesMatcher(Ptr<detail::FeaturesMatcher> features_matcher)141     void setFeaturesMatcher(Ptr<detail::FeaturesMatcher> features_matcher)
142         { features_matcher_ = features_matcher; }
143 
matchingMask() const144     const cv::UMat& matchingMask() const { return matching_mask_; }
setMatchingMask(const cv::UMat & mask)145     void setMatchingMask(const cv::UMat &mask)
146     {
147         CV_Assert(mask.type() == CV_8U && mask.cols == mask.rows);
148         matching_mask_ = mask.clone();
149     }
150 
bundleAdjuster()151     Ptr<detail::BundleAdjusterBase> bundleAdjuster() { return bundle_adjuster_; }
bundleAdjuster() const152     const Ptr<detail::BundleAdjusterBase> bundleAdjuster() const { return bundle_adjuster_; }
setBundleAdjuster(Ptr<detail::BundleAdjusterBase> bundle_adjuster)153     void setBundleAdjuster(Ptr<detail::BundleAdjusterBase> bundle_adjuster)
154         { bundle_adjuster_ = bundle_adjuster; }
155 
warper()156     Ptr<WarperCreator> warper() { return warper_; }
warper() const157     const Ptr<WarperCreator> warper() const { return warper_; }
setWarper(Ptr<WarperCreator> creator)158     void setWarper(Ptr<WarperCreator> creator) { warper_ = creator; }
159 
exposureCompensator()160     Ptr<detail::ExposureCompensator> exposureCompensator() { return exposure_comp_; }
exposureCompensator() const161     const Ptr<detail::ExposureCompensator> exposureCompensator() const { return exposure_comp_; }
setExposureCompensator(Ptr<detail::ExposureCompensator> exposure_comp)162     void setExposureCompensator(Ptr<detail::ExposureCompensator> exposure_comp)
163         { exposure_comp_ = exposure_comp; }
164 
seamFinder()165     Ptr<detail::SeamFinder> seamFinder() { return seam_finder_; }
seamFinder() const166     const Ptr<detail::SeamFinder> seamFinder() const { return seam_finder_; }
setSeamFinder(Ptr<detail::SeamFinder> seam_finder)167     void setSeamFinder(Ptr<detail::SeamFinder> seam_finder) { seam_finder_ = seam_finder; }
168 
blender()169     Ptr<detail::Blender> blender() { return blender_; }
blender() const170     const Ptr<detail::Blender> blender() const { return blender_; }
setBlender(Ptr<detail::Blender> b)171     void setBlender(Ptr<detail::Blender> b) { blender_ = b; }
172 
173     /** @overload */
174     CV_WRAP Status estimateTransform(InputArrayOfArrays images);
175     /** @brief These functions try to match the given images and to estimate rotations of each camera.
176 
177     @note Use the functions only if you're aware of the stitching pipeline, otherwise use
178     Stitcher::stitch.
179 
180     @param images Input images.
181     @param rois Region of interest rectangles.
182     @return Status code.
183      */
184     Status estimateTransform(InputArrayOfArrays images, const std::vector<std::vector<Rect> > &rois);
185 
186     /** @overload */
187     CV_WRAP Status composePanorama(OutputArray pano);
188     /** @brief These functions try to compose the given images (or images stored internally from the other function
189     calls) into the final pano under the assumption that the image transformations were estimated
190     before.
191 
192     @note Use the functions only if you're aware of the stitching pipeline, otherwise use
193     Stitcher::stitch.
194 
195     @param images Input images.
196     @param pano Final pano.
197     @return Status code.
198      */
199     Status composePanorama(InputArrayOfArrays images, OutputArray pano);
200 
201     /** @overload */
202     CV_WRAP Status stitch(InputArrayOfArrays images, OutputArray pano);
203     /** @brief These functions try to stitch the given images.
204 
205     @param images Input images.
206     @param rois Region of interest rectangles.
207     @param pano Final pano.
208     @return Status code.
209      */
210     Status stitch(InputArrayOfArrays images, const std::vector<std::vector<Rect> > &rois, OutputArray pano);
211 
component() const212     std::vector<int> component() const { return indices_; }
cameras() const213     std::vector<detail::CameraParams> cameras() const { return cameras_; }
workScale() const214     CV_WRAP double workScale() const { return work_scale_; }
215 
216 private:
217     //Stitcher() {}
218 
219     Status matchImages();
220     Status estimateCameraParams();
221 
222     double registr_resol_;
223     double seam_est_resol_;
224     double compose_resol_;
225     double conf_thresh_;
226     Ptr<detail::FeaturesFinder> features_finder_;
227     Ptr<detail::FeaturesMatcher> features_matcher_;
228     cv::UMat matching_mask_;
229     Ptr<detail::BundleAdjusterBase> bundle_adjuster_;
230     bool do_wave_correct_;
231     detail::WaveCorrectKind wave_correct_kind_;
232     Ptr<WarperCreator> warper_;
233     Ptr<detail::ExposureCompensator> exposure_comp_;
234     Ptr<detail::SeamFinder> seam_finder_;
235     Ptr<detail::Blender> blender_;
236 
237     std::vector<cv::UMat> imgs_;
238     std::vector<std::vector<cv::Rect> > rois_;
239     std::vector<cv::Size> full_img_sizes_;
240     std::vector<detail::ImageFeatures> features_;
241     std::vector<detail::MatchesInfo> pairwise_matches_;
242     std::vector<cv::UMat> seam_est_imgs_;
243     std::vector<int> indices_;
244     std::vector<detail::CameraParams> cameras_;
245     double work_scale_;
246     double seam_scale_;
247     double seam_work_aspect_;
248     double warped_image_scale_;
249 };
250 
251 CV_EXPORTS_W Ptr<Stitcher> createStitcher(bool try_use_gpu = false);
252 
253 //! @} stitching
254 
255 } // namespace cv
256 
257 #endif // __OPENCV_STITCHING_STITCHER_HPP__
258