Joe Verbout
/
main
opencv on mbed
Embed:
(wiki syntax)
Show/hide line numbers
seam_finders.hpp
00001 /*M/////////////////////////////////////////////////////////////////////////////////////// 00002 // 00003 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. 00004 // 00005 // By downloading, copying, installing or using the software you agree to this license. 00006 // If you do not agree to this license, do not download, install, 00007 // copy or use the software. 00008 // 00009 // 00010 // License Agreement 00011 // For Open Source Computer Vision Library 00012 // 00013 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved. 00014 // Copyright (C) 2009, Willow Garage Inc., all rights reserved. 00015 // Third party copyrights are property of their respective owners. 00016 // 00017 // Redistribution and use in source and binary forms, with or without modification, 00018 // are permitted provided that the following conditions are met: 00019 // 00020 // * Redistribution's of source code must retain the above copyright notice, 00021 // this list of conditions and the following disclaimer. 00022 // 00023 // * Redistribution's in binary form must reproduce the above copyright notice, 00024 // this list of conditions and the following disclaimer in the documentation 00025 // and/or other materials provided with the distribution. 00026 // 00027 // * The name of the copyright holders may not be used to endorse or promote products 00028 // derived from this software without specific prior written permission. 00029 // 00030 // This software is provided by the copyright holders and contributors "as is" and 00031 // any express or implied warranties, including, but not limited to, the implied 00032 // warranties of merchantability and fitness for a particular purpose are disclaimed. 00033 // In no event shall the Intel Corporation or contributors be liable for any direct, 00034 // indirect, incidental, special, exemplary, or consequential damages 00035 // (including, but not limited to, procurement of substitute goods or services; 00036 // loss of use, data, or profits; or business interruption) however caused 00037 // and on any theory of liability, whether in contract, strict liability, 00038 // or tort (including negligence or otherwise) arising in any way out of 00039 // the use of this software, even if advised of the possibility of such damage. 00040 // 00041 //M*/ 00042 00043 #ifndef __OPENCV_STITCHING_SEAM_FINDERS_HPP__ 00044 #define __OPENCV_STITCHING_SEAM_FINDERS_HPP__ 00045 00046 #include <set> 00047 #include "opencv2/core.hpp" 00048 #include "opencv2/opencv_modules.hpp" 00049 00050 namespace cv { 00051 namespace detail { 00052 00053 //! @addtogroup stitching_seam 00054 //! @{ 00055 00056 /** @brief Base class for a seam estimator. 00057 */ 00058 class CV_EXPORTS SeamFinder 00059 { 00060 public: 00061 virtual ~SeamFinder() {} 00062 /** @brief Estimates seams. 00063 00064 @param src Source images 00065 @param corners Source image top-left corners 00066 @param masks Source image masks to update 00067 */ 00068 virtual void find(const std::vector<UMat> &src, const std::vector<Point> &corners, 00069 std::vector<UMat> &masks) = 0; 00070 }; 00071 00072 /** @brief Stub seam estimator which does nothing. 00073 */ 00074 class CV_EXPORTS NoSeamFinder : public SeamFinder 00075 { 00076 public: 00077 void find(const std::vector<UMat>&, const std::vector<Point>&, std::vector<UMat>&) {} 00078 }; 00079 00080 /** @brief Base class for all pairwise seam estimators. 00081 */ 00082 class CV_EXPORTS PairwiseSeamFinder : public SeamFinder 00083 { 00084 public: 00085 virtual void find(const std::vector<UMat> &src, const std::vector<Point> &corners, 00086 std::vector<UMat> &masks); 00087 00088 protected: 00089 void run(); 00090 /** @brief Resolves masks intersection of two specified images in the given ROI. 00091 00092 @param first First image index 00093 @param second Second image index 00094 @param roi Region of interest 00095 */ 00096 virtual void findInPair(size_t first, size_t second, Rect roi) = 0; 00097 00098 std::vector<UMat> images_; 00099 std::vector<Size> sizes_; 00100 std::vector<Point> corners_; 00101 std::vector<UMat> masks_; 00102 }; 00103 00104 /** @brief Voronoi diagram-based seam estimator. 00105 */ 00106 class CV_EXPORTS VoronoiSeamFinder : public PairwiseSeamFinder 00107 { 00108 public: 00109 virtual void find(const std::vector<UMat> &src, const std::vector<Point> &corners, 00110 std::vector<UMat> &masks); 00111 virtual void find(const std::vector<Size> &size, const std::vector<Point> &corners, 00112 std::vector<UMat> &masks); 00113 private: 00114 void findInPair(size_t first, size_t second, Rect roi); 00115 }; 00116 00117 00118 class CV_EXPORTS DpSeamFinder : public SeamFinder 00119 { 00120 public: 00121 enum CostFunction { COLOR, COLOR_GRAD }; 00122 00123 DpSeamFinder(CostFunction costFunc = COLOR); 00124 00125 CostFunction costFunction() const { return costFunc_; } 00126 void setCostFunction(CostFunction val) { costFunc_ = val; } 00127 00128 virtual void find(const std::vector<UMat> &src, const std::vector<Point> &corners, 00129 std::vector<UMat> &masks); 00130 00131 private: 00132 enum ComponentState 00133 { 00134 FIRST = 1, SECOND = 2, INTERS = 4, 00135 INTERS_FIRST = INTERS | FIRST, 00136 INTERS_SECOND = INTERS | SECOND 00137 }; 00138 00139 class ImagePairLess 00140 { 00141 public: 00142 ImagePairLess(const std::vector<Mat> &images, const std::vector<Point> &corners) 00143 : src_(&images[0]), corners_(&corners[0]) {} 00144 00145 bool operator() (const std::pair<size_t, size_t> &l, const std::pair<size_t, size_t> &r) const 00146 { 00147 Point c1 = corners_[l.first] + Point(src_[l.first].cols / 2, src_[l.first].rows / 2); 00148 Point c2 = corners_[l.second] + Point(src_[l.second].cols / 2, src_[l.second].rows / 2); 00149 int d1 = (c1 - c2).dot(c1 - c2); 00150 00151 c1 = corners_[r.first] + Point(src_[r.first].cols / 2, src_[r.first].rows / 2); 00152 c2 = corners_[r.second] + Point(src_[r.second].cols / 2, src_[r.second].rows / 2); 00153 int d2 = (c1 - c2).dot(c1 - c2); 00154 00155 return d1 < d2; 00156 } 00157 00158 private: 00159 const Mat *src_; 00160 const Point *corners_; 00161 }; 00162 00163 class ClosePoints 00164 { 00165 public: 00166 ClosePoints(int minDist) : minDist_(minDist) {} 00167 00168 bool operator() (const Point &p1, const Point &p2) const 00169 { 00170 int dist2 = (p1.x-p2.x) * (p1.x-p2.x) + (p1.y-p2.y) * (p1.y-p2.y); 00171 return dist2 < minDist_ * minDist_; 00172 } 00173 00174 private: 00175 int minDist_; 00176 }; 00177 00178 void process( 00179 const Mat &image1, const Mat &image2, Point tl1, Point tl2, Mat &mask1, Mat &mask2); 00180 00181 void findComponents(); 00182 00183 void findEdges(); 00184 00185 void resolveConflicts( 00186 const Mat &image1, const Mat &image2, Point tl1, Point tl2, Mat &mask1, Mat &mask2); 00187 00188 void computeGradients(const Mat &image1, const Mat &image2); 00189 00190 bool hasOnlyOneNeighbor(int comp); 00191 00192 bool closeToContour(int y, int x, const Mat_<uchar> &contourMask); 00193 00194 bool getSeamTips(int comp1, int comp2, Point &p1, Point &p2); 00195 00196 void computeCosts( 00197 const Mat &image1, const Mat &image2, Point tl1, Point tl2, 00198 int comp, Mat_<float> &costV, Mat_<float> &costH); 00199 00200 bool estimateSeam( 00201 const Mat &image1, const Mat &image2, Point tl1, Point tl2, int comp, 00202 Point p1, Point p2, std::vector<Point> &seam, bool &isHorizontal); 00203 00204 void updateLabelsUsingSeam( 00205 int comp1, int comp2, const std::vector<Point> &seam, bool isHorizontalSeam); 00206 00207 CostFunction costFunc_; 00208 00209 // processing images pair data 00210 Point unionTl_, unionBr_; 00211 Size unionSize_; 00212 Mat_<uchar> mask1_, mask2_; 00213 Mat_<uchar> contour1mask_, contour2mask_; 00214 Mat_<float> gradx1_, grady1_; 00215 Mat_<float> gradx2_, grady2_; 00216 00217 // components data 00218 int ncomps_; 00219 Mat_<int> labels_; 00220 std::vector<ComponentState> states_; 00221 std::vector<Point> tls_, brs_; 00222 std::vector<std::vector<Point> > contours_; 00223 std::set<std::pair<int, int> > edges_; 00224 }; 00225 00226 /** @brief Base class for all minimum graph-cut-based seam estimators. 00227 */ 00228 class CV_EXPORTS GraphCutSeamFinderBase 00229 { 00230 public: 00231 enum CostType { COST_COLOR, COST_COLOR_GRAD }; 00232 }; 00233 00234 /** @brief Minimum graph cut-based seam estimator. See details in @cite V03 . 00235 */ 00236 class CV_EXPORTS GraphCutSeamFinder : public GraphCutSeamFinderBase, public SeamFinder 00237 { 00238 public: 00239 GraphCutSeamFinder(int cost_type = COST_COLOR_GRAD, float terminal_cost = 10000.f, 00240 float bad_region_penalty = 1000.f); 00241 00242 ~GraphCutSeamFinder(); 00243 00244 void find(const std::vector<UMat> &src, const std::vector<Point> &corners, 00245 std::vector<UMat> &masks); 00246 00247 private: 00248 // To avoid GCGraph dependency 00249 class Impl; 00250 Ptr<PairwiseSeamFinder> impl_; 00251 }; 00252 00253 00254 #ifdef HAVE_OPENCV_CUDALEGACY 00255 class CV_EXPORTS GraphCutSeamFinderGpu : public GraphCutSeamFinderBase, public PairwiseSeamFinder 00256 { 00257 public: 00258 GraphCutSeamFinderGpu(int cost_type = COST_COLOR_GRAD, float terminal_cost = 10000.f, 00259 float bad_region_penalty = 1000.f) 00260 : cost_type_(cost_type), terminal_cost_(terminal_cost), 00261 bad_region_penalty_(bad_region_penalty) {} 00262 00263 void find(const std::vector<cv::UMat> &src, const std::vector<cv::Point> &corners, 00264 std::vector<cv::UMat> &masks); 00265 void findInPair(size_t first, size_t second, Rect roi); 00266 00267 private: 00268 void setGraphWeightsColor(const cv::Mat &img1, const cv::Mat &img2, const cv::Mat &mask1, const cv::Mat &mask2, 00269 cv::Mat &terminals, cv::Mat &leftT, cv::Mat &rightT, cv::Mat &top, cv::Mat &bottom); 00270 void setGraphWeightsColorGrad(const cv::Mat &img1, const cv::Mat &img2, const cv::Mat &dx1, const cv::Mat &dx2, 00271 const cv::Mat &dy1, const cv::Mat &dy2, const cv::Mat &mask1, const cv::Mat &mask2, 00272 cv::Mat &terminals, cv::Mat &leftT, cv::Mat &rightT, cv::Mat &top, cv::Mat &bottom); 00273 std::vector<Mat> dx_, dy_; 00274 int cost_type_; 00275 float terminal_cost_; 00276 float bad_region_penalty_; 00277 }; 00278 #endif 00279 00280 //! @} 00281 00282 } // namespace detail 00283 } // namespace cv 00284 00285 #endif // __OPENCV_STITCHING_SEAM_FINDERS_HPP__ 00286
Generated on Tue Jul 12 2022 16:42:40 by 1.7.2