segmentation.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #ifndef OPENCV_IMGPROC_SEGMENTATION_HPP
  5. #define OPENCV_IMGPROC_SEGMENTATION_HPP
  6. #include "opencv2/imgproc.hpp"
  7. namespace cv {
  8. namespace segmentation {
  9. //! @addtogroup imgproc_segmentation
  10. //! @{
  11. /** @brief Intelligent Scissors image segmentation
  12. *
  13. * This class is used to find the path (contour) between two points
  14. * which can be used for image segmentation.
  15. *
  16. * Usage example:
  17. * @snippet snippets/imgproc_segmentation.cpp usage_example_intelligent_scissors
  18. *
  19. * Reference: <a href="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.138.3811&rep=rep1&type=pdf">"Intelligent Scissors for Image Composition"</a>
  20. * algorithm designed by Eric N. Mortensen and William A. Barrett, Brigham Young University
  21. * @cite Mortensen95intelligentscissors
  22. */
  23. class CV_EXPORTS_W_SIMPLE IntelligentScissorsMB
  24. {
  25. public:
  26. CV_WRAP
  27. IntelligentScissorsMB();
  28. /** @brief Specify weights of feature functions
  29. *
  30. * Consider keeping weights normalized (sum of weights equals to 1.0)
  31. * Discrete dynamic programming (DP) goal is minimization of costs between pixels.
  32. *
  33. * @param weight_non_edge Specify cost of non-edge pixels (default: 0.43f)
  34. * @param weight_gradient_direction Specify cost of gradient direction function (default: 0.43f)
  35. * @param weight_gradient_magnitude Specify cost of gradient magnitude function (default: 0.14f)
  36. */
  37. CV_WRAP
  38. IntelligentScissorsMB& setWeights(float weight_non_edge, float weight_gradient_direction, float weight_gradient_magnitude);
  39. /** @brief Specify gradient magnitude max value threshold
  40. *
  41. * Zero limit value is used to disable gradient magnitude thresholding (default behavior, as described in original article).
  42. * Otherwize pixels with `gradient magnitude >= threshold` have zero cost.
  43. *
  44. * @note Thresholding should be used for images with irregular regions (to avoid stuck on parameters from high-contract areas, like embedded logos).
  45. *
  46. * @param gradient_magnitude_threshold_max Specify gradient magnitude max value threshold (default: 0, disabled)
  47. */
  48. CV_WRAP
  49. IntelligentScissorsMB& setGradientMagnitudeMaxLimit(float gradient_magnitude_threshold_max = 0.0f);
  50. /** @brief Switch to "Laplacian Zero-Crossing" edge feature extractor and specify its parameters
  51. *
  52. * This feature extractor is used by default according to article.
  53. *
  54. * Implementation has additional filtering for regions with low-amplitude noise.
  55. * This filtering is enabled through parameter of minimal gradient amplitude (use some small value 4, 8, 16).
  56. *
  57. * @note Current implementation of this feature extractor is based on processing of grayscale images (color image is converted to grayscale image first).
  58. *
  59. * @note Canny edge detector is a bit slower, but provides better results (especially on color images): use setEdgeFeatureCannyParameters().
  60. *
  61. * @param gradient_magnitude_min_value Minimal gradient magnitude value for edge pixels (default: 0, check is disabled)
  62. */
  63. CV_WRAP
  64. IntelligentScissorsMB& setEdgeFeatureZeroCrossingParameters(float gradient_magnitude_min_value = 0.0f);
  65. /** @brief Switch edge feature extractor to use Canny edge detector
  66. *
  67. * @note "Laplacian Zero-Crossing" feature extractor is used by default (following to original article)
  68. *
  69. * @sa Canny
  70. */
  71. CV_WRAP
  72. IntelligentScissorsMB& setEdgeFeatureCannyParameters(
  73. double threshold1, double threshold2,
  74. int apertureSize = 3, bool L2gradient = false
  75. );
  76. /** @brief Specify input image and extract image features
  77. *
  78. * @param image input image. Type is #CV_8UC1 / #CV_8UC3
  79. */
  80. CV_WRAP
  81. IntelligentScissorsMB& applyImage(InputArray image);
  82. /** @brief Specify custom features of input image
  83. *
  84. * Customized advanced variant of applyImage() call.
  85. *
  86. * @param non_edge Specify cost of non-edge pixels. Type is CV_8UC1. Expected values are `{0, 1}`.
  87. * @param gradient_direction Specify gradient direction feature. Type is CV_32FC2. Values are expected to be normalized: `x^2 + y^2 == 1`
  88. * @param gradient_magnitude Specify cost of gradient magnitude function: Type is CV_32FC1. Values should be in range `[0, 1]`.
  89. * @param image **Optional parameter**. Must be specified if subset of features is specified (non-specified features are calculated internally)
  90. */
  91. CV_WRAP
  92. IntelligentScissorsMB& applyImageFeatures(
  93. InputArray non_edge, InputArray gradient_direction, InputArray gradient_magnitude,
  94. InputArray image = noArray()
  95. );
  96. /** @brief Prepares a map of optimal paths for the given source point on the image
  97. *
  98. * @note applyImage() / applyImageFeatures() must be called before this call
  99. *
  100. * @param sourcePt The source point used to find the paths
  101. */
  102. CV_WRAP void buildMap(const Point& sourcePt);
  103. /** @brief Extracts optimal contour for the given target point on the image
  104. *
  105. * @note buildMap() must be called before this call
  106. *
  107. * @param targetPt The target point
  108. * @param[out] contour The list of pixels which contains optimal path between the source and the target points of the image. Type is CV_32SC2 (compatible with `std::vector<Point>`)
  109. * @param backward Flag to indicate reverse order of retrived pixels (use "true" value to fetch points from the target to the source point)
  110. */
  111. CV_WRAP void getContour(const Point& targetPt, OutputArray contour, bool backward = false) const;
  112. #ifndef CV_DOXYGEN
  113. struct Impl;
  114. inline Impl* getImpl() const { return impl.get(); }
  115. protected:
  116. std::shared_ptr<Impl> impl;
  117. #endif
  118. };
  119. //! @}
  120. } // namespace segmentation
  121. } // namespace cv
  122. #endif // OPENCV_IMGPROC_SEGMENTATION_HPP