forked from marcusstenbeck/tnm034-qr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtnm034.m
More file actions
200 lines (153 loc) · 6.33 KB
/
tnm034.m
File metadata and controls
200 lines (153 loc) · 6.33 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
function strout = tnm034(imin)
% DESCRIPTION
% A function that removes noise from the supplied image
% PARAMETERS
% IN:
% imin: The input image of the captured QR-code.
% OUT:
% strout: The resulting character string of the coded message.
% The string must be in a pre-defined format given in the course description.
image = imread(imin); % reads the given image
if isrgb(image)
image = rgb2gray(image);
end
double_image = im2double(image); % turning the pixel-values into double-values
normalized_image = double_image / max(double_image(:)); % normalizes between 0 and 1
[height, width] = size(normalized_image);
clean_image = noise(normalized_image); % calls a function to remove all the noise in the
%picture
% Hard thresholding, prepare image for high-pass filtering
clean_image(clean_image<0.5) = 0;
clean_image(clean_image>=0.5) = 1;
im_edges = edge_detection(clean_image);
% Hard thresholding, convert image to binary
im_edges(im_edges<0.05) = 0;
im_edges(im_edges>=0.05) = 1;
% Make sure edges are of single pixel width
im_edges = bwmorph(im_edges, 'skel', Inf);
elements = 1:numel(im_edges);
x_axis = ones(7,numel(elements));
y_axis = ones(7,numel(elements));
edges = find(im_edges == 1);
% Ratio of the QR-codes fiducial marks
ratio = [1 1 3 1 1];
points = zeros(1,(numel(edges)-5));
% Search image for fiducial marks
% fulfilling the ratio
for i = 1:(numel(edges)-5)
vec = edges(i:(i+5));
dv = diff(vec);
result = ( dv'/norm(dv') ) / ( ratio/norm(ratio) );
if result > .95
points(i) = vec(1) + round((vec(6) - vec(1))/2);
end
end
points = points(points>0);
%'find vectors'
% pre-allocate variables to speed up the program
T = ones(1,numel(elements));
P = ones(1,numel(elements));
Q = ones(1,numel(elements));
black_points = points;
for i = 1:numel(black_points)
origin = [(black_points(i) - height * floor((black_points(i)-1)/height)) (floor((black_points(i)-1)/height) + 1)];
x_axis(:,black_points(i)) = find_edge_positions(im_edges, [height, width], origin, 'horizontal');
y_axis(:,black_points(i)) = find_edge_positions(im_edges, [height, width], origin, 'vertical');
end
% use x- and y-axis as r- and s- axis temporarily
r_axis = x_axis;
s_axis = y_axis;
% r_axis = 7 värden ifrån (y,x) i r-led
% s_axis = 7 värden ifrån (y,x) i s-led
%'calculate weighting'
for i = 1:numel(black_points)
T(black_points(i)) = ( central_symmetry(x_axis(:,black_points(i))) + central_symmetry(y_axis(:,black_points(i))) + central_symmetry(r_axis(:,black_points(i))) + central_symmetry(s_axis(:,black_points(i))) )/4;
P(black_points(i)) = ( ratio_characteristic(x_axis(:,black_points(i))) + ratio_characteristic(y_axis(:,black_points(i))) + ratio_characteristic(r_axis(:,black_points(i))) + ratio_characteristic(s_axis(:,black_points(i))) )/4;
Q(black_points(i)) = ( square_characteristic(x_axis(:,black_points(i)),y_axis(:,black_points(i))) + square_characteristic(r_axis(:,black_points(i)), s_axis(:,black_points(i))) )/2;
end
B = ( T + P + Q )/3;
B = vec2mat(B,height);
%'calculate FIP coordinates'
[b,ix] = sort(B(:),'ascend');
[x y] = ind2sub(size(B), ix(1:12));
% REGION GROWING
%'region growing'
number_of_regions = 0;
counter = 1;
im_segmented = zeros(height, width);
while number_of_regions < 3
region_size = 1;
ncoord_pos = 0;
ncoord_elem = 20;
ncoord_list = zeros(ncoord_elem, 2);
sy = y(counter);
sx = x(counter);
while region_size < numel(im_edges)
% get seed point
% grow seed point until it has reached the edges of the im_edges with a
% 4-connective algorithm
neighbors = [-1 0; 1 0; 0 -1; 0 1];
for i = 1:4
xn = sx + neighbors(i, 2);
yn = sy + neighbors(i, 1);
inside_image = (xn >= 1) && (yn >= 1) && (xn <= width) && (yn <= height);
if(inside_image && (im_segmented(yn,xn) == 0) && (im_edges(yn,xn) == 0))
ncoord_pos = ncoord_pos + 1;
ncoord_list(ncoord_pos,:) = [yn xn];
im_segmented(yn,xn) = 1;
end
end
if((ncoord_pos + 10) > ncoord_elem)
ncoord_elem = ncoord_elem + 50;
ncoord_list((ncoord_pos + 1):ncoord_elem,:) = 0;
end
if ncoord_pos <= 0
% Get number of regions in image
number_of_regions = max(max(bwlabel(im_segmented)));
break
end
sx = ncoord_list(1,2);
sy = ncoord_list(1,1);
ncoord_list = ncoord_list(2:end,:);
ncoord_pos = ncoord_pos - 1;
end
counter = counter + 1;
end
im_segmented = bwlabel(im_segmented, 4);
fips = zeros(3,2);
fips(1,:) = cell2mat(struct2cell(regionprops(im_segmented==1, 'Centroid')));
fips(2,:) = cell2mat(struct2cell(regionprops(im_segmented==2, 'Centroid')));
fips(3,:) = cell2mat(struct2cell(regionprops(im_segmented==3, 'Centroid')));
if(fips(1,2) == max(fips(:,2)))
bottom = round(fips(1,end:-1:1));
if(fips(2,1) == max(fips(:,1)))
right = round(fips(2,end:-1:1));
corner = round(fips(3,end:-1:1));
else
right = round(fips(3,end:-1:1));
corner = round(fips(2,end:-1:1));
end
elseif (fips(2,2) == max(fips(:,2)))
bottom = round(fips(2,end:-1:1));
if(fips(1,1) == max(fips(:,1)))
right = round(fips(1,end:-1:1));
corner = round(fips(3,end:-1:1));
else
right = round(fips(3,end:-1:1));
corner = round(fips(1,end:-1:1));
end
else
bottom = round(fips(3,end:-1:1));
if(fips(2,1) == max(fips(:,1)))
right = round(fips(2,end:-1:1));
corner = round(fips(1,end:-1:1));
else
right = round(fips(1,end:-1:1));
corner = round(fips(2,end:-1:1));
end
end
[rotated_image, new_corner, new_right, new_bottom] = rotation(normalized_image, corner, right, bottom);
cropped = crop(rotated_image, new_corner, new_right, new_bottom);
scaled = scale(cropped);
strout = getinfo(scaled);
return