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.

 Akzeptierte Antwort

Akira Agata
Akira Agata am 21 Sep. 2017
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 Kommentare

Ayaz Wazir
Ayaz Wazir am 28 Sep. 2017
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.
Akira Agata
Akira Agata am 2 Okt. 2017
Bearbeitet: Akira Agata am 2 Okt. 2017
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

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by