stitching.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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/python/stitching.py
  98. A basic example on image stitching in Python.
  99. */
  100. /** @example samples/cpp/stitching_detailed.cpp
  101. A detailed example on image stitching
  102. */
  103. /** @brief High level image stitcher.
  104. It's possible to use this class without being aware of the entire stitching pipeline. However, to
  105. be able to achieve higher stitching stability and quality of the final images at least being
  106. familiar with the theory is recommended.
  107. @note
  108. - A basic example on image stitching can be found at
  109. opencv_source_code/samples/cpp/stitching.cpp
  110. - A basic example on image stitching in Python can be found at
  111. opencv_source_code/samples/python/stitching.py
  112. - A detailed example on image stitching can be found at
  113. opencv_source_code/samples/cpp/stitching_detailed.cpp
  114. */
  115. class CV_EXPORTS_W Stitcher
  116. {
  117. public:
  118. /**
  119. * When setting a resolution for stitching, this values is a placeholder
  120. * for preserving the original resolution.
  121. */
  122. #if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900/*MSVS 2015*/)
  123. static constexpr double ORIG_RESOL = -1.0;
  124. #else
  125. // support MSVS 2013
  126. static const double ORIG_RESOL; // Initialized in stitcher.cpp
  127. #endif
  128. enum Status
  129. {
  130. OK = 0,
  131. ERR_NEED_MORE_IMGS = 1,
  132. ERR_HOMOGRAPHY_EST_FAIL = 2,
  133. ERR_CAMERA_PARAMS_ADJUST_FAIL = 3
  134. };
  135. enum Mode
  136. {
  137. /** Mode for creating photo panoramas. Expects images under perspective
  138. transformation and projects resulting pano to sphere.
  139. @sa detail::BestOf2NearestMatcher SphericalWarper
  140. */
  141. PANORAMA = 0,
  142. /** Mode for composing scans. Expects images under affine transformation does
  143. not compensate exposure by default.
  144. @sa detail::AffineBestOf2NearestMatcher AffineWarper
  145. */
  146. SCANS = 1,
  147. };
  148. /** @brief Creates a Stitcher configured in one of the stitching modes.
  149. @param mode Scenario for stitcher operation. This is usually determined by source of images
  150. to stitch and their transformation. Default parameters will be chosen for operation in given
  151. scenario.
  152. @return Stitcher class instance.
  153. */
  154. CV_WRAP static Ptr<Stitcher> create(Mode mode = Stitcher::PANORAMA);
  155. CV_WRAP double registrationResol() const { return registr_resol_; }
  156. CV_WRAP void setRegistrationResol(double resol_mpx) { registr_resol_ = resol_mpx; }
  157. CV_WRAP double seamEstimationResol() const { return seam_est_resol_; }
  158. CV_WRAP void setSeamEstimationResol(double resol_mpx) { seam_est_resol_ = resol_mpx; }
  159. CV_WRAP double compositingResol() const { return compose_resol_; }
  160. CV_WRAP void setCompositingResol(double resol_mpx) { compose_resol_ = resol_mpx; }
  161. CV_WRAP double panoConfidenceThresh() const { return conf_thresh_; }
  162. CV_WRAP void setPanoConfidenceThresh(double conf_thresh) { conf_thresh_ = conf_thresh; }
  163. CV_WRAP bool waveCorrection() const { return do_wave_correct_; }
  164. CV_WRAP void setWaveCorrection(bool flag) { do_wave_correct_ = flag; }
  165. CV_WRAP InterpolationFlags interpolationFlags() const { return interp_flags_; }
  166. CV_WRAP void setInterpolationFlags(InterpolationFlags interp_flags) { interp_flags_ = interp_flags; }
  167. detail::WaveCorrectKind waveCorrectKind() const { return wave_correct_kind_; }
  168. void setWaveCorrectKind(detail::WaveCorrectKind kind) { wave_correct_kind_ = kind; }
  169. Ptr<Feature2D> featuresFinder() { return features_finder_; }
  170. const Ptr<Feature2D> featuresFinder() const { return features_finder_; }
  171. void setFeaturesFinder(Ptr<Feature2D> features_finder)
  172. { features_finder_ = features_finder; }
  173. Ptr<detail::FeaturesMatcher> featuresMatcher() { return features_matcher_; }
  174. const Ptr<detail::FeaturesMatcher> featuresMatcher() const { return features_matcher_; }
  175. void setFeaturesMatcher(Ptr<detail::FeaturesMatcher> features_matcher)
  176. { features_matcher_ = features_matcher; }
  177. const cv::UMat& matchingMask() const { return matching_mask_; }
  178. void setMatchingMask(const cv::UMat &mask)
  179. {
  180. CV_Assert(mask.type() == CV_8U && mask.cols == mask.rows);
  181. matching_mask_ = mask.clone();
  182. }
  183. Ptr<detail::BundleAdjusterBase> bundleAdjuster() { return bundle_adjuster_; }
  184. const Ptr<detail::BundleAdjusterBase> bundleAdjuster() const { return bundle_adjuster_; }
  185. void setBundleAdjuster(Ptr<detail::BundleAdjusterBase> bundle_adjuster)
  186. { bundle_adjuster_ = bundle_adjuster; }
  187. Ptr<detail::Estimator> estimator() { return estimator_; }
  188. const Ptr<detail::Estimator> estimator() const { return estimator_; }
  189. void setEstimator(Ptr<detail::Estimator> estimator)
  190. { estimator_ = estimator; }
  191. Ptr<WarperCreator> warper() { return warper_; }
  192. const Ptr<WarperCreator> warper() const { return warper_; }
  193. void setWarper(Ptr<WarperCreator> creator) { warper_ = creator; }
  194. Ptr<detail::ExposureCompensator> exposureCompensator() { return exposure_comp_; }
  195. const Ptr<detail::ExposureCompensator> exposureCompensator() const { return exposure_comp_; }
  196. void setExposureCompensator(Ptr<detail::ExposureCompensator> exposure_comp)
  197. { exposure_comp_ = exposure_comp; }
  198. Ptr<detail::SeamFinder> seamFinder() { return seam_finder_; }
  199. const Ptr<detail::SeamFinder> seamFinder() const { return seam_finder_; }
  200. void setSeamFinder(Ptr<detail::SeamFinder> seam_finder) { seam_finder_ = seam_finder; }
  201. Ptr<detail::Blender> blender() { return blender_; }
  202. const Ptr<detail::Blender> blender() const { return blender_; }
  203. void setBlender(Ptr<detail::Blender> b) { blender_ = b; }
  204. /** @brief These functions try to match the given images and to estimate rotations of each camera.
  205. @note Use the functions only if you're aware of the stitching pipeline, otherwise use
  206. Stitcher::stitch.
  207. @param images Input images.
  208. @param masks Masks for each input image specifying where to look for keypoints (optional).
  209. @return Status code.
  210. */
  211. CV_WRAP Status estimateTransform(InputArrayOfArrays images, InputArrayOfArrays masks = noArray());
  212. /** @overload */
  213. CV_WRAP Status composePanorama(OutputArray pano);
  214. /** @brief These functions try to compose the given images (or images stored internally from the other function
  215. calls) into the final pano under the assumption that the image transformations were estimated
  216. before.
  217. @note Use the functions only if you're aware of the stitching pipeline, otherwise use
  218. Stitcher::stitch.
  219. @param images Input images.
  220. @param pano Final pano.
  221. @return Status code.
  222. */
  223. Status composePanorama(InputArrayOfArrays images, OutputArray pano);
  224. /** @overload */
  225. CV_WRAP Status stitch(InputArrayOfArrays images, OutputArray pano);
  226. /** @brief These functions try to stitch the given images.
  227. @param images Input images.
  228. @param masks Masks for each input image specifying where to look for keypoints (optional).
  229. @param pano Final pano.
  230. @return Status code.
  231. */
  232. CV_WRAP Status stitch(InputArrayOfArrays images, InputArrayOfArrays masks, OutputArray pano);
  233. std::vector<int> component() const { return indices_; }
  234. std::vector<detail::CameraParams> cameras() const { return cameras_; }
  235. CV_WRAP double workScale() const { return work_scale_; }
  236. UMat resultMask() const { return result_mask_; }
  237. private:
  238. Status matchImages();
  239. Status estimateCameraParams();
  240. double registr_resol_;
  241. double seam_est_resol_;
  242. double compose_resol_;
  243. double conf_thresh_;
  244. InterpolationFlags interp_flags_;
  245. Ptr<Feature2D> features_finder_;
  246. Ptr<detail::FeaturesMatcher> features_matcher_;
  247. cv::UMat matching_mask_;
  248. Ptr<detail::BundleAdjusterBase> bundle_adjuster_;
  249. Ptr<detail::Estimator> estimator_;
  250. bool do_wave_correct_;
  251. detail::WaveCorrectKind wave_correct_kind_;
  252. Ptr<WarperCreator> warper_;
  253. Ptr<detail::ExposureCompensator> exposure_comp_;
  254. Ptr<detail::SeamFinder> seam_finder_;
  255. Ptr<detail::Blender> blender_;
  256. std::vector<cv::UMat> imgs_;
  257. std::vector<cv::UMat> masks_;
  258. std::vector<cv::Size> full_img_sizes_;
  259. std::vector<detail::ImageFeatures> features_;
  260. std::vector<detail::MatchesInfo> pairwise_matches_;
  261. std::vector<cv::UMat> seam_est_imgs_;
  262. std::vector<int> indices_;
  263. std::vector<detail::CameraParams> cameras_;
  264. UMat result_mask_;
  265. double work_scale_;
  266. double seam_scale_;
  267. double seam_work_aspect_;
  268. double warped_image_scale_;
  269. };
  270. /**
  271. * @deprecated use Stitcher::create
  272. */
  273. CV_DEPRECATED Ptr<Stitcher> createStitcher(bool try_use_gpu = false);
  274. /**
  275. * @deprecated use Stitcher::create
  276. */
  277. CV_DEPRECATED Ptr<Stitcher> createStitcherScans(bool try_use_gpu = false);
  278. //! @} stitching
  279. } // namespace cv
  280. #endif // OPENCV_STITCHING_STITCHER_HPP