seam_finders.hpp
Go to the documentation of this file.
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_SEAM_FINDERS_HPP__
44 #define __OPENCV_STITCHING_SEAM_FINDERS_HPP__
45 
46 #include <set>
47 #include "opencv2/core/core.hpp"
48 #include "opencv2/core/gpumat.hpp"
49 
50 namespace cv {
51 namespace detail {
52 
53 class CV_EXPORTS SeamFinder
54 {
55 public:
56  virtual ~SeamFinder() {}
57  virtual void find(const std::vector<Mat> &src, const std::vector<Point> &corners,
58  std::vector<Mat> &masks) = 0;
59 };
60 
61 
62 class CV_EXPORTS NoSeamFinder : public SeamFinder
63 {
64 public:
65  void find(const std::vector<Mat>&, const std::vector<Point>&, std::vector<Mat>&) {}
66 };
67 
68 
69 class CV_EXPORTS PairwiseSeamFinder : public SeamFinder
70 {
71 public:
72  virtual void find(const std::vector<Mat> &src, const std::vector<Point> &corners,
73  std::vector<Mat> &masks);
74 
75 protected:
76  void run();
77  virtual void findInPair(size_t first, size_t second, Rect roi) = 0;
78 
79  std::vector<Mat> images_;
80  std::vector<Size> sizes_;
81  std::vector<Point> corners_;
82  std::vector<Mat> masks_;
83 };
84 
85 
86 class CV_EXPORTS VoronoiSeamFinder : public PairwiseSeamFinder
87 {
88 public:
89  virtual void find(const std::vector<Size> &size, const std::vector<Point> &corners,
90  std::vector<Mat> &masks);
91 private:
92  void findInPair(size_t first, size_t second, Rect roi);
93 };
94 
95 
96 class CV_EXPORTS DpSeamFinder : public SeamFinder
97 {
98 public:
99  enum CostFunction { COLOR, COLOR_GRAD };
100 
101  DpSeamFinder(CostFunction costFunc = COLOR);
102 
103  CostFunction costFunction() const { return costFunc_; }
104  void setCostFunction(CostFunction val) { costFunc_ = val; }
105 
106  virtual void find(const std::vector<Mat> &src, const std::vector<Point> &corners,
107  std::vector<Mat> &masks);
108 
109 private:
110  enum ComponentState
111  {
112  FIRST = 1, SECOND = 2, INTERS = 4,
113  INTERS_FIRST = INTERS | FIRST,
114  INTERS_SECOND = INTERS | SECOND
115  };
116 
117  class ImagePairLess
118  {
119  public:
120  ImagePairLess(const std::vector<Mat> &images, const std::vector<Point> &corners)
121  : src_(&images[0]), corners_(&corners[0]) {}
122 
123  bool operator() (const std::pair<size_t, size_t> &l, const std::pair<size_t, size_t> &r) const
124  {
125  Point c1 = corners_[l.first] + Point(src_[l.first].cols / 2, src_[l.first].rows / 2);
126  Point c2 = corners_[l.second] + Point(src_[l.second].cols / 2, src_[l.second].rows / 2);
127  int d1 = (c1 - c2).dot(c1 - c2);
128 
129  c1 = corners_[r.first] + Point(src_[r.first].cols / 2, src_[r.first].rows / 2);
130  c2 = corners_[r.second] + Point(src_[r.second].cols / 2, src_[r.second].rows / 2);
131  int d2 = (c1 - c2).dot(c1 - c2);
132 
133  return d1 < d2;
134  }
135 
136  private:
137  const Mat *src_;
138  const Point *corners_;
139  };
140 
141  class ClosePoints
142  {
143  public:
144  ClosePoints(int minDist) : minDist_(minDist) {}
145 
146  bool operator() (const Point &p1, const Point &p2) const
147  {
148  int dist2 = (p1.x-p2.x) * (p1.x-p2.x) + (p1.y-p2.y) * (p1.y-p2.y);
149  return dist2 < minDist_ * minDist_;
150  }
151 
152  private:
153  int minDist_;
154  };
155 
156  void process(
157  const Mat &image1, const Mat &image2, Point tl1, Point tl2, Mat &mask1, Mat &mask2);
158 
159  void findComponents();
160 
161  void findEdges();
162 
163  void resolveConflicts(
164  const Mat &image1, const Mat &image2, Point tl1, Point tl2, Mat &mask1, Mat &mask2);
165 
166  void computeGradients(const Mat &image1, const Mat &image2);
167 
168  bool hasOnlyOneNeighbor(int comp);
169 
170  bool closeToContour(int y, int x, const Mat_<uchar> &contourMask);
171 
172  bool getSeamTips(int comp1, int comp2, Point &p1, Point &p2);
173 
174  void computeCosts(
175  const Mat &image1, const Mat &image2, Point tl1, Point tl2,
176  int comp, Mat_<float> &costV, Mat_<float> &costH);
177 
178  bool estimateSeam(
179  const Mat &image1, const Mat &image2, Point tl1, Point tl2, int comp,
180  Point p1, Point p2, std::vector<Point> &seam, bool &isHorizontal);
181 
182  void updateLabelsUsingSeam(
183  int comp1, int comp2, const std::vector<Point> &seam, bool isHorizontalSeam);
184 
185  CostFunction costFunc_;
186 
187  // processing images pair data
188  Point unionTl_, unionBr_;
189  Size unionSize_;
190  Mat_<uchar> mask1_, mask2_;
191  Mat_<uchar> contour1mask_, contour2mask_;
192  Mat_<float> gradx1_, grady1_;
193  Mat_<float> gradx2_, grady2_;
194 
195  // components data
196  int ncomps_;
197  Mat_<int> labels_;
198  std::vector<ComponentState> states_;
199  std::vector<Point> tls_, brs_;
200  std::vector<std::vector<Point> > contours_;
201  std::set<std::pair<int, int> > edges_;
202 };
203 
204 
205 class CV_EXPORTS GraphCutSeamFinderBase
206 {
207 public:
208  enum { COST_COLOR, COST_COLOR_GRAD };
209 };
210 
211 
212 class CV_EXPORTS GraphCutSeamFinder : public GraphCutSeamFinderBase, public SeamFinder
213 {
214 public:
215  GraphCutSeamFinder(int cost_type = COST_COLOR_GRAD, float terminal_cost = 10000.f,
216  float bad_region_penalty = 1000.f);
217 
219 
220  void find(const std::vector<Mat> &src, const std::vector<Point> &corners,
221  std::vector<Mat> &masks);
222 
223 private:
224  // To avoid GCGraph dependency
225  class Impl;
227 };
228 
229 
231 {
232 public:
233  GraphCutSeamFinderGpu(int cost_type = COST_COLOR_GRAD, float terminal_cost = 10000.f,
234  float bad_region_penalty = 1000.f)
235  : cost_type_(cost_type), terminal_cost_(terminal_cost),
236  bad_region_penalty_(bad_region_penalty) {}
237 
238  void find(const std::vector<cv::Mat> &src, const std::vector<cv::Point> &corners,
239  std::vector<cv::Mat> &masks);
240  void findInPair(size_t first, size_t second, Rect roi);
241 
242 private:
243  void setGraphWeightsColor(const cv::Mat &img1, const cv::Mat &img2, const cv::Mat &mask1, const cv::Mat &mask2,
244  cv::Mat &terminals, cv::Mat &leftT, cv::Mat &rightT, cv::Mat &top, cv::Mat &bottom);
245  void setGraphWeightsColorGrad(const cv::Mat &img1, const cv::Mat &img2, const cv::Mat &dx1, const cv::Mat &dx2,
246  const cv::Mat &dy1, const cv::Mat &dy2, const cv::Mat &mask1, const cv::Mat &mask2,
247  cv::Mat &terminals, cv::Mat &leftT, cv::Mat &rightT, cv::Mat &top, cv::Mat &bottom);
248  std::vector<Mat> dx_, dy_;
249  int cost_type_;
250  float terminal_cost_;
251  float bad_region_penalty_;
252 };
253 
254 } // namespace detail
255 } // namespace cv
256 
257 #endif // __OPENCV_STITCHING_SEAM_FINDERS_HPP__
GLdouble GLdouble GLdouble r
Point2i Point
Definition: core.hpp:893
GLenum GLint GLint y
Definition: core_c.h:613
CvPoint2D32f p2
Definition: legacy.hpp:578
IplImage CvRect * roi
Definition: legacy.hpp:234
GLdouble GLdouble GLdouble GLdouble top
void setCostFunction(CostFunction val)
Definition: seam_finders.hpp:104
Size2i Size
Definition: core.hpp:896
virtual ~SeamFinder()
Definition: seam_finders.hpp:56
GLuint src
Definition: core_c.h:1650
std::vector< Point > corners_
Definition: seam_finders.hpp:81
Definition: seam_finders.hpp:212
std::vector< Mat > images_
Definition: seam_finders.hpp:79
void find(const std::vector< Mat > &, const std::vector< Point > &, std::vector< Mat > &)
Definition: seam_finders.hpp:65
void process(const Mat_< T1 > &m1, Mat_< T2 > &m2, Op op)
Definition: mat.hpp:1084
Definition: seam_finders.hpp:205
GLuint GLfloat * val
Definition: seam_finders.hpp:69
IplImage CvMemStorage CvSeq ** comp
Definition: legacy.hpp:2917
Definition: seam_finders.hpp:53
GLint * first
Definition: legacy.hpp:1133
const CvArr * image2
Definition: imgproc_c.h:64
Definition: seam_finders.hpp:230
GLenum GLint x
Definition: core_c.h:632
CostFunction
Definition: seam_finders.hpp:99
The Core Functionality.
The n-dimensional matrix class.
Definition: core.hpp:1688
Definition: seam_finders.hpp:96
CvSize CvPoint2D32f * corners
Definition: calib3d.hpp:215
GraphCutSeamFinderGpu(int cost_type=COST_COLOR_GRAD, float terminal_cost=10000.f, float bad_region_penalty=1000.f)
Definition: seam_finders.hpp:233
CostFunction costFunction() const
Definition: seam_finders.hpp:103
std::vector< Size > sizes_
Definition: seam_finders.hpp:80
Definition: seam_finders.hpp:86
int int int * second
Definition: legacy.hpp:1115
Smart pointer to dynamically allocated objects.
Definition: core.hpp:1268
GLdouble GLdouble GLdouble bottom
std::vector< Mat > masks_
Definition: seam_finders.hpp:82
Definition: seam_finders.hpp:62
DataType< _Tp >::work_type dot(const Vector< _Tp > &v1, const Vector< _Tp > &v2)
Definition: operations.hpp:2465
GLclampf f
GLsizeiptr size
Definition: core_c.h:939