seam_finders.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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_SEAM_FINDERS_HPP
  43. #define OPENCV_STITCHING_SEAM_FINDERS_HPP
  44. #include <set>
  45. #include "opencv2/core.hpp"
  46. #include "opencv2/opencv_modules.hpp"
  47. namespace cv {
  48. namespace detail {
  49. //! @addtogroup stitching_seam
  50. //! @{
  51. /** @brief Base class for a seam estimator.
  52. */
  53. class CV_EXPORTS_W SeamFinder
  54. {
  55. public:
  56. CV_WRAP virtual ~SeamFinder() {}
  57. enum { NO, VORONOI_SEAM, DP_SEAM };
  58. /** @brief Estimates seams.
  59. @param src Source images
  60. @param corners Source image top-left corners
  61. @param masks Source image masks to update
  62. */
  63. CV_WRAP virtual void find(const std::vector<UMat> &src, const std::vector<Point> &corners,
  64. CV_IN_OUT std::vector<UMat> &masks) = 0;
  65. CV_WRAP static Ptr<SeamFinder> createDefault(int type);
  66. };
  67. /** @brief Stub seam estimator which does nothing.
  68. */
  69. class CV_EXPORTS_W NoSeamFinder : public SeamFinder
  70. {
  71. public:
  72. CV_WRAP void find(const std::vector<UMat>&, const std::vector<Point>&, CV_IN_OUT std::vector<UMat>&) CV_OVERRIDE {}
  73. };
  74. /** @brief Base class for all pairwise seam estimators.
  75. */
  76. class CV_EXPORTS_W PairwiseSeamFinder : public SeamFinder
  77. {
  78. public:
  79. CV_WRAP virtual void find(const std::vector<UMat> &src, const std::vector<Point> &corners,
  80. CV_IN_OUT std::vector<UMat> &masks) CV_OVERRIDE;
  81. protected:
  82. void run();
  83. /** @brief Resolves masks intersection of two specified images in the given ROI.
  84. @param first First image index
  85. @param second Second image index
  86. @param roi Region of interest
  87. */
  88. virtual void findInPair(size_t first, size_t second, Rect roi) = 0;
  89. std::vector<UMat> images_;
  90. std::vector<Size> sizes_;
  91. std::vector<Point> corners_;
  92. std::vector<UMat> masks_;
  93. };
  94. /** @brief Voronoi diagram-based seam estimator.
  95. */
  96. class CV_EXPORTS_W VoronoiSeamFinder : public PairwiseSeamFinder
  97. {
  98. public:
  99. CV_WRAP virtual void find(const std::vector<UMat> &src, const std::vector<Point> &corners,
  100. CV_IN_OUT std::vector<UMat> &masks) CV_OVERRIDE;
  101. virtual void find(const std::vector<Size> &size, const std::vector<Point> &corners,
  102. std::vector<UMat> &masks);
  103. private:
  104. void findInPair(size_t first, size_t second, Rect roi) CV_OVERRIDE;
  105. };
  106. class CV_EXPORTS_W DpSeamFinder : public SeamFinder
  107. {
  108. public:
  109. enum CostFunction { COLOR, COLOR_GRAD };
  110. DpSeamFinder(CostFunction costFunc = COLOR);
  111. CV_WRAP DpSeamFinder(String costFunc );
  112. CostFunction costFunction() const { return costFunc_; }
  113. void setCostFunction(CostFunction val) { costFunc_ = val; }
  114. CV_WRAP void setCostFunction(String val);
  115. virtual void find(const std::vector<UMat> &src, const std::vector<Point> &corners,
  116. std::vector<UMat> &masks) CV_OVERRIDE;
  117. private:
  118. enum ComponentState
  119. {
  120. FIRST = 1, SECOND = 2, INTERS = 4,
  121. INTERS_FIRST = INTERS | FIRST,
  122. INTERS_SECOND = INTERS | SECOND
  123. };
  124. class ImagePairLess
  125. {
  126. public:
  127. ImagePairLess(const std::vector<Mat> &images, const std::vector<Point> &corners)
  128. : src_(&images[0]), corners_(&corners[0]) {}
  129. bool operator() (const std::pair<size_t, size_t> &l, const std::pair<size_t, size_t> &r) const
  130. {
  131. Point c1 = corners_[l.first] + Point(src_[l.first].cols / 2, src_[l.first].rows / 2);
  132. Point c2 = corners_[l.second] + Point(src_[l.second].cols / 2, src_[l.second].rows / 2);
  133. int d1 = (c1 - c2).dot(c1 - c2);
  134. c1 = corners_[r.first] + Point(src_[r.first].cols / 2, src_[r.first].rows / 2);
  135. c2 = corners_[r.second] + Point(src_[r.second].cols / 2, src_[r.second].rows / 2);
  136. int d2 = (c1 - c2).dot(c1 - c2);
  137. return d1 < d2;
  138. }
  139. private:
  140. const Mat *src_;
  141. const Point *corners_;
  142. };
  143. class ClosePoints
  144. {
  145. public:
  146. ClosePoints(int minDist) : minDist_(minDist) {}
  147. bool operator() (const Point &p1, const Point &p2) const
  148. {
  149. int dist2 = (p1.x-p2.x) * (p1.x-p2.x) + (p1.y-p2.y) * (p1.y-p2.y);
  150. return dist2 < minDist_ * minDist_;
  151. }
  152. private:
  153. int minDist_;
  154. };
  155. void process(
  156. const Mat &image1, const Mat &image2, Point tl1, Point tl2, Mat &mask1, Mat &mask2);
  157. void findComponents();
  158. void findEdges();
  159. void resolveConflicts(
  160. const Mat &image1, const Mat &image2, Point tl1, Point tl2, Mat &mask1, Mat &mask2);
  161. void computeGradients(const Mat &image1, const Mat &image2);
  162. bool hasOnlyOneNeighbor(int comp);
  163. bool closeToContour(int y, int x, const Mat_<uchar> &contourMask);
  164. bool getSeamTips(int comp1, int comp2, Point &p1, Point &p2);
  165. void computeCosts(
  166. const Mat &image1, const Mat &image2, Point tl1, Point tl2,
  167. int comp, Mat_<float> &costV, Mat_<float> &costH);
  168. bool estimateSeam(
  169. const Mat &image1, const Mat &image2, Point tl1, Point tl2, int comp,
  170. Point p1, Point p2, std::vector<Point> &seam, bool &isHorizontal);
  171. void updateLabelsUsingSeam(
  172. int comp1, int comp2, const std::vector<Point> &seam, bool isHorizontalSeam);
  173. CostFunction costFunc_;
  174. // processing images pair data
  175. Point unionTl_, unionBr_;
  176. Size unionSize_;
  177. Mat_<uchar> mask1_, mask2_;
  178. Mat_<uchar> contour1mask_, contour2mask_;
  179. Mat_<float> gradx1_, grady1_;
  180. Mat_<float> gradx2_, grady2_;
  181. // components data
  182. int ncomps_;
  183. Mat_<int> labels_;
  184. std::vector<ComponentState> states_;
  185. std::vector<Point> tls_, brs_;
  186. std::vector<std::vector<Point> > contours_;
  187. std::set<std::pair<int, int> > edges_;
  188. };
  189. /** @brief Base class for all minimum graph-cut-based seam estimators.
  190. */
  191. class CV_EXPORTS GraphCutSeamFinderBase
  192. {
  193. public:
  194. enum CostType { COST_COLOR, COST_COLOR_GRAD };
  195. };
  196. /** @brief Minimum graph cut-based seam estimator. See details in @cite V03 .
  197. */
  198. class CV_EXPORTS_W GraphCutSeamFinder : public GraphCutSeamFinderBase, public SeamFinder
  199. {
  200. public:
  201. GraphCutSeamFinder(int cost_type = COST_COLOR_GRAD, float terminal_cost = 10000.f,
  202. float bad_region_penalty = 1000.f);
  203. CV_WRAP GraphCutSeamFinder(String cost_type,float terminal_cost = 10000.f,
  204. float bad_region_penalty = 1000.f);
  205. ~GraphCutSeamFinder();
  206. CV_WRAP void find(const std::vector<UMat> &src, const std::vector<Point> &corners,
  207. std::vector<UMat> &masks) CV_OVERRIDE;
  208. private:
  209. // To avoid GCGraph dependency
  210. class Impl;
  211. Ptr<PairwiseSeamFinder> impl_;
  212. };
  213. #ifdef HAVE_OPENCV_CUDALEGACY
  214. class CV_EXPORTS GraphCutSeamFinderGpu : public GraphCutSeamFinderBase, public PairwiseSeamFinder
  215. {
  216. public:
  217. GraphCutSeamFinderGpu(int cost_type = COST_COLOR_GRAD, float terminal_cost = 10000.f,
  218. float bad_region_penalty = 1000.f)
  219. : cost_type_(cost_type), terminal_cost_(terminal_cost),
  220. bad_region_penalty_(bad_region_penalty) {}
  221. void find(const std::vector<cv::UMat> &src, const std::vector<cv::Point> &corners,
  222. std::vector<cv::UMat> &masks) CV_OVERRIDE;
  223. void findInPair(size_t first, size_t second, Rect roi) CV_OVERRIDE;
  224. private:
  225. void setGraphWeightsColor(const cv::Mat &img1, const cv::Mat &img2, const cv::Mat &mask1, const cv::Mat &mask2,
  226. cv::Mat &terminals, cv::Mat &leftT, cv::Mat &rightT, cv::Mat &top, cv::Mat &bottom);
  227. void setGraphWeightsColorGrad(const cv::Mat &img1, const cv::Mat &img2, const cv::Mat &dx1, const cv::Mat &dx2,
  228. const cv::Mat &dy1, const cv::Mat &dy2, const cv::Mat &mask1, const cv::Mat &mask2,
  229. cv::Mat &terminals, cv::Mat &leftT, cv::Mat &rightT, cv::Mat &top, cv::Mat &bottom);
  230. std::vector<Mat> dx_, dy_;
  231. int cost_type_;
  232. float terminal_cost_;
  233. float bad_region_penalty_;
  234. };
  235. #endif
  236. //! @}
  237. } // namespace detail
  238. } // namespace cv
  239. #endif // OPENCV_STITCHING_SEAM_FINDERS_HPP