slic.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*********************************************************************
  2. * Software License Agreement (BSD License)
  3. *
  4. * Copyright (c) 2013
  5. * Radhakrishna Achanta
  6. * email : Radhakrishna [dot] Achanta [at] epfl [dot] ch
  7. * web : http://ivrl.epfl.ch/people/achanta
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials provided
  18. * with the distribution.
  19. * * Neither the name of the copyright holders nor the names of its
  20. * contributors may be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  30. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  33. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. *********************************************************************/
  36. /*
  37. "SLIC Superpixels Compared to State-of-the-art Superpixel Methods"
  38. Radhakrishna Achanta, Appu Shaji, Kevin Smith, Aurelien Lucchi, Pascal Fua,
  39. and Sabine Susstrunk, IEEE TPAMI, Volume 34, Issue 11, Pages 2274-2282,
  40. November 2012.
  41. "SLIC Superpixels" Radhakrishna Achanta, Appu Shaji, Kevin Smith,
  42. Aurelien Lucchi, Pascal Fua, and Sabine Süsstrunk, EPFL Technical
  43. Report no. 149300, June 2010.
  44. OpenCV port by: Cristian Balint <cristian dot balint at gmail dot com>
  45. */
  46. #ifndef __OPENCV_SLIC_HPP__
  47. #define __OPENCV_SLIC_HPP__
  48. #ifdef __cplusplus
  49. #include <opencv2/core.hpp>
  50. namespace cv
  51. {
  52. namespace ximgproc
  53. {
  54. //! @addtogroup ximgproc_superpixel
  55. //! @{
  56. enum SLICType { SLIC = 100, SLICO = 101, MSLIC = 102 };
  57. /** @brief Class implementing the SLIC (Simple Linear Iterative Clustering) superpixels
  58. algorithm described in @cite Achanta2012.
  59. SLIC (Simple Linear Iterative Clustering) clusters pixels using pixel channels and image plane space
  60. to efficiently generate compact, nearly uniform superpixels. The simplicity of approach makes it
  61. extremely easy to use a lone parameter specifies the number of superpixels and the efficiency of
  62. the algorithm makes it very practical.
  63. Several optimizations are available for SLIC class:
  64. SLICO stands for "Zero parameter SLIC" and it is an optimization of baseline SLIC descibed in @cite Achanta2012.
  65. MSLIC stands for "Manifold SLIC" and it is an optimization of baseline SLIC described in @cite Liu_2017_IEEE.
  66. */
  67. class CV_EXPORTS_W SuperpixelSLIC : public Algorithm
  68. {
  69. public:
  70. /** @brief Calculates the actual amount of superpixels on a given segmentation computed
  71. and stored in SuperpixelSLIC object.
  72. */
  73. CV_WRAP virtual int getNumberOfSuperpixels() const = 0;
  74. /** @brief Calculates the superpixel segmentation on a given image with the initialized
  75. parameters in the SuperpixelSLIC object.
  76. This function can be called again without the need of initializing the algorithm with
  77. createSuperpixelSLIC(). This save the computational cost of allocating memory for all the
  78. structures of the algorithm.
  79. @param num_iterations Number of iterations. Higher number improves the result.
  80. The function computes the superpixels segmentation of an image with the parameters initialized
  81. with the function createSuperpixelSLIC(). The algorithms starts from a grid of superpixels and
  82. then refines the boundaries by proposing updates of edges boundaries.
  83. */
  84. CV_WRAP virtual void iterate( int num_iterations = 10 ) = 0;
  85. /** @brief Returns the segmentation labeling of the image.
  86. Each label represents a superpixel, and each pixel is assigned to one superpixel label.
  87. @param labels_out Return: A CV_32SC1 integer array containing the labels of the superpixel
  88. segmentation. The labels are in the range [0, getNumberOfSuperpixels()].
  89. The function returns an image with the labels of the superpixel segmentation. The labels are in
  90. the range [0, getNumberOfSuperpixels()].
  91. */
  92. CV_WRAP virtual void getLabels( OutputArray labels_out ) const = 0;
  93. /** @brief Returns the mask of the superpixel segmentation stored in SuperpixelSLIC object.
  94. @param image Return: CV_8U1 image mask where -1 indicates that the pixel is a superpixel border,
  95. and 0 otherwise.
  96. @param thick_line If false, the border is only one pixel wide, otherwise all pixels at the border
  97. are masked.
  98. The function return the boundaries of the superpixel segmentation.
  99. */
  100. CV_WRAP virtual void getLabelContourMask( OutputArray image, bool thick_line = true ) const = 0;
  101. /** @brief Enforce label connectivity.
  102. @param min_element_size The minimum element size in percents that should be absorbed into a bigger
  103. superpixel. Given resulted average superpixel size valid value should be in 0-100 range, 25 means
  104. that less then a quarter sized superpixel should be absorbed, this is default.
  105. The function merge component that is too small, assigning the previously found adjacent label
  106. to this component. Calling this function may change the final number of superpixels.
  107. */
  108. CV_WRAP virtual void enforceLabelConnectivity( int min_element_size = 25 ) = 0;
  109. };
  110. /** @brief Initialize a SuperpixelSLIC object
  111. @param image Image to segment
  112. @param algorithm Chooses the algorithm variant to use:
  113. SLIC segments image using a desired region_size, and in addition SLICO will optimize using adaptive compactness factor,
  114. while MSLIC will optimize using manifold methods resulting in more content-sensitive superpixels.
  115. @param region_size Chooses an average superpixel size measured in pixels
  116. @param ruler Chooses the enforcement of superpixel smoothness factor of superpixel
  117. The function initializes a SuperpixelSLIC object for the input image. It sets the parameters of choosed
  118. superpixel algorithm, which are: region_size and ruler. It preallocate some buffers for future
  119. computing iterations over the given image. For enanched results it is recommended for color images to
  120. preprocess image with little gaussian blur using a small 3 x 3 kernel and additional conversion into
  121. CieLAB color space. An example of SLIC versus SLICO and MSLIC is ilustrated in the following picture.
  122. ![image](pics/superpixels_slic.png)
  123. */
  124. CV_EXPORTS_W Ptr<SuperpixelSLIC> createSuperpixelSLIC( InputArray image, int algorithm = SLICO,
  125. int region_size = 10, float ruler = 10.0f );
  126. //! @}
  127. }
  128. }
  129. #endif
  130. #endif