Monday, September 15, 2008

A18 - Pattern Recognition

For this activity, the aim is to automatically classify images of piattos and pillows chips using features extracted from the training images. There were four training images for the piattos and another four images for the pillows. The features used were red-and-green contrast and area.
The area was obtained by simply binarizing the images and summing the pixels. red-and-green contrast is defined by the following.
We let r be the contrast in the red channel: r = (max(r) - min(r))/(max(r) + min(r))
We let g be the contrast in the green channel: g = (max(g) - min(g))/(max(g) + min(g))
The red-and-green contrast is given by the rg = sqrt(r*r + g*g).

I designated piattos as 1 and pillows as 2.


Piattos features:
---RG---- ----Area----
0.3130230 5428.
0.2596636 6817.
0.2721711 5223.
0.3666842 5742.
Pillows features:
---RG---- ----Area----
0.8614653 5689.
0.8959559 4862.
0.9718898 3951.
0.9224472 3537.

Then I input a series of images consisting of four consecutive piattos and four consecutive pillows.
Test features:
---RG---- ----Area----
0.3322144 7569.
0.3121461 8399.
0.4000078 9213.
0.3930316 9213.
1.0195367 3282.
1.043473 3077.
1.0175605 3390.
0.9543794 2730.


The output of the program below, which made use of minimum distance classification, was
1 1 1 1 2 2 2 2
The chips were correctly classified with 100% accuracy.
If I add another class, the vcut class, using the same features, the accuracy will drop to 50%. This is because piattos and vcut nearly have the same color and the same area.


//for piattos
piattos = [];
for i = 1:4
M = imread(strcat('D:\ap186\september17\piatos'+string(i)+'.jpg'));
r = M(:,:,1);
g = M(:,:,2);
contrast_r = (max(r)-min(r))/(max(r)+min(r));
contrast_g = (max(g)-min(g))/(max(g)+min(g));
piattos(1,i) = sqrt(contrast_r*contrast_r + contrast_g*contrast_g);
M_gray = im2gray(M);
M_bw = im2bw(abs(1-M_gray),0.78);
piattos(2,i) = sum(M_bw);
end

//for pillows
pillow = [];
for i = 1:4;
M = imread(strcat('D:\ap186\september17\pillow'+string(i)+'.jpg'));
r = M(:,:,1);
g = M(:,:,2);
contrast_r = (max(r)-min(r))/(max(r)+min(r));
contrast_g = (max(g)-min(g))/(max(g)+min(g));
pillow(1,i) = sqrt(contrast_r*contrast_r + contrast_g*contrast_g);
M_gray = im2gray(M);
M_bw = im2bw(abs(1-M_gray),0.6);
pillow(2,i) = sum(M_bw);
end

m = [];
m(:,1) = sum(piattos,'c')/size(piattos,2);
m(:,2) = sum(pillow,'c')/size(pillow,2);

//for test samples
test = [];
for i = 1:8
M = imread(strcat('D:\ap186\september17\test\'+string(i)+'.jpg'));
r = M(:,:,1);
g = M(:,:,2);
contrast_r = (max(r)-min(r))/(max(r)+min(r));
contrast_g = (max(g)-min(g))/(max(g)+min(g));
test(1,i) = sqrt(contrast_r*contrast_r + contrast_g*contrast_g);
M_gray = im2gray(M);
M_bw = im2bw(abs(1-M_gray),0.63);
test(2,i) = sum(M_bw);
end

//Minimum distance classification
d = [];
for i = 1:8
for j = 1:2
d(j,i) = test(:,i)'*m(:,j) - m(:,j)'*m(:,j);
end
end

d = abs(d);
x =[];
for i = 1:8
x(i) = find(d==min(d(:,i)));
end
for i = 1:length(x);
x(i) = x(i) - 2*(i-1);
end

x

SELF-GRADE: 10/10 because I have 100% accuracy

No comments: