How can I label objects (circle or square) in an image?

7 views (last 30 days)
Hello,
Im dealing with a problem with labeling any objects shown in an image. For example I have 1 square and 1 circle and want to measure it by size whether it is a circle or square. C_circle=(4⋅π^2⋅r^2)/(2⋅π⋅r)^2 =1 and when less than 1, it is a square.
So first I start with putting the to image to grey and binary.
Then to get the image objects i use:
cc = bwconncomp(BW)
cc
L = labelmatrix(cc);
But its always getting more objects than shown? Like for instance (image of a square and circle):
cc =
struct with fields:
Connectivity: 8
ImageSize: [463 621]
NumObjects: 3
PixelIdxList: {[1545×1 double] [18096×1 double] [123787]}
After that I use [B,L,N] = bwboundaries(BW); , but when I look for the number of objects in N, I always get the double amount of objects? Any ideas why?
Further on to get the stats of the image
stats= regionprops(L,'Centroid', 'Area', 'Perimeter');
Perimeter = cat(1,stats.Perimeter);
Area = cat(1,stats.Area);
Centroid = cat(1, stats.Centroid);
Now my last question how can i locate the object within the image and assign it? So I can give the objects names in a prompt
My full code:
clear
picture = imread('cirvier.jpg');
grijs = rgb2gray (picture);
% [height,width]=size(grijs);
% y=zeros(1,256);
c=double(grijs);
% Picture threshold/binair
BW = imbinarize(grijs);
cc = bwconncomp(BW)
cc
L = labelmatrix(cc);
% RGB_label = label2rgb(L, @copper, 'c', 'shuffle');
% imshow(RGB_label,'InitialMagnification','fit')
BW1 = BW;
BW2 = bwperim(BW,8);
figure
imshow(BW2);
%Get stats
[B,L,N] = bwboundaries(BW);
% objecten = N/2;
% disp (objecten);
stats= regionprops(L,'Centroid', 'Area', 'Perimeter');
Perimeter = cat(1,stats.Perimeter);
Area = cat(1,stats.Area);
Centroid = cat(1, stats.Centroid);
CircleMetric = (Perimeter.^2)./(4*pi*Area);

Accepted Answer

Sid Singh
Sid Singh on 22 Oct 2019
Edited: Sid Singh on 22 Oct 2019
Hi, MATLAB can prompt the user for input and it is done by the following command.
As for drawing them, you can follow this page
As for localizing objects in the image, you are already getting that in the Centroid.
You could be getting more objects in the image because of thresholding noise, try morphological processing before the connected component analysis.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!