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.

 採用された回答

Akira Agata
Akira Agata 2017 年 9 月 21 日

1 投票

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 件のコメント

Ayaz Wazir
Ayaz Wazir 2017 年 9 月 28 日
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 2017 年 10 月 2 日
編集済み: Akira Agata 2017 年 10 月 2 日
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

サインインしてコメントする。

その他の回答 (1 件)

質問済み:

2017 年 9 月 21 日

編集済み:

2017 年 10 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by