-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnnmex.m
83 lines (71 loc) · 2.05 KB
/
nnmex.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
function out_r_nn = nnmex(res, img, nn)
[M,N,~] = size(res);
[X,Y,~] = size(img);
%% Propagation
%forward
for x = 2:M-6
for y = 2:N-6
[minD,I] = min([nn(x,y,3),nn(x-1,y,3),nn(x,y-1,3)]);
if I == 2 && nn(x-1,y,1)+1 <= X-6
nn(x,y,1:2) = nn(x-1,y,1:2);
nn(x,y,1) = nn(x,y,1) + 1;
nn(x,y,3) = minD;
elseif I == 3 && nn(x,y-1,2)+1 <= Y-6
nn(x,y,1:2) = nn(x,y-1,1:2);
nn(x,y,2) = nn(x,y,2) + 1;
nn(x,y,3) = minD;
end
end
end
%backward
for x = M-7:-1:1
for y = N-7:-1:1
%propagation
[minD,I] = min([nn(x,y,3),nn(x+1,y,3),nn(x,y+1,3)]);
if I == 2 && nn(x+1,y,1)-1 >0
nn(x,y,1:2) = nn(x+1,y,1:2);
nn(x,y,1) = nn(x,y,1) - 1;
nn(x,y,3) = minD;
elseif I == 3 && nn(x,y+1,2)-1 > 0
nn(x,y,1:2) = nn(x,y+1,1:2);
nn(x,y,2) = nn(x,y,2) - 1;
nn(x,y,3) = minD;
end
end
end
%% Random Search
a = 0.5;
w = max(X,Y);
for x = 1:M-6
for y = 1:N-6
cand_x = [0];
cand_y = [0];
max_n = ceil( log(1/w) / log(0.5) ) ;
% select out all candidates of offset
for n = 0 : max_n
fl = 1;
while fl
Ux = round(w*(2*rand()-1)*a^n);
Uy = round(w*(2*rand()-1)*a^n);
x2 = nn(x,y,1) + Ux;
y2 = nn(x,y,2) + Uy;
fl = x2<1 || x2>X-6 || y2<1 || y2>Y-6;
end
cand_x = [cand_x ; x2];
cand_y = [cand_y ; y2];
end
% pick the one with min distence
for i =2:max_n + 2
x2 = cand_x(i);
y2 = cand_y(i);
Dist = sum(sum(sum( (res(x:x+6,y:y+6,:)-img(x2:x2+6,y2:y2+6,:)).^2 )));
if Dist < nn(x,y,3)
nn(x,y,3) = Dist;
nn(x,y,1) = x2;
nn(x,y,2) = y2;
end
end
end
end
out_r_nn = nn;
end