OpenCV453
seam_finders.hpp
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.hpp"
48#include "opencv2/opencv_modules.hpp"
49
50namespace cv {
51namespace detail {
52
55
58class CV_EXPORTS_W SeamFinder
59{
60public:
61 CV_WRAP virtual ~SeamFinder() {}
62 enum { NO, VORONOI_SEAM, DP_SEAM };
69 CV_WRAP virtual void find(const std::vector<UMat> &src, const std::vector<Point> &corners,
70 CV_IN_OUT std::vector<UMat> &masks) = 0;
71 CV_WRAP static Ptr<SeamFinder> createDefault(int type);
72};
73
76class CV_EXPORTS_W NoSeamFinder : public SeamFinder
77{
78public:
79 CV_WRAP void find(const std::vector<UMat>&, const std::vector<Point>&, CV_IN_OUT std::vector<UMat>&) CV_OVERRIDE {}
80};
81
84class CV_EXPORTS_W PairwiseSeamFinder : public SeamFinder
85{
86public:
87 CV_WRAP virtual void find(const std::vector<UMat> &src, const std::vector<Point> &corners,
88 CV_IN_OUT std::vector<UMat> &masks) CV_OVERRIDE;
89
90protected:
91 void run();
98 virtual void findInPair(size_t first, size_t second, Rect roi) = 0;
99
100 std::vector<UMat> images_;
101 std::vector<Size> sizes_;
102 std::vector<Point> corners_;
103 std::vector<UMat> masks_;
104};
105
108class CV_EXPORTS_W VoronoiSeamFinder : public PairwiseSeamFinder
109{
110public:
111 CV_WRAP virtual void find(const std::vector<UMat> &src, const std::vector<Point> &corners,
112 CV_IN_OUT std::vector<UMat> &masks) CV_OVERRIDE;
113 virtual void find(const std::vector<Size> &size, const std::vector<Point> &corners,
114 std::vector<UMat> &masks);
115private:
116 void findInPair(size_t first, size_t second, Rect roi) CV_OVERRIDE;
117};
118
119
120class CV_EXPORTS_W DpSeamFinder : public SeamFinder
121{
122public:
123 enum CostFunction { COLOR, COLOR_GRAD };
124
125 DpSeamFinder(CostFunction costFunc = COLOR);
126 CV_WRAP DpSeamFinder(String costFunc );
127
128 CostFunction costFunction() const { return costFunc_; }
129 void setCostFunction(CostFunction val) { costFunc_ = val; }
130 CV_WRAP void setCostFunction(String val);
131
132 virtual void find(const std::vector<UMat> &src, const std::vector<Point> &corners,
133 std::vector<UMat> &masks) CV_OVERRIDE;
134
135private:
136 enum ComponentState
137 {
138 FIRST = 1, SECOND = 2, INTERS = 4,
139 INTERS_FIRST = INTERS | FIRST,
140 INTERS_SECOND = INTERS | SECOND
141 };
142
143 class ImagePairLess
144 {
145 public:
146 ImagePairLess(const std::vector<Mat> &images, const std::vector<Point> &corners)
147 : src_(&images[0]), corners_(&corners[0]) {}
148
149 bool operator() (const std::pair<size_t, size_t> &l, const std::pair<size_t, size_t> &r) const
150 {
151 Point c1 = corners_[l.first] + Point(src_[l.first].cols / 2, src_[l.first].rows / 2);
152 Point c2 = corners_[l.second] + Point(src_[l.second].cols / 2, src_[l.second].rows / 2);
153 int d1 = (c1 - c2).dot(c1 - c2);
154
155 c1 = corners_[r.first] + Point(src_[r.first].cols / 2, src_[r.first].rows / 2);
156 c2 = corners_[r.second] + Point(src_[r.second].cols / 2, src_[r.second].rows / 2);
157 int d2 = (c1 - c2).dot(c1 - c2);
158
159 return d1 < d2;
160 }
161
162 private:
163 const Mat *src_;
164 const Point *corners_;
165 };
166
167 class ClosePoints
168 {
169 public:
170 ClosePoints(int minDist) : minDist_(minDist) {}
171
172 bool operator() (const Point &p1, const Point &p2) const
173 {
174 int dist2 = (p1.x-p2.x) * (p1.x-p2.x) + (p1.y-p2.y) * (p1.y-p2.y);
175 return dist2 < minDist_ * minDist_;
176 }
177
178 private:
179 int minDist_;
180 };
181
182 void process(
183 const Mat &image1, const Mat &image2, Point tl1, Point tl2, Mat &mask1, Mat &mask2);
184
185 void findComponents();
186
187 void findEdges();
188
189 void resolveConflicts(
190 const Mat &image1, const Mat &image2, Point tl1, Point tl2, Mat &mask1, Mat &mask2);
191
192 void computeGradients(const Mat &image1, const Mat &image2);
193
194 bool hasOnlyOneNeighbor(int comp);
195
196 bool closeToContour(int y, int x, const Mat_<uchar> &contourMask);
197
198 bool getSeamTips(int comp1, int comp2, Point &p1, Point &p2);
199
200 void computeCosts(
201 const Mat &image1, const Mat &image2, Point tl1, Point tl2,
202 int comp, Mat_<float> &costV, Mat_<float> &costH);
203
204 bool estimateSeam(
205 const Mat &image1, const Mat &image2, Point tl1, Point tl2, int comp,
206 Point p1, Point p2, std::vector<Point> &seam, bool &isHorizontal);
207
208 void updateLabelsUsingSeam(
209 int comp1, int comp2, const std::vector<Point> &seam, bool isHorizontalSeam);
210
211 CostFunction costFunc_;
212
213 // processing images pair data
214 Point unionTl_, unionBr_;
215 Size unionSize_;
216 Mat_<uchar> mask1_, mask2_;
217 Mat_<uchar> contour1mask_, contour2mask_;
218 Mat_<float> gradx1_, grady1_;
219 Mat_<float> gradx2_, grady2_;
220
221 // components data
222 int ncomps_;
223 Mat_<int> labels_;
224 std::vector<ComponentState> states_;
225 std::vector<Point> tls_, brs_;
226 std::vector<std::vector<Point> > contours_;
227 std::set<std::pair<int, int> > edges_;
228};
229
233{
234public:
235 enum CostType { COST_COLOR, COST_COLOR_GRAD };
236};
237
240class CV_EXPORTS_W GraphCutSeamFinder : public GraphCutSeamFinderBase, public SeamFinder
241{
242public:
243 GraphCutSeamFinder(int cost_type = COST_COLOR_GRAD, float terminal_cost = 10000.f,
244 float bad_region_penalty = 1000.f);
245 CV_WRAP GraphCutSeamFinder(String cost_type,float terminal_cost = 10000.f,
246 float bad_region_penalty = 1000.f);
247
249
250 CV_WRAP void find(const std::vector<UMat> &src, const std::vector<Point> &corners,
251 std::vector<UMat> &masks) CV_OVERRIDE;
252
253private:
254 // To avoid GCGraph dependency
255 class Impl;
257};
258
259
260#ifdef HAVE_OPENCV_CUDALEGACY
261class CV_EXPORTS GraphCutSeamFinderGpu : public GraphCutSeamFinderBase, public PairwiseSeamFinder
262{
263public:
264 GraphCutSeamFinderGpu(int cost_type = COST_COLOR_GRAD, float terminal_cost = 10000.f,
265 float bad_region_penalty = 1000.f)
266 : cost_type_(cost_type), terminal_cost_(terminal_cost),
267 bad_region_penalty_(bad_region_penalty) {}
268
269 void find(const std::vector<cv::UMat> &src, const std::vector<cv::Point> &corners,
270 std::vector<cv::UMat> &masks) CV_OVERRIDE;
271 void findInPair(size_t first, size_t second, Rect roi) CV_OVERRIDE;
272
273private:
274 void setGraphWeightsColor(const cv::Mat &img1, const cv::Mat &img2, const cv::Mat &mask1, const cv::Mat &mask2,
275 cv::Mat &terminals, cv::Mat &leftT, cv::Mat &rightT, cv::Mat &top, cv::Mat &bottom);
276 void setGraphWeightsColorGrad(const cv::Mat &img1, const cv::Mat &img2, const cv::Mat &dx1, const cv::Mat &dx2,
277 const cv::Mat &dy1, const cv::Mat &dy2, const cv::Mat &mask1, const cv::Mat &mask2,
278 cv::Mat &terminals, cv::Mat &leftT, cv::Mat &rightT, cv::Mat &top, cv::Mat &bottom);
279 std::vector<Mat> dx_, dy_;
280 int cost_type_;
281 float terminal_cost_;
282 float bad_region_penalty_;
283};
284#endif
285
287
288} // namespace detail
289} // namespace cv
290
291#endif // OPENCV_STITCHING_SEAM_FINDERS_HPP
Template matrix class derived from Mat
Definition: mat.hpp:2199
n-dimensional dense array class
Definition: mat.hpp:802
_Tp y
y coordinate of the point
Definition: core/types.hpp:187
_Tp x
x coordinate of the point
Definition: core/types.hpp:186
Template class for 2D rectangles
Definition: core/types.hpp:421
Template class for specifying the size of an image or rectangle.
Definition: core/types.hpp:316
Definition: seam_finders.hpp:121
Base class for all minimum graph-cut-based seam estimators.
Definition: seam_finders.hpp:233
Minimum graph cut-based seam estimator. See details in .
Definition: seam_finders.hpp:241
Stub seam estimator which does nothing.
Definition: seam_finders.hpp:77
Base class for all pairwise seam estimators.
Definition: seam_finders.hpp:85
virtual void findInPair(size_t first, size_t second, Rect roi)=0
Resolves masks intersection of two specified images in the given ROI.
Base class for a seam estimator.
Definition: seam_finders.hpp:59
Voronoi diagram-based seam estimator.
Definition: seam_finders.hpp:109
"black box" representation of the file storage associated with a file on disk.
Definition: aruco.hpp:75
Definition: cvstd_wrapper.hpp:74