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('p6-in1.jpg');
ShowColorImage(S,'Original Image');

S2 = im2bw(S,0.5);
ShowImage(S2,'Binary Image');

2. Labeling the objects in the image. The advance programming language will support this function and label the objects automatically.

// Labeled the component(s), and plot the centroid on the original image
S3 = SearchBlobs(S2);
ShowImage(S3,'Labeled Image');

3. Getting the properties of the objects that require for calculation, which are: the width and height for the object, the area of the object, and the centroid of the object.

ToBeCalculated = CreateFeatureStruct(%t); // Feature struct is generated.
S4 = AnalyzeBlobs(S3,ToBeCalculated);

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

for cnt = 1:length(S4)
score1(cnt) = 1-abs(S4(cnt).BoundingBox(3)-S4(cnt).BoundingBox(4))/max([S4(cnt).BoundingBox(3),S4(cnt).BoundingBox(4)]);

score2(cnt) = 1 - abs(%pi*((mean([S4(cnt).BoundingBox(3),S4(cnt).BoundingBox(4)]))/2)^2-length(S4(cnt).PixelIndexList))/length(S4(cnt).PixelIndexList);

score = mean([score1 score2],'c');

5. Display the result visually

[m,n] = size(S2);

for cnt = 1:length(S4)
t = gce();
t.font_foreground = color('red');

So now, can you tell which one is the round object by just looking at the "roundness index"?