How to remove unwanted small blob?

I want to remove the small blob and only keep the largest connected blob.
How can I do it?

1 Comment

Tan Wen Kun
Tan Wen Kun on 11 Nov 2015
Edited: Tan Wen Kun on 12 Nov 2015
I want the keep the region which in black box only.
Other small blob I want turn to background color

Sign in to comment.

 Accepted Answer

Image Analyst
Image Analyst on 11 Nov 2015
First of all, don't use watershed or whatever to break up the blobs into sub-blobs. Then use imclearborder() to get rid of blobs touching the borders, then use bwareafilt(binaryImage, 1) to get just the largest blob.

9 Comments

so what method should I use to get the blob?
You should use the two functions I mentioned, imclearborder() and bwareafilt().
IM = edge(im2double(rgb2gray(imread('jump.jpg'))),'canny');
figure;imshow(IM,[]);
% load IM
[r c] = size(IM);
data = IM(:);
[center,U,obj_fcn] = fcm(data,4); % Fuzzy C-means classification with 4 classes
% Finding the pixels for each class
maxU = max(U);
index1 = find(U(1,:) == maxU);
index2 = find(U(2,:) == maxU);
index3 = find(U(3,:) == maxU);
index4 = find(U(4,:) == maxU);
% Assigning pixel to each class by giving them a specific value
fcmImage(1:length(data))=0;
fcmImage(index1)= 1;
fcmImage(index2)= 0.66;
fcmImage(index3)= 0.33;
fcmImage(index4)= 0.0;
% Reshapeing the array to a image
imagNew = reshape(fcmImage,r,c);
figure;imshow(imagNew,[]);
gradmag = imagNew;
g = gradmag - min(gradmag(:));
g = g / max(g(:));
th = graythresh(g); %# Otsu's method.
a = imhmax(g,th/2); %# Conservatively remove local maxima.
th = graythresh(a);
b = a > th/4; %# Conservative global threshold.
c = imclose(b,ones(8)); %# Try to close contours.
d = imfill(c,'holes'); %# Not a bad segmentation by itself.
%# Use the rough segmentation to define markers.
%#g2 = imimposemin(g, ~ imdilate( bwperim(a), ones(4) ));
%#L = watershed(g2);
%#Lrgb = label2rgb(L);
%#figure;imshow(g2,[]);
g3 = imclearborder(d);
g4 = bwareafilt(g3,1);
Tan Wen Kun
Tan Wen Kun on 12 Nov 2015
Edited: Tan Wen Kun on 12 Nov 2015
This the example I trying.
What I want to do is to get the blob first then only remove the small blob and keep the largest blob.
If do not use watershed, I cannot get the blob and I dunno two function you give need to use at where.
I don't have the fuzzy toolbox. Please post the (badly-named) "b" image and I'll just start from there.
Tan Wen Kun
Tan Wen Kun on 12 Nov 2015
Edited: Tan Wen Kun on 12 Nov 2015
I just use matlab to run. I think no need fuzzy toolbox
Not that one. I don't want the edge image. Don't you have a binary image?
binary is c and d, b is edge image
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
fullFileName = fullfile(pwd, 'd.jpg');
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Binarize the gray scale image.
binaryImage = grayImage > 128;
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Get rid of blobs touching the border
binaryImage = imclearborder(binaryImage);
subplot(2, 2, 3);
imshow(binaryImage, []);
title('With border cleared', 'FontSize', fontSize, 'Interpreter', 'None');
% Get the largest remaining blob:
binaryImage = bwareafilt(binaryImage, 1);
subplot(2, 2, 4);
imshow(binaryImage, []);
title('Largest Remaining Blob', 'FontSize', fontSize, 'Interpreter', 'None');

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!