grayconnected
Select contiguous image region with similar gray values using flood-fill technique
Description
finds a connected region of similar intensity in the grayscale image
BW
= grayconnected(I
,row
,column
)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.
Examples
Segment Image Using Flood-Fill Technique
Read and display a grayscale image.
I = imread('cameraman.tif');
imshow(I)
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))
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))
Create Binary Mask from Connected Pixels
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
I
— Grayscale image
numeric matrix
Grayscale image, specified as a numeric matrix.
Data Types: single
| double
| int8
| int16
| int32
| uint8
| uint16
| uint32
row
— Row index of seed pixel
positive integer
Row index of seed pixel, specified as a positive integer.
Data Types: single
| double
| int8
| int16
| int32
| uint8
| uint16
| uint32
column
— Column index of seed pixel
positive integer
Column index of seed pixel, specified as a positive integer.
Data Types: single
| double
| int8
| int16
| int32
| uint8
| uint16
| uint32
tolerance
— Tolerance of intensity values
numeric scalar
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
BW
— Binary mask
logical matrix
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
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
grayconnected
supports the generation of C code (requires MATLAB® Coder™). Note that if you choose the genericMATLAB Host Computer
target platform,grayconnected
generates code that uses a precompiled, platform-specific shared library. Use of a shared library preserves performance optimizations but limits the target platforms for which code can be generated. For more information, see Types of Code Generation Support in Image Processing Toolbox.
Version History
Introduced in R2015b
See Also
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other bat365 country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)