fast_marching_inl.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #ifndef OPENCV_VIDEOSTAB_FAST_MARCHING_INL_HPP
  43. #define OPENCV_VIDEOSTAB_FAST_MARCHING_INL_HPP
  44. #include "opencv2/videostab/fast_marching.hpp"
  45. namespace cv
  46. {
  47. namespace videostab
  48. {
  49. template <typename Inpaint>
  50. Inpaint FastMarchingMethod::run(const cv::Mat &mask, Inpaint inpaint)
  51. {
  52. using namespace cv;
  53. CV_Assert(mask.type() == CV_8U);
  54. static const int lut[4][2] = {{-1,0}, {0,-1}, {1,0}, {0,1}};
  55. mask.copyTo(flag_);
  56. flag_.create(mask.size());
  57. dist_.create(mask.size());
  58. index_.create(mask.size());
  59. narrowBand_.clear();
  60. size_ = 0;
  61. // init
  62. for (int y = 0; y < flag_.rows; ++y)
  63. {
  64. for (int x = 0; x < flag_.cols; ++x)
  65. {
  66. if (flag_(y,x) == KNOWN)
  67. dist_(y,x) = 0.f;
  68. else
  69. {
  70. int n = 0;
  71. int nunknown = 0;
  72. for (int i = 0; i < 4; ++i)
  73. {
  74. int xn = x + lut[i][0];
  75. int yn = y + lut[i][1];
  76. if (xn >= 0 && xn < flag_.cols && yn >= 0 && yn < flag_.rows)
  77. {
  78. n++;
  79. if (flag_(yn,xn) != KNOWN)
  80. nunknown++;
  81. }
  82. }
  83. if (n>0 && nunknown == n)
  84. {
  85. dist_(y,x) = inf_;
  86. flag_(y,x) = INSIDE;
  87. }
  88. else
  89. {
  90. dist_(y,x) = 0.f;
  91. flag_(y,x) = BAND;
  92. inpaint(x, y);
  93. narrowBand_.push_back(DXY(0.f,x,y));
  94. index_(y,x) = size_++;
  95. }
  96. }
  97. }
  98. }
  99. // make heap
  100. for (int i = size_/2-1; i >= 0; --i)
  101. heapDown(i);
  102. // main cycle
  103. while (size_ > 0)
  104. {
  105. int x = narrowBand_[0].x;
  106. int y = narrowBand_[0].y;
  107. heapRemoveMin();
  108. flag_(y,x) = KNOWN;
  109. for (int n = 0; n < 4; ++n)
  110. {
  111. int xn = x + lut[n][0];
  112. int yn = y + lut[n][1];
  113. if (xn >= 0 && xn < flag_.cols && yn >= 0 && yn < flag_.rows && flag_(yn,xn) != KNOWN)
  114. {
  115. dist_(yn,xn) = std::min(std::min(solve(xn-1, yn, xn, yn-1), solve(xn+1, yn, xn, yn-1)),
  116. std::min(solve(xn-1, yn, xn, yn+1), solve(xn+1, yn, xn, yn+1)));
  117. if (flag_(yn,xn) == INSIDE)
  118. {
  119. flag_(yn,xn) = BAND;
  120. inpaint(xn, yn);
  121. heapAdd(DXY(dist_(yn,xn),xn,yn));
  122. }
  123. else
  124. {
  125. int i = index_(yn,xn);
  126. if (dist_(yn,xn) < narrowBand_[i].dist)
  127. {
  128. narrowBand_[i].dist = dist_(yn,xn);
  129. heapUp(i);
  130. }
  131. // works better if it's commented out
  132. /*else if (dist(yn,xn) > narrowBand[i].dist)
  133. {
  134. narrowBand[i].dist = dist(yn,xn);
  135. heapDown(i);
  136. }*/
  137. }
  138. }
  139. }
  140. }
  141. return inpaint;
  142. }
  143. } // namespace videostab
  144. } // namespace cv
  145. #endif