# Remove specific objects from an image

42 views (last 30 days)

Show older comments

I want to remove some objects from a binary image based upon length to width ratio of the rectangular labeled each object, such that when a particular threshold is set the components satisfied then it removes t hose objects and show a new image.

Anybody has any idea?

### Accepted Answer

Adam Danz
on 18 Oct 2019

Edited: Adam Danz
on 18 Oct 2019

Following similar logic to this answer, we'll use regionprops to get the length and width of the bounding box rectangles. Then we can compute the length:width ratio, set a threshold, and determine which boxes are greater than that threshold.

Then you can plot accepted objects with a red rectangle and rejected objects with a yellow rectangle.

See below for a second method.

% Read image as RGB

I = imread('bwimage.png'); %including the pull path is better

% Convert to binary

BW = imbinarize(rgb2gray(I));

% Get rid of white border

BWConv = bwconvhull(~BW);

BW = BW & BWConv;

% get region centers and major axis, and orientation

stats = regionprops('table',BW,'BoundingBox');

% Compute length/width ratio

stats.LenWdRatio = stats.BoundingBox(:,3) ./ stats.BoundingBox(:,4);

% Set threshold and determine which objects

% are greater than the threshold.

thresh = 2.0;

stats.isGreater = stats.LenWdRatio > thresh;

% Show bounding box for objects that met the threshold

figure()

h = imshow(BW);

axis on

hold on

% Plot red rectangles around accepted objects

arrayfun(@(i)rectangle('Position',stats.BoundingBox(i,:),'EdgeColor','r'), find(stats.isGreater));

% Plot yellow rectangles around rejected objects

arrayfun(@(i)rectangle('Position',stats.BoundingBox(i,:),'EdgeColor','y'), find(~stats.isGreater));

Second method: using major:minor axis length

Instead of using the horizontal and vertical extent of the bounding box, you could use the ratio of the MajorAxisLength to the MinorAxisLength. Here's an example why this method might be better. Suppose you have an long object that stretchs along a diagonal with slope=1. The width and height of the bounding box will be equal even though it's a very long and narrow object. If you used the ratio of the Major:Minor axis lengths, the object will be detected as long and narrow even through the bounding box is square.

To use this method, make the following changes.

% get region centers and major axis, and orientation

stats = regionprops('table',BW,'BoundingBox','MajorAxisLength','MinorAxisLength');

% Compute length/width ratio

stats.LenWdRatio = stats.MajorAxisLength ./ stats.MinorAxisLength;

[Update]

I forgot to show how to remove the objects.

When you call regionprop, add the SubarrayIdx property to the list.

stats = regionprops('table',BW,'BoundingBox','MajorAxisLength','MinorAxisLength','SubarrayIdx');

Then you can loop through each rejected object and replace its values in BW with false (black).

% Remove rejected objects (fill with black)

objRemoveIdx = find(~stats.isGreater);

for i = find(~stats.isGreater).'

BW(stats.SubarrayIdx{i,1},stats.SubarrayIdx{i,2}) = false;

end

% Show same figure with objects removed (yellow rectangles show where they were)

figure()

h = imshow(BW);

axis on

hold on

% Plot red rectangles around accepted objects

arrayfun(@(i)rectangle('Position',stats.BoundingBox(i,:),'EdgeColor','r'), find(stats.isGreater));

% Plot yellow rectangles around rejected objects

arrayfun(@(i)rectangle('Position',stats.BoundingBox(i,:),'EdgeColor','y'), find(~stats.isGreater));

##### 8 Comments

Laura
on 9 Mar 2023

Adam Danz
on 9 Mar 2023

### More Answers (0)

### See Also

### Categories

### Products

### Community Treasure Hunt

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

Start Hunting!