1/*M/////////////////////////////////////////////////////////////////////////////////////// 2// 3// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. 4// 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. 8// 9// 10// License Agreement 11// For Open Source Computer Vision Library 12// 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. 16// 17// Redistribution and use in source and binary forms, with or without modification, 18// are permitted provided that the following conditions are met: 19// 20// * Redistribution's of source code must retain the above copyright notice, 21// this list of conditions and the following disclaimer. 22// 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. 26// 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. 29// 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. 40// 41//M*/ 42 43#include "precomp.hpp" 44#include "opencv2/videostab/fast_marching.hpp" 45#include "opencv2/videostab/ring_buffer.hpp" 46 47namespace cv 48{ 49namespace videostab 50{ 51 52float FastMarchingMethod::solve(int x1, int y1, int x2, int y2) const 53{ 54 float sol = inf_; 55 if (y1 >=0 && y1 < flag_.rows && x1 >= 0 && x1 < flag_.cols && flag_(y1,x1) == KNOWN) 56 { 57 float t1 = dist_(y1,x1); 58 if (y2 >=0 && y2 < flag_.rows && x2 >= 0 && x2 < flag_.cols && flag_(y2,x2) == KNOWN) 59 { 60 float t2 = dist_(y2,x2); 61 float r = std::sqrt(2 - sqr(t1 - t2)); 62 float s = (t1 + t2 - r) / 2; 63 64 if (s >= t1 && s >= t2) 65 sol = s; 66 else 67 { 68 s += r; 69 if (s >= t1 && s >= t2) 70 sol = s; 71 } 72 } 73 else 74 sol = 1 + t1; 75 } 76 else if (y2 >=0 && y2 < flag_.rows && x2 >= 0 && x2 < flag_.cols && flag_(y2,x2) == KNOWN) 77 sol = 1 + dist_(y2,x1); 78 return sol; 79} 80 81 82void FastMarchingMethod::heapUp(int idx) 83{ 84 int p = (idx-1)/2; 85 while (idx > 0 && narrowBand_[idx] < narrowBand_[p]) 86 { 87 std::swap(indexOf(narrowBand_[p]), indexOf(narrowBand_[idx])); 88 std::swap(narrowBand_[p], narrowBand_[idx]); 89 idx = p; 90 p = (idx-1)/2; 91 } 92} 93 94 95void FastMarchingMethod::heapDown(int idx) 96{ 97 int l, r, smallest; 98 for(;;) 99 { 100 l = 2*idx+1; 101 r = 2*idx+2; 102 smallest = idx; 103 104 if (l < size_ && narrowBand_[l] < narrowBand_[smallest]) smallest = l; 105 if (r < size_ && narrowBand_[r] < narrowBand_[smallest]) smallest = r; 106 107 if (smallest == idx) 108 break; 109 else 110 { 111 std::swap(indexOf(narrowBand_[idx]), indexOf(narrowBand_[smallest])); 112 std::swap(narrowBand_[idx], narrowBand_[smallest]); 113 idx = smallest; 114 } 115 } 116} 117 118 119void FastMarchingMethod::heapAdd(const DXY &dxy) 120{ 121 if (static_cast<int>(narrowBand_.size()) < size_ + 1) 122 narrowBand_.resize(size_*2 + 1); 123 narrowBand_[size_] = dxy; 124 indexOf(dxy) = size_++; 125 heapUp(size_-1); 126} 127 128 129void FastMarchingMethod::heapRemoveMin() 130{ 131 if (size_ > 0) 132 { 133 size_--; 134 std::swap(indexOf(narrowBand_[0]), indexOf(narrowBand_[size_])); 135 std::swap(narrowBand_[0], narrowBand_[size_]); 136 heapDown(0); 137 } 138} 139 140} // namespace videostab 141} // namespace cv 142