MATLAB Answers


how to make Geometric shapes detection?

Asked by mete polat on 27 Dec 2015
Latest activity Commented on by Image Analyst
on 20 May 2019 at 20:42
i have popup menu to detect shapes. for example, i need when i click circle, just circles will show in rgb image on axes2. i am new to this, so couldn't arrange anything. Can anyone help me? i upload my codes. i think, you can understand exactly what i mean, when you check color menu on my codes. thank you.

  1 Comment

You forgot to attach the image.

Sign in to comment.

2 Answers

Answer by Image Analyst
on 29 Dec 2015
 Accepted Answer

mete, because you did not attach your image, to answer your question I had to write a program to draw random shapes. Please see attached demo. It creates an image with random shapes and then computes the circularity of the shapes and assigns the shape to the shape with the theoretical circularity that is closest to the shape's actual circularity. Someday I hope to add another method where I find and count peaks in the centroid to perimeter distance. For now, it will create an image like this:


the images which will use will be uploaded from any pc. the program shouldn't work on just one image. but if you need,
you can use this one.
First turn your image into a binary image so that all you have to worry about are the shapes. Then use my shapes code on that binary image.
% Read in a color shapes demo image.
folder = pwd;
baseFileName = 'test.bmp';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
title('Original Color Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Get the color of the background from the upper left pixel
backgroundRGB = impixel(rgbImage, 1,1)
% Get a binary image so that we just need to worry about shapes, not colors.
binaryImage = ~(redChannel == backgroundRGB(1) & greenChannel == backgroundRGB(2) & blueChannel == backgroundRGB(3));
% Display the binary image.
subplot(2, 2, 2);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
Thank you it is working :D

Sign in to comment.

Answer by Anindya Banerjee on 17 May 2019 at 14:46

@image analyst your code is not working! in the line pwd its showing error..can you please fix it?


Not sure what your choice is, and how you want to "detect a image from our choice". How many images do you have and what would make you detect one image over another?
My demo works with the synthesized images it creates. If your images or "choices" are different than that then you may need to adapt the code to work with your situation.
I meant to say that suppose I want to use this code to detect shapes for any arbitary image from local directory. In that case,what part of the code should be changed?
See where it says:
% Now create a demo image.
[binaryImage, numSidesCircularity] = CreateDemoImage();
Replace that code with whatever code you want to create a binary image, for example to read in your image with imread() and call imbinarize() or however you want to do it.

Sign in to comment.