stitching.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. #ifndef __OPENCV_STITCHING_STITCHER_HPP__
  43. #define __OPENCV_STITCHING_STITCHER_HPP__
  44. #include "opencv2/core.hpp"
  45. #include "opencv2/features2d.hpp"
  46. #include "opencv2/stitching/warpers.hpp"
  47. #include "opencv2/stitching/detail/matchers.hpp"
  48. #include "opencv2/stitching/detail/motion_estimators.hpp"
  49. #include "opencv2/stitching/detail/exposure_compensate.hpp"
  50. #include "opencv2/stitching/detail/seam_finders.hpp"
  51. #include "opencv2/stitching/detail/blenders.hpp"
  52. #include "opencv2/stitching/detail/camera.hpp"
  53. /**
  54. @defgroup stitching Images stitching
  55. This figure illustrates the stitching module pipeline implemented in the Stitcher class. Using that
  56. class it's possible to configure/remove some steps, i.e. adjust the stitching pipeline according to
  57. the particular needs. All building blocks from the pipeline are available in the detail namespace,
  58. one can combine and use them separately.
  59. The implemented stitching pipeline is very similar to the one proposed in @cite BL07 .
  60. ![image](StitchingPipeline.jpg)
  61. @{
  62. @defgroup stitching_match Features Finding and Images Matching
  63. @defgroup stitching_rotation Rotation Estimation
  64. @defgroup stitching_autocalib Autocalibration
  65. @defgroup stitching_warp Images Warping
  66. @defgroup stitching_seam Seam Estimation
  67. @defgroup stitching_exposure Exposure Compensation
  68. @defgroup stitching_blend Image Blenders
  69. @}
  70. */
  71. namespace cv {
  72. //! @addtogroup stitching
  73. //! @{
  74. /** @brief High level image stitcher.
  75. It's possible to use this class without being aware of the entire stitching pipeline. However, to
  76. be able to achieve higher stitching stability and quality of the final images at least being
  77. familiar with the theory is recommended.
  78. @note
  79. - A basic example on image stitching can be found at
  80. opencv_source_code/samples/cpp/stitching.cpp
  81. - A detailed example on image stitching can be found at
  82. opencv_source_code/samples/cpp/stitching_detailed.cpp
  83. */
  84. class CV_EXPORTS_W Stitcher
  85. {
  86. public:
  87. enum { ORIG_RESOL = -1 };
  88. enum Status
  89. {
  90. OK = 0,
  91. ERR_NEED_MORE_IMGS = 1,
  92. ERR_HOMOGRAPHY_EST_FAIL = 2,
  93. ERR_CAMERA_PARAMS_ADJUST_FAIL = 3
  94. };
  95. // Stitcher() {}
  96. /** @brief Creates a stitcher with the default parameters.
  97. @param try_use_gpu Flag indicating whether GPU should be used whenever it's possible.
  98. @return Stitcher class instance.
  99. */
  100. static Stitcher createDefault(bool try_use_gpu = false);
  101. CV_WRAP double registrationResol() const { return registr_resol_; }
  102. CV_WRAP void setRegistrationResol(double resol_mpx) { registr_resol_ = resol_mpx; }
  103. CV_WRAP double seamEstimationResol() const { return seam_est_resol_; }
  104. CV_WRAP void setSeamEstimationResol(double resol_mpx) { seam_est_resol_ = resol_mpx; }
  105. CV_WRAP double compositingResol() const { return compose_resol_; }
  106. CV_WRAP void setCompositingResol(double resol_mpx) { compose_resol_ = resol_mpx; }
  107. CV_WRAP double panoConfidenceThresh() const { return conf_thresh_; }
  108. CV_WRAP void setPanoConfidenceThresh(double conf_thresh) { conf_thresh_ = conf_thresh; }
  109. CV_WRAP bool waveCorrection() const { return do_wave_correct_; }
  110. CV_WRAP void setWaveCorrection(bool flag) { do_wave_correct_ = flag; }
  111. detail::WaveCorrectKind waveCorrectKind() const { return wave_correct_kind_; }
  112. void setWaveCorrectKind(detail::WaveCorrectKind kind) { wave_correct_kind_ = kind; }
  113. Ptr<detail::FeaturesFinder> featuresFinder() { return features_finder_; }
  114. const Ptr<detail::FeaturesFinder> featuresFinder() const { return features_finder_; }
  115. void setFeaturesFinder(Ptr<detail::FeaturesFinder> features_finder)
  116. { features_finder_ = features_finder; }
  117. Ptr<detail::FeaturesMatcher> featuresMatcher() { return features_matcher_; }
  118. const Ptr<detail::FeaturesMatcher> featuresMatcher() const { return features_matcher_; }
  119. void setFeaturesMatcher(Ptr<detail::FeaturesMatcher> features_matcher)
  120. { features_matcher_ = features_matcher; }
  121. const cv::UMat& matchingMask() const { return matching_mask_; }
  122. void setMatchingMask(const cv::UMat &mask)
  123. {
  124. CV_Assert(mask.type() == CV_8U && mask.cols == mask.rows);
  125. matching_mask_ = mask.clone();
  126. }
  127. Ptr<detail::BundleAdjusterBase> bundleAdjuster() { return bundle_adjuster_; }
  128. const Ptr<detail::BundleAdjusterBase> bundleAdjuster() const { return bundle_adjuster_; }
  129. void setBundleAdjuster(Ptr<detail::BundleAdjusterBase> bundle_adjuster)
  130. { bundle_adjuster_ = bundle_adjuster; }
  131. Ptr<WarperCreator> warper() { return warper_; }
  132. const Ptr<WarperCreator> warper() const { return warper_; }
  133. void setWarper(Ptr<WarperCreator> creator) { warper_ = creator; }
  134. Ptr<detail::ExposureCompensator> exposureCompensator() { return exposure_comp_; }
  135. const Ptr<detail::ExposureCompensator> exposureCompensator() const { return exposure_comp_; }
  136. void setExposureCompensator(Ptr<detail::ExposureCompensator> exposure_comp)
  137. { exposure_comp_ = exposure_comp; }
  138. Ptr<detail::SeamFinder> seamFinder() { return seam_finder_; }
  139. const Ptr<detail::SeamFinder> seamFinder() const { return seam_finder_; }
  140. void setSeamFinder(Ptr<detail::SeamFinder> seam_finder) { seam_finder_ = seam_finder; }
  141. Ptr<detail::Blender> blender() { return blender_; }
  142. const Ptr<detail::Blender> blender() const { return blender_; }
  143. void setBlender(Ptr<detail::Blender> b) { blender_ = b; }
  144. /** @overload */
  145. CV_WRAP Status estimateTransform(InputArrayOfArrays images);
  146. /** @brief These functions try to match the given images and to estimate rotations of each camera.
  147. @note Use the functions only if you're aware of the stitching pipeline, otherwise use
  148. Stitcher::stitch.
  149. @param images Input images.
  150. @param rois Region of interest rectangles.
  151. @return Status code.
  152. */
  153. Status estimateTransform(InputArrayOfArrays images, const std::vector<std::vector<Rect> > &rois);
  154. /** @overload */
  155. CV_WRAP Status composePanorama(OutputArray pano);
  156. /** @brief These functions try to compose the given images (or images stored internally from the other function
  157. calls) into the final pano under the assumption that the image transformations were estimated
  158. before.
  159. @note Use the functions only if you're aware of the stitching pipeline, otherwise use
  160. Stitcher::stitch.
  161. @param images Input images.
  162. @param pano Final pano.
  163. @return Status code.
  164. */
  165. Status composePanorama(InputArrayOfArrays images, OutputArray pano);
  166. /** @overload */
  167. CV_WRAP Status stitch(InputArrayOfArrays images, OutputArray pano);
  168. /** @brief These functions try to stitch the given images.
  169. @param images Input images.
  170. @param rois Region of interest rectangles.
  171. @param pano Final pano.
  172. @return Status code.
  173. */
  174. Status stitch(InputArrayOfArrays images, const std::vector<std::vector<Rect> > &rois, OutputArray pano);
  175. std::vector<int> component() const { return indices_; }
  176. std::vector<detail::CameraParams> cameras() const { return cameras_; }
  177. CV_WRAP double workScale() const { return work_scale_; }
  178. private:
  179. //Stitcher() {}
  180. Status matchImages();
  181. Status estimateCameraParams();
  182. double registr_resol_;
  183. double seam_est_resol_;
  184. double compose_resol_;
  185. double conf_thresh_;
  186. Ptr<detail::FeaturesFinder> features_finder_;
  187. Ptr<detail::FeaturesMatcher> features_matcher_;
  188. cv::UMat matching_mask_;
  189. Ptr<detail::BundleAdjusterBase> bundle_adjuster_;
  190. bool do_wave_correct_;
  191. detail::WaveCorrectKind wave_correct_kind_;
  192. Ptr<WarperCreator> warper_;
  193. Ptr<detail::ExposureCompensator> exposure_comp_;
  194. Ptr<detail::SeamFinder> seam_finder_;
  195. Ptr<detail::Blender> blender_;
  196. std::vector<cv::UMat> imgs_;
  197. std::vector<std::vector<cv::Rect> > rois_;
  198. std::vector<cv::Size> full_img_sizes_;
  199. std::vector<detail::ImageFeatures> features_;
  200. std::vector<detail::MatchesInfo> pairwise_matches_;
  201. std::vector<cv::UMat> seam_est_imgs_;
  202. std::vector<int> indices_;
  203. std::vector<detail::CameraParams> cameras_;
  204. double work_scale_;
  205. double seam_scale_;
  206. double seam_work_aspect_;
  207. double warped_image_scale_;
  208. };
  209. CV_EXPORTS_W Ptr<Stitcher> createStitcher(bool try_use_gpu = false);
  210. //! @} stitching
  211. } // namespace cv
  212. #endif // __OPENCV_STITCHING_STITCHER_HPP__