clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 25;
baseFileName = '4.png';
folder = [];
fullFileName = fullfile(folder, baseFileName);
grayImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
grayImage = rgb2gray(grayImage);
end
subplot(2, 1, 1);
imshow(grayImage, []);
axis on;
axis image;
caption = sprintf('Original Gray Scale Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
binaryImage = grayImage > 128;
labeledImage = bwlabel(binaryImage);
props = regionprops(labeledImage, 'BoundingBox');
bb = [props.BoundingBox];
allWidths = bb(3:4:end)
allHeights = bb(4:4:end)
aspectRatio = [allWidths./allHeights ; allHeights ./ allWidths]
aspectRatios = max(aspectRatio, [], 1)
compactIndexes = find(aspectRatios > 1.5)
binaryImage = ismember(labeledImage, compactIndexes);
subplot(2, 1, 2);
imshow(binaryImage, []);
axis on;
axis image;
caption = sprintf('Final binary image with only high aspect ratio blobs');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
Best Answer