MATLAB Answers

Matlab code for extract shape features from an image including circle and rectangle

10 views (last 30 days)
Tamara Ahmed
Tamara Ahmed on 27 May 2021
Commented: Amit on 5 Jun 2021
Hello, I have a project that goes beyond image processing in matlab. I need the special code to extract shape properties from images using Hog and regionprops, noting that the image contains a rectangle and a circle. I will attach an image as an example. The images whose properties are to be extracted are the words encoded by Morse code
  1 Comment
Amit on 5 Jun 2021
Follow following steps,
  1. First of all you need to binarize the image and find edges of image using canny edge detection.
  2. Then you need use regionprops to extract various isolated regions in the image.
  3. You need to find centroid of regions.
  4. Then at various angles, you can find distance between centroid of region and point on edge of image.
  5. This makes your shape descriptors.
  6. You can compare these descriptors with descriptors of known share to categorize your query or unkown object.
This should work for you.
I have my IEEE paper published regarding above process, you can send request to me on,, I will send you my IEEE paper where this process is explained in details with images showing intermediate results.

Sign in to comment.

Accepted Answer

darova on 27 May 2021
Just binarize the image
A = imread('5C54390E-80C8-47DD-9293-822E85D06145.jpeg');
A1 = im2bw(A);
L = bwlabel(~A1); % label each region
s = regionprops(L,'area'); % find area of each region
ar = cat(1,s.Area);
[~,n] = min(ar); % find minimum area (circle)
imshow(L==n) % find index with minimum area and show

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 28 May 2021
I know you already have accepted an answer, but I thought you might like to see my approach.
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 = 20;
folder = pwd;
baseFileName = 'morse code.jpeg';
grayImage = imread(baseFileName);
% 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 = min(grayImage, [], 3);
% Display the image.
subplot(1, 2, 1);
imshow(grayImage, []);
axis('on', 'image');
title('Reference Image', 'FontSize', fontSize, 'Interpreter', 'None');
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
% Binarize the image.
% Get a mask of the dark parts in the image.
% imhist(image1);
% grid on;
mask = ~imbinarize(grayImage);
% Get rid of noise by eliminating blobs smaller than 4000 pixels.
mask = bwareaopen(mask, 4000);
subplot(1, 2, 2);
imshow(mask, []);
axis('on', 'image');
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Measure areas
props = regionprops(mask, 'Area', 'Centroid');
allAreas = [props.Area]
% Plot what it is over the original image.
subplot(1, 2, 1);
for k = 1 : length(props)
xCenter = props(k).Centroid(1);
yCenter = props(k).Centroid(2);
if allAreas(k) > 9600
blobType = 'dash';
blobType = 'dot';
text(xCenter, yCenter, blobType, 'Color', 'r', ...
'FontWeight', 'bold', 'FontSize', 18, ...
'HorizontalAlignment', 'center',...
'VerticalAlignment', 'middle');
% Get an array where 1 is dash and 0 is dot
dotOrDash = allAreas > 9600
See the identification labeled in red over the image.
You get:
dotOrDash =
1×4 logical array
1 1 0 1
Is this what you are looking for?
Image Analyst
Image Analyst on 4 Jun 2021
Not really. The accepted answer says this:
s = regionprops(L,'area'); % find area of each region
ar = cat(1,s.Area);
while mine says this:
% Measure areas
props = regionprops(mask, 'Area', 'Centroid');
allAreas = [props.Area]
Those are essentially the same - they both give the area of all the blobs in the image. Not sure what your definition of "shape" is but both of our solutions characterize based on area alone. The difference is the accepted answer only extracts and gives you the area of the smallest blob, while mine characterizes every single blob. Neither answer distinguishes between blobs on the basis of shape, other than different shapes have different areas. If the area of the dot and dash were identical, then we would not be able to distinguish the type by area - we'd have to use a different metric, like EquivDiameter, MajorAxisLength, or circularity or something else.
I don't want to sound like I'm arguing with you -- I just want to make sure you understand what the programs do (his finds the smallest shape only, and mine characterize all shapes) because it seems like you think darova's answer gives you something more than it actually does. His finds one shape (the smallest) and does not determine whether it's a circular or rectangular shape, while mine finds all four shapes and does determine the shape for all four shapes in the image. With my code, you could have a look up table of letters and patterns so you could look up the pattern (that I called dotOrDash) to determine the letter corresponding to that Morse Code pattern.

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!