Wednesday, September 14, 2011

A15 - Pattern Recognition

Humans have an intrinsic capability to differentiate things from each other. That is to recognize an unknown object and classify the group it belongs by just looking at patterns such as color, shape, size, texture, etc. The amazing thing is, we do this complex task in just a short period of time. Not so long ago, computers arrived in the playing field to at least mimic this impressive feat of humans. Computers' do this by doing an inspection on an object's characteristics repeatedly. But for a computer to be able to do this, humans must teach it first. The question is how?

The first important thing a computer must define is a set of object features or pattern. These features are quantifiable properties such as color and size. These features are then used to create classifiers to conveniently group together objects into a class sharing common properties.

In this blog post, I'll show you how to extract features from objects and finally use them to create a function that will do pattern recognition.

We first collect 4 sets of objects with each set considered as an independent class and contains 10 objects having the same property. For each class, the samples are divided into two groups. Five will be used as the training set and the remaining as test set. The objects I considered here are 1-peso coins, 5-peso coins, playing cards and fresh leaves as shown below

 
Figure 1. A class of 1-peso coins.

 
Figure 2. A class of 5-peso coins.

 
Figure 3. A class of playing cards.

 
Figure 4. A class of fresh leaves.

Three features are extracted from the objects of each class, (1) Area in sq. pixels, (2) Perimeter in pixels, and (3) Mean of the green chromatic information from the normalized chromaticity coordinates rgb. These features will form the feature vector x.

The first two features can be extracted using image processing tools from my previous blog posts. As a preliminary step, we binarize the training and test sets for each class as shown below.

 
Figure 5. Binarized version of the training sets (a) 1-peso coins,
(b) 5-peso coins, (c) cards and (d) leaves.
(Note: images shown do not have same scale as original)

Figure 6. Binarized version of the test sets (a) 1-peso coins,
(b) 5-peso coins, (c) cards and (d) leaves.
(Note: images shown do not have same scale as original)

The area and perimeter can be obtained by using the Scilab functions bwlabel and follow, respectively. 

The green chromatic information of the objects, on the other hand, can be extracted by taking the pixel values of the green channel and dividing by the sum of the pixel values of the red, green and blue channels (g = G/(R+G+B)). Finally, since we only need a single value to represent each object, the mean is taken such that the feature to be used is <g>.

The resulting feature vectors for each training object are

 Table 1. Feature Vector for each training object.
 

To be able to clearly see the differences in the values of the features for each object, we collapse table 1 into a 3D plot.

 
Figure 7. 3D plot of the features for each object.

It can be seen in figure 7 that same objects tend to cluster together and thus creating a distinct separation from others.

Before moving on, note the values obtained by the feature vectors in table 1. The columns pertaining to the area and perimeter values have relatively larger values than that of column 3. If we are to combine these features and use it as a classification tool, large values will definitely dominate and the values for average G will be negligible. Thus, it is necessary to just normalize the area and perimeter values by the maximum area and perimeter values of the entire training sets, respectively. Such that we divide all area values by 10669 and perimeter values by 404. By doing so, the information of the relative sizes of each object will not be lost because a single normalization factor is used. Then, table 1 will be converted into a normalized version shown in table 2.

Table 2. Normalized Feature Vector for each training object.
 

We can then take the mean feature vector for each of the 4 classes using the formula

 
Equation 1. Mean feature vector.

where x is the feature vector of class wj and N is the number of samples of class wj.

The resulting mean feature vectors (using the feature vectors in table 2) are

Table 3. Mean Feature Vector for each class.

We then extract the features of the test objects in figure 6

Table 4. Feature Vector for each test object.

With the same idea as that of table 2, we normalize the values of the area and perimeter columns in table 4 by a normalization factor same as that used in table 2 (10669 for area and 404 for perimeter). The resulting normalized feature vectors are

Table 5. Normalized Feature Vector for each test object.

We then proceed to the classification process. We can determine the class membership of each test object by using its feature vector x and find the class to which it is closest. We can do this by using the Euclidean distance D

 
Equation 2. Euclidean distance 

where
 
Equation 3. Euclidean norm.

The class of the test object with feature vector x is then assigned to the index with the minimum Euclidean distance. 

Table 6. Euclidean distance of each test object's feature vector 
from the mean feature vector of a class. Minimum values are 
shaded in yellow.

We also have an option to use the derived smallest Euclidean distance formula of 

 
Equation 4. Minimum Euclidean distance.

The class of the test object with feature vector x is then assigned however to the index with the maximum d. Since d is already the minimum distance, thus finding its maximum would give the most minimum distance.

Table 7. Minimum distance of each test object's feature vector 
from the mean feature vector of a class. Maximum values are 
shaded in yellow.

By looking at the classification results of both tables 6 and 7, the process yields 100% classification accuracy. That is, all test objects were correctly classified to the class they really belong.


I enjoyed this activity much because of its possible great applications. Overall, I would give myself a 10.0 for being able to extract features of objects, for being able to use these features to create a classification tool and for having 100% correct classification.

References:
[1] 'Pattern Recognition', 2010 Applied Physics 186 manual by Dr. Maricor Soriano



No comments:

Post a Comment