1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
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.
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.
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.
43 #ifndef __OPENCV_VIDEOSTAB_FAST_MARCHING_INL_HPP__
44 #define __OPENCV_VIDEOSTAB_FAST_MARCHING_INL_HPP__
46 #include "opencv2/videostab/fast_marching.hpp"
53 template <typename Inpaint>
54 void FastMarchingMethod::run(const cv::Mat &mask, Inpaint inpaint)
59 CV_Assert(mask.type() == CV_8U);
61 static const int lut[4][2] = {{-1,0}, {0,-1}, {1,0}, {0,1}};
64 flag_.create(mask.size());
65 dist_.create(mask.size());
66 index_.create(mask.size());
71 for (int y = 0; y < flag_.rows; ++y)
73 for (int x = 0; x < flag_.cols; ++x)
75 if (flag_(y,x) == KNOWN)
82 for (int i = 0; i < 4; ++i)
84 int xn = x + lut[i][0];
85 int yn = y + lut[i][1];
87 if (xn >= 0 && xn < flag_.cols && yn >= 0 && yn < flag_.rows)
90 if (flag_(yn,xn) != KNOWN)
95 if (n>0 && nunknown == n)
106 narrowBand_.push_back(DXY(0.f,x,y));
107 index_(y,x) = size_++;
114 for (int i = size_/2-1; i >= 0; --i)
120 int x = narrowBand_[0].x;
121 int y = narrowBand_[0].y;
125 for (int n = 0; n < 4; ++n)
127 int xn = x + lut[n][0];
128 int yn = y + lut[n][1];
130 if (xn >= 0 && xn < flag_.cols && yn >= 0 && yn < flag_.rows && flag_(yn,xn) != KNOWN)
132 dist_(yn,xn) = min(min(solve(xn-1, yn, xn, yn-1), solve(xn+1, yn, xn, yn-1)),
133 min(solve(xn-1, yn, xn, yn+1), solve(xn+1, yn, xn, yn+1)));
135 if (flag_(yn,xn) == INSIDE)
139 heapAdd(DXY(dist_(yn,xn),xn,yn));
143 int i = index_(yn,xn);
144 if (dist_(yn,xn) < narrowBand_[i].dist)
146 narrowBand_[i].dist = dist_(yn,xn);
149 // works better if it's commented out
150 /*else if (dist(yn,xn) > narrowBand[i].dist)
152 narrowBand[i].dist = dist(yn,xn);
161 } // namespace videostab