openCV library for Renesas RZ/A
Dependents: RZ_A2M_Mbed_samples
include/opencv2/videostab/fast_marching_inl.hpp@0:0e0631af0305, 2021-01-29 (annotated)
- Committer:
- RyoheiHagimoto
- Date:
- Fri Jan 29 04:53:38 2021 +0000
- Revision:
- 0:0e0631af0305
copied from https://github.com/d-kato/opencv-lib.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RyoheiHagimoto | 0:0e0631af0305 | 1 | /*M/////////////////////////////////////////////////////////////////////////////////////// |
RyoheiHagimoto | 0:0e0631af0305 | 2 | // |
RyoheiHagimoto | 0:0e0631af0305 | 3 | // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. |
RyoheiHagimoto | 0:0e0631af0305 | 4 | // |
RyoheiHagimoto | 0:0e0631af0305 | 5 | // By downloading, copying, installing or using the software you agree to this license. |
RyoheiHagimoto | 0:0e0631af0305 | 6 | // If you do not agree to this license, do not download, install, |
RyoheiHagimoto | 0:0e0631af0305 | 7 | // copy or use the software. |
RyoheiHagimoto | 0:0e0631af0305 | 8 | // |
RyoheiHagimoto | 0:0e0631af0305 | 9 | // |
RyoheiHagimoto | 0:0e0631af0305 | 10 | // License Agreement |
RyoheiHagimoto | 0:0e0631af0305 | 11 | // For Open Source Computer Vision Library |
RyoheiHagimoto | 0:0e0631af0305 | 12 | // |
RyoheiHagimoto | 0:0e0631af0305 | 13 | // Copyright (C) 2000-2008, Intel Corporation, all rights reserved. |
RyoheiHagimoto | 0:0e0631af0305 | 14 | // Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved. |
RyoheiHagimoto | 0:0e0631af0305 | 15 | // Third party copyrights are property of their respective owners. |
RyoheiHagimoto | 0:0e0631af0305 | 16 | // |
RyoheiHagimoto | 0:0e0631af0305 | 17 | // Redistribution and use in source and binary forms, with or without modification, |
RyoheiHagimoto | 0:0e0631af0305 | 18 | // are permitted provided that the following conditions are met: |
RyoheiHagimoto | 0:0e0631af0305 | 19 | // |
RyoheiHagimoto | 0:0e0631af0305 | 20 | // * Redistribution's of source code must retain the above copyright notice, |
RyoheiHagimoto | 0:0e0631af0305 | 21 | // this list of conditions and the following disclaimer. |
RyoheiHagimoto | 0:0e0631af0305 | 22 | // |
RyoheiHagimoto | 0:0e0631af0305 | 23 | // * Redistribution's in binary form must reproduce the above copyright notice, |
RyoheiHagimoto | 0:0e0631af0305 | 24 | // this list of conditions and the following disclaimer in the documentation |
RyoheiHagimoto | 0:0e0631af0305 | 25 | // and/or other materials provided with the distribution. |
RyoheiHagimoto | 0:0e0631af0305 | 26 | // |
RyoheiHagimoto | 0:0e0631af0305 | 27 | // * The name of the copyright holders may not be used to endorse or promote products |
RyoheiHagimoto | 0:0e0631af0305 | 28 | // derived from this software without specific prior written permission. |
RyoheiHagimoto | 0:0e0631af0305 | 29 | // |
RyoheiHagimoto | 0:0e0631af0305 | 30 | // This software is provided by the copyright holders and contributors "as is" and |
RyoheiHagimoto | 0:0e0631af0305 | 31 | // any express or implied warranties, including, but not limited to, the implied |
RyoheiHagimoto | 0:0e0631af0305 | 32 | // warranties of merchantability and fitness for a particular purpose are disclaimed. |
RyoheiHagimoto | 0:0e0631af0305 | 33 | // In no event shall the Intel Corporation or contributors be liable for any direct, |
RyoheiHagimoto | 0:0e0631af0305 | 34 | // indirect, incidental, special, exemplary, or consequential damages |
RyoheiHagimoto | 0:0e0631af0305 | 35 | // (including, but not limited to, procurement of substitute goods or services; |
RyoheiHagimoto | 0:0e0631af0305 | 36 | // loss of use, data, or profits; or business interruption) however caused |
RyoheiHagimoto | 0:0e0631af0305 | 37 | // and on any theory of liability, whether in contract, strict liability, |
RyoheiHagimoto | 0:0e0631af0305 | 38 | // or tort (including negligence or otherwise) arising in any way out of |
RyoheiHagimoto | 0:0e0631af0305 | 39 | // the use of this software, even if advised of the possibility of such damage. |
RyoheiHagimoto | 0:0e0631af0305 | 40 | // |
RyoheiHagimoto | 0:0e0631af0305 | 41 | //M*/ |
RyoheiHagimoto | 0:0e0631af0305 | 42 | |
RyoheiHagimoto | 0:0e0631af0305 | 43 | #ifndef OPENCV_VIDEOSTAB_FAST_MARCHING_INL_HPP |
RyoheiHagimoto | 0:0e0631af0305 | 44 | #define OPENCV_VIDEOSTAB_FAST_MARCHING_INL_HPP |
RyoheiHagimoto | 0:0e0631af0305 | 45 | |
RyoheiHagimoto | 0:0e0631af0305 | 46 | #include "opencv2/videostab/fast_marching.hpp" |
RyoheiHagimoto | 0:0e0631af0305 | 47 | |
RyoheiHagimoto | 0:0e0631af0305 | 48 | namespace cv |
RyoheiHagimoto | 0:0e0631af0305 | 49 | { |
RyoheiHagimoto | 0:0e0631af0305 | 50 | namespace videostab |
RyoheiHagimoto | 0:0e0631af0305 | 51 | { |
RyoheiHagimoto | 0:0e0631af0305 | 52 | |
RyoheiHagimoto | 0:0e0631af0305 | 53 | template <typename Inpaint> |
RyoheiHagimoto | 0:0e0631af0305 | 54 | Inpaint FastMarchingMethod::run(const cv::Mat &mask, Inpaint inpaint) |
RyoheiHagimoto | 0:0e0631af0305 | 55 | { |
RyoheiHagimoto | 0:0e0631af0305 | 56 | using namespace cv; |
RyoheiHagimoto | 0:0e0631af0305 | 57 | |
RyoheiHagimoto | 0:0e0631af0305 | 58 | CV_Assert(mask.type() == CV_8U); |
RyoheiHagimoto | 0:0e0631af0305 | 59 | |
RyoheiHagimoto | 0:0e0631af0305 | 60 | static const int lut[4][2] = {{-1,0}, {0,-1}, {1,0}, {0,1}}; |
RyoheiHagimoto | 0:0e0631af0305 | 61 | |
RyoheiHagimoto | 0:0e0631af0305 | 62 | mask.copyTo(flag_); |
RyoheiHagimoto | 0:0e0631af0305 | 63 | flag_.create(mask.size()); |
RyoheiHagimoto | 0:0e0631af0305 | 64 | dist_.create(mask.size()); |
RyoheiHagimoto | 0:0e0631af0305 | 65 | index_.create(mask.size()); |
RyoheiHagimoto | 0:0e0631af0305 | 66 | narrowBand_.clear(); |
RyoheiHagimoto | 0:0e0631af0305 | 67 | size_ = 0; |
RyoheiHagimoto | 0:0e0631af0305 | 68 | |
RyoheiHagimoto | 0:0e0631af0305 | 69 | // init |
RyoheiHagimoto | 0:0e0631af0305 | 70 | for (int y = 0; y < flag_.rows; ++y) |
RyoheiHagimoto | 0:0e0631af0305 | 71 | { |
RyoheiHagimoto | 0:0e0631af0305 | 72 | for (int x = 0; x < flag_.cols; ++x) |
RyoheiHagimoto | 0:0e0631af0305 | 73 | { |
RyoheiHagimoto | 0:0e0631af0305 | 74 | if (flag_(y,x) == KNOWN) |
RyoheiHagimoto | 0:0e0631af0305 | 75 | dist_(y,x) = 0.f; |
RyoheiHagimoto | 0:0e0631af0305 | 76 | else |
RyoheiHagimoto | 0:0e0631af0305 | 77 | { |
RyoheiHagimoto | 0:0e0631af0305 | 78 | int n = 0; |
RyoheiHagimoto | 0:0e0631af0305 | 79 | int nunknown = 0; |
RyoheiHagimoto | 0:0e0631af0305 | 80 | |
RyoheiHagimoto | 0:0e0631af0305 | 81 | for (int i = 0; i < 4; ++i) |
RyoheiHagimoto | 0:0e0631af0305 | 82 | { |
RyoheiHagimoto | 0:0e0631af0305 | 83 | int xn = x + lut[i][0]; |
RyoheiHagimoto | 0:0e0631af0305 | 84 | int yn = y + lut[i][1]; |
RyoheiHagimoto | 0:0e0631af0305 | 85 | |
RyoheiHagimoto | 0:0e0631af0305 | 86 | if (xn >= 0 && xn < flag_.cols && yn >= 0 && yn < flag_.rows) |
RyoheiHagimoto | 0:0e0631af0305 | 87 | { |
RyoheiHagimoto | 0:0e0631af0305 | 88 | n++; |
RyoheiHagimoto | 0:0e0631af0305 | 89 | if (flag_(yn,xn) != KNOWN) |
RyoheiHagimoto | 0:0e0631af0305 | 90 | nunknown++; |
RyoheiHagimoto | 0:0e0631af0305 | 91 | } |
RyoheiHagimoto | 0:0e0631af0305 | 92 | } |
RyoheiHagimoto | 0:0e0631af0305 | 93 | |
RyoheiHagimoto | 0:0e0631af0305 | 94 | if (n>0 && nunknown == n) |
RyoheiHagimoto | 0:0e0631af0305 | 95 | { |
RyoheiHagimoto | 0:0e0631af0305 | 96 | dist_(y,x) = inf_; |
RyoheiHagimoto | 0:0e0631af0305 | 97 | flag_(y,x) = INSIDE; |
RyoheiHagimoto | 0:0e0631af0305 | 98 | } |
RyoheiHagimoto | 0:0e0631af0305 | 99 | else |
RyoheiHagimoto | 0:0e0631af0305 | 100 | { |
RyoheiHagimoto | 0:0e0631af0305 | 101 | dist_(y,x) = 0.f; |
RyoheiHagimoto | 0:0e0631af0305 | 102 | flag_(y,x) = BAND; |
RyoheiHagimoto | 0:0e0631af0305 | 103 | inpaint(x, y); |
RyoheiHagimoto | 0:0e0631af0305 | 104 | |
RyoheiHagimoto | 0:0e0631af0305 | 105 | narrowBand_.push_back(DXY(0.f,x,y)); |
RyoheiHagimoto | 0:0e0631af0305 | 106 | index_(y,x) = size_++; |
RyoheiHagimoto | 0:0e0631af0305 | 107 | } |
RyoheiHagimoto | 0:0e0631af0305 | 108 | } |
RyoheiHagimoto | 0:0e0631af0305 | 109 | } |
RyoheiHagimoto | 0:0e0631af0305 | 110 | } |
RyoheiHagimoto | 0:0e0631af0305 | 111 | |
RyoheiHagimoto | 0:0e0631af0305 | 112 | // make heap |
RyoheiHagimoto | 0:0e0631af0305 | 113 | for (int i = size_/2-1; i >= 0; --i) |
RyoheiHagimoto | 0:0e0631af0305 | 114 | heapDown(i); |
RyoheiHagimoto | 0:0e0631af0305 | 115 | |
RyoheiHagimoto | 0:0e0631af0305 | 116 | // main cycle |
RyoheiHagimoto | 0:0e0631af0305 | 117 | while (size_ > 0) |
RyoheiHagimoto | 0:0e0631af0305 | 118 | { |
RyoheiHagimoto | 0:0e0631af0305 | 119 | int x = narrowBand_[0].x; |
RyoheiHagimoto | 0:0e0631af0305 | 120 | int y = narrowBand_[0].y; |
RyoheiHagimoto | 0:0e0631af0305 | 121 | heapRemoveMin(); |
RyoheiHagimoto | 0:0e0631af0305 | 122 | |
RyoheiHagimoto | 0:0e0631af0305 | 123 | flag_(y,x) = KNOWN; |
RyoheiHagimoto | 0:0e0631af0305 | 124 | for (int n = 0; n < 4; ++n) |
RyoheiHagimoto | 0:0e0631af0305 | 125 | { |
RyoheiHagimoto | 0:0e0631af0305 | 126 | int xn = x + lut[n][0]; |
RyoheiHagimoto | 0:0e0631af0305 | 127 | int yn = y + lut[n][1]; |
RyoheiHagimoto | 0:0e0631af0305 | 128 | |
RyoheiHagimoto | 0:0e0631af0305 | 129 | if (xn >= 0 && xn < flag_.cols && yn >= 0 && yn < flag_.rows && flag_(yn,xn) != KNOWN) |
RyoheiHagimoto | 0:0e0631af0305 | 130 | { |
RyoheiHagimoto | 0:0e0631af0305 | 131 | dist_(yn,xn) = std::min(std::min(solve(xn-1, yn, xn, yn-1), solve(xn+1, yn, xn, yn-1)), |
RyoheiHagimoto | 0:0e0631af0305 | 132 | std::min(solve(xn-1, yn, xn, yn+1), solve(xn+1, yn, xn, yn+1))); |
RyoheiHagimoto | 0:0e0631af0305 | 133 | |
RyoheiHagimoto | 0:0e0631af0305 | 134 | if (flag_(yn,xn) == INSIDE) |
RyoheiHagimoto | 0:0e0631af0305 | 135 | { |
RyoheiHagimoto | 0:0e0631af0305 | 136 | flag_(yn,xn) = BAND; |
RyoheiHagimoto | 0:0e0631af0305 | 137 | inpaint(xn, yn); |
RyoheiHagimoto | 0:0e0631af0305 | 138 | heapAdd(DXY(dist_(yn,xn),xn,yn)); |
RyoheiHagimoto | 0:0e0631af0305 | 139 | } |
RyoheiHagimoto | 0:0e0631af0305 | 140 | else |
RyoheiHagimoto | 0:0e0631af0305 | 141 | { |
RyoheiHagimoto | 0:0e0631af0305 | 142 | int i = index_(yn,xn); |
RyoheiHagimoto | 0:0e0631af0305 | 143 | if (dist_(yn,xn) < narrowBand_[i].dist) |
RyoheiHagimoto | 0:0e0631af0305 | 144 | { |
RyoheiHagimoto | 0:0e0631af0305 | 145 | narrowBand_[i].dist = dist_(yn,xn); |
RyoheiHagimoto | 0:0e0631af0305 | 146 | heapUp(i); |
RyoheiHagimoto | 0:0e0631af0305 | 147 | } |
RyoheiHagimoto | 0:0e0631af0305 | 148 | // works better if it's commented out |
RyoheiHagimoto | 0:0e0631af0305 | 149 | /*else if (dist(yn,xn) > narrowBand[i].dist) |
RyoheiHagimoto | 0:0e0631af0305 | 150 | { |
RyoheiHagimoto | 0:0e0631af0305 | 151 | narrowBand[i].dist = dist(yn,xn); |
RyoheiHagimoto | 0:0e0631af0305 | 152 | heapDown(i); |
RyoheiHagimoto | 0:0e0631af0305 | 153 | }*/ |
RyoheiHagimoto | 0:0e0631af0305 | 154 | } |
RyoheiHagimoto | 0:0e0631af0305 | 155 | } |
RyoheiHagimoto | 0:0e0631af0305 | 156 | } |
RyoheiHagimoto | 0:0e0631af0305 | 157 | } |
RyoheiHagimoto | 0:0e0631af0305 | 158 | |
RyoheiHagimoto | 0:0e0631af0305 | 159 | return inpaint; |
RyoheiHagimoto | 0:0e0631af0305 | 160 | } |
RyoheiHagimoto | 0:0e0631af0305 | 161 | |
RyoheiHagimoto | 0:0e0631af0305 | 162 | } // namespace videostab |
RyoheiHagimoto | 0:0e0631af0305 | 163 | } // namespace cv |
RyoheiHagimoto | 0:0e0631af0305 | 164 | |
RyoheiHagimoto | 0:0e0631af0305 | 165 | #endif |