Showing posts with label object detection. Show all posts
Showing posts with label object detection. Show all posts

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);

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

5. Display the result visually

ShowImage(S2,'0');
[m,n] = size(S2);

for cnt = 1:length(S4)
xnumb(S4(cnt).Centroid(1),m-S4(cnt).Centroid(2),score(cnt));
t = gce();
t.font_foreground = color('red');
end



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

Playing with “Edge Detection”

A lot of articles explain what is edge detection, how to apply it, and the usage of it. Different edge detection techniques such as sobel, prewitt, and canny are easily found from any image processing software.

However, when we really want to perform edge detection for certain purpose, the problems arise. Look at the following example:



1. Original image
S = imread('p5-in1.jpg');
ShowColorImage(S,'0');



2. Edge detection using different methods
S2 = RGB2Gray(S);
S3 = edge(S2,'sobel');
ShowImage(S3,'0');





S4 = edge(S2,'prewitt');
ShowImage(S4,'0');





S5 = edge(S2,'canny');
ShowImage(S5,'0');




It seems like great but the usage of these output are useless until we really know what we want to do. For exmaple, we want to find the white rabbit, we need to perform some pre-processing before the edge detection so that we can fully utilize the function to suite to our needs.

3. Edge detection for the white rabbit
Since we know the RGB value for white color is [255,255,255], we can pre-process the image to extract the white color region from the image.

Sw = S(:,:,1)>200 & S(:,:,2)>200 & S(:,:,3)>200;
Sw2 = edge(double(Sw),'prewitt');
ShowImage(Sw2,'0');




Well, it seems like make more sense. Let's have a look on how to perform edge detection on the black rabbit:

4. Edge detection for the black rabbit

Sb = S(:,:,1)<40>
Sb2 = edge(double(Sb),'prewitt');
ShowImage(Sb2,'0');



The post processing can be used to remove unwanted noise.





Detecting Object in an Image

1. How to detect an object in an image?
Determining the features from the object that you want to detect is the key. The feature in this case is something that differentiates the object from others, such as color, shape, size, etc…

2. What are the techniques for object detection?

The image processing techniques such as morphology or color processing usually did this job. A simple example in Scilab of detecting ‘white rabbit’ is shown as follow, in this case, ‘color’ is the feature used to distinguish the white rabbit from other:



// Original Image
S = imread('p3-in1.jpg');
ShowColorImage(S,'0');



// Gray scale image
S2 = rgb2gray(S);
ShowImage(S2,'0');



// Find the white color
S3 = S2>180;
ShowImage(S3,'0');



// Morphology technique, image erosion to erase the unwanted components
se = CreateStructureElement('vertical_line', 10);
S4 = ErodeImage(S3, se);
se = CreateStructureElement('horizontal_line', 10);
S4 = ErodeImage(S4, se);
ShowImage(S4,'0');


// Labeled the component(s), and plot the centroid on the original image
S5 = S4.*1;
IsCalculated = CreateFeatureStruct(%f); // Feature struct is generated.
IsCalculated.Centroid = %t; // The bounding box shall be calculated for each blob.
S6 = AnalyzeBlobs(S5, IsCalculated);
ShowColorImage(S,'0');
plot(S6(1).Centroid(1),S6(1).Centroid(2),'r*');