-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
answer_45.cpp
588 lines (482 loc) · 13.4 KB
/
answer_45.cpp
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <math.h>
// RGB to Gray scale
cv::Mat BGR2GRAY(cv::Mat img){
// get height and width
int height = img.rows;
int width = img.cols;
int channel = img.channels();
// prepare output
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1);
// BGR -> Gray
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
out.at<uchar>(y, x) = (int)((float)img.at<cv::Vec3b>(y, x)[0] * 0.0722 + \
(float)img.at<cv::Vec3b>(y, x)[1] * 0.7152 + \
(float)img.at<cv::Vec3b>(y, x)[2] * 0.2126);
}
}
return out;
}
float clip(float value, float min, float max){
return fmin(fmax(value, 0), 255);
}
// gaussian filter
cv::Mat gaussian_filter(cv::Mat img, double sigma, int kernel_size){
int height = img.rows;
int width = img.cols;
int channel = img.channels();
// prepare output
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3);
if (channel == 1) {
out = cv::Mat::zeros(height, width, CV_8UC1);
}
// prepare kernel
int pad = floor(kernel_size / 2);
int _x = 0, _y = 0;
double kernel_sum = 0;
// get gaussian kernel
float kernel[kernel_size][kernel_size];
for (int y = 0; y < kernel_size; y++){
for (int x = 0; x < kernel_size; x++){
_y = y - pad;
_x = x - pad;
kernel[y][x] = 1 / (2 * M_PI * sigma * sigma) * exp( - (_x * _x + _y * _y) / (2 * sigma * sigma));
kernel_sum += kernel[y][x];
}
}
for (int y = 0; y < kernel_size; y++){
for (int x = 0; x < kernel_size; x++){
kernel[y][x] /= kernel_sum;
}
}
// filtering
double v = 0;
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
// for BGR
if (channel == 3){
for (int c = 0; c < channel; c++){
v = 0;
for (int dy = -pad; dy < pad + 1; dy++){
for (int dx = -pad; dx < pad + 1; dx++){
if (((x + dx) >= 0) && ((y + dy) >= 0) && ((x + dx) < width) && ((y + dy) < height)){
v += (double)img.at<cv::Vec3b>(y + dy, x + dx)[c] * kernel[dy + pad][dx + pad];
}
}
}
out.at<cv::Vec3b>(y, x)[c] = (uchar)clip(v, 0, 255);
}
} else {
// for Gray
v = 0;
for (int dy = -pad; dy < pad + 1; dy++){
for (int dx = -pad; dx < pad + 1; dx++){
if (((x + dx) >= 0) && ((y + dy) >= 0) && ((x + dx) < width) && ((y + dy) < height)){
v += (double)img.at<uchar>(y + dy, x + dx) * kernel[dy + pad][dx + pad];
}
}
}
out.at<uchar>(y, x) = (uchar)clip(v, 0, 255);
}
}
}
return out;
}
// Sobel filter
cv::Mat sobel_filter(cv::Mat img, int kernel_size, bool horizontal){
int height = img.rows;
int width = img.cols;
int channel = img.channels();
// prepare output
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1);
// prepare kernel
double kernel[kernel_size][kernel_size] = {{1, 2, 1}, {0, 0, 0}, {-1, -2, -1}};
if (horizontal){
kernel[0][1] = 0;
kernel[0][2] = -1;
kernel[1][0] = 2;
kernel[1][2] = -2;
kernel[2][0] = 1;
kernel[2][1] = 0;
}
int pad = floor(kernel_size / 2);
double v = 0;
// filtering
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
v = 0;
for (int dy = -pad; dy < pad + 1; dy++){
for (int dx = -pad; dx < pad + 1; dx++){
if (((y + dy) >= 0) && (( x + dx) >= 0) && ((y + dy) < height) && ((x + dx) < width)){
v += (double)img.at<uchar>(y + dy, x + dx) * kernel[dy + pad][dx + pad];
}
}
}
out.at<uchar>(y, x) = (uchar)clip(v, 0, 255);
}
}
return out;
}
// get edge
cv::Mat get_edge(cv::Mat fx, cv::Mat fy){
// get height and width
int height = fx.rows;
int width = fx.cols;
// prepare output
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1);
double _fx, _fy;
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
_fx = (double)fx.at<uchar>(y, x);
_fy = (double)fy.at<uchar>(y, x);
out.at<uchar>(y, x) = (uchar)clip(sqrt(_fx * _fx + _fy * _fy), 0, 255);
}
}
return out;
}
// get angle
cv::Mat get_angle(cv::Mat fx, cv::Mat fy){
// get height and width
int height = fx.rows;
int width = fx.cols;
// prepare output
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1);
double _fx, _fy;
double angle;
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
_fx = fmax((double)fx.at<uchar>(y, x), 0.000001);
_fy = (double)fy.at<uchar>(y, x);
angle = atan2(_fy, _fx);
angle = angle / M_PI * 180;
if(angle < -22.5){
angle = 180 + angle;
} else if (angle >= 157.5) {
angle = angle - 180;
}
// quantization
if (angle <= 22.5){
out.at<uchar>(y, x) = 0;
} else if (angle <= 67.5){
out.at<uchar>(y, x) = 45;
} else if (angle <= 112.5){
out.at<uchar>(y, x) = 90;
} else {
out.at<uchar>(y, x) = 135;
}
}
}
return out;
}
// non maximum suppression
cv::Mat non_maximum_suppression(cv::Mat angle, cv::Mat edge){
int height = angle.rows;
int width = angle.cols;
int channel = angle.channels();
int dx1, dx2, dy1, dy2;
int now_angle;
// prepare output
cv::Mat _edge = cv::Mat::zeros(height, width, CV_8UC1);
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
now_angle = angle.at<uchar>(y, x);
// angle condition
if (now_angle == 0){
dx1 = -1;
dy1 = 0;
dx2 = 1;
dy2 = 0;
} else if(now_angle == 45) {
dx1 = -1;
dy1 = 1;
dx2 = 1;
dy2 = -1;
} else if(now_angle == 90){
dx1 = 0;
dy1 = -1;
dx2 = 0;
dy2 = 1;
} else {
dx1 = -1;
dy1 = -1;
dx2 = 1;
dy2 = 1;
}
if (x == 0){
dx1 = fmax(dx1, 0);
dx2 = fmax(dx2, 0);
}
if (x == (width - 1)){
dx1 = fmin(dx1, 0);
dx2 = fmin(dx2, 0);
}
if (y == 0){
dy1 = fmax(dy1, 0);
dy2 = fmax(dy2, 0);
}
if (y == (height - 1)){
dy1 = fmin(dy1, 0);
dy2 = fmin(dy2, 0);
}
// if pixel is max among adjuscent pixels, pixel is kept
if (fmax(fmax(edge.at<uchar>(y, x), edge.at<uchar>(y + dy1, x + dx1)), edge.at<uchar>(y + dy2, x + dx2)) == edge.at<uchar>(y, x)) {
_edge.at<uchar>(y, x) = edge.at<uchar>(y, x);
}
}
}
return _edge;
}
// histerisis
cv::Mat histerisis(cv::Mat edge, int HT, int LT){
int height = edge.rows;
int width = edge.cols;
int channle = edge.channels();
// prepare output
cv::Mat _edge = cv::Mat::zeros(height, width, CV_8UC1);
int now_pixel;
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
// get pixel
now_pixel = edge.at<uchar>(y, x);
// if pixel >= HT
if (now_pixel >= HT){
_edge.at<uchar>(y, x) = 255;
}
// if LT < pixel < HT
else if (now_pixel > LT) {
for (int dy = -1; dy < 2; dy++){
for (int dx = -1; dx < 2; dx++){
// if 8 nearest neighbor pixel >= HT
if (edge.at<uchar>(fmin(fmax(y + dy, 0), 255), fmin(fmax(x + dx, 0), 255)) >= HT){
_edge.at<uchar>(y, x) = 255;
}
}
}
}
}
}
return _edge;
}
// Canny
cv::Mat Canny(cv::Mat img){
// BGR -> Gray
cv::Mat gray = BGR2GRAY(img);
// gaussian filter
cv::Mat gaussian = gaussian_filter(gray, 1.4, 5);
// sobel filter (vertical)
cv::Mat fy = sobel_filter(gaussian, 3, false);
// sobel filter (horizontal)
cv::Mat fx = sobel_filter(gaussian, 3, true);
// get edge
cv::Mat edge = get_edge(fx, fy);
// get angle
cv::Mat angle = get_angle(fx, fy);
// edge non-maximum suppression
edge = non_maximum_suppression(angle, edge);
// histerisis
edge = histerisis(edge, 100, 30);
return edge;
}
//------
// hough
const int ANGLE_T = 180;
const int RHO_MAX = 320;
// hough table
struct struct_hough_table {
int table[RHO_MAX * 2][ANGLE_T];
};
// hough vote
struct_hough_table Hough_vote(struct_hough_table hough_table, cv::Mat img){
int height = img.rows;
int width = img.cols;
int rho = 0;
double angle = 0;
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
// if not edge, skip
if (img.at<uchar>(y, x) != 255){
continue;
}
// 0 <= angle t < 180
for (int t = 0; t < ANGLE_T; t++){
angle = M_PI / 180 * t;
rho = (int)(x * cos(angle) + y * sin(angle));
hough_table.table[rho + RHO_MAX][t] ++;
}
}
}
return hough_table;
}
// hough nms
struct_hough_table Hough_NMS(struct_hough_table hough_table){
// output hough table
struct_hough_table output_hough_table;
// initialize 0
for (int rho = 0; rho < RHO_MAX * 2; rho++){
for (int t = 0; t < ANGLE_T; t++){
output_hough_table.table[rho][t] = 0;
}
}
// top N x, y
int N = 30;
int top_N_rho[N], top_N_t[N], top_N_vote[N];
int tmp_rho, tmp_t, tmp_vote, tmp_rho2, tmp_t2, tmp_vote2;
int rho, t;
for (int n = 0; n < N; n++){
top_N_rho[n] = -1;
top_N_t[n] = -1;
top_N_vote[n] = -1;
}
for (int rho = 0; rho < RHO_MAX * 2; rho++){
for (int t = 0; t < ANGLE_T; t++){
if (hough_table.table[rho][t] == 0){
continue;
}
// compare to left top
if (((t - 1) >= 0) && ((rho - 1) >= 0)){
if (hough_table.table[rho][t] < hough_table.table[rho - 1][t - 1]){
continue;
}
}
// comparet to top
if ((rho - 1) >= 0){
if (hough_table.table[rho][t] < hough_table.table[rho - 1][t]){
continue;
}
}
// compare to left top
if (((t + 1) < ANGLE_T) && ((rho - 1) >= 0)){
if (hough_table.table[rho][t] < hough_table.table[rho - 1][t + 1]){
continue;
}
}
// compare to left
if ((t - 1) >= 0){
if (hough_table.table[rho][t] < hough_table.table[rho][t - 1]){
continue;
}
}
// compare to right
if ((t + 1) < ANGLE_T){
if (hough_table.table[rho][t] < hough_table.table[rho][t + 1]){
continue;
}
}
// compare to left bottom
if (((t - 1) >= 0) && ((rho + 1) < RHO_MAX * 2)){
if (hough_table.table[rho][t] < hough_table.table[rho + 1][t - 1]){
continue;
}
}
// compare to bottom
if ((rho + 1) < RHO_MAX * 2){
if (hough_table.table[rho][t] < hough_table.table[rho + 1][t]){
continue;
}
}
// compare to right bottom
if (((t + 1) < ANGLE_T) && ((rho + 1) < RHO_MAX * 2)){
if (hough_table.table[rho][t] < hough_table.table[rho + 1][t + 1]){
continue;
}
}
// Select top N votes
for (int n = 0; n < N; n++){
if (top_N_vote[n] <= hough_table.table[rho][t]){
tmp_vote = top_N_vote[n];
tmp_rho = top_N_rho[n];
tmp_t = top_N_t[n];
top_N_vote[n] = hough_table.table[rho][t];
top_N_rho[n] = rho;
top_N_t[n] = t;
for (int m = n + 1; m < N - 1; m++){
tmp_vote2 = top_N_vote[m];
tmp_rho2 = top_N_rho[m];
tmp_t2 = top_N_t[m];
top_N_vote[m] = tmp_vote;
top_N_rho[m] = tmp_rho;
top_N_t[m] = tmp_t;
tmp_vote = tmp_vote2;
tmp_rho = tmp_rho2;
tmp_t = tmp_t2;
}
top_N_vote[N - 1] = tmp_vote;
top_N_rho[N - 1] = tmp_rho;
top_N_t[N - 1] = tmp_t;
break;
}
}
}
}
// get pixel for top N votes
for (int n = 0; n < N; n++){
if (top_N_rho[n] == -1){
break;
}
rho = top_N_rho[n];
t = top_N_t[n];
output_hough_table.table[rho][t] = hough_table.table[rho][t];
}
return output_hough_table;
}
// Inverse hough transformation
cv::Mat Hough_inverse(struct_hough_table hough_table, cv::Mat img){
int height = img.rows;
int width = img.cols;
double _cos, _sin;
int y, x;
for (int rho = 0; rho < RHO_MAX * 2; rho++){
for (int t = 0; t < ANGLE_T; t++){
// if not vote, skip
if (hough_table.table[rho][t] < 1){
continue;
}
_cos = cos(t * M_PI / 180);
_sin = sin(t * M_PI / 180);
if ((_sin == 0) || (_cos == 0)){
continue;
}
for (int x = 0; x < width; x++){
y = (int)(- _cos / _sin * x + (rho - RHO_MAX) / _sin);
if ((y >= 0) && (y < height)){
img.at<cv::Vec3b>(y, x) = cv::Vec3b(0, 0, 255);
}
}
for (int y = 0; y < height; y++){
x = (int)(- _sin / _cos * y + (rho - RHO_MAX) / _cos);
if ((x >= 0) && (x < width)){
img.at<cv::Vec3b>(y, x) = cv::Vec3b(0, 0, 255);
}
}
}
}
return img;
}
// hough step 2
int Hough_line(cv::Mat img){
// get edge by canny
cv::Mat edge = Canny(img);
// hough
struct_hough_table hough_table;
// initialize 0
for (int rho = 0; rho < RHO_MAX * 2; rho++){
for (int t = 0; t < ANGLE_T; t++){
hough_table.table[rho][t] = 0;
}
}
// hough vote
hough_table = Hough_vote(hough_table, edge);
// hough NMS
hough_table = Hough_NMS(hough_table);
return 0;
}
int main(int argc, const char* argv[]){
// read image
cv::Mat img = cv::imread("thorino.jpg", cv::IMREAD_COLOR);
// Hough line detection
Hough_line(img);
return 0;
}