Extraction of rectangular blob from binary image

i have a binary image having many blobs of different shapes. the one blob is full rectangle. now i want to extract the rectangular blob.

 Accepted Answer

How about comparing length around the blob and length around the bounding box of the blob, like:
% sample blogs
I = imread('pillsetc.png');
Iblob = rgb2gray(I) > 200;
% measure perimeter and bounding box for each blob
stats = regionprops(Iblob,{'BoundingBox','perimeter'});
stats = struct2table(stats);
% extract the blob whose perimeter is close to round length of its bounding box
stats.Ratio = 2*sum(stats.BoundingBox(:,3:4),2)./stats.Perimeter;
idx = abs(1 - stats.Ratio) < 0.1;
stats(~idx,:) = [];
% show the result
imshow(Iblob);
hold on
for kk = 1:height(stats)
rectangle('Position', stats.BoundingBox(kk,:),...
'LineWidth', 3,...
'EdgeColor', 'g',...
'LineStyle', ':');
end

2 Comments

Dear Akira Agata, i want to extract the blob which is very very closed to rectangular shape as in the image with red circle. the image is given below.
OK. How about the following coce? In this code, I have added the 2nd metric to identify the rectangular shape.
% Reading Rect.jpg and make it's blob image
I = imread('Rect.jpg');
I = rgb2gray(I);
Iblob = I > 127;
Iblob = imclearborder(Iblob,4);
% measure perimeter and bounding box for each blob
stats = regionprops(Iblob,{'Area','BoundingBox','perimeter'});
stats = struct2table(stats);
% 1st metric: ratio between perimeter and round length of its bounding box
stats.Metric1 = 2*sum(stats.BoundingBox(:,3:4),2)./stats.Perimeter;
idx1 = abs(1 - stats.Metric1) < 0.1;
% 2nd metric: ratio between blob area and it's bounding box's area
stats.Metric2 = stats.Area./(stats.BoundingBox(:,3).*stats.BoundingBox(:,4));
idx2 = stats.Metric2 > 0.8;
idx = idx1 & idx2;
stats(~idx,:) = [];
% show the result
imshow(Iblob);
hold on
for kk = 1:height(stats)
rectangle('Position', stats.BoundingBox(kk,:),...
'LineWidth', 3,...
'EdgeColor', 'g',...
'LineStyle', ':');
end

Sign in to comment.

Asked:

on 21 Sep 2017

Edited:

on 2 Oct 2017

Community Treasure Hunt

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

Start Hunting!