Detecting Objects' Shape(I) : Round Object

With the powerful technical computing language, the job of detection an object's shape no longer require lengthy codes. What important is the fundamental of mathematic that define the properties of the shape of objects. Let's look into the detection of round object in this section.

1. Original image and the negative image (value 0 represent background while 1 represents object).

S = imread('pic9.jpg');
S = im2bw(S);
S2 = ~S;

2. Labeling the objects in the image. The advance programming language will support this function and label the objects automatically.
S3 = bwlabel(S2);

3. Getting the properties of the objects that require for calculation, which are: the longest and shortest diameter for the object, the area and the perimeter of the object, and the centroid of the object.
S4 = regionprops(S3,'MinorAxisLength','MajorAxisLength','Area','Perimeter','centroid');

4. Now, evaluate the "roundness" of the objects. this is evaluated by 3 criteria:
a. Comparing the longest and shortest diameter for the object,
b. Comparing the Area with the formula pi*r^2.
c. Comparing the Perimeter with the formula 2*pi*r
The "scores" are normalized so that 1 represent "best fit"

for cnt = 1:length(S4)
score1(cnt) = abs(1-(S4(cnt).MajorAxisLength-S4(cnt).MinorAxisLength)...
score2(cnt) = abs(1 - abs(pi*((mean([S4(cnt).MajorAxisLength,...
score3(cnt) = abs(1 - abs(pi*(max([S4(cnt).MajorAxisLength,...
score = mean([score1;score2;score3]);

5. Display the result visually

for cnt = 1:length(S4)