lsc.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*********************************************************************
  2. * Software License Agreement (BSD License)
  3. *
  4. * Copyright (c) 2014, 2015
  5. * Zhengqin Li <li-zq12 at mails dot tsinghua dot edu dot cn>
  6. * Jiansheng Chen <jschenthu at mail dot tsinghua dot edu dot cn>
  7. * Tsinghua University
  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. "Superpixel Segmentation using Linear Spectral Clustering"
  38. Zhengqin Li, Jiansheng Chen, IEEE Conference on Computer Vision and Pattern
  39. Recognition (CVPR), Jun. 2015
  40. OpenCV port by: Cristian Balint <cristian dot balint at gmail dot com>
  41. */
  42. #ifndef __OPENCV_LSC_HPP__
  43. #define __OPENCV_LSC_HPP__
  44. #ifdef __cplusplus
  45. #include <opencv2/core.hpp>
  46. namespace cv
  47. {
  48. namespace ximgproc
  49. {
  50. //! @addtogroup ximgproc_superpixel
  51. //! @{
  52. /** @brief Class implementing the LSC (Linear Spectral Clustering) superpixels
  53. algorithm described in @cite LiCVPR2015LSC.
  54. LSC (Linear Spectral Clustering) produces compact and uniform superpixels with low
  55. computational costs. Basically, a normalized cuts formulation of the superpixel
  56. segmentation is adopted based on a similarity metric that measures the color
  57. similarity and space proximity between image pixels. LSC is of linear computational
  58. complexity and high memory efficiency and is able to preserve global properties of images
  59. */
  60. class CV_EXPORTS_W SuperpixelLSC : public Algorithm
  61. {
  62. public:
  63. /** @brief Calculates the actual amount of superpixels on a given segmentation computed
  64. and stored in SuperpixelLSC object.
  65. */
  66. CV_WRAP virtual int getNumberOfSuperpixels() const = 0;
  67. /** @brief Calculates the superpixel segmentation on a given image with the initialized
  68. parameters in the SuperpixelLSC object.
  69. This function can be called again without the need of initializing the algorithm with
  70. createSuperpixelLSC(). This save the computational cost of allocating memory for all the
  71. structures of the algorithm.
  72. @param num_iterations Number of iterations. Higher number improves the result.
  73. The function computes the superpixels segmentation of an image with the parameters initialized
  74. with the function createSuperpixelLSC(). The algorithms starts from a grid of superpixels and
  75. then refines the boundaries by proposing updates of edges boundaries.
  76. */
  77. CV_WRAP virtual void iterate( int num_iterations = 10 ) = 0;
  78. /** @brief Returns the segmentation labeling of the image.
  79. Each label represents a superpixel, and each pixel is assigned to one superpixel label.
  80. @param labels_out Return: A CV_32SC1 integer array containing the labels of the superpixel
  81. segmentation. The labels are in the range [0, getNumberOfSuperpixels()].
  82. The function returns an image with the labels of the superpixel segmentation. The labels are in
  83. the range [0, getNumberOfSuperpixels()].
  84. */
  85. CV_WRAP virtual void getLabels( OutputArray labels_out ) const = 0;
  86. /** @brief Returns the mask of the superpixel segmentation stored in SuperpixelLSC object.
  87. @param image Return: CV_8U1 image mask where -1 indicates that the pixel is a superpixel border,
  88. and 0 otherwise.
  89. @param thick_line If false, the border is only one pixel wide, otherwise all pixels at the border
  90. are masked.
  91. The function return the boundaries of the superpixel segmentation.
  92. */
  93. CV_WRAP virtual void getLabelContourMask( OutputArray image, bool thick_line = true ) const = 0;
  94. /** @brief Enforce label connectivity.
  95. @param min_element_size The minimum element size in percents that should be absorbed into a bigger
  96. superpixel. Given resulted average superpixel size valid value should be in 0-100 range, 25 means
  97. that less then a quarter sized superpixel should be absorbed, this is default.
  98. The function merge component that is too small, assigning the previously found adjacent label
  99. to this component. Calling this function may change the final number of superpixels.
  100. */
  101. CV_WRAP virtual void enforceLabelConnectivity( int min_element_size = 20 ) = 0;
  102. };
  103. /** @brief Class implementing the LSC (Linear Spectral Clustering) superpixels
  104. @param image Image to segment
  105. @param region_size Chooses an average superpixel size measured in pixels
  106. @param ratio Chooses the enforcement of superpixel compactness factor of superpixel
  107. The function initializes a SuperpixelLSC object for the input image. It sets the parameters of
  108. superpixel algorithm, which are: region_size and ruler. It preallocate some buffers for future
  109. computing iterations over the given image. An example of LSC is ilustrated in the following picture.
  110. For enanched results it is recommended for color images to preprocess image with little gaussian blur
  111. with a small 3 x 3 kernel and additional conversion into CieLAB color space.
  112. ![image](pics/superpixels_lsc.png)
  113. */
  114. CV_EXPORTS_W Ptr<SuperpixelLSC> createSuperpixelLSC( InputArray image, int region_size = 10, float ratio = 0.075f );
  115. //! @}
  116. }
  117. }
  118. #endif
  119. #endif