Remove unwanted small and connected regions in the image
14 views (last 30 days)
Show older comments
Hi everybody,
I need to extract small connected regions in the image . Is there a solution to this problem?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1545672/image.png)
0 Comments
Answers (2)
Image Analyst
on 21 Nov 2023
I know you accepted the other answer but I don't think it's right. It assumes you want to "count connected components in a binary image" however what you actually said is that you want to "Remove unwanted small and connected regions".
To remove small regions, bwconncomp actually won't do the job as the answer suggested. It does not remove blobs, it just labels them. To actually remove them you'd want bwareaopen. You could also use bwareafilt if you want to specify a range of sizes to accept or reject, or specify a number of blobs to keep. Below are some examples:
mask = bwareaopen(mask, 10); % Keep only blobs with an area of 10 pixels or more.
mask = bwareafilt(mask, [10, 500]); % Keep only blobs with an area of 10 pixels to 500 pixels.
mask = bwareafilt(mask, 3); % Keep the 3 largest blobs ONLY.
3 Comments
Image Analyst
on 22 Nov 2023
% finding connected components in binary image
output_struc = bwconncomp(image)
do that? It doesn't. It doesn't remove anything, much less the blobs you wanted to remove. But whatever, I guess since you accepted it you somehow figured it out.
To find out how to extract regions based on multiple measurements, see my Image Segmentation Tutorial
For example you might want to remove blobs based on both the aspect ratio and the area to get rid of roundish blobs but leave stick-like blobs.
By the way, the complicated code you gave in your last comment could be done more simply as
largestBlob = bwareafilt(Image, 1); % Extract largest blob only.
imshow(largestBlob);
Yash
on 21 Nov 2023
Hey Mary,
Since you have provided a binary image in the problem, I understand you want a workaround to find and count connected components in a binary image. For this you can try using the 'bwconncomp' function from the Image Processing Toolbox in MATLAB.
% finding connected components in binary image
output_struc = bwconncomp(image)
The function bwconncomp(image) identifies and counts the connected components in the binary image given the input 'image'. The output from this function includes the total number of connected components, which are essentially regions of interest (ROIs), and the pixel indices associated with each component.
To know more about this function, you can refer to its documentation here: https://in.mathworks.com/help/images/ref/bwconncomp.html
Hope this helps!
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!