How to calculate position and Dimensions of patterns(Square, Circle, Rectangle...) of the contours in an image?

3 views (last 30 days)
I have a bunch of images with different patterns and i am interested in finding the Dimensions and Position of the patterns in an image. I started with cropping the image and used a Guassian filter.I found out the contour for calculations using Canny edge detector.Now if anyone could help me in finding the dimensions and positions of the patterns in this image. And also i was wondering if i have to use machine learning technique if i have to develop an algorithm for several image.ma contourresult_layer085.png(Original image)

Accepted Answer

Vinai Datta Thatiparthi
Vinai Datta Thatiparthi on 19 Jul 2019
Hey Imran!
From the description, it appears that you wanted to isolate the figures (circles and squares) from the image and find out their positions and dimensions.
The first part of the question is to convert the image into a binary format to allow the command ‘regionprops’ to function properly.
This can be evaluated by –
im = imread('figCrop.png'); % Read your image
im = rgb2gray(im); % Convert to grayscale
im1 = imcrop(im,[223 118, 1333, 1273]); % Crop the image appropriately
im2 = imbinarize(im1); % Binarize the image
im3 = imfill(im2, 'holes'); % Unnecessary, but you can include this as well
J = medfilt2(im3,[15 15]); % Apply median filtering, since salt&pepper noise
Now, to evaluate the centroids and dimensions of the image using the command ‘regionprops’,
s = regionprops(J,'centroid');
centroids = cat(1,s.Centroid);
imshow(J)
hold on
plot(centroids(:,1),centroids(:,2),'b*') % Plot the centroids
hold off
stats = regionprops('table',J,'Centroid',...
'MajorAxisLength','MinorAxisLength');
Case ID 03674408.jpg
The structure ‘s’ now holds the information of all the centroids of the image. Note that there are a few additional centroids that have been identified incorrectly because of the noise in the image that couldn’t be cleared by median filtering i.e. ‘medfilt2’.
From the table ‘stats’, you now have the information of the positions of the objects and their corresponding dimensions.
Additionally, to calculate and plot the radii of the circles in the image, use the following code -
diameters = mean([stats.MajorAxisLength stats.MinorAxisLength],2);
radii = diameters/2; % Plot the radii of the circles
hold on
viscircles(stats.Centroid,radii);
hold off

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!