-
Notifications
You must be signed in to change notification settings - Fork 1
/
lab3.m
121 lines (103 loc) · 3.05 KB
/
lab3.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
clf('reset')
hand = imread('hand.ppm', 'ppm');
book = imread('book.ppm', 'ppm');
imagesc(hand);
figure;
imagesc(book);
data1 = normalize_and_label(hand, 0);
data2 = normalize_and_label(book, 1);
test_data = [data1; data2];
figure;
hold on;
plot(data2(:,1), data2(:,2), '.');
plot(data1(:,1), data1(:,2), '.r');
legend('Hand holding book', 'Hand');
xlabel('green');
ylabel('red');
% data1 = data1(1:10,:)
% data2 = data2(1:10,:)
% test_data = [data1; data2];
%%%% ASSIGNMENT ONE %%%%
[mu sigma] = bayes(test_data)
theta = [0:0.01:2*pi];
x1 = 2*sigma(1,1)*cos(theta) + mu(1,1);
y1 = 2*sigma(1,2)*sin(theta) + mu(1,2);
x2 = 2*sigma(2,1)*cos(theta) + mu(2,1);
y2 = 2*sigma(2,2)*sin(theta) + mu(2,2);
plot(x1, y1, 'r');
plot(x2, y2);
p = prior(test_data)
feature_vector = test_data(:,1:2);
g = discriminant(test_data(:,1:2), mu, sigma, p)
%%%% ASSIGNMENT TWO %%%%
[M N] = size(test_data);
[dummy class] = max(g, [], 2);
class = class - 1;
delta = (class == test_data(:,end));
error_test = 1.0-sum(class == test_data(:,end))/M
ax = [0.2 0.5 0.2 0.45];
axis(ax);
x = ax(1):0.01:ax(2);
y = ax(3):0.01:ax(4);
[z1 z2] = meshgrid(x, y);
z1 = reshape(z1, size(z1,1)*size(z1,2), 1);
z2 = reshape(z2, size(z2,1)*size(z2,2), 1);
g = discriminant([z1 z2], mu, sigma, p);
gg = g(:,1) - g(:,2);
gg = reshape(gg, length(y), length(x));
[c,h] = contour(x, y, gg, [0.0 0.0]);
set(h, 'LineWidth', 3);
book_rg = zeros(size(book,1), size(book,2), 2);
for y=1:size(book,1)
for x=1:size(book,2)
s = sum(book(y,x,:));
if (s > 0)
book_rg(y,x,:) = [double(book(y,x,1))/s double(book(y,x,2))/s];
end
end
end
tmp = reshape(book_rg, size(book_rg,1)*size(book_rg,2), 2);
g = discriminant(tmp, mu, sigma, p);
gg = g(:,1) - g(:,2);
gg = reshape(gg, size(book_rg,1), size(book_rg,2));
mask = gg < 0;
mask3D(:,:,1) = mask;
mask3D(:,:,2) = mask;
mask3D(:,:,3) = mask;
result_im = uint8(double(book) .* mask3D);
figure;
imagesc(result_im);
%%%% ASSIGNMENT THREE AND FOUR %%%%
% Should produce the same as bayes(test_data)
w = ones(M,1) ./ M;
[mu sigma] = bayes_weight(test_data, w)
T = 6;
[mu sigma p alpha classes] = adaboost(test_data, T)
class = adaboost_discriminant(test_data(:,1:N-1), mu, sigma, p, alpha, classes, T);
boost_error_test = 1.0-sum(class == test_data(:,end))/M
figure;
hold on;
plot(data2(:,1), data2(:,2), '.');
plot(data1(:,1), data1(:,2), '.r');
legend('Hand holding book', 'Hand');
ax = [0.2 0.5 0.2 0.45];
axis(ax);
x = ax(1):0.01:ax(2);
y = ax(3):0.01:ax(4);
[z1 z2] = meshgrid(x, y);
z1 = reshape(z1, size(z1,1)*size(z1,2), 1);
z2 = reshape(z2, size(z2,1)*size(z2,2), 1);
g = adaboost_discriminant([z1 z2], mu, sigma, p, alpha, classes, T);
gg = reshape(g, length(y), length(x));
[c,h] = contour(x, y, gg, [0.5 0.5]);
set(h, 'LineWidth', 3);
tmp = reshape(book_rg, size(book_rg,1)*size(book_rg,2), 2);
g = adaboost_discriminant(tmp, mu, sigma, p, alpha, classes, T);
gg = reshape(g, size(book_rg,1), size(book_rg,2));
mask = gg > 0.5;
mask3D(:,:,1) = mask;
mask3D(:,:,2) = mask;
mask3D(:,:,3) = mask;
result_im = uint8(double(book) .* mask3D);
figure;
imagesc(result_im);