MATLAB: Image processing, change values of binary image from 0 to 1

image processingImage Processing Toolboximage segmentationsize filtering

Hello, i have turned an image from grey scale to binary (BW). I have reached a point where i have measured the area of all the connected components of my image and based on that i want to remove some components .
Do you know how can i achieve that?
thanks in advance!!!

Best Answer

Use bwareafilt():
minAcceptableRegionArea = 1; % pixel. Whatever you want.
maxAcceptableRegionArea = 1000; % pixels. Whatever you want.
BW2 = bwareafilt(BW2, [minAcceptableRegionArea, maxAcceptableRegionArea]);