# Documentation

### This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English verison of the page.

# superpixels

2-D superpixel oversegmentation of images

## Syntax

``````[L,NumLabels] = superpixels(A,N)``````
``````[L,NumLabels] = superpixels(___,Name,Value,...)``````

## Description

example

``````[L,NumLabels] = superpixels(A,N)``` computes superpixels of the 2-D grayscale or RGB image `A`. `N` specifies the number of superpixels you want to create. The function returns `L`, a label matrix of type `double`, and `NumLabels`, the actual number of superpixels that were computed.The `superpixels` function uses the simple linear iterative clustering (SLIC) algorithm [1]. This algorithm groups pixels into regions with similar values. Using these regions in image processing operations, such as segmentation, can reduce the complexity of these operations.```
``````[L,NumLabels] = superpixels(___,Name,Value,...)``` computes superpixels of image A using with Name-Value pairs used to control aspects of the segmentation.```

## Examples

collapse all

`A = imread('kobi.png');`

Calculate superpixels of the image.

`[L,N] = superpixels(A,500);`

Display the superpixel boundaries overlaid on the original image.

```figure BW = boundarymask(L); imshow(imoverlay(A,BW,'cyan'),'InitialMagnification',67)```

Set the color of each pixel in the output image to the mean RGB color of the superpixel region.

```outputImage = zeros(size(A),'like',A); idx = label2idx(L); numRows = size(A,1); numCols = size(A,2); for labelVal = 1:N redIdx = idx{labelVal}; greenIdx = idx{labelVal}+numRows*numCols; blueIdx = idx{labelVal}+2*numRows*numCols; outputImage(redIdx) = mean(A(redIdx)); outputImage(greenIdx) = mean(A(greenIdx)); outputImage(blueIdx) = mean(A(blueIdx)); end figure imshow(outputImage,'InitialMagnification',67)```

## Input Arguments

collapse all

Input image, specified as a real, nonsparse matrix. For `int16` data, `A` must be a 2-D grayscale image. For all other data types, `A` can be a 2-D grayscale or 2-D RGB image. When the parameter `isInputLab` is `true`, the input image must be `single` or `double`.

Data Types: `single` | `double` | `int16` | `uint8` | `uint16`

Desired number of superpixels, specified as a numeric scalar.

Data Types: `single` | `double` | `int8` | `int16` | `int32` | `int64` | `uint8` | `uint16` | `uint32` | `uint64`

### Name-Value Pair Arguments

Specify optional comma-separated pairs of `Name,Value` arguments. `Name` is the argument name and `Value` is the corresponding value. `Name` must appear inside single quotes (`' '`). You can specify several name and value pair arguments in any order as `Name1,Value1,...,NameN,ValueN`.

Example: ```B = superpixels(A,100,'NumIterations', 20);```

collapse all

Shape of superpixels, specified as a numeric scalar. The compactness parameter of the SLIC algorithm controls the shape of superpixels. A higher value makes superpixels more regularly shaped, that is, a square. A lower value makes superpixels adhere to boundaries better, making them irregularly shaped. The allowed range is `[0 Inf)`. Typical values for compactness are in the range `[1,20]`.

Data Types: `single` | `double` | `int8` | `int16` | `int32` | `int64` | `uint8` | `uint16` | `uint32` | `uint64`

Input image data is in the L*a*b* colorspace, specified as the logical scalar `true` or `false`.

Data Types: `single` | `double` | `int8` | `int16` | `int32` | `int64` | `uint8` | `uint16` | `uint32` | `uint64` | `logical`

Algorithm used to compute superpixels, specified as one of the following values. The `superpixels` function uses two variations of the simple linear iterative clustering (SLIC) algorithm.

Value

Meaning

`'slic0'`

`superpixels` uses the SLIC0 algorithm to refine `'Compactness'` adaptively after the first iteration. This is the default.

`'slic'`

`'Compactness'` is constant during clustering.

Data Types: `char`

Number of iterations used in the clustering phase of the algorithm, specified as a numeric scalar. For most problems, it is not necessary to adjust this parameter.

Data Types: `single` | `double` | `int8` | `int16` | `int32` | `int64` | `uint8` | `uint16` | `uint32` | `uint64`

## Output Arguments

collapse all

Label matrix, returned as a numeric array of type `double`. The values are positive integers, where `1` indicates the first region, `2` the second region, and so on for each superpixel region in the image.

Number of superpixels computed, returned as a numeric scalar of type `double`.

## References

[1] Radhakrishna Achanta, Appu Shaji, Kevin Smith, Aurelien Lucchi, Pascal Fua, and Sabine Susstrunk, SLIC Superpixels Compared to State-of-the-art Superpixel Methods. IEEE Transactions on Pattern Analysis and Machine Intelligence, Volume 34, Issue 11, pp. 2274-2282, May 2012