Shape detection in image

Hello, I have an image with many shapes and I need to write some Matlab code which remove all the shapes except the rectangle.. Does it availabe to do it using only with strel,imclose and bwareaopen? if you think yes i will be very happy to hear your opinion.
Thanks a lot!
The Image:

Answers (1)

Try this code:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Open an image.
% Browse for the image file.
[baseFileName, folder] = uigetfile('*.*', 'Specify an image file');
fullImageFileName = fullfile(folder, baseFileName);
if folder == 0
return;
end
% Read in image into an array.
[rgbImage, storedColorMap] = imread(fullImageFileName);
[rows, columns, numberOfColorBands] = size(rgbImage)
% If it's monochrome (indexed), convert it to color.
% Check to see if it's an 8-bit image needed later for scaling).
if strcmpi(class(rgbImage), 'uint8')
% Flag for 256 gray levels.
eightBit = true;
else
eightBit = false;
end
if numberOfColorBands == 1
if isempty(storedColorMap)
% Just a simple gray level image, not indexed with a stored color map.
% Create a 3D true color image where we copy the monochrome image into all 3 (R, G, & B) color planes.
rgbImage = cat(3, rgbImage, rgbImage, rgbImage);
else
% It's an indexed image.
rgbImage = ind2rgb(rgbImage, storedColorMap);
% ind2rgb() will convert it to double and normalize it to the range 0-1.
% Convert back to uint8 in the range 0-255, if needed.
if eightBit
rgbImage = uint8(255 * rgbImage);
end
end
end
% Display the original image.
subplot(2, 2, 1);
imshow(rgbImage);
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full screen.
set(gcf,'name','Image Analysis Demo','numbertitle','off')
drawnow; % Make it display immediately.
if numberOfColorBands > 1
title('Original Color Image', 'FontSize', fontSize);
grayImage = rgbImage(:,:,1);
else
caption = sprintf('Original Indexed Image\n(converted to true color with its stored colormap)');
title(caption, 'FontSize', fontSize);
grayImage = rgbImage;
end
% Display it.
subplot(2, 2, 2);
imshow(grayImage, []);
title('Grayscale Image', 'FontSize', fontSize);
% Binarize the image.
binaryImage = grayImage < 100;
% Display it.
subplot(2, 2, 3);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
% Remove small objects.
binaryImage = bwareaopen(binaryImage, 300);
% Display it.
subplot(2, 2, 4);
imshow(binaryImage, []);
title('Cleaned Binary Image', 'FontSize', fontSize);
[labeledImage numberOfObjcts] = bwlabel(binaryImage);
blobMeasurements = regionprops(labeledImage,'Perimeter','Area', 'Centroid');
% for square ((a>17) && (a<20))
% for circle ((a>13) && (a<17))
% for triangle ((a>20) && (a<30))
circularities = [blobMeasurements.Perimeter].^2 ./ (4 * pi * [blobMeasurements.Area])
hold on;
% Say what they are
for blobNumber = 1 : numberOfObjcts
if circularities(blobNumber) < 1.19
message = sprintf('The circularity of object #%d is %.3f, so the object is a circle',...
blobNumber, circularities(blobNumber));
theLabel = 'Circle';
elseif circularities(blobNumber) < 1.53
message = sprintf('The circularity of object #%d is %.3f, so the object is a Rectangle',...
blobNumber, circularities(blobNumber));
theLabel = 'Rectangle';
else
message = sprintf('The circularity of object #%d is %.3f, so the object is a triangle',...
blobNumber, circularities(blobNumber));
theLabel = 'Triangle';
end
text(blobMeasurements(blobNumber).Centroid(1), blobMeasurements(blobNumber).Centroid(2),...
theLabel, 'Color', 'r');
uiwait(msgbox(message));
end

6 Comments

Excellent code !!!!!
With small adjustment on the reading image part:
% Open, Read, & Display Image
baseFileName = 'F:\PhD\MATLAB CODING\BlobsDemo\shape.png';
rgbImg = imread(baseFileName);
[rows, columns, numberOfColorBands] = size(rgbImg);
subplot(2, 2, 1);
imshow(rgbImg);
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full screen.
set(gcf,'name','Image Analysis Demo','numbertitle','off')
drawnow; % Make it display immediately.
Success.
Although there is 75% detected correctly, 25% wrong detection occur.
@Image Analyst.. Can you suggest me regarding this matter, on how can the code detect a square shape correctly..
@Syukri Yazed, for a square, you can call regionprops and look at the bounding box. If it's a perfect square the width and height will be the same
labeledImage = bwlabel(binaryImage);
% Measure the bounding boxes.
props = regionprops(binaryImage, 'BoundingBox');
allBB = vertcat(props.BoundingBox);
allWidths = allBB(:, 3)
allHeights = allBB(:, 4)
% Determine which blobs are square or not.
isASquare = allWidths == allHeights
% Extract the squares
squaresOnlyImage = ismember(labeledImage, find(isASquare));
imshow(squaresOnlyImage);
I've got 2 results the first result was not successful, the second result was a success; but with difference images.
Here I attach my code with the images and the results.
Could you please look into this..
Thank you.
@Image AnalystThis code is detetcting all images as triangle.Kindly give the code detetcting all the basic shapes correctly in every image.
Also kindly share the code that counts each shape in the given image

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Tags

Asked:

on 25 Sep 2015

Commented:

on 5 Aug 2021

Community Treasure Hunt

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

Start Hunting!