Sunday, October 12, 2008
Activity 18: Minimum Distance Classification
Moving on to more awesome applications of image processing, we have pattern recognition. Face recognition, retinal scans, object classification - the stuff of SciFi movie legends are actually used by scientists for their value in scientific analysis. One of the most common form of pattern recognition is to recognize different objects from data gathered on a training set.

In this activity we recognized Coke bottle caps, 25 centavo coins, Supplemantary medicine and 5 centavo coins.


The test and training images were white balanced first using the reference white algorithm. After which a code shown below was performed to classify each object and see whether the computer would be able to classify the objects properly. The characteristics I used to make these qualifications are as follows: pixel area, ratio of height and width, average red component (NCC), and average green component (NCC).

For the feature set I calculated the pixel area by summing up the binarized image. The ratio of height and width is computed as the maximum vertical minus the minimum vertical divided by its horizontal counterpart. Since the cropped images I used have the same dimensions, I just got the average red/green over the whole image. The feature vector of a class is represented by the mean of feature vectors of all training images used. The method used to classify the test images is the Euclidean distance. The training feature set which have the shortest distance over the test features is the class of that test image.

Since the objects I gathered are distinct from each other, I obtained correct classification for all 12 test objects. The code I used is threefold. The first is the code I used for the Training Set, the second for the Test and the third for the Classification.

First:
chdir('G:\poy\poy backup\physics\186\paper 18');
parameters = []; //1 = area, 2 = h/w, 3 = r, 4 = g
se = ones(3,3);
for n = 1:3
for i = 1:3
I = imread("images" + string(i) + ".JPG");
//Binarizing
I2 = I(:, :, 1) + I(:, :, 2) + I(:, :, 3);
I2 = I2/3;
I2(I2 > 0.40) = 2;
I2(I2 <= 0.40) = 1;
I2(I2 == 2) = 0;
//Closing operation
I2 = erode(dilate(I2, se), se);
//Area
area = sum(I2);
parameters(n, i, 1) = area;
//height/width
[v, h] = find(I2 == 1);
parameters(n, i, 2) = (max(v) - min(v))/(max(h) - min(h));
//average red and green (NCC)
ave = I(:, :, 1) + I(:, :, 2) + I(:, :, 3);
r = I(:, :, 1)./ave;
g = I(:, :, 2)./ave;
r = r.*I2;
g = g.*I2;
parameters(n, i, 3) = sum(r)/area;
parameters(n, i, 4) = sum(g)/area;
end
end
fprintfMat("param1.txt", parameters(:, :, 1));
fprintfMat("param2.txt", parameters(:, :, 2));
fprintfMat("param3.txt", parameters(:, :, 3));
fprintfMat("param4.txt", parameters(:, :, 4));

Second:
chdir('G:\poy\poy backup\physics\186\paper 18');
parameters = []; //1 = area, 2 = h/w, 3 = r, 4 = g
se = ones(3,3);
for n = 13:24
for i = 13:24
I = imread("images" + string(i) + ".JPG");
//Binarizing
I2 = I(:, :, 1) + I(:, :, 2) + I(:, :, 3);
I2 = I2/3;
I2(I2 > 0.40) = 2;
I2(I2 <= 0.40) = 1;
I2(I2 == 2) = 0;
//Closing operation
I2 = erode(dilate(I2, se), se);
//Area
area = sum(I2);
parameters(n, i, 1) = area;
//height/width
[v, h] = find(I2 == 1);
parameters(n, i, 2) = (max(v) - min(v))/(max(h) - min(h));
//average red and green (NCC)
ave = I(:, :, 1) + I(:, :, 2) + I(:, :, 3);
r = I(:, :, 1)./ave;
g = I(:, :, 2)./ave;
parameters(n, i, 3) = sum(r)/area;
parameters(n, i, 4) = sum(g)/area;
end
end
fprintfMat("param1-2.txt", parameters(:, :, 1));
fprintfMat("param2-2.txt", parameters(:, :, 2));
fprintfMat("param3-2.txt", parameters(:, :, 3));
fprintfMat("param4-2.txt", parameters(:, :, 4));

Third:
//Classifying the test images
parameters_train(:, :, 1) = fscanfMat("param1.txt");//area
parameters_train(:, :, 2) = fscanfMat("param2.txt");//h/w
parameters_train(:, :, 3) = fscanfMat("param3.txt");//mean(r)
parameters_train(:, :, 4) = fscanfMat("param4.txt");//mean(g)
for i = 1:4
for j = 1:4
m(i, j) = mean(parameters_train(i, :, j));
end
end
clear parameters_train;
normfactor = max(m(:, 1));
m(:, 1) = m(:, 1)/normfactor;

test(:, :, 1) = fscanfMat("param1-2.txt");
test(:, :, 2) = fscanfMat("param2-2.txt");
test(:, :, 3) = fscanfMat("param3-2.txt");
test(:, :, 4) = fscanfMat("param4-2.txt");
test(:, :, 1) = test(:, :, 1)/normfactor;

for i = 1:4
for j = 1:4
for k = 1:4
t(k) = test(i, j, k);
end
for l = 1:4
d = t' - m(l, :);
dist(i, j, l) = sqrt(d*d');//distance of image(i, j) on class l
end
end
end

correct = 0;
for i = 1:4
for j = 1:4
shortest = find(dist(:,j,i) == min(dist(:,j,i)));
if shortest == i
correct = correct + 1;
end
end
end

Acknowledgments:

Thanks Ed for the code. I give myself 9 neutrinos for this activity, things are still a bit fuzzy for me.
posted by poy @ 9:08 PM  
0 Comments:
Post a Comment
<< Home
 
 
About Me

Name: poy
Home: Quezon City, NCR, Philippines
About Me:
See my complete profile
Previous Post
Archives
Template by
Blogger templates