MATLAB: Extraction of rectangular blob from binary image

extractionimage processingImage Processing Toolboximage segmentation

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.

Best 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