Remove unwanted small and connected regions in the image

Hi everybody,
I need to extract small connected regions in the image . Is there a solution to this problem?

Answers (2)

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

CC = bwconncomp(Image)
numPixels = cellfun(@numel,CC.PixelIdxList);
[biggest,idx] = max(numPixels);
Image(CC.PixelIdxList{idx}) = 0;
figure,imshow(Image)
Hi Image Analyst and thank you for your calarification of the subject and your reply.
in fact I used the lines of code mentioned above and in thr obatined result some vessels are removed!! So I want something more powerful and efficient
And how does @Yash's solution of
% 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);
Thanks, I appreciate your suggestions and will give them a try.

Sign in to comment.

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!

Asked:

on 21 Nov 2023

Commented:

on 22 Nov 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!