This is machine translation

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

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

grayslice

Convert grayscale image to indexed image using multilevel thresholding

Syntax

X = grayslice(I,N)
X = grayslice(I,thresholds)

Description

example

X = grayslice(I,N) converts a grayscale image to an indexed image by using multilevel thresholding approach. The function automatically calculates the threshold values based on N. To know about threshold calculation, see Algorithms.

example

X = grayslice(I,thresholds) returns an indexed image by multilevel thresholding of input image using a set of thresholds.

Examples

collapse all

Read grayscale image into the workspace.

I = imread('snowflakes.png');

Threshold the intensity image, returning an indexed image.

X = grayslice(I,16);

Display the original image and the indexed image, using one of the standard colormaps.

imshow(I)

figure
imshow(X,jet(16))

Read a grayscale image into the workspace. Display the image.

I = imread('coins.png');
imshow(I)

Specify the threshold values for multilevel thresholding.

thresholds = [45 65 84 108 134 157 174 189 206 228];

Convert the input grayscale image to an indexed image.

X = grayslice(I,thresholds);

Display the indexed image. Set the colormap of the indexed image to jet. The length of colormap, m, is the maximum intensity value in the indexed image.

m = double(max(X(:)));

figure
imshow(X,colormap(jet(m)))

Input Arguments

collapse all

Input grayscale image, specified as a m-by-n matrix. The input grayscale image must be real and nonsparse.

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

Number of threshold values, specified as a positive scalar. The value represents the total number of thresholds to be used for multilevel thresholding.

Set of thresholds, specified as a vector. The number of threshold values to be used for multilevel thresholding is equal to length(thresholds). You can choose the threshold values to lie within the range of the image data type. If the input grayscale image is of class:

  • uint8, the threshold values can lie within the range [0 255].

  • int16 or uint16, threshold values can lie within the range [0 65535].

  • single or double, the threshold values can lie within the range [0 1].

Output Arguments

collapse all

Output indexed image, returned as a m-by-n matrix of the same size as the input grayscale image. The class of the output indexed image depends on number of threshold values used for multilevel thresholding.

  • If the number of threshold values is less than 256, then X is of class uint8. In this case, the range of intensity values in X is either [0 N] or [0 length(thresholds)].

  • If the number of threshold values is greater than or equal to 256, then X is of class double. In this case, the range of intensity values in X is either [1 N+1] or [1 length(thresholds)+1].

Data Types: uint8 | double

Algorithms

The function performs multilevel thresholding of the input grayscale image and returns an indexed image as the output. If you specify the number of thresholds N at the input, the threshold values for multilevel thresholding are calculated as

maxintensity×.

maxintensity is the maximum permissible intensity value for a given class of the input grayscale image. If the input grayscale image is of class:

  • uint8, the value of maxintensity is 255.

  • int16 or uint16, the value of maxintensity is 65535.

  • single or double, the value of maxintensity is 1.

You can view the thresholded image using imshow(X,map) with a colormap of appropriate length.

See Also

Introduced before R2006a