Skip to content

Commit f07e304

Browse files
committed
Merge pull request #90 from bkueng/superpixel_SEEDS
Integration of SEEDS superpixel algorithm to opencv_contrib
2 parents 4114d06 + 728b825 commit f07e304

File tree

13 files changed

+2138
-1
lines changed

13 files changed

+2138
-1
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.autosave
2+
*.pyc
3+
*.user
4+
*~
5+
.*.swp
6+
.DS_Store
7+
.sw[a-z]
8+
Thumbs.db
9+
tags
10+
tegra/
166 KB
Loading
137 KB
Loading
747 KB
Loading
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
Superpixels
2+
===========
3+
4+
SuperpixelSEEDS
5+
---------------
6+
.. ocv:class:: SuperpixelSEEDS : public Algorithm
7+
8+
Class implementing the SEEDS (Superpixels Extracted via Energy-Driven Sampling) superpixels algorithm described in [VBRV14]_. The algorithm uses an efficient hill-climbing algorithm to optimize the superpixels' energy function that is based on color histograms and a boundary term, which is optional. The energy function encourages superpixels to be of the same color, and if the boundary term is activated, the superpixels have smooth boundaries and are of similar shape. In practice it starts from a regular grid of superpixels and moves the pixels or blocks of pixels at the boundaries to refine the solution. The algorithm runs in real-time using a single CPU.
9+
10+
.. [VBRV14] Michael Van den Bergh, Xavier Boix, Gemma Roig, Luc Van Gool: SEEDS: Superpixels Extracted via Energy-Driven Sampling. International Journal of Computer Vision (IJCV), 2014.
11+
12+
.. highlight:: cpp
13+
14+
15+
SuperpixelSEEDS::createSuperpixelSEEDS()
16+
----------------------------------------
17+
Initializes a SuperpixelSEEDS object.
18+
19+
.. ocv:function:: SuperpixelSEEDS::createSuperpixelSEEDS(int image_width, int image_height, int image_channels, int num_superpixels, int num_levels, int use_prior = 2, int histogram_bins=5, bool double_step = false)
20+
21+
.. ocv:pyfunction:: cv2.SuperpixelSEEDS.createSuperpixelSEEDS(image_width, image_height, image_channels, num_superpixels, num_levels, use_prior = 2, histogram_bins=5, double_step = false) -> <SuperpixelSEEDS object>
22+
23+
24+
:param image_width: Image width.
25+
26+
:param image_height: Image height.
27+
28+
:param image_channels: Number of channels of the image.
29+
30+
:param num_superpixels: Desired number of superpixels. Note that the actual number may be smaller due to restrictions (depending on the image size and num_levels). Use getNumberOfSuperpixels() to get the actual number.
31+
32+
:param num_levels: Number of block levels. The more levels, the more accurate is the segmentation, but needs more memory and CPU time.
33+
34+
:param prior: enable 3x3 shape smoothing term if >0. A larger value leads to smoother shapes. prior must be in the range [0, 5].
35+
36+
:param histogram_bins: Number of histogram bins.
37+
38+
:param double_step: If true, iterate each block level twice for higher accuracy.
39+
40+
The function initializes a SuperpixelSEEDS object for the input ``image``. It stores the parameters of the image: ``image_width``, ``image_height`` and ``image_channels``. It also sets the parameters of the SEEDS superpixel algorithm, which are: ``num_superpixels``, ``num_levels``, ``use_prior``, ``histogram_bins`` and ``double_step``.
41+
42+
The number of levels in ``num_levels`` defines the amount of block levels that the algorithm use in the optimization. The initialization is a grid, in which the superpixels are equally distributed through the width and the height of the image. The larger blocks correspond to the superpixel size, and the levels with smaller blocks are formed by dividing the larger blocks into 2 x 2 blocks of pixels, recursively until the smaller block level. An example of initialization of 4 block levels is illustrated in the following figure.
43+
44+
45+
.. image:: pics/superpixels_blocks.png
46+
47+
48+
SuperpixelSEEDS::iterate()
49+
--------------------------
50+
Calculates the superpixel segmentation on a given image with the initialized parameters in the SuperpixelSEEDS object. This function can be called again for other images without the need of initializing the algorithm with createSuperpixelSEEDS(). This save the computational cost of allocating memory for all the structures of the algorithm.
51+
52+
.. ocv:function:: void SuperpixelSEEDS::iterate(InputArray img, int num_iterations=4)
53+
54+
.. ocv:pyfunction:: cv2.SuperpixelSEEDS.iterate(image, num_iterations)
55+
56+
57+
:param img: Input image. Supported formats: CV_8U, CV_16U, CV_32F. Image size & number of channels must match with the initialized image size & channels with the function createSuperpixelSEEDS(). It should be in HSV or Lab color space. Lab is a bit better, but also slower.
58+
59+
:param num_iterations: Number of pixel level iterations. Higher number improves the result.
60+
61+
The function computes the superpixels segmentation of an image with the parameters initialized with the function createSuperpixelSEEDS(). The algorithms starts from a grid of superpixels and then refines the boundaries by proposing updates of blocks of pixels that lie at the boundaries from large to smaller size, finalizing with proposing pixel updates. An illustrative example can be seen below.
62+
63+
.. image:: pics/superpixels_blocks2.png
64+
65+
SuperpixelSEEDS::getNumberOfSuperpixels()
66+
-----------------------------------------
67+
Calculates the superpixel segmentation on a given image stored in SuperpixelSEEDS object.
68+
69+
.. ocv:function:: void SuperpixelSEEDS::getNumberOfSuperpixels(InputArray img, int num_iterations=4)
70+
71+
.. ocv:pyfunction:: cv2.SuperpixelSEEDS.getNumberOfSuperpixels(img, num_iterations=4)
72+
73+
74+
:param img: Input image. Supported formats: CV_8U, CV_16U, CV_32F image size & number of channels must match with the initialized image size & channels with the function createSuperpixelSEEDS().
75+
76+
:param num_iterations: Number of pixel level iterations. Higher number improves the result.
77+
78+
The function computes the superpixels segmentation of an image with the parameters initialized with the function createSuperpixelSEEDS().
79+
80+
81+
SuperpixelSEEDS::getLabels()
82+
----------------------------
83+
Returns the segmentation labeling of the image. Each label represents a superpixel, and each pixel is assigned to one superpixel label.
84+
85+
.. ocv:function:: void SuperpixelSEEDS::getLabels(OutputArray labels_out)
86+
87+
.. ocv:pyfunction:: cv2.SuperpixelSEEDS.getLabels(labels_out)
88+
89+
90+
:param labels_out: Return: A CV_32UC1 integer array containing the labels of the superpixel segmentation. The labels are in the range [0, getNumberOfSuperpixels()].
91+
92+
The function returns an image with ssthe labels of the superpixel segmentation. The labels are in the range [0, getNumberOfSuperpixels()].
93+
94+
95+
SuperpixelSEEDS::getLabelContourMask()
96+
--------------------------------------
97+
Returns the mask of the superpixel segmentation stored in SuperpixelSEEDS object.
98+
99+
.. ocv:function:: void SuperpixelSEEDS::getLabelContourMask(OutputArray image, bool thick_line = false)
100+
101+
.. ocv:pyfunction:: cv2.SuperpixelSEEDS.getLabelContourMask(image, thick_line = false)
102+
103+
:param image: Return: CV_8UC1 image mask where -1 indicates that the pixel is a superpixel border, and 0 otherwise.
104+
105+
:param thick_line: If false, the border is only one pixel wide, otherwise all pixels at the border are masked.
106+
107+
The function return the boundaries of the superpixel segmentation.
108+
109+
110+
.. note::
111+
112+
* (Python) A demo on how to generate superpixels in images from the webcam can be found at opencv_source_code/samples/python2/seeds.py
113+
114+
* (cpp) A demo on how to generate superpixels in images from the webcam can be found at opencv_source_code/modules/ximgproc/samples/seeds.cpp. By adding a file image as a command line argument, the static image will be used instead of the webcam.
115+
116+
* It will show a window with the video from the webcam with the superpixel boundaries marked in red (see below). Use Space to switch between different output modes. At the top of the window there are 4 sliders, from which the user can change on-the-fly the number of superpixels, the number of block levels, the strength of the boundary prior term to modify the shape, and the number of iterations at pixel level. This is useful to play with the parameters and set them to the user convenience. In the console the frame-rate of the algorithm is indicated.
117+
118+
.. image:: pics/superpixels_demo.png

