MATLAB: How to reduce blob using aspect ratio

aspect ratioblob reduce.

.
I have a binary image with 4 blobs. 3 of them has an aspect ratio of more than 1. and 1 has aspect ratio of 1. Now I want to reduce that blobs which aspect ratio more than 1 in binary image. How could i do this. Can some one please provide a code??
A picture is provided for understanding.

Best Answer

Dominic, here is the full demo:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 25;
%===============================================================================

% Get the name of the image the user wants to use.
baseFileName = '4.png';
% Get the full filename, with path prepended.
folder = []; % Determine where demo folder is (works with all versions).
fullFileName = fullfile(folder, baseFileName);
%===============================================================================
% Read in a demo image.
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the image.

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 up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
% Threshold the image.
binaryImage = grayImage > 128;
% Find connected components (blobs).
labeledImage = bwlabel(binaryImage);
% Make the measurements of the bounding boxes.
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) % or whatever.
% Extract only those blobs with high aspect ratios.
binaryImage = ismember(labeledImage, compactIndexes);
% Display the image.
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');