-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwang.m
More file actions
151 lines (125 loc) · 3.4 KB
/
wang.m
File metadata and controls
151 lines (125 loc) · 3.4 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
function nimg = wang(img)
if(ischar(img)), img = imread(img);
end
%figure(1), imshow(img);
% Step 1
if(ndims(img) == 3), img = rgb2gray(img);
end
img = double(img);
[height width] = size(img);
adjustment = mean([height/300 width/400]);
one_hundred = 100; %100*adjustment
ratio = 6.2; %4
ten_of_height = 12; %10*adjustment
five_rows_above = 5*adjustment; %5*adjustment
% Step 2
[ll lh hl hh] = dwt2(img,'db1','mode','sym');
[height_hl width_hl] = size(hl);
% Step 3
abs_hl = abs(hl);
hl_norm = abs_hl ./ max(abs_hl(:));
hl_bin = hl_norm >= graythresh(hl_norm);
avg_height_v = [];
[l num] = bwlabel(hl_bin, 8);
hl_bin = double(hl_bin);
for i = 1 : num,
[rows cols] = find(l == i);
line_height = max(rows) - min(rows) + 1;
indexes = sub2ind([height_hl width_hl], rows, cols);
if(line_height > one_hundred), hl_bin(indexes) = 0;
else
hl_bin(indexes) = line_height;
avg_height_v(end+1) = line_height;
end
end
avg_height_v = mean(avg_height_v);
hl_vi_bin = hl_bin > avg_height_v;
hl_vi = hl .* hl_vi_bin;
% Step 4
abs_lh = abs(lh);
avg_lh = mean(abs_lh(:));
p = abs_lh .* (abs_lh >= avg_lh);
p_bin = p ~= 0;
% Step 5
bs = floor(1/5 * width_hl);
lines = zeros([height_hl width_hl]);
transition = double(hl_vi_bin);
transition = transition(:, 1 : end-1) - transition(:, 2 : end);
transition = transition ~= 0;
transition = [transition zeros([height_hl 1])];
for i = height_hl : -1 : 1,
for j = 1 : width_hl-bs, lines(i, j) = sum(transition(i, j : j+bs));
end
end
lines = lines .* hl_vi_bin;
for i = height_hl : -1 : 1, lines(i, lines(i, :) ~= max(lines(i, :))) = 0;
end
for i = height_hl : -1 : five_rows_above+1,
for j = 1 : width_hl-bs,
if(lines(i, j)),
se = ones([1 floor(bs/2)]);
lines(i, j) = lines(i, j) && max(max(imopen(p_bin(i-five_rows_above : i-1, j : j+bs), se)));
end
end
end
[l num] = bwlabel(lines, 8);
candidates_x = [];
candidates_y = [];
for k = 1 : num,
[rows cols] = find(l == k);
candidates_y(end+1) = min(rows);
candidates_x(end+1) = min(cols);
end
% Step 6, 7, 8, and 9
nimg = zeros([height width]);
for k = 1 : length(candidates_x)
sy = candidates_y(k);
sx = candidates_x(k);
ey = min([sy + ten_of_height, height_hl]);
ex = min([sx + floor(ten_of_height * ratio), width_hl]);
candidate_width = ex - sx;
% Step 7
window_width = floor(candidate_width/3);
new_sx = sx;
max_value = 0;
for j = sx : sx+window_width,
value = sum(sum(transition(sy : ey, j : j+window_width)));
if(value > max_value),
max_value = value;
new_sx = j;
end
end
new_ex = ex;
max_value = 0;
for j = ex : -1 : ex-window_width,
value = sum(sum(transition(sy : ey, j-window_width : j)));
if(value > max_value),
max_value = value;
new_ex = j;
end
end
% Step 8
new_sy = sy;
for i = sy : -1 : 1,
if(~sum(hl_vi(i, sx : ex))), new_sy = i; break;
end
end
new_ey = ey;
for i = ey : height_hl,
if(~sum(hl_vi(i, sx : ex))), new_ey = i; break;
end
end
sx = new_sx;
ex = new_ex;
sy = new_sy;
ey = new_ey;
% Step 9
sx = 2*sx;
ex = 2*ex;
sy = 2*sy;
ey = 2*ey;
img_candidate = zeros([height width]);
img_candidate(sy : ey, sx : ex) = 1;
nimg = nimg + img_candidate;
end
%figure(2); imshow(nimg);