seeds.hpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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) 2014, Beat Kueng (beat-kueng@gmx.net), Lukas Vogel, Morten Lysgaard
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #ifndef __OPENCV_SEEDS_HPP__
  42. #define __OPENCV_SEEDS_HPP__
  43. #ifdef __cplusplus
  44. #include <opencv2/core.hpp>
  45. namespace cv
  46. {
  47. namespace ximgproc
  48. {
  49. //! @addtogroup ximgproc_superpixel
  50. //! @{
  51. /** @brief Class implementing the SEEDS (Superpixels Extracted via Energy-Driven Sampling) superpixels
  52. algorithm described in @cite VBRV14 .
  53. The algorithm uses an efficient hill-climbing algorithm to optimize the superpixels' energy
  54. function that is based on color histograms and a boundary term, which is optional. The energy
  55. function encourages superpixels to be of the same color, and if the boundary term is activated, the
  56. superpixels have smooth boundaries and are of similar shape. In practice it starts from a regular
  57. grid of superpixels and moves the pixels or blocks of pixels at the boundaries to refine the
  58. solution. The algorithm runs in real-time using a single CPU.
  59. */
  60. class CV_EXPORTS_W SuperpixelSEEDS : public Algorithm
  61. {
  62. public:
  63. /** @brief Calculates the superpixel segmentation on a given image stored in SuperpixelSEEDS object.
  64. The function computes the superpixels segmentation of an image with the parameters initialized
  65. with the function createSuperpixelSEEDS().
  66. */
  67. CV_WRAP virtual int getNumberOfSuperpixels() = 0;
  68. /** @brief Calculates the superpixel segmentation on a given image with the initialized
  69. parameters in the SuperpixelSEEDS object.
  70. This function can be called again for other images without the need of initializing the
  71. algorithm with createSuperpixelSEEDS(). This save the computational cost of allocating memory
  72. for all the structures of the algorithm.
  73. @param img Input image. Supported formats: CV_8U, CV_16U, CV_32F. Image size & number of
  74. channels must match with the initialized image size & channels with the function
  75. createSuperpixelSEEDS(). It should be in HSV or Lab color space. Lab is a bit better, but also
  76. slower.
  77. @param num_iterations Number of pixel level iterations. Higher number improves the result.
  78. The function computes the superpixels segmentation of an image with the parameters initialized
  79. with the function createSuperpixelSEEDS(). The algorithms starts from a grid of superpixels and
  80. then refines the boundaries by proposing updates of blocks of pixels that lie at the boundaries
  81. from large to smaller size, finalizing with proposing pixel updates. An illustrative example
  82. can be seen below.
  83. ![image](pics/superpixels_blocks2.png)
  84. */
  85. CV_WRAP virtual void iterate(InputArray img, int num_iterations=4) = 0;
  86. /** @brief Returns the segmentation labeling of the image.
  87. Each label represents a superpixel, and each pixel is assigned to one superpixel label.
  88. @param labels_out Return: A CV_32UC1 integer array containing the labels of the superpixel
  89. segmentation. The labels are in the range [0, getNumberOfSuperpixels()].
  90. The function returns an image with ssthe labels of the superpixel segmentation. The labels are in
  91. the range [0, getNumberOfSuperpixels()].
  92. */
  93. CV_WRAP virtual void getLabels(OutputArray labels_out) = 0;
  94. /** @brief Returns the mask of the superpixel segmentation stored in SuperpixelSEEDS object.
  95. @param image Return: CV_8UC1 image mask where -1 indicates that the pixel is a superpixel border,
  96. and 0 otherwise.
  97. @param thick_line If false, the border is only one pixel wide, otherwise all pixels at the border
  98. are masked.
  99. The function return the boundaries of the superpixel segmentation.
  100. @note
  101. - (Python) A demo on how to generate superpixels in images from the webcam can be found at
  102. opencv_source_code/samples/python2/seeds.py
  103. - (cpp) A demo on how to generate superpixels in images from the webcam can be found at
  104. opencv_source_code/modules/ximgproc/samples/seeds.cpp. By adding a file image as a command
  105. line argument, the static image will be used instead of the webcam.
  106. - It will show a window with the video from the webcam with the superpixel boundaries marked
  107. in red (see below). Use Space to switch between different output modes. At the top of the
  108. window there are 4 sliders, from which the user can change on-the-fly the number of
  109. superpixels, the number of block levels, the strength of the boundary prior term to modify
  110. the shape, and the number of iterations at pixel level. This is useful to play with the
  111. parameters and set them to the user convenience. In the console the frame-rate of the
  112. algorithm is indicated.
  113. ![image](pics/superpixels_demo.png)
  114. */
  115. CV_WRAP virtual void getLabelContourMask(OutputArray image, bool thick_line = false) = 0;
  116. virtual ~SuperpixelSEEDS() {}
  117. };
  118. /** @brief Initializes a SuperpixelSEEDS object.
  119. @param image_width Image width.
  120. @param image_height Image height.
  121. @param image_channels Number of channels of the image.
  122. @param num_superpixels Desired number of superpixels. Note that the actual number may be smaller
  123. due to restrictions (depending on the image size and num_levels). Use getNumberOfSuperpixels() to
  124. get the actual number.
  125. @param num_levels Number of block levels. The more levels, the more accurate is the segmentation,
  126. but needs more memory and CPU time.
  127. @param prior enable 3x3 shape smoothing term if \>0. A larger value leads to smoother shapes. prior
  128. must be in the range [0, 5].
  129. @param histogram_bins Number of histogram bins.
  130. @param double_step If true, iterate each block level twice for higher accuracy.
  131. The function initializes a SuperpixelSEEDS object for the input image. It stores the parameters of
  132. the image: image_width, image_height and image_channels. It also sets the parameters of the SEEDS
  133. superpixel algorithm, which are: num_superpixels, num_levels, use_prior, histogram_bins and
  134. double_step.
  135. The number of levels in num_levels defines the amount of block levels that the algorithm use in the
  136. optimization. The initialization is a grid, in which the superpixels are equally distributed through
  137. the width and the height of the image. The larger blocks correspond to the superpixel size, and the
  138. levels with smaller blocks are formed by dividing the larger blocks into 2 x 2 blocks of pixels,
  139. recursively until the smaller block level. An example of initialization of 4 block levels is
  140. illustrated in the following figure.
  141. ![image](pics/superpixels_blocks.png)
  142. */
  143. CV_EXPORTS_W Ptr<SuperpixelSEEDS> createSuperpixelSEEDS(
  144. int image_width, int image_height, int image_channels,
  145. int num_superpixels, int num_levels, int prior = 2,
  146. int histogram_bins=5, bool double_step = false);
  147. //! @}
  148. }
  149. }
  150. #endif
  151. #endif