How can I label objects (circle or square) in an image?
5 次查看(过去 30 天)
显示 更早的评论
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);
0 个评论
采纳的回答
Sid Singh
2019-10-22
编辑:Sid Singh
2019-10-22
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.
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!