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)

Use the flood-fill technique to segment the sky in the image. 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))

Segment the jacket of the cameraman in the image. Select a pixel in the sky 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))

Create small sample image.

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

Create mask image, specifying the seed location by row and column and the tolerance. Since the seed location specifies the pixel with the value 23 and the tolerance is 3 , the range of grayscale values is [20,26].

seedrow = 4
seedrow = 4
seedcol = 1
seedcol = 1
tol = 3 
tol = 3
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

Introduced in R2015b