fast_marching.cpp 4.44 KB
Newer Older
wester committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "precomp.hpp"
#include "opencv2/videostab/fast_marching.hpp"
wester committed
45 46

using namespace std;
wester committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

namespace cv
{
namespace videostab
{

float FastMarchingMethod::solve(int x1, int y1, int x2, int y2) const
{
    float sol = inf_;
    if (y1 >=0 && y1 < flag_.rows && x1 >= 0 && x1 < flag_.cols && flag_(y1,x1) == KNOWN)
    {
        float t1 = dist_(y1,x1);
        if (y2 >=0 && y2 < flag_.rows && x2 >= 0 && x2 < flag_.cols && flag_(y2,x2) == KNOWN)
        {
            float t2 = dist_(y2,x2);
wester committed
62
            float r = sqrt(2 - sqr(t1 - t2));
wester committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
            float s = (t1 + t2 - r) / 2;

            if (s >= t1 && s >= t2)
                sol = s;
            else
            {
                s += r;
                if (s >= t1 && s >= t2)
                    sol = s;
            }
        }
        else
            sol = 1 + t1;
    }
    else if (y2 >=0 && y2 < flag_.rows && x2 >= 0 && x2 < flag_.cols && flag_(y2,x2) == KNOWN)
        sol = 1 + dist_(y2,x1);
    return sol;
}


void FastMarchingMethod::heapUp(int idx)
{
    int p = (idx-1)/2;
    while (idx > 0 && narrowBand_[idx] < narrowBand_[p])
    {
        std::swap(indexOf(narrowBand_[p]), indexOf(narrowBand_[idx]));
        std::swap(narrowBand_[p], narrowBand_[idx]);
        idx = p;
        p = (idx-1)/2;
    }
}


void FastMarchingMethod::heapDown(int idx)
{
    int l, r, smallest;
    for(;;)
    {
        l = 2*idx+1;
        r = 2*idx+2;
        smallest = idx;

        if (l < size_ && narrowBand_[l] < narrowBand_[smallest]) smallest = l;
        if (r < size_ && narrowBand_[r] < narrowBand_[smallest]) smallest = r;

        if (smallest == idx)
            break;
        else
        {
            std::swap(indexOf(narrowBand_[idx]), indexOf(narrowBand_[smallest]));
            std::swap(narrowBand_[idx], narrowBand_[smallest]);
            idx = smallest;
        }
    }
}


void FastMarchingMethod::heapAdd(const DXY &dxy)
{
    if (static_cast<int>(narrowBand_.size()) < size_ + 1)
        narrowBand_.resize(size_*2 + 1);
    narrowBand_[size_] = dxy;
    indexOf(dxy) = size_++;
    heapUp(size_-1);
}


void FastMarchingMethod::heapRemoveMin()
{
    if (size_ > 0)
    {
        size_--;
        std::swap(indexOf(narrowBand_[0]), indexOf(narrowBand_[size_]));
        std::swap(narrowBand_[0], narrowBand_[size_]);
        heapDown(0);
    }
}

} // namespace videostab
} // namespace cv