modules/ximgproc/doc/ximgproc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ ximgproc. Extended Image Processing
99

1010
structured_edge_detection
1111
edge_aware_filters
12+
superpixels

modules/ximgproc/include/opencv2/ximgproc.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@
3939

4040
#include "ximgproc/edge_filter.hpp"
4141
#include "ximgproc/structured_edge_detection.hpp"
42+
#include "ximgproc/seeds.hpp"
4243

43-
#endif
44+
#endif
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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 ([email protected]), 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+
42+
#ifndef __OPENCV_SEEDS_HPP__
43+
#define __OPENCV_SEEDS_HPP__
44+
#ifdef __cplusplus
45+
46+
#include <opencv2/core.hpp>
47+
48+
namespace cv
49+
{
50+
namespace ximgproc
51+
{
52+
53+
54+
//! Superpixel implementation: "SEEDS: Superpixels Extracted via Energy-Driven Sampling", IJCV 2014
55+
class CV_EXPORTS_W SuperpixelSEEDS : public Algorithm
56+
{
57+
public:
58+
59+
/*! get the actual number of superpixels */
60+
CV_WRAP virtual int getNumberOfSuperpixels() = 0;
61+
62+
/*!
63+
* calculate the segmentation on a given image. To get the result use getLabels()
64+
* @param img input image. supported formats: CV_8U, CV_16U, CV_32F
65+
* image size & number of channels must match with the
66+
* initialized image size & channels.
67+
* @param num_iterations number of pixel level iterations. higher number
68+
* improves the result
69+
*/
70+
CV_WRAP virtual void iterate(InputArray img, int num_iterations=4) = 0;
71+
72+
/*!
73+
* retrieve the segmentation results.
74+
* @param labels_out Return: A CV_32UC1 integer array containing the labels
75+
* labels are in the range [0, getNumberOfSuperpixels()]
76+
*/
77+
CV_WRAP virtual void getLabels(OutputArray labels_out) = 0;
78+
79+
/*!
80+
* get an image mask with the contour of the superpixels. useful for test output.
81+
* @param image Return: CV_8UC1 image mask where -1 is a superpixel border
82+
* pixel and 0 an interior pixel.
83+
* @param thick_line if false, border is only one pixel wide, otherwise
84+
* all border pixels are masked
85+
*/
86+
CV_WRAP virtual void getLabelContourMask(OutputArray image, bool thick_line = false) = 0;
87+
88+
virtual ~SuperpixelSEEDS() {}
89+
};
90+
91+
/*! Creates a SuperpixelSEEDS object.
92+
* @param image_width image width
93+
* @param image_height image height
94+
* @param image_channels number of channels the image has
95+
* @param num_superpixels desired number of superpixels. Note that the actual
96+
* number can be smaller due to further restrictions.
97+
* use getNumberOfSuperpixels to get the actual number.
98+
* @param num_levels number of block levels: the more levels, the more
99+
* accurate is the segmentation, but needs more memory
100+
* and CPU time.
101+
* @param histogram_bins number of histogram bins.
102+
* @param prior enable 3x3 shape smoothing term if >0. a larger value
103+
* leads to smoother shapes.
104+
* range: [0, 5]
105+
* @param double_step if true, iterate each block level twice for higher
106+
* accuracy.
107+
*/
108+
CV_EXPORTS_W Ptr<SuperpixelSEEDS> createSuperpixelSEEDS(
109+
int image_width, int image_height, int image_channels,
110+
int num_superpixels, int num_levels, int prior = 2,
111+
int histogram_bins=5, bool double_step = false);
112+
113+
114+
}
115+
}
116+
#endif
117+
#endif

0 commit comments

Comments
 (0)