This repository has been archived by the owner on May 25, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
sh_ed.h
385 lines (347 loc) · 9.66 KB
/
sh_ed.h
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#ifndef SH_ED_H
#define SH_ED_H
inline void sh_ed_xxxx_edge_detect(image_type& bmp_img, int hreshold)
{
int grad;
image_type tmp(bmp_img);
for (int px = 1; px < bmp_img.width(); ++px)
{
for (int py = 1; py < bmp_img.height(); ++py)
{
// horizonal gradient
grad = abs(bmp_img.at(px, py).gray() - bmp_img.at(px - 1, py).gray());
tmp.at(px - 1, py).is_black(grad > hreshold);
// vertical gradient
grad = abs(bmp_img.at(px, py).gray() - bmp_img.at(px, py - 1).gray());
tmp.at(px, py - 1).is_black(grad > hreshold? true : tmp.at(px, py - 1).is_black()); // merge
}
}
bmp_img = tmp;
}
inline void sh_ed_edge_detect_impl(double* o1, int n, image_type& bmp_img, int hreshold)
{
int mid = n >> 1;
image_type tmp(bmp_img);
for (int px = 0; px < bmp_img.width() - n; ++px)
{
for (int py = 0; py < bmp_img.height() - n; ++py)
{
double g1 = 0;
for (int k = 0; k < n * n; ++k)
{
g1 += tmp.at(px + k % n, py + k / n).gray() * o1[k];
}
double g = fabs(g1);
if (g / (n * n) > hreshold) bmp_img.at(px + mid, py + mid).is_black(true);
else bmp_img.at(px + mid, py + mid).is_black(false);
}
}
}
inline void sh_ed_edge_detect_impl(double* o1, double* o2, int n, image_type& bmp_img, int hreshold)
{
int mid = n >> 1;
image_type tmp(bmp_img);
for (int px = 0; px < bmp_img.width() - n; ++px)
{
for (int py = 0; py < bmp_img.height() - n; ++py)
{
double g1 = 0;
double g2 = 0;
for (int k = 0; k < n * n; ++k)
{
g1 += tmp.at(px + k / n, py + k % n).gray() * o1[k];
g2 += tmp.at(px + k / n, py + k % n).gray() * o2[k];
}
double g = (fabs(g1) + fabs(g2)) / 2;
//double g = sqrt(g1 * g1 + g2 * g2) / 2;
if (g / (n * n) > hreshold) bmp_img.at(px + mid, py + mid).is_black(true);
else bmp_img.at(px + mid, py + mid).is_black(false);
}
}
}
inline void sh_ed_edge_detect_impl(double* o1, double* o2, double* o3, int n, image_type& bmp_img, int hreshold)
{
int mid = n >> 1;
image_type tmp(bmp_img);
for (int px = 0; px < bmp_img.width() - n; ++px)
{
for (int py = 0; py < bmp_img.height() - n; ++py)
{
double g1 = 0;
double g2 = 0;
double g3 = 0;
for (int k = 0; k < n * n; ++k)
{
g1 += tmp.at(px + k / n, py + k % n).gray() * o1[k];
g2 += tmp.at(px + k / n, py + k % n).gray() * o2[k];
g3 += tmp.at(px + k / n, py + k % n).gray() * o3[k];
}
double g = (fabs(g1) + fabs(g2) + fabs(g3)) / 3;
if (g / (n * n) > hreshold) bmp_img.at(px + mid, py + mid).is_black(true);
else bmp_img.at(px + mid, py + mid).is_black(false);
}
}
}
inline void sh_ed_edge_detect_impl(double* o1, double* o2, double* o3, double* o4, int n, image_type& bmp_img, int hreshold)
{
int mid = n >> 1;
image_type tmp(bmp_img);
for (int px = 0; px < bmp_img.width() - n; ++px)
{
for (int py = 0; py < bmp_img.height() - n; ++py)
{
double g1 = 0;
double g2 = 0;
double g3 = 0;
double g4 = 0;
for (int k = 0; k < n * n; ++k)
{
g1 += tmp.at(px + k / n, py + k % n).gray() * o1[k];
g2 += tmp.at(px + k / n, py + k % n).gray() * o2[k];
g3 += tmp.at(px + k / n, py + k % n).gray() * o3[k];
g4 += tmp.at(px + k / n, py + k % n).gray() * o4[k];
}
double g = (fabs(g1) + fabs(g2) + fabs(g3) + fabs(g4)) / 4;
if (g / (n * n) > hreshold) bmp_img.at(px + mid, py + mid).is_black(true);
else bmp_img.at(px + mid, py + mid).is_black(false);
}
}
}
inline void sh_ed_edge_detect_impl(double* o1, double* o2, double* o3, double* o4, double* o5, double* o6, double* o7, double* o8, int n, image_type& bmp_img, int hreshold)
{
int mid = n >> 1;
image_type tmp(bmp_img);
for (int px = 0; px < bmp_img.width() - n; ++px)
{
for (int py = 0; py < bmp_img.height() - n; ++py)
{
double g1 = 0;
double g2 = 0;
double g3 = 0;
double g4 = 0;
double g5 = 0;
double g6 = 0;
double g7 = 0;
double g8 = 0;
for (int k = 0; k < n * n; ++k)
{
g1 += tmp.at(px + k / n, py + k % n).gray() * o1[k];
g2 += tmp.at(px + k / n, py + k % n).gray() * o2[k];
g3 += tmp.at(px + k / n, py + k % n).gray() * o3[k];
g4 += tmp.at(px + k / n, py + k % n).gray() * o4[k];
g5 += tmp.at(px + k / n, py + k % n).gray() * o5[k];
g6 += tmp.at(px + k / n, py + k % n).gray() * o6[k];
g7 += tmp.at(px + k / n, py + k % n).gray() * o7[k];
g8 += tmp.at(px + k / n, py + k % n).gray() * o8[k];
}
double g = (fabs(g1) + fabs(g2) + fabs(g3) + fabs(g4) + fabs(g5) + fabs(g6) + fabs(g7) + fabs(g8)) / 8;
if (g / (n * n) > hreshold) bmp_img.at(px + mid, py + mid).is_black(true);
else bmp_img.at(px + mid, py + mid).is_black(false);
}
}
}
inline void sh_ed_roberts_edge_detect(image_type& bmp_img, int hreshold = 3)
{
double rx[4] = { 1, 0
, 0, -1
};
double ry[4] = { 0, 1
, -1, 0
};
sh_ed_edge_detect_impl(rx, ry, 2, bmp_img, hreshold);
}
inline void sh_ed_sobel_2_edge_detect(image_type& bmp_img, int hreshold = 5)
{
double s1[9] = { -1, -2, -1
, 0, 0, 0
, 1, 2, 1
};
double s2[9] = { -1, 0, 1
, -2, 0, 2
, -1, 0, 1
};
sh_ed_edge_detect_impl(s1, s2, 3, bmp_img, hreshold);
}
inline void sh_ed_sobel_4_edge_detect(image_type& bmp_img, int hreshold = 5)
{
double s1[9] = { -1, -2, -1
, 0, 0, 0
, 1, 2, 1
};
double s2[9] = { -1, 0, 1
, -2, 0, 2
, -1, 0, 1
};
double s3[9] = { 0, -1, -2
, 1, 0, -1
, 2, 1, 0
};
double s4[9] = { 0, 1, 2
, -1, 0, 1
, -2, -1, 0
};
sh_ed_edge_detect_impl(s1, s2, s3, s4, 3, bmp_img, hreshold);
}
inline void sh_ed_prewitt_edge_detect(image_type& bmp_img, int hreshold = 5)
{
double px[9] = { -1, -1, -1
, 0, 0, 0
, 1, 1, 1
};
double py[9] = { -1, 0, 1
, -1, 0, 1
, -1, 0, 1
};
sh_ed_edge_detect_impl(px, py, 3, bmp_img, hreshold);
}
inline void sh_ed_prewitt_7_edge_detect(image_type& bmp_img, int hreshold = 5)
{
double hp[49] ={ 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
-1, -1, -1, -1, -1, -1, -1,
0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
};
double vp[49] ={ 0, 0, -1, 0, 1, 0, 0,
0, 0, -1, 0, 1, 0, 0,
0, 0, -1, 0, 1, 0, 0,
0, 0, -1, 0, 1, 0, 0,
0, 0, -1, 0, 1, 0, 0,
0, 0, -1, 0, 1, 0, 0,
0, 0, -1, 0, 1, 0, 0,
};
sh_ed_edge_detect_impl(hp, vp, 7, bmp_img, hreshold);
}
inline void sh_ed_kirsch_edge_detect(image_type& bmp_img, int hreshold = 5)
{
double k1[9] = { 5, 5, 5
, -3, 0, -3
, -3, -3, -3
};
double k2[9] = { -3, 5, 5
, -3, 0, 5
, -3, -3, -3
};
double k3[9] = { -3, -3, 5
, -3, 0, 5
, -3, -3, 5
};
double k4[9] = { -3, -3, -3
, -3, 0, 5
, -3, 5, 5
};
double k5[9] = { -3, -3, -3
, -3, 0, -3
, 5, 5, 5
};
double k6[9] = { -3, -3, -3
, 5, 0, -3
, 5, 5, -3
};
double k7[9] = { 5, -3, -3
, 5, 0, -3
, 5, -3, -3
};
double k8[9] = { 5, 5, -3
, 5, 0, -3
, -3, -3, -3
};
sh_ed_edge_detect_impl(k1, k2, k3, k4, k5, k6, k7, k8, 3, bmp_img, hreshold);
}
// horizontal
inline void sh_ed_kirsch_edge_detect_h(image_type& bmp_img, int hreshold = 5)
{
double k1[9] = { 5, 5, 5
, -3, 0, -3
, -3, -3, -3
};
double k2[9] = { -3, -3, -3
, -3, 0, -3
, 5, 5, 5
};
sh_ed_edge_detect_impl(k1, k2, 3, bmp_img, hreshold);
}
// vertical
inline void sh_ed_kirsch_edge_detect_v(image_type& bmp_img, int hreshold = 5)
{
double k1[9] = { -3, -3, 5
, -3, 0, 5
, -3, -3, 5
};
double k2[9] = { 5, -3, -3
, 5, 0, -3
, 5, -3, -3
};
sh_ed_edge_detect_impl(k1, k2, 3, bmp_img, hreshold);
}
// right-diagonal
inline void sh_ed_kirsch_edge_detect_r(image_type& bmp_img, int hreshold = 5)
{
double k1[9] = { -3, 5, 5
, -3, 0, 5
, -3, -3, -3
};
double k2[9] = { -3, -3, -3
, 5, 0, -3
, 5, 5, -3
};
sh_ed_edge_detect_impl(k1, k2, 3, bmp_img, hreshold);
}
// left-diagonal
inline void sh_ed_kirsch_edge_detect_l(image_type& bmp_img, int hreshold = 5)
{
double k1[9] = { 5, 5, -3
, 5, 0, -3
, -3, -3, -3
};
double k2[9] = { -3, -3, -3
, -3, 0, 5
, -3, 5, 5
};
sh_ed_edge_detect_impl(k1, k2, 3, bmp_img, hreshold);
}
inline void sh_ed_isotropic_sobel_edge_detect(image_type& bmp_img, int hreshold = 5)
{
double sx[9] = { -1, -1.414213, -1
, 0, 0, 0
, 1, 1.414213, 1
};
double sy[9] = { -1, 0, 1
, -1.414213, 0, 1.414213
, -1, 0, 1
};
sh_ed_edge_detect_impl(sx, sy, 3, bmp_img, hreshold);
}
inline void sh_ed_gauss_laplacian_edge_detect(image_type& bmp_img, int hreshold = 5)
{
double op[25] ={ -2, -4, -4, -4, -2,
-4, 0, 8, 0, -4,
-4, 8, 24, 8, -4,
-4, 0, 8, 0, -4,
-2, -4, -4, -4, -2
};
sh_ed_edge_detect_impl(op, 5, bmp_img, hreshold);
}
inline double marr_operator(int x, int y, double dr)
{
double xy2 = double(x * x + y * y);
double a = (xy2 - 2 * dr * dr) / (2 * 3.1415926 * dr * dr * dr * dr * dr * dr);
double b = exp(-xy2 / (2 * dr * dr));
return (a * b);
}
inline void sh_ed_marr_edge_detect(image_type& bmp_img, int hreshold = 5, int n = 5, double dr = 0.6)
{
int marr_n = n >> 1;
scoped_buffer<double> buf((2 * marr_n + 1) * (2 * marr_n + 1));
for (int y = 0; y < n; ++y)
{
for (int x = 0; x < n; ++x)
{
buf[y * n + x] = marr_operator(x - marr_n, y - marr_n, dr);
}
}
sh_ed_edge_detect_impl(buf.data(), n, bmp_img, hreshold);
}
#endif // SH_ED_H