Filter löschen
Filter löschen

Can anyone explain the following code? (Shape recognition)

2 Ansichten (letzte 30 Tage)
TY
TY am 18 Sep. 2016
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!

Antworten (1)

Gopichandh Danala
Gopichandh Danala am 30 Sep. 2016
Bearbeitet: Gopichandh Danala am 30 Sep. 2016
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 Kommentare
TY
TY am 1 Okt. 2016
Bearbeitet: TY am 1 Okt. 2016
Further, he sets the condition for identifying each shape as follow: if ((rectangle <1.05) & (rectangle >0.95))
Why the shape factor is set between 0.95 and 1.05?
And can you also explain the factor of circle, rectangle, triangle and diamond?
Thanks
Gopichandh Danala
Gopichandh Danala am 3 Okt. 2016
Bearbeitet: Gopichandh Danala am 3 Okt. 2016
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

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by