-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_scratch.m
143 lines (111 loc) · 4.07 KB
/
find_scratch.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
function out = find_scratch(lines, img, sharpImg)
sharpImg = double(sharpImg);
% may use cross corelation
distance_constraint = 5;
stack_size = 1000;
cross_correlation_nbr = 10;
On = 1;
Visited = 0.5;
num_lines = length(lines);
[M, N] = size(img);
out = zeros(size(img));
stack = zeros(stack_size, 2);
for line_id = 1:num_lines % each line
p1 = lines(line_id).point1;
p2 = lines(line_id).point2;
dir = (p2 - p1) / norm(p2 - p1);
normal = [dir(2), -dir(1)];
len = [0:1:norm(p2 - p1), norm(p2 - p1)]; % small bug (twice norm(p2 - p1))
potential_scratch_pixels = - ones(ceil(4 * sqrt(M^2 + N^2)), 2); % initial memory size is 4 * diag of image
k = 1;
for l = len
center = round(p1 + l * dir);
% boundary detection
if sum(center <= 0) > 0 || sum(center(2) > M) > 0 || sum(center(1) > N) > 0
continue;
end
if sharpImg(center(2), center(1)) == On % is scratch point
% ---
%recursive part, find connected component
sharpImg(center(2), center(1)) = Visited;
stack(1, :) = center;
s_ptr = 1;
while true
% get the scratch point from the stack, and collect all the connected component.
point = stack(s_ptr, :);
s_ptr = s_ptr - 1;
potential_scratch_pixels(k, :) = point;
k = k + 1;
%{
% 4-connected neighborhoods
neighbors = [point(1), point(2) + 1;
point(1), point(2) - 1;
point(1) + 1, point(2);
point(1) - 1, point(2)];
num_nbr = 4;
%}
% 8-connected neighborhoods
neighbors = [point(1), point(2) + 1;
point(1), point(2) - 1;
point(1) + 1, point(2);
point(1) - 1, point(2);
point(1) + 1, point(2) + 1;
point(1) - 1, point(2) - 1;
point(1) + 1, point(2) - 1;
point(1) - 1, point(2) + 1];
num_nbr = 8;
for pt = 1:num_nbr
if sum(neighbors(pt, :) <= 0) > 0 || sum(neighbors(pt, 2) > M) > 0 || sum(neighbors(pt, 1) > N) > 0
continue;
end
u = neighbors(pt, :) - p1;
if sqrt(norm(u)^2 - (u * dir')^2) > distance_constraint
continue;
end
if sharpImg(neighbors(pt, 2), neighbors(pt, 1)) == On
% check stack size
if s_ptr == stack_size
stack_size = stack_size * 2;
stack = [stack; zeros(size(stack))];
end
sharpImg(neighbors(pt, 2), neighbors(pt, 1)) = Visited;
s_ptr = s_ptr + 1;
stack(s_ptr, :) = neighbors(pt, :);
end
end
if s_ptr == 0
break;
end
end % end while
% ---
end
end
k = k - 1;
% check each side of the scratch
for point_id = 1:k
% should be modified, neighbor pixels
p_pix = potential_scratch_pixels(point_id, :);
out(p_pix(2), p_pix(1)) = is_scratch_pixel(img, p_pix, normal, cross_correlation_nbr);
end
end
end %end function
function bool = is_scratch_pixel(img, p_pix, normal, cross_correlation_nbr)
len = 1:cross_correlation_nbr;
pattern = zeros(cross_correlation_nbr, 2);
[M, N] = size(img);
for dir = 1:2 % two side
cur_dir = (-1)^dir * normal;
for l = len
pt = round(p_pix + l * cur_dir);
if sum(pt < 1) > 0 || pt(2) > M || pt(1) > N % check boundary
continue;
end
pattern(l, dir) = img(pt(2), pt(1));
end
end
if abs(corrcoef(pattern(:, 1), pattern(:, 2))) > 0.1
bool = true;
else
bool = false;
end
end