Renesas / opencv-lib

Dependents:   RZ_A2M_Mbed_samples

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers fast_marching_inl.hpp Source File

fast_marching_inl.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-2011, 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_VIDEOSTAB_FAST_MARCHING_INL_HPP
00044 #define OPENCV_VIDEOSTAB_FAST_MARCHING_INL_HPP
00045 
00046 #include "opencv2/videostab/fast_marching.hpp"
00047 
00048 namespace cv
00049 {
00050 namespace videostab
00051 {
00052 
00053 template <typename Inpaint>
00054 Inpaint FastMarchingMethod::run(const cv::Mat &mask, Inpaint inpaint)
00055 {
00056     using namespace cv;
00057 
00058     CV_Assert(mask.type() == CV_8U);
00059 
00060     static const int lut[4][2] = {{-1,0}, {0,-1}, {1,0}, {0,1}};
00061 
00062     mask.copyTo(flag_);
00063     flag_.create(mask.size());
00064     dist_.create(mask.size());
00065     index_.create(mask.size());
00066     narrowBand_.clear();
00067     size_ = 0;
00068 
00069     // init
00070     for (int y = 0; y < flag_.rows; ++y)
00071     {
00072         for (int x = 0; x < flag_.cols; ++x)
00073         {
00074             if (flag_(y,x) == KNOWN)
00075                 dist_(y,x) = 0.f;
00076             else
00077             {
00078                 int n = 0;
00079                 int nunknown = 0;
00080 
00081                 for (int i = 0; i < 4; ++i)
00082                 {
00083                     int xn = x + lut[i][0];
00084                     int yn = y + lut[i][1];
00085 
00086                     if (xn >= 0 && xn < flag_.cols && yn >= 0 && yn < flag_.rows)
00087                     {
00088                         n++;
00089                         if (flag_(yn,xn) != KNOWN)
00090                             nunknown++;
00091                     }
00092                 }
00093 
00094                 if (n>0 && nunknown == n)
00095                 {
00096                     dist_(y,x) = inf_;
00097                     flag_(y,x) = INSIDE;
00098                 }
00099                 else
00100                 {
00101                     dist_(y,x) = 0.f;
00102                     flag_(y,x) = BAND;
00103                     inpaint(x, y);
00104 
00105                     narrowBand_.push_back(DXY(0.f,x,y));
00106                     index_(y,x) = size_++;
00107                 }
00108             }
00109         }
00110     }
00111 
00112     // make heap
00113     for (int i = size_/2-1; i >= 0; --i)
00114         heapDown(i);
00115 
00116     // main cycle
00117     while (size_ > 0)
00118     {
00119         int x = narrowBand_[0].x;
00120         int y = narrowBand_[0].y;
00121         heapRemoveMin();
00122 
00123         flag_(y,x) = KNOWN;
00124         for (int n = 0; n < 4; ++n)
00125         {
00126             int xn = x + lut[n][0];
00127             int yn = y + lut[n][1];
00128 
00129             if (xn >= 0 && xn < flag_.cols && yn >= 0 && yn < flag_.rows && flag_(yn,xn) != KNOWN)
00130             {
00131                 dist_(yn,xn) = std::min(std::min(solve(xn-1, yn, xn, yn-1), solve(xn+1, yn, xn, yn-1)),
00132                                         std::min(solve(xn-1, yn, xn, yn+1), solve(xn+1, yn, xn, yn+1)));
00133 
00134                 if (flag_(yn,xn) == INSIDE)
00135                 {
00136                     flag_(yn,xn) = BAND;
00137                     inpaint(xn, yn);
00138                     heapAdd(DXY(dist_(yn,xn),xn,yn));
00139                 }
00140                 else
00141                 {
00142                     int i = index_(yn,xn);
00143                     if (dist_(yn,xn) < narrowBand_[i].dist)
00144                     {
00145                         narrowBand_[i].dist = dist_(yn,xn);
00146                         heapUp(i);
00147                     }
00148                     // works better if it's commented out
00149                     /*else if (dist(yn,xn) > narrowBand[i].dist)
00150                     {
00151                         narrowBand[i].dist = dist(yn,xn);
00152                         heapDown(i);
00153                     }*/
00154                 }
00155             }
00156         }
00157     }
00158 
00159     return inpaint;
00160 }
00161 
00162 } // namespace videostab
00163 } // namespace cv
00164 
00165 #endif