-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherr.m
41 lines (31 loc) · 1.1 KB
/
err.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
function [tell sparse_approx_img index] = err(image, Dictionaries, lambda, completedTsets)
% image: any image; we presume it is in grey scale
% Dictionaries: our trained dictionaries
% lambda: for lars
% completeTset: number of our completed training sets
% tell (char): return the alphabet that dictionary learning think is correct
% sparse_approx_img: the image generated using the trained dictionary
% alphas: lars co-efficients
% different coefficients generated using different dictionaries
alphas = {};
image_vec = convert_image_to_vector(image);
for i = 1:completedTsets,
a = lars(Dictionaries{i}, image_vec, lambda);
a = a(size(a,1),:)';
alphas{i} = a;
end
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
err = [];
image_vec_norm = norm(image_vec);
for i = 1:completedTsets,
D = Dictionaries{i};
a = alphas{i}
% error
im = D*a;
im = 255*mat2gray(im); %conversion
err(i)= norm(im-image_vec)/image_vec_norm
end
index = find(min(err) == err);
tell = alphabet(index);
sparse_approx_img = convert_vector_to_image(Dictionaries{index}*alphas{index}, 20);
end