Main Content

grayconnected

Select contiguous image region with similar gray values using flood-fill technique

Description

example

BW = grayconnected(I,row,column) finds a connected region of similar intensity in the grayscale image I. Specify the row and column indices of the starting point, the seed pixel. The function returns a binary mask, BW, that indicates which pixels are 8-connected to the seed pixel with a similar intensity.

BW = grayconnected(I,row,column,tolerance) specifies the range of intensity values to include in the mask, as in [(seedvalue-tolerance),(seedvalue+tolerance)].

Examples

collapse all

Read and display a grayscale image.

I = imread('cameraman.tif');
imshow(I)

Figure contains an axes object. The axes object contains an object of type image.

Segment the sky in the image by using the flood-fill technique. Select a pixel in the sky to be the seed location. This example uses the pixel with (row, column) coordinate (50, 50). Call the grayconnected function, specifying the image to be segmented and this seed location.

J = grayconnected(I,50,50);

Display the segmented region in color over the original image by using the labeloverlay function. The segmented region includes sky pixels that are 8-connected to the seed pixel. The region does not include pixels of similar intensity that are disconnected, such as the sky visible between the legs of the tripod.

imshow(labeloverlay(I,J))

Figure contains an axes object. The axes object contains an object of type image.

Segment the jacket of the cameraman by using the flood-fill. Select a pixel in the jacket to be the seed location. This example specifies the seed pixel with (row, column) coordinate (110, 65). Call the grayconnected function, specifying the image to be segmented and this seed location.

J2 = grayconnected(I,110,65);

Display the segmented image in color over the original image. The segmented region includes all pixels that are 8-connected to the seed pixel. The tripod and the hair of the cameraman have similar intensity to the jacket, so they are included in the segmented region.

imshow(labeloverlay(I,J2))

Figure contains an axes object. The axes object contains an object of type image.

Create small sample image.

I = uint8([20 22 24 23 25 20 100
           21 19 12 13 12 30 6
           22 11 13 12 24 25 5
           23 13 13 13 24 25 5
           24 27 13 12 12 13 5
           25 26  5 28 29 50 6]);

Specify the row and column indices of the seed location. The value at the seed location is 23.

seedrow = 4;
seedcol = 1;

Specify the tolerance.

tol = 3;

Create mask image, specifying the seed location and tolerance. The mask includes all pixels that are 8-connected to the seed pixel and have a value in the range [20, 26]. The mask excludes pixels with grayscale values within the tolerance range that are not 8-connected, such as the pixel with (row, column) coordinate (3, 6).

BW = grayconnected(I,seedrow,seedcol,tol)
BW = 6x7 logical array

   1   1   1   1   1   1   0
   1   0   0   0   0   0   0
   1   0   0   0   0   0   0
   1   0   0   0   0   0   0
   1   0   0   0   0   0   0
   1   1   0   0   0   0   0

Input Arguments

collapse all

Grayscale image, specified as a numeric matrix.

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

Row index of seed pixel, specified as a positive integer.

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

Column index of seed pixel, specified as a positive integer.

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

Tolerance of intensity values to include in the mask, specified as a numeric scalar. The mask includes all pixels with a value in the range [(seedvalue-tolerance),(seedvalue+tolerance)]. By default, the tolerance is 32 for integer-valued images and 0.1 for floating point images.

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

Output Arguments

collapse all

Binary mask of the connected region, returned as a logical array of the same size as I. All of the foreground pixels indicate image pixels that are 8-connected to the seed pixel with similar intensity.

Data Types: logical

Extended Capabilities

Version History

Introduced in R2015b