Tuesday, October 6, 2009

ACTIVITY 14 Pattern Recognition

In the succeeding 3 activities, we generated codes that can be used in pattern recognition using different algorithms. For this activity, I discuss the results of the classification when the minimum distance algorithm is used.

The patterns I used are images of oranges, nagaraya, grapes and eggnog as shown in Figures 1 to 4. The following features were determined from these patterns: redness, green-ness and area. Each pattern was represented by a feature vector, which is an ordered set of the feature values. The patterns were classified according to their class, namely, orange, nagaraya, grape and eggnog.

Figure 1. Nagaraya Training and Testing Images.


Figure 2. Orange Training and Testing Images.

Figure 3. Grapes Training and Testing Images.

Figure 4. Eggnog Training and Testing Images.

Determination of the Feature Values

1. & 2. Redness and Green-ness

First, a patch consisting of pure red, pure blue and pure green (primary colors) strips was created (see Figure 5). The patch was used in the image segmentation process. An advantage of creating a patch is there is no more need to select the portion of the pattern for the determination of the RGB values. Since it is our aim to approximate the pattern recognition capability of the humans, it is better that it is not the human who gets to decide what portion of the pattern will be considered. The code for the image segmentation (parametric, assuming Gaussian distribution) process is shown below. Since the patch used contains pure primary colors, then finding the probability that the pixels in the pattern have chromaticities r and g (p_r and p_g) is the same asdetermining how red (redness) and how green (green-ness) the pattern is. The first two rows of Figures 6 to 9 are the images of the p_r and the p_g of the patterns. As you can see, only when there is a comparable difference in the R or G values among the pixels in the image will there be a clear distinction between the pattern and the background. If there is no significant difference, then the image will just appear black at all points like the p_g of most patterns. The third row in these figures is the binarized image of the p_r*p_g of the patterns. The probabilitieswere multiplied and binarized in order to partially, if not totally, segment the pattern from its background. Finally, the redness and green-ness of the patterns were measured by getting the mean of the p_r and p_g, respectively, of the portions in the image where the binarized p_r*p_g is equal to 1 (pattern only without considering the background).

SciLab Code:
patch = imread('F:\Documents\5th year\AP186\activity 14\patch.jpg');

I = patch(:,:,1) + patch(:,:,2) + patch(:,:,3);
Pr = patch(:,:,1).*(I.^(-1));
Pg = patch(:,:,2).*(I.^(-1));
m_r = mean(Pr);
m_g = mean(Pg);
s_r = stdev(Pr);
s_g = stdev(Pg);

fname = 'F:\Documents\5th year\AP186\activity 14\orange\orange1.jpg';
image = imread(fname);

Int = image(:,:,1) +image(:,:,2) + image(:,:,3);
Int(find(Int==0))= 100000;
Ir = image(:,:,1).*(Int.^(-1));
Ig = image(:,:,2).*(Int.^(-1));

p_g = ((1/s_g)*(1/sqrt(2*%pi)))*exp(-((Ig - m_g).^2)/(2*(s_g^2)));
p_g = 1-p_g;

p_r = ((1/s_r)*(1/sqrt(2*%pi)))*exp(-((Ir - m_r).^2)/(2*(s_r^2)));
p_r = 1-p_r;

prod = im2bw(p_r.*p_g,0.035);
pmG = mean(p_g(find(prod==1)));
pmR = mean(p_r(find(prod==1)));
area = sum(prod);

Figure 5. Patch consisting of the three primary colors: red, green and blue.


Figure 6. Training images of the nagaraya based on its (a) red content and (b) green content.
(c) BW images of the nagaraya. Note: (a) - 1st row, (b) - 2nd row, (c) - 3rd row.

Figure 7. Training images of the oranges based on its (a) red content and (b) green content.
(c) BW images of the oranges. Note: (a) - 1st row, (b) - 2nd row, (c) - 3rd row.

Figure 8. Training images of the grapes based on its (a) red content and (b) green content.
(c) BW images of the grapes. Note: (a) - 1st row, (b) - 2nd row, (c) - 3rd row.

Figure 9. Training images of the eggnog based on its (a) red content and (b) green content.
(c) BW images of the eggnog. Note: (a) - 1st row, (b) - 2nd row, (c) - 3rd row.

3. Area
The area of the patterns were determined by simply getting the number (sum) of 1s in the binarized p_r*p_g (see last line of code above). Looking at the binarized images in Figures 6 to 9 (third row), you can see that there are small holes in some parts or excess regions. But since these are only few (much less than the total area), the values I got are still good approximations of the area of the patterns.

After determining the feature vectors of all the patterns, I divided the images into a training set and a test set. In the training set, the average of the feature vectors of all the patterns belonging to one class were determined. Table 1 is the summary of the mean feature vectors of the different classes.It was then used in the classification of the patterns in the test set using the minimum distance algorithm, which uses the equations given below. Also shown below is the simple code implementing the said algorithm. The algorithm basically computes the distance (or difference) between the feature vector of the test pattern and the mean feature vectors in the training set. The class of the mean feature vector that gives the minimum distance is the classification of the test pattern.



SciLab Code:
for k = 1:4,
distance(k) = sqrt((FVs(:,k) - test)'*(FVs(:,k) - test));
end;
classification = find(distance == min(distance));



Table 1. Feature vectors of the objects as determined from the training images.

The output of the code (classification of all the test patterns) is not presented anymore since it was just a matrix of numbers 1, 2, 3 and 4 corresponding to the four classes (oranges, nagaraya, grapes and eggnog). But I was able to classify correctly all the test patterns, and even the training patterns (100%). This means the features I chose are good separators or distinctions of the four classes.

I give myself a grade of 10 for this acitivity.

No comments:

Post a Comment