Wednesday, September 9, 2009

Activity 14 - Pattern Recognition

In this activity, we are to classify a set of images into its corresponding classes. A class is a type of an object or group of objects (eg. P1 coin, 25 c coin, flower, etc.). To recognize a sample object with unknown class, we need to do pattern recognition. Minimum distance classification is used in this activity to classify the objects. Basically, a training set of objects is fed into the algorithm and patterns are extracted from these training set. To specify the patterns, features are selected. These features are the patterns used to determine whether a sample object belongs to a kind of class using minimum distance classification.

Three features are used: normalized r, g and per^2 / area to identify the class belonging of a certain object. If the distance of an object's feature vector from the mean of a certain class feature space is at minimum, then that object belongs to that class. This is governed by the Euclidean distance:
.
Equating Dj = y, we shall compute the distance using the equation below. The distance to which the class is minimum, that sample object belongs to that class.

Basically, the closeness of a sample feature vector to the mean of a certain class feature vector is determined by obtaining the minimum distance. To apply this, the original image taken is presented to the left. Then, white balancing is applied to the image (right).


The objects are named as piso, ben, leafe, leafs and santan with 5 classes in total. Half of the set of classes are used as training and half are used as samples. Again, the features r and g are obtained by taking a part of the object and the normalized red and green computed (A12). Then, thresholding is used to segment the ROI and determine the perimeter using follow of scilab and the total number of pixels are calculated. The area is calculated using the morphological operations (A9). I tried to use only as third feature the area or perimeter, but there has been no consistency in the results and the feature is shape and size invariant, that's why I choose to use the ratio of per^2 / area since it is a dimensionless unit.

I also tried to use the eccentricity of the region to as feature since it is invariant in the capture angle of images but I've got a problem to non-regular polygons because I don't know where to obtain the major and minor axes. That's why I opt to use other features such as perimeter and area ratio.

Shown below is the plot of the results in rg axes (left). It can be observed that the graph follows that of the rg Normalized chromaticity space as shown to its right. The 3D graph of the distribution of mean of training and samples feature vectors in the [r g per^2/area] coordinate system is presented right below of the two plots. It is interesting to note that the P1 and 25c coins have almost equal ratio of per^2/area since for a circle that ratio is equal to 4pi. But for the sake of non-circular objects, this feature is very distinct. Thus, the two coins present errors in the object classification. As seen also, the cluster of the 25c and P1 sample data are very close to each other. This is because of the dilapidated texture of the coins. The rg plot is presented to exemplify that these features describe the normalized rg values fo each classes correctly.



The algorithm presents 84% accuracy in object classification. The errors obtained are incurred from the P1 and 25c samples classification missing 2 correct classification for both P1 and 25c samples. In the next activity using Linear Discriminant Analysis, the 25 c and P1 data is expected to be classified at higher accuracy.

I give myself 9 points for finishing and understanding the activity but failed to finish early.

Acknowledgments:
I acknowledge Jaya and Orly for bringing up intellectual discussions about the algo.


CAVEAT
Be careful to put '.' during element division as shown in red texts in the code below.

Code:

Scilab (image processing)

function [img] = imopen(img_bw, se);
img = dilate(erode(img_bw, se), se);
endfunction

function [img] = imclose(img_bw, se);
img = erode(dilate(img_bw, se), se);
endfunction

// intialize
folder = string('piso sample');
chdir('E:\Documents and Settings\vergara\My Documents\acads\1st sem 09-10\186\act14\' + folder);
data = [];
fname = string('piso_s');
n = 5;

// rg feature
for i = 1:n
imgc = imread(fname + string(i) + 'c.jpg');
//imgc = imread('piso_t1c.jpg');
R = imgc(:,:,1);
G = imgc(:,:,2);
B = imgc(:,:,3);
I = R + G + B;
r(i) = mean(R ./ I);
g(i) = mean(G ./ I);
end


// area feature
for i = 1:n
img = imread(fname + string(i) + '.jpg');
//img = imread('piso_t3.jpg');
img = img(:,:,3); // blue channel
bw = abs(im2bw(img, 0.65) - 1); // 0.65 // 0.55
se1 = [1 1 1; 1 1 1;1 1 1];
imgb_m = imclose(imopen(bw, se1), se1);
scf(), imshow(imgb_m, [])
ma(i) = sum(imgb_m);
[t, y] = follow(imgb_m);
//scf(), plot2d(t,y)
per(i) = size(t, 1);
// eccentricity
//[row, col] = find(imgb_m == 1);
// vert = max(row) - min(row);
// horz = max(col) - min(col);
// if vert > horz, e = sqrt(vert^2 - horz^2) / vert^2;
// else e = sqrt(horz^2 - vert^2 ) / horz^2;
// end
// ecc(i) = e + 1;
end

data(:,1) = r;
data(:,2) = g;
data(:,3) = per;
data = cat(2, data, ma);
data = cat(2, data, per.^2 ./ ma);

chdir('E:\Documents and Settings\vergara\My Documents\acads\1st sem 09-10\186\act14\' + 'data');
fprintfMat(fname + '.txt', data, '%0.5f');


Matlab (plotting and classification)
%%
load ben_s.txt
load ben_t.txt
load leafe_s.txt
load leafe_t.txt
load leafs_s.txt
load leafs_t.txt
load piso_s.txt
load piso_t.txt
load santan_s.txt
load santan_t.txt

%%
x = 1; y = 2; z = 5;
hold on
plot3(mean(ben_t(:,x)), mean(ben_t(:,y)), mean(ben_t(:,z)), 'bo', 'MarkerFaceColor', 'k')
plot3(mean(leafe_t(:,x)), mean(leafe_t(:,y)), mean(leafe_t(:,z)), 'go', 'MarkerFaceColor', 'k')
plot3(mean(leafs_t(:,x)), mean(leafs_t(:,y)), mean(leafs_t(:,z)), 'yo', 'MarkerFaceColor', 'k')
plot3(mean(santan_t(:,x)), mean(santan_t(:,y)), mean(santan_t(:,z)), 'ro', 'MarkerFaceColor', 'k')
plot3(mean(piso_t(:,x)), mean(piso_t(:,y)), mean(piso_t(:,z)), 'mo', 'MarkerFaceColor', 'k')
xlabel('r'); ylabel('g'); zlabel('per^2 / area');

%%
plot3(ben_s(:,x), ben_s(:,y), ben_s(:,z), 'bs', 'MarkerFaceColor', 'b'), hold on
plot3(leafe_s(:,x), leafe_s(:,y), leafe_s(:,z), 'gs', 'MarkerFaceColor', 'g')
plot3(leafs_s(:,x), leafs_s(:,y), leafs_s(:,z), 'ys', 'MarkerFaceColor', 'y')
plot3(santan_s(:,x), santan_s(:,y), santan_s(:,z), 'rs', 'MarkerFaceColor', 'r')
plot3(piso_s(:,x), piso_s(:,y), piso_s(:,z), 'ms', 'MarkerFaceColor', 'm')
grid on;
% xlabel('r'); ylabel('g'); zlabel('per^2 / area');

%%
m1 = mean(cat(2, ben_t(:,1:2), ben_t(:,5)), 1);
m2 = mean(cat(2, leafe_t(:,1:2), leafe_t(:,5)), 1);
m3 = mean(cat(2, leafs_t(:,1:2), leafs_t(:,5)), 1);
m4 = mean(cat(2, santan_t(:,1:2), santan_t(:,5)), 1);
m5 = mean(cat(2, piso_t(:,1:2), piso_t(:,5)), 1);

%%
x1 = cat(2, ben_s(:,1:2), ben_s(:,5));
x2 = cat(2, leafe_s(:,1:2), leafe_s(:,5));
x3 = cat(2, leafs_s(:,1:2), leafs_s(:,5));
x4 = cat(2, santan_s(:,1:2), santan_s(:,5));
x5 = cat(2, piso_s(:,1:2), piso_s(:,5));

%% minimum distance classification
y = [];
h = [];
for i = 1:5 % classes
for j = 1:5 % samples
for k = 1:5 % mean classes
mm = eval(['m' int2str(k)]);
xx = eval(['x' int2str(i)]);
y = abs(xx(j,:) - mm);
z(k) = abs(sqrt(y*y'));
end
h = cat(1, h, [find(z == min(z)) i]);
end
end

0 comments:

Post a Comment

Followers