You are here : matlabImage Processingbwareafilt

bwareafilt() - Image Processing

BW2 = bwareafilt(BW,range) extracts
all connected components (objects) from the binary image BW,
where the area of the objects is in range, producing
another binary image BW2. range 
is a 2-by-1 vector of minimum and maximum sizes (inclusive). bwareafilt removes
objects that do not meet the criterion. The default connectivity is 8.exampleBW2 = bwareafilt(BW,n) keeps
the n largest objects. In the event of a tie
for n-th place, only the first n objects
are included in BW2.BW2 = bwareafilt(BW,n,keep) retains
the n largest objects if keep is 'largest' (the
default) or the n smallest if keep is 'smallest'.


Syntax

BW2 = bwareafilt(BW,range) exampleBW2 = bwareafilt(BW,n) exampleBW2 = bwareafilt(BW,n,keep)


Example

Filter Binary Image by Area of ObjectsOpen This Example
Read image.BW = imread('text.png');
Filter image, retaining only those objects with areas between 40 and 50.BW2 = bwareafilt(BW,[40 50]);
Display the original image and filtered image side by side.imshowpair(BW,BW2,'montage')

Filter Binary Image by Size of ObjectsOpen This Example
Read image.BW = imread('text.png');
Filter image, retaining only the 5 objects with the largest areas.BW2 = bwareafilt(BW,5);
Display the original image and the filtered image side by side.imshowpair(BW,BW2,'montage')

Related ExamplesFilter Images on Region Properties Using Image Region Analyzer
App


Output / Return Value


Limitations


Alternatives / See Also


Reference