hfs.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #include "opencv2/core.hpp"
  5. namespace cv { namespace hfs {
  6. /** @defgroup hfs Hierarchical Feature Selection for Efficient Image Segmentation
  7. The opencv hfs module contains an efficient algorithm to segment an image.
  8. This module is implemented based on the paper Hierarchical Feature Selection for Efficient
  9. Image Segmentation, ECCV 2016. The original project was developed by
  10. Yun Liu(https://github.com/yun-liu/hfs).
  11. Introduction to Hierarchical Feature Selection
  12. ----------------------------------------------
  13. This algorithm is executed in 3 stages:
  14. In the first stage, the algorithm uses SLIC (simple linear iterative clustering) algorithm
  15. to obtain the superpixel of the input image.
  16. In the second stage, the algorithm view each superpixel as a node in the graph.
  17. It will calculate a feature vector for each edge of the graph. It then calculates a weight
  18. for each edge based on the feature vector and trained SVM parameters. After obtaining
  19. weight for each edge, it will exploit EGB (Efficient Graph-based Image Segmentation)
  20. algorithm to merge some nodes in the graph thus obtaining a coarser segmentation
  21. After these operations, a post process will be executed to merge regions that are smaller
  22. then a specific number of pixels into their nearby region.
  23. In the third stage, the algorithm exploits the similar mechanism to further merge
  24. the small regions obtained in the second stage into even coarser segmentation.
  25. After these three stages, we can obtain the final segmentation of the image.
  26. For further details about the algorithm, please refer to the original paper:
  27. Hierarchical Feature Selection for Efficient Image Segmentation, ECCV 2016
  28. */
  29. //! @addtogroup hfs
  30. //! @{
  31. class CV_EXPORTS_W HfsSegment : public Algorithm {
  32. public:
  33. /** @brief: set and get the parameter segEgbThresholdI.
  34. * This parameter is used in the second stage mentioned above.
  35. * It is a constant used to threshold weights of the edge when merging
  36. * adjacent nodes when applying EGB algorithm. The segmentation result
  37. * tends to have more regions remained if this value is large and vice versa.
  38. */
  39. CV_WRAP virtual void setSegEgbThresholdI(float c) = 0;
  40. CV_WRAP virtual float getSegEgbThresholdI() = 0;
  41. /** @brief: set and get the parameter minRegionSizeI.
  42. * This parameter is used in the second stage
  43. * mentioned above. After the EGB segmentation, regions that have fewer
  44. * pixels then this parameter will be merged into it's adjacent region.
  45. */
  46. CV_WRAP virtual void setMinRegionSizeI(int n) = 0;
  47. CV_WRAP virtual int getMinRegionSizeI() = 0;
  48. /** @brief: set and get the parameter segEgbThresholdII.
  49. * This parameter is used in the third stage
  50. * mentioned above. It serves the same purpose as segEgbThresholdI.
  51. * The segmentation result tends to have more regions remained if
  52. * this value is large and vice versa.
  53. */
  54. CV_WRAP virtual void setSegEgbThresholdII(float c) = 0;
  55. CV_WRAP virtual float getSegEgbThresholdII() = 0;
  56. /** @brief: set and get the parameter minRegionSizeII.
  57. * This parameter is used in the third stage
  58. * mentioned above. It serves the same purpose as minRegionSizeI
  59. */
  60. CV_WRAP virtual void setMinRegionSizeII(int n) = 0;
  61. CV_WRAP virtual int getMinRegionSizeII() = 0;
  62. /** @brief: set and get the parameter spatialWeight.
  63. * This parameter is used in the first stage
  64. * mentioned above(the SLIC stage). It describes how important is the role
  65. * of position when calculating the distance between each pixel and it's
  66. * center. The exact formula to calculate the distance is
  67. * \f$colorDistance + spatialWeight \times spatialDistance\f$.
  68. * The segmentation result tends to have more local consistency
  69. * if this value is larger.
  70. */
  71. CV_WRAP virtual void setSpatialWeight(float w) = 0;
  72. CV_WRAP virtual float getSpatialWeight() = 0;
  73. /** @brief: set and get the parameter slicSpixelSize.
  74. * This parameter is used in the first stage mentioned
  75. * above(the SLIC stage). It describes the size of each
  76. * superpixel when initializing SLIC. Every superpixel
  77. * approximately has \f$slicSpixelSize \times slicSpixelSize\f$
  78. * pixels in the beginning.
  79. */
  80. CV_WRAP virtual void setSlicSpixelSize(int n) = 0;
  81. CV_WRAP virtual int getSlicSpixelSize() = 0;
  82. /** @brief: set and get the parameter numSlicIter.
  83. * This parameter is used in the first stage. It
  84. * describes how many iteration to perform when executing SLIC.
  85. */
  86. CV_WRAP virtual void setNumSlicIter(int n) = 0;
  87. CV_WRAP virtual int getNumSlicIter() = 0;
  88. /** @brief do segmentation gpu
  89. * @param src: the input image
  90. * @param ifDraw: if draw the image in the returned Mat. if this parameter is false,
  91. * then the content of the returned Mat is a matrix of index, describing the region
  92. * each pixel belongs to. And it's data type is CV_16U. If this parameter is true,
  93. * then the returned Mat is a segmented picture, and color of each region is the
  94. * average color of all pixels in that region. And it's data type is the same as
  95. * the input image
  96. */
  97. CV_WRAP virtual Mat performSegmentGpu(InputArray src, bool ifDraw = true) = 0;
  98. /** @brief do segmentation with cpu
  99. * This method is only implemented for reference.
  100. * It is highly NOT recommanded to use it.
  101. */
  102. CV_WRAP virtual Mat performSegmentCpu(InputArray src, bool ifDraw = true) = 0;
  103. /** @brief: create a hfs object
  104. * @param height: the height of the input image
  105. * @param width: the width of the input image
  106. * @param segEgbThresholdI: parameter segEgbThresholdI
  107. * @param minRegionSizeI: parameter minRegionSizeI
  108. * @param segEgbThresholdII: parameter segEgbThresholdII
  109. * @param minRegionSizeII: parameter minRegionSizeII
  110. * @param spatialWeight: parameter spatialWeight
  111. * @param slicSpixelSize: parameter slicSpixelSize
  112. * @param numSlicIter: parameter numSlicIter
  113. */
  114. CV_WRAP static Ptr<HfsSegment> create(int height, int width,
  115. float segEgbThresholdI = 0.08f, int minRegionSizeI = 100,
  116. float segEgbThresholdII = 0.28f, int minRegionSizeII = 200,
  117. float spatialWeight = 0.6f, int slicSpixelSize = 8, int numSlicIter = 5);
  118. };
  119. //! @}
  120. }} // namespace cv { namespace hfs {