opencv on mbed

Dependencies:   mbed

Committer:
joeverbout
Date:
Thu Mar 31 21:16:38 2016 +0000
Revision:
0:ea44dc9ed014
OpenCV on mbed attempt

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joeverbout 0:ea44dc9ed014 1 /*M///////////////////////////////////////////////////////////////////////////////////////
joeverbout 0:ea44dc9ed014 2 //
joeverbout 0:ea44dc9ed014 3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
joeverbout 0:ea44dc9ed014 4 //
joeverbout 0:ea44dc9ed014 5 // By downloading, copying, installing or using the software you agree to this license.
joeverbout 0:ea44dc9ed014 6 // If you do not agree to this license, do not download, install,
joeverbout 0:ea44dc9ed014 7 // copy or use the software.
joeverbout 0:ea44dc9ed014 8 //
joeverbout 0:ea44dc9ed014 9 //
joeverbout 0:ea44dc9ed014 10 // License Agreement
joeverbout 0:ea44dc9ed014 11 // For Open Source Computer Vision Library
joeverbout 0:ea44dc9ed014 12 //
joeverbout 0:ea44dc9ed014 13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
joeverbout 0:ea44dc9ed014 14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
joeverbout 0:ea44dc9ed014 15 // Third party copyrights are property of their respective owners.
joeverbout 0:ea44dc9ed014 16 //
joeverbout 0:ea44dc9ed014 17 // Redistribution and use in source and binary forms, with or without modification,
joeverbout 0:ea44dc9ed014 18 // are permitted provided that the following conditions are met:
joeverbout 0:ea44dc9ed014 19 //
joeverbout 0:ea44dc9ed014 20 // * Redistribution's of source code must retain the above copyright notice,
joeverbout 0:ea44dc9ed014 21 // this list of conditions and the following disclaimer.
joeverbout 0:ea44dc9ed014 22 //
joeverbout 0:ea44dc9ed014 23 // * Redistribution's in binary form must reproduce the above copyright notice,
joeverbout 0:ea44dc9ed014 24 // this list of conditions and the following disclaimer in the documentation
joeverbout 0:ea44dc9ed014 25 // and/or other materials provided with the distribution.
joeverbout 0:ea44dc9ed014 26 //
joeverbout 0:ea44dc9ed014 27 // * The name of the copyright holders may not be used to endorse or promote products
joeverbout 0:ea44dc9ed014 28 // derived from this software without specific prior written permission.
joeverbout 0:ea44dc9ed014 29 //
joeverbout 0:ea44dc9ed014 30 // This software is provided by the copyright holders and contributors "as is" and
joeverbout 0:ea44dc9ed014 31 // any express or implied warranties, including, but not limited to, the implied
joeverbout 0:ea44dc9ed014 32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
joeverbout 0:ea44dc9ed014 33 // In no event shall the Intel Corporation or contributors be liable for any direct,
joeverbout 0:ea44dc9ed014 34 // indirect, incidental, special, exemplary, or consequential damages
joeverbout 0:ea44dc9ed014 35 // (including, but not limited to, procurement of substitute goods or services;
joeverbout 0:ea44dc9ed014 36 // loss of use, data, or profits; or business interruption) however caused
joeverbout 0:ea44dc9ed014 37 // and on any theory of liability, whether in contract, strict liability,
joeverbout 0:ea44dc9ed014 38 // or tort (including negligence or otherwise) arising in any way out of
joeverbout 0:ea44dc9ed014 39 // the use of this software, even if advised of the possibility of such damage.
joeverbout 0:ea44dc9ed014 40 //
joeverbout 0:ea44dc9ed014 41 //M*/
joeverbout 0:ea44dc9ed014 42
joeverbout 0:ea44dc9ed014 43 #ifndef __OPENCV_STITCHING_SEAM_FINDERS_HPP__
joeverbout 0:ea44dc9ed014 44 #define __OPENCV_STITCHING_SEAM_FINDERS_HPP__
joeverbout 0:ea44dc9ed014 45
joeverbout 0:ea44dc9ed014 46 #include <set>
joeverbout 0:ea44dc9ed014 47 #include "opencv2/core.hpp"
joeverbout 0:ea44dc9ed014 48 #include "opencv2/opencv_modules.hpp"
joeverbout 0:ea44dc9ed014 49
joeverbout 0:ea44dc9ed014 50 namespace cv {
joeverbout 0:ea44dc9ed014 51 namespace detail {
joeverbout 0:ea44dc9ed014 52
joeverbout 0:ea44dc9ed014 53 //! @addtogroup stitching_seam
joeverbout 0:ea44dc9ed014 54 //! @{
joeverbout 0:ea44dc9ed014 55
joeverbout 0:ea44dc9ed014 56 /** @brief Base class for a seam estimator.
joeverbout 0:ea44dc9ed014 57 */
joeverbout 0:ea44dc9ed014 58 class CV_EXPORTS SeamFinder
joeverbout 0:ea44dc9ed014 59 {
joeverbout 0:ea44dc9ed014 60 public:
joeverbout 0:ea44dc9ed014 61 virtual ~SeamFinder() {}
joeverbout 0:ea44dc9ed014 62 /** @brief Estimates seams.
joeverbout 0:ea44dc9ed014 63
joeverbout 0:ea44dc9ed014 64 @param src Source images
joeverbout 0:ea44dc9ed014 65 @param corners Source image top-left corners
joeverbout 0:ea44dc9ed014 66 @param masks Source image masks to update
joeverbout 0:ea44dc9ed014 67 */
joeverbout 0:ea44dc9ed014 68 virtual void find(const std::vector<UMat> &src, const std::vector<Point> &corners,
joeverbout 0:ea44dc9ed014 69 std::vector<UMat> &masks) = 0;
joeverbout 0:ea44dc9ed014 70 };
joeverbout 0:ea44dc9ed014 71
joeverbout 0:ea44dc9ed014 72 /** @brief Stub seam estimator which does nothing.
joeverbout 0:ea44dc9ed014 73 */
joeverbout 0:ea44dc9ed014 74 class CV_EXPORTS NoSeamFinder : public SeamFinder
joeverbout 0:ea44dc9ed014 75 {
joeverbout 0:ea44dc9ed014 76 public:
joeverbout 0:ea44dc9ed014 77 void find(const std::vector<UMat>&, const std::vector<Point>&, std::vector<UMat>&) {}
joeverbout 0:ea44dc9ed014 78 };
joeverbout 0:ea44dc9ed014 79
joeverbout 0:ea44dc9ed014 80 /** @brief Base class for all pairwise seam estimators.
joeverbout 0:ea44dc9ed014 81 */
joeverbout 0:ea44dc9ed014 82 class CV_EXPORTS PairwiseSeamFinder : public SeamFinder
joeverbout 0:ea44dc9ed014 83 {
joeverbout 0:ea44dc9ed014 84 public:
joeverbout 0:ea44dc9ed014 85 virtual void find(const std::vector<UMat> &src, const std::vector<Point> &corners,
joeverbout 0:ea44dc9ed014 86 std::vector<UMat> &masks);
joeverbout 0:ea44dc9ed014 87
joeverbout 0:ea44dc9ed014 88 protected:
joeverbout 0:ea44dc9ed014 89 void run();
joeverbout 0:ea44dc9ed014 90 /** @brief Resolves masks intersection of two specified images in the given ROI.
joeverbout 0:ea44dc9ed014 91
joeverbout 0:ea44dc9ed014 92 @param first First image index
joeverbout 0:ea44dc9ed014 93 @param second Second image index
joeverbout 0:ea44dc9ed014 94 @param roi Region of interest
joeverbout 0:ea44dc9ed014 95 */
joeverbout 0:ea44dc9ed014 96 virtual void findInPair(size_t first, size_t second, Rect roi) = 0;
joeverbout 0:ea44dc9ed014 97
joeverbout 0:ea44dc9ed014 98 std::vector<UMat> images_;
joeverbout 0:ea44dc9ed014 99 std::vector<Size> sizes_;
joeverbout 0:ea44dc9ed014 100 std::vector<Point> corners_;
joeverbout 0:ea44dc9ed014 101 std::vector<UMat> masks_;
joeverbout 0:ea44dc9ed014 102 };
joeverbout 0:ea44dc9ed014 103
joeverbout 0:ea44dc9ed014 104 /** @brief Voronoi diagram-based seam estimator.
joeverbout 0:ea44dc9ed014 105 */
joeverbout 0:ea44dc9ed014 106 class CV_EXPORTS VoronoiSeamFinder : public PairwiseSeamFinder
joeverbout 0:ea44dc9ed014 107 {
joeverbout 0:ea44dc9ed014 108 public:
joeverbout 0:ea44dc9ed014 109 virtual void find(const std::vector<UMat> &src, const std::vector<Point> &corners,
joeverbout 0:ea44dc9ed014 110 std::vector<UMat> &masks);
joeverbout 0:ea44dc9ed014 111 virtual void find(const std::vector<Size> &size, const std::vector<Point> &corners,
joeverbout 0:ea44dc9ed014 112 std::vector<UMat> &masks);
joeverbout 0:ea44dc9ed014 113 private:
joeverbout 0:ea44dc9ed014 114 void findInPair(size_t first, size_t second, Rect roi);
joeverbout 0:ea44dc9ed014 115 };
joeverbout 0:ea44dc9ed014 116
joeverbout 0:ea44dc9ed014 117
joeverbout 0:ea44dc9ed014 118 class CV_EXPORTS DpSeamFinder : public SeamFinder
joeverbout 0:ea44dc9ed014 119 {
joeverbout 0:ea44dc9ed014 120 public:
joeverbout 0:ea44dc9ed014 121 enum CostFunction { COLOR, COLOR_GRAD };
joeverbout 0:ea44dc9ed014 122
joeverbout 0:ea44dc9ed014 123 DpSeamFinder(CostFunction costFunc = COLOR);
joeverbout 0:ea44dc9ed014 124
joeverbout 0:ea44dc9ed014 125 CostFunction costFunction() const { return costFunc_; }
joeverbout 0:ea44dc9ed014 126 void setCostFunction(CostFunction val) { costFunc_ = val; }
joeverbout 0:ea44dc9ed014 127
joeverbout 0:ea44dc9ed014 128 virtual void find(const std::vector<UMat> &src, const std::vector<Point> &corners,
joeverbout 0:ea44dc9ed014 129 std::vector<UMat> &masks);
joeverbout 0:ea44dc9ed014 130
joeverbout 0:ea44dc9ed014 131 private:
joeverbout 0:ea44dc9ed014 132 enum ComponentState
joeverbout 0:ea44dc9ed014 133 {
joeverbout 0:ea44dc9ed014 134 FIRST = 1, SECOND = 2, INTERS = 4,
joeverbout 0:ea44dc9ed014 135 INTERS_FIRST = INTERS | FIRST,
joeverbout 0:ea44dc9ed014 136 INTERS_SECOND = INTERS | SECOND
joeverbout 0:ea44dc9ed014 137 };
joeverbout 0:ea44dc9ed014 138
joeverbout 0:ea44dc9ed014 139 class ImagePairLess
joeverbout 0:ea44dc9ed014 140 {
joeverbout 0:ea44dc9ed014 141 public:
joeverbout 0:ea44dc9ed014 142 ImagePairLess(const std::vector<Mat> &images, const std::vector<Point> &corners)
joeverbout 0:ea44dc9ed014 143 : src_(&images[0]), corners_(&corners[0]) {}
joeverbout 0:ea44dc9ed014 144
joeverbout 0:ea44dc9ed014 145 bool operator() (const std::pair<size_t, size_t> &l, const std::pair<size_t, size_t> &r) const
joeverbout 0:ea44dc9ed014 146 {
joeverbout 0:ea44dc9ed014 147 Point c1 = corners_[l.first] + Point(src_[l.first].cols / 2, src_[l.first].rows / 2);
joeverbout 0:ea44dc9ed014 148 Point c2 = corners_[l.second] + Point(src_[l.second].cols / 2, src_[l.second].rows / 2);
joeverbout 0:ea44dc9ed014 149 int d1 = (c1 - c2).dot(c1 - c2);
joeverbout 0:ea44dc9ed014 150
joeverbout 0:ea44dc9ed014 151 c1 = corners_[r.first] + Point(src_[r.first].cols / 2, src_[r.first].rows / 2);
joeverbout 0:ea44dc9ed014 152 c2 = corners_[r.second] + Point(src_[r.second].cols / 2, src_[r.second].rows / 2);
joeverbout 0:ea44dc9ed014 153 int d2 = (c1 - c2).dot(c1 - c2);
joeverbout 0:ea44dc9ed014 154
joeverbout 0:ea44dc9ed014 155 return d1 < d2;
joeverbout 0:ea44dc9ed014 156 }
joeverbout 0:ea44dc9ed014 157
joeverbout 0:ea44dc9ed014 158 private:
joeverbout 0:ea44dc9ed014 159 const Mat *src_;
joeverbout 0:ea44dc9ed014 160 const Point *corners_;
joeverbout 0:ea44dc9ed014 161 };
joeverbout 0:ea44dc9ed014 162
joeverbout 0:ea44dc9ed014 163 class ClosePoints
joeverbout 0:ea44dc9ed014 164 {
joeverbout 0:ea44dc9ed014 165 public:
joeverbout 0:ea44dc9ed014 166 ClosePoints(int minDist) : minDist_(minDist) {}
joeverbout 0:ea44dc9ed014 167
joeverbout 0:ea44dc9ed014 168 bool operator() (const Point &p1, const Point &p2) const
joeverbout 0:ea44dc9ed014 169 {
joeverbout 0:ea44dc9ed014 170 int dist2 = (p1.x-p2.x) * (p1.x-p2.x) + (p1.y-p2.y) * (p1.y-p2.y);
joeverbout 0:ea44dc9ed014 171 return dist2 < minDist_ * minDist_;
joeverbout 0:ea44dc9ed014 172 }
joeverbout 0:ea44dc9ed014 173
joeverbout 0:ea44dc9ed014 174 private:
joeverbout 0:ea44dc9ed014 175 int minDist_;
joeverbout 0:ea44dc9ed014 176 };
joeverbout 0:ea44dc9ed014 177
joeverbout 0:ea44dc9ed014 178 void process(
joeverbout 0:ea44dc9ed014 179 const Mat &image1, const Mat &image2, Point tl1, Point tl2, Mat &mask1, Mat &mask2);
joeverbout 0:ea44dc9ed014 180
joeverbout 0:ea44dc9ed014 181 void findComponents();
joeverbout 0:ea44dc9ed014 182
joeverbout 0:ea44dc9ed014 183 void findEdges();
joeverbout 0:ea44dc9ed014 184
joeverbout 0:ea44dc9ed014 185 void resolveConflicts(
joeverbout 0:ea44dc9ed014 186 const Mat &image1, const Mat &image2, Point tl1, Point tl2, Mat &mask1, Mat &mask2);
joeverbout 0:ea44dc9ed014 187
joeverbout 0:ea44dc9ed014 188 void computeGradients(const Mat &image1, const Mat &image2);
joeverbout 0:ea44dc9ed014 189
joeverbout 0:ea44dc9ed014 190 bool hasOnlyOneNeighbor(int comp);
joeverbout 0:ea44dc9ed014 191
joeverbout 0:ea44dc9ed014 192 bool closeToContour(int y, int x, const Mat_<uchar> &contourMask);
joeverbout 0:ea44dc9ed014 193
joeverbout 0:ea44dc9ed014 194 bool getSeamTips(int comp1, int comp2, Point &p1, Point &p2);
joeverbout 0:ea44dc9ed014 195
joeverbout 0:ea44dc9ed014 196 void computeCosts(
joeverbout 0:ea44dc9ed014 197 const Mat &image1, const Mat &image2, Point tl1, Point tl2,
joeverbout 0:ea44dc9ed014 198 int comp, Mat_<float> &costV, Mat_<float> &costH);
joeverbout 0:ea44dc9ed014 199
joeverbout 0:ea44dc9ed014 200 bool estimateSeam(
joeverbout 0:ea44dc9ed014 201 const Mat &image1, const Mat &image2, Point tl1, Point tl2, int comp,
joeverbout 0:ea44dc9ed014 202 Point p1, Point p2, std::vector<Point> &seam, bool &isHorizontal);
joeverbout 0:ea44dc9ed014 203
joeverbout 0:ea44dc9ed014 204 void updateLabelsUsingSeam(
joeverbout 0:ea44dc9ed014 205 int comp1, int comp2, const std::vector<Point> &seam, bool isHorizontalSeam);
joeverbout 0:ea44dc9ed014 206
joeverbout 0:ea44dc9ed014 207 CostFunction costFunc_;
joeverbout 0:ea44dc9ed014 208
joeverbout 0:ea44dc9ed014 209 // processing images pair data
joeverbout 0:ea44dc9ed014 210 Point unionTl_, unionBr_;
joeverbout 0:ea44dc9ed014 211 Size unionSize_;
joeverbout 0:ea44dc9ed014 212 Mat_<uchar> mask1_, mask2_;
joeverbout 0:ea44dc9ed014 213 Mat_<uchar> contour1mask_, contour2mask_;
joeverbout 0:ea44dc9ed014 214 Mat_<float> gradx1_, grady1_;
joeverbout 0:ea44dc9ed014 215 Mat_<float> gradx2_, grady2_;
joeverbout 0:ea44dc9ed014 216
joeverbout 0:ea44dc9ed014 217 // components data
joeverbout 0:ea44dc9ed014 218 int ncomps_;
joeverbout 0:ea44dc9ed014 219 Mat_<int> labels_;
joeverbout 0:ea44dc9ed014 220 std::vector<ComponentState> states_;
joeverbout 0:ea44dc9ed014 221 std::vector<Point> tls_, brs_;
joeverbout 0:ea44dc9ed014 222 std::vector<std::vector<Point> > contours_;
joeverbout 0:ea44dc9ed014 223 std::set<std::pair<int, int> > edges_;
joeverbout 0:ea44dc9ed014 224 };
joeverbout 0:ea44dc9ed014 225
joeverbout 0:ea44dc9ed014 226 /** @brief Base class for all minimum graph-cut-based seam estimators.
joeverbout 0:ea44dc9ed014 227 */
joeverbout 0:ea44dc9ed014 228 class CV_EXPORTS GraphCutSeamFinderBase
joeverbout 0:ea44dc9ed014 229 {
joeverbout 0:ea44dc9ed014 230 public:
joeverbout 0:ea44dc9ed014 231 enum CostType { COST_COLOR, COST_COLOR_GRAD };
joeverbout 0:ea44dc9ed014 232 };
joeverbout 0:ea44dc9ed014 233
joeverbout 0:ea44dc9ed014 234 /** @brief Minimum graph cut-based seam estimator. See details in @cite V03 .
joeverbout 0:ea44dc9ed014 235 */
joeverbout 0:ea44dc9ed014 236 class CV_EXPORTS GraphCutSeamFinder : public GraphCutSeamFinderBase, public SeamFinder
joeverbout 0:ea44dc9ed014 237 {
joeverbout 0:ea44dc9ed014 238 public:
joeverbout 0:ea44dc9ed014 239 GraphCutSeamFinder(int cost_type = COST_COLOR_GRAD, float terminal_cost = 10000.f,
joeverbout 0:ea44dc9ed014 240 float bad_region_penalty = 1000.f);
joeverbout 0:ea44dc9ed014 241
joeverbout 0:ea44dc9ed014 242 ~GraphCutSeamFinder();
joeverbout 0:ea44dc9ed014 243
joeverbout 0:ea44dc9ed014 244 void find(const std::vector<UMat> &src, const std::vector<Point> &corners,
joeverbout 0:ea44dc9ed014 245 std::vector<UMat> &masks);
joeverbout 0:ea44dc9ed014 246
joeverbout 0:ea44dc9ed014 247 private:
joeverbout 0:ea44dc9ed014 248 // To avoid GCGraph dependency
joeverbout 0:ea44dc9ed014 249 class Impl;
joeverbout 0:ea44dc9ed014 250 Ptr<PairwiseSeamFinder> impl_;
joeverbout 0:ea44dc9ed014 251 };
joeverbout 0:ea44dc9ed014 252
joeverbout 0:ea44dc9ed014 253
joeverbout 0:ea44dc9ed014 254 #ifdef HAVE_OPENCV_CUDALEGACY
joeverbout 0:ea44dc9ed014 255 class CV_EXPORTS GraphCutSeamFinderGpu : public GraphCutSeamFinderBase, public PairwiseSeamFinder
joeverbout 0:ea44dc9ed014 256 {
joeverbout 0:ea44dc9ed014 257 public:
joeverbout 0:ea44dc9ed014 258 GraphCutSeamFinderGpu(int cost_type = COST_COLOR_GRAD, float terminal_cost = 10000.f,
joeverbout 0:ea44dc9ed014 259 float bad_region_penalty = 1000.f)
joeverbout 0:ea44dc9ed014 260 : cost_type_(cost_type), terminal_cost_(terminal_cost),
joeverbout 0:ea44dc9ed014 261 bad_region_penalty_(bad_region_penalty) {}
joeverbout 0:ea44dc9ed014 262
joeverbout 0:ea44dc9ed014 263 void find(const std::vector<cv::UMat> &src, const std::vector<cv::Point> &corners,
joeverbout 0:ea44dc9ed014 264 std::vector<cv::UMat> &masks);
joeverbout 0:ea44dc9ed014 265 void findInPair(size_t first, size_t second, Rect roi);
joeverbout 0:ea44dc9ed014 266
joeverbout 0:ea44dc9ed014 267 private:
joeverbout 0:ea44dc9ed014 268 void setGraphWeightsColor(const cv::Mat &img1, const cv::Mat &img2, const cv::Mat &mask1, const cv::Mat &mask2,
joeverbout 0:ea44dc9ed014 269 cv::Mat &terminals, cv::Mat &leftT, cv::Mat &rightT, cv::Mat &top, cv::Mat &bottom);
joeverbout 0:ea44dc9ed014 270 void setGraphWeightsColorGrad(const cv::Mat &img1, const cv::Mat &img2, const cv::Mat &dx1, const cv::Mat &dx2,
joeverbout 0:ea44dc9ed014 271 const cv::Mat &dy1, const cv::Mat &dy2, const cv::Mat &mask1, const cv::Mat &mask2,
joeverbout 0:ea44dc9ed014 272 cv::Mat &terminals, cv::Mat &leftT, cv::Mat &rightT, cv::Mat &top, cv::Mat &bottom);
joeverbout 0:ea44dc9ed014 273 std::vector<Mat> dx_, dy_;
joeverbout 0:ea44dc9ed014 274 int cost_type_;
joeverbout 0:ea44dc9ed014 275 float terminal_cost_;
joeverbout 0:ea44dc9ed014 276 float bad_region_penalty_;
joeverbout 0:ea44dc9ed014 277 };
joeverbout 0:ea44dc9ed014 278 #endif
joeverbout 0:ea44dc9ed014 279
joeverbout 0:ea44dc9ed014 280 //! @}
joeverbout 0:ea44dc9ed014 281
joeverbout 0:ea44dc9ed014 282 } // namespace detail
joeverbout 0:ea44dc9ed014 283 } // namespace cv
joeverbout 0:ea44dc9ed014 284
joeverbout 0:ea44dc9ed014 285 #endif // __OPENCV_STITCHING_SEAM_FINDERS_HPP__
joeverbout 0:ea44dc9ed014 286