Can anyone explain the following code? (Shape recognition)
2 次查看(过去 30 天)
显示 更早的评论
Hi all, I found an algorithm to segment different regular shapes on the File Exchange at https://www.mathworks.com/matlabcentral/fileexchange/20666-shape-recognition-many-shape-
However I don't really understand what is the criteria for him to segment the shape, especially the following code:
bw = bwareaopen(img1_gray,30);
se = strel('disk',2);
bw = imclose(bw,se);
bw = imfill(bw,'holes');
L = bwlabel(bw);
s = regionprops(L, 'basic');
dt = regionprops(L, 'area');
dim = size(s);
BW_filled = imfill(bw,'holes');
boundaries = bwboundaries(BW_filled);
for k=1:dim(1)
b= boundaries{k};
dim = size(b);
for i=1:dim(1)
distance{k}(1,i) = sqrt ( ( b(i,2) - s(k).Centroid(1) )^2 + ( b(i,1) - s(k).Centroid(2) )^2 );
end
a=max(distance{k});
b=min(distance{k});
c=dt(k).Area;
circles=a-b;
squares = c/(4*b^2);
rectangles=c/(4*b*(a^2-b^2)^0.5);
triangles=(c*3^0.5)/((a+b)^2);
elip =c/(a*b*pi);
diamonds= (c*( a^2 - b^2 )^0.5) / (2*a^2*b);
Do anyone know why the criteria for identifying square is c/(4*b^2), ellipse is c/(a*b*pi) and so on... ?
Thank you so much. I would give the best answer to the one who can explain the above code for me!
0 个评论
回答(1 个)
Gopichandh Danala
2016-9-30
编辑:Gopichandh Danala
2016-9-30
I assume that you fully understood the code still,
I will explain briefly the variables used:
a=max(distance{k}); %%this is the max Radius of your segment or image or whatever
b=min(distance{k}); %%this is the min Radius...
c=dt(k).Area; %%this is the area of your segment...
Now:The code is not identifying square or ellipse..
It is computing the shape factor with repect to different shapes as they used with respect to area (c), max(a) and min(b) radius
Ex: Factor of square = Area (c) / {Area of square block with min side (min radius = b)}
Area of suare block = side of square block = 2*b, so area of square block = (2b).^2 = 4b^2
So,Factor of square = c/(4b^2)..
In the same way :
Factor of ellipse: Area (c) / area of ellipse block Area of ellipse block = πab
So, Factor of ellipse = c/(πab)..
The rest are also done in the same way
If you have any questions let me know
2 个评论
Gopichandh Danala
2016-10-3
编辑:Gopichandh Danala
2016-10-3
U have to accept the above answer if it answered your question properly then I will invest more time on this question for your further questions
And you are missing the point of shape factor..
Ex: if shape factor of rectangle = 1, it means your segment is a perfect rectangle and if it is <1 it is less than a perfect rectangle and vice versa
In your comment < 1.05 && > 0.95 could mean his boundaries to assume the shape being close to a rectangle...
Like: between >0.95 and <1.05 is almost a rectanlge...this is approximation and depends on user
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!