stitching.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. #if defined(Status)
  54. # warning Detected X11 'Status' macro definition, it can cause build conflicts. Please, include this header before any X11 headers.
  55. #endif
  56. /**
  57. @defgroup stitching Images stitching
  58. This figure illustrates the stitching module pipeline implemented in the Stitcher class. Using that
  59. class it's possible to configure/remove some steps, i.e. adjust the stitching pipeline according to
  60. the particular needs. All building blocks from the pipeline are available in the detail namespace,
  61. one can combine and use them separately.
  62. The implemented stitching pipeline is very similar to the one proposed in @cite BL07 .
  63. ![stitching pipeline](StitchingPipeline.jpg)
  64. Camera models
  65. -------------
  66. There are currently 2 camera models implemented in stitching pipeline.
  67. - _Homography model_ expecting perspective transformations between images
  68. implemented in @ref cv::detail::BestOf2NearestMatcher cv::detail::HomographyBasedEstimator
  69. cv::detail::BundleAdjusterReproj cv::detail::BundleAdjusterRay
  70. - _Affine model_ expecting affine transformation with 6 DOF or 4 DOF implemented in
  71. @ref cv::detail::AffineBestOf2NearestMatcher cv::detail::AffineBasedEstimator
  72. cv::detail::BundleAdjusterAffine cv::detail::BundleAdjusterAffinePartial cv::AffineWarper
  73. Homography model is useful for creating photo panoramas captured by camera,
  74. while affine-based model can be used to stitch scans and object captured by
  75. specialized devices. Use @ref cv::Stitcher::create to get preconfigured pipeline for one
  76. of those models.
  77. @note
  78. Certain detailed settings of @ref cv::Stitcher might not make sense. Especially
  79. you should not mix classes implementing affine model and classes implementing
  80. Homography model, as they work with different transformations.
  81. @{
  82. @defgroup stitching_match Features Finding and Images Matching
  83. @defgroup stitching_rotation Rotation Estimation
  84. @defgroup stitching_autocalib Autocalibration
  85. @defgroup stitching_warp Images Warping
  86. @defgroup stitching_seam Seam Estimation
  87. @defgroup stitching_exposure Exposure Compensation
  88. @defgroup stitching_blend Image Blenders
  89. @}
  90. */
  91. namespace cv {
  92. //! @addtogroup stitching
  93. //! @{
  94. /** @example samples/cpp/stitching.cpp
  95. A basic example on image stitching
  96. */
  97. /** @example samples/cpp/stitching_detailed.cpp
  98. A detailed example on image stitching
  99. */
  100. /** @brief High level image stitcher.
  101. It's possible to use this class without being aware of the entire stitching pipeline. However, to
  102. be able to achieve higher stitching stability and quality of the final images at least being
  103. familiar with the theory is recommended.
  104. @note
  105. - A basic example on image stitching can be found at
  106. opencv_source_code/samples/cpp/stitching.cpp
  107. - A detailed example on image stitching can be found at
  108. opencv_source_code/samples/cpp/stitching_detailed.cpp
  109. */
  110. class CV_EXPORTS_W Stitcher
  111. {
  112. public:
  113. enum { ORIG_RESOL = -1 };
  114. enum Status
  115. {
  116. OK = 0,
  117. ERR_NEED_MORE_IMGS = 1,
  118. ERR_HOMOGRAPHY_EST_FAIL = 2,
  119. ERR_CAMERA_PARAMS_ADJUST_FAIL = 3
  120. };
  121. enum Mode
  122. {
  123. /** Mode for creating photo panoramas. Expects images under perspective
  124. transformation and projects resulting pano to sphere.
  125. @sa detail::BestOf2NearestMatcher SphericalWarper
  126. */
  127. PANORAMA = 0,
  128. /** Mode for composing scans. Expects images under affine transformation does
  129. not compensate exposure by default.
  130. @sa detail::AffineBestOf2NearestMatcher AffineWarper
  131. */
  132. SCANS = 1,
  133. };
  134. // Stitcher() {}
  135. /** @brief Creates a stitcher with the default parameters.
  136. @param try_use_gpu Flag indicating whether GPU should be used whenever it's possible.
  137. @return Stitcher class instance.
  138. */
  139. static Stitcher createDefault(bool try_use_gpu = false);
  140. /** @brief Creates a Stitcher configured in one of the stitching modes.
  141. @param mode Scenario for stitcher operation. This is usually determined by source of images
  142. to stitch and their transformation. Default parameters will be chosen for operation in given
  143. scenario.
  144. @param try_use_gpu Flag indicating whether GPU should be used whenever it's possible.
  145. @return Stitcher class instance.
  146. */
  147. static Ptr<Stitcher> create(Mode mode = PANORAMA, bool try_use_gpu = false);
  148. CV_WRAP double registrationResol() const { return registr_resol_; }
  149. CV_WRAP void setRegistrationResol(double resol_mpx) { registr_resol_ = resol_mpx; }
  150. CV_WRAP double seamEstimationResol() const { return seam_est_resol_; }
  151. CV_WRAP void setSeamEstimationResol(double resol_mpx) { seam_est_resol_ = resol_mpx; }
  152. CV_WRAP double compositingResol() const { return compose_resol_; }
  153. CV_WRAP void setCompositingResol(double resol_mpx) { compose_resol_ = resol_mpx; }
  154. CV_WRAP double panoConfidenceThresh() const { return conf_thresh_; }
  155. CV_WRAP void setPanoConfidenceThresh(double conf_thresh) { conf_thresh_ = conf_thresh; }
  156. CV_WRAP bool waveCorrection() const { return do_wave_correct_; }
  157. CV_WRAP void setWaveCorrection(bool flag) { do_wave_correct_ = flag; }
  158. detail::WaveCorrectKind waveCorrectKind() const { return wave_correct_kind_; }
  159. void setWaveCorrectKind(detail::WaveCorrectKind kind) { wave_correct_kind_ = kind; }
  160. Ptr<detail::FeaturesFinder> featuresFinder() { return features_finder_; }
  161. const Ptr<detail::FeaturesFinder> featuresFinder() const { return features_finder_; }
  162. void setFeaturesFinder(Ptr<detail::FeaturesFinder> features_finder)
  163. { features_finder_ = features_finder; }
  164. Ptr<detail::FeaturesMatcher> featuresMatcher() { return features_matcher_; }
  165. const Ptr<detail::FeaturesMatcher> featuresMatcher() const { return features_matcher_; }
  166. void setFeaturesMatcher(Ptr<detail::FeaturesMatcher> features_matcher)
  167. { features_matcher_ = features_matcher; }
  168. const cv::UMat& matchingMask() const { return matching_mask_; }
  169. void setMatchingMask(const cv::UMat &mask)
  170. {
  171. CV_Assert(mask.type() == CV_8U && mask.cols == mask.rows);
  172. matching_mask_ = mask.clone();
  173. }
  174. Ptr<detail::BundleAdjusterBase> bundleAdjuster() { return bundle_adjuster_; }
  175. const Ptr<detail::BundleAdjusterBase> bundleAdjuster() const { return bundle_adjuster_; }
  176. void setBundleAdjuster(Ptr<detail::BundleAdjusterBase> bundle_adjuster)
  177. { bundle_adjuster_ = bundle_adjuster; }
  178. /* TODO OpenCV ABI 4.x
  179. Ptr<detail::Estimator> estimator() { return estimator_; }
  180. const Ptr<detail::Estimator> estimator() const { return estimator_; }
  181. void setEstimator(Ptr<detail::Estimator> estimator)
  182. { estimator_ = estimator; }
  183. */
  184. Ptr<WarperCreator> warper() { return warper_; }
  185. const Ptr<WarperCreator> warper() const { return warper_; }
  186. void setWarper(Ptr<WarperCreator> creator) { warper_ = creator; }
  187. Ptr<detail::ExposureCompensator> exposureCompensator() { return exposure_comp_; }
  188. const Ptr<detail::ExposureCompensator> exposureCompensator() const { return exposure_comp_; }
  189. void setExposureCompensator(Ptr<detail::ExposureCompensator> exposure_comp)
  190. { exposure_comp_ = exposure_comp; }
  191. Ptr<detail::SeamFinder> seamFinder() { return seam_finder_; }
  192. const Ptr<detail::SeamFinder> seamFinder() const { return seam_finder_; }
  193. void setSeamFinder(Ptr<detail::SeamFinder> seam_finder) { seam_finder_ = seam_finder; }
  194. Ptr<detail::Blender> blender() { return blender_; }
  195. const Ptr<detail::Blender> blender() const { return blender_; }
  196. void setBlender(Ptr<detail::Blender> b) { blender_ = b; }
  197. /** @overload */
  198. CV_WRAP Status estimateTransform(InputArrayOfArrays images);
  199. /** @brief These functions try to match the given images and to estimate rotations of each camera.
  200. @note Use the functions only if you're aware of the stitching pipeline, otherwise use
  201. Stitcher::stitch.
  202. @param images Input images.
  203. @param rois Region of interest rectangles.
  204. @return Status code.
  205. */
  206. Status estimateTransform(InputArrayOfArrays images, const std::vector<std::vector<Rect> > &rois);
  207. /** @overload */
  208. CV_WRAP Status composePanorama(OutputArray pano);
  209. /** @brief These functions try to compose the given images (or images stored internally from the other function
  210. calls) into the final pano under the assumption that the image transformations were estimated
  211. before.
  212. @note Use the functions only if you're aware of the stitching pipeline, otherwise use
  213. Stitcher::stitch.
  214. @param images Input images.
  215. @param pano Final pano.
  216. @return Status code.
  217. */
  218. CV_WRAP Status composePanorama(InputArrayOfArrays images, OutputArray pano);
  219. /** @overload */
  220. CV_WRAP Status stitch(InputArrayOfArrays images, OutputArray pano);
  221. /** @brief These functions try to stitch the given images.
  222. @param images Input images.
  223. @param rois Region of interest rectangles.
  224. @param pano Final pano.
  225. @return Status code.
  226. */
  227. Status stitch(InputArrayOfArrays images, const std::vector<std::vector<Rect> > &rois, OutputArray pano);
  228. std::vector<int> component() const { return indices_; }
  229. std::vector<detail::CameraParams> cameras() const { return cameras_; }
  230. CV_WRAP double workScale() const { return work_scale_; }
  231. private:
  232. //Stitcher() {}
  233. Status matchImages();
  234. Status estimateCameraParams();
  235. double registr_resol_;
  236. double seam_est_resol_;
  237. double compose_resol_;
  238. double conf_thresh_;
  239. Ptr<detail::FeaturesFinder> features_finder_;
  240. Ptr<detail::FeaturesMatcher> features_matcher_;
  241. cv::UMat matching_mask_;
  242. Ptr<detail::BundleAdjusterBase> bundle_adjuster_;
  243. /* TODO OpenCV ABI 4.x
  244. Ptr<detail::Estimator> estimator_;
  245. */
  246. bool do_wave_correct_;
  247. detail::WaveCorrectKind wave_correct_kind_;
  248. Ptr<WarperCreator> warper_;
  249. Ptr<detail::ExposureCompensator> exposure_comp_;
  250. Ptr<detail::SeamFinder> seam_finder_;
  251. Ptr<detail::Blender> blender_;
  252. std::vector<cv::UMat> imgs_;
  253. std::vector<std::vector<cv::Rect> > rois_;
  254. std::vector<cv::Size> full_img_sizes_;
  255. std::vector<detail::ImageFeatures> features_;
  256. std::vector<detail::MatchesInfo> pairwise_matches_;
  257. std::vector<cv::UMat> seam_est_imgs_;
  258. std::vector<int> indices_;
  259. std::vector<detail::CameraParams> cameras_;
  260. double work_scale_;
  261. double seam_scale_;
  262. double seam_work_aspect_;
  263. double warped_image_scale_;
  264. };
  265. CV_EXPORTS_W Ptr<Stitcher> createStitcher(bool try_use_gpu = false);
  266. CV_EXPORTS_W Ptr<Stitcher> createStitcherScans(bool try_use_gpu = false);
  267. //! @} stitching
  268. } // namespace cv
  269. #endif // OPENCV_STITCHING_STITCHER_HPP