-
Notifications
You must be signed in to change notification settings - Fork 4
/
Edge.cpp
790 lines (651 loc) · 22.8 KB
/
Edge.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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
// Edge.cpp: implementation of the CEdge class.
//
//////////////////////////////////////////////////////////////////////
#include "Edge.h"
#include "Util.h"
#include "Filter.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CEdge::CEdge()
{
}
CEdge::~CEdge()
{
}
// 캐니 에지 추출 : Canny() 함수 이용 (Extraction canny edge : using cv::Canny())
cv::Mat CEdge::GS_canny_edge_Canny(cv::Mat src_image, double low_threshold, double high_threshold)
{
CUtil cUtil;
// 초기화 (Initialization)
cv::Mat dst_image = cUtil.GS_createImage(src_image.size(), src_image.channels());
if (dst_image.channels() != 1) return dst_image;
//
cv::Mat canny_edge = dst_image.clone();
// (1) Canny() 함수 수행시는 src_image와 dst_image의 depth는 같아야 함. (depth of src_image and dst_image shoud be the same in Canny())
// (2) low_threshold, high_threshold는 Edge linking 처리하기 위한 값.(low_threshold, high_threshold are for Edge linking processing)
cv::Canny(src_image, canny_edge, low_threshold, high_threshold, 3);
cv::convertScaleAbs(canny_edge, dst_image, 1, 0);
return dst_image;
}
// 캐니 에지 추출 (Extraction canny edg)
cv::Mat CEdge::GS_canny_edge(cv::Mat src_image, double sigma, int threshold, int method)
{
int i, j;
double Gx_sum, Gy_sum, ret_var;
CUtil cUtil;
CFilter cFilter;
// 초기화 (Initialization)
int height = src_image.rows;
int width = src_image.cols;
cv::Mat dst_image = cUtil.GS_createImage(src_image.size(), src_image.channels());
if (dst_image.channels() != 1) return dst_image;
// 가우시안 스무딩 (Gaussian smoothing)
int mask_height, mask_width;
GS_get_LoG_mask(sigma, &mask_height, &mask_width);
cv::Mat gs_image = cFilter.GS_gaussian_smoothing_filtering(src_image, sigma);
// 처리 시간을 줄이기 위해 마스크를 쓰지 않고 직접 곱한다.
for (i = 0; i<height - mask_height + ((mask_height - 1) / 2); i++)
{
for (j = 0; j < width - mask_width + ((mask_width - 1) / 2); j++)
{
// 소벨
if (method == 0)
{
// 수직 마스크 (vertical mask)
Gx_sum = -gs_image.at<uchar>(i, j)
- 2 * gs_image.at<uchar>(i + 1, j)
- gs_image.at<uchar>(i + 2, j)
+ gs_image.at<uchar>(i, j + 2)
+ 2 * gs_image.at<uchar>(i + 1, j + 2)
+ gs_image.at<uchar>(i + 2, j + 2);
// 수평 마스크 (horizontal mask)
Gy_sum = -gs_image.at<uchar>(i, j)
- 2 * gs_image.at<uchar>(i, j+1)
- gs_image.at<uchar>(i, j+2)
+ gs_image.at<uchar>(i+2, j)
+ 2 * gs_image.at<uchar>(i + 2, j + 1)
+ gs_image.at<uchar>(i + 2, j + 2);
}
// 프리윗
else if (method == 1)
{
// 수직 마스크 (vertical mask)
Gx_sum = -gs_image.at<uchar>(i, j)
- gs_image.at<uchar>(i + 1, j)
- gs_image.at<uchar>(i + 2, j)
+ gs_image.at<uchar>(i, j + 2)
+ gs_image.at<uchar>(i + 1, j + 2)
+ gs_image.at<uchar>(i + 2, j + 2);
// 수평 마스크 (horizontal mask)
Gy_sum = -gs_image.at<uchar>(i, j)
- gs_image.at<uchar>(i, j + 1)
- gs_image.at<uchar>(i, j + 2)
+ gs_image.at<uchar>(i + 2, j)
+ gs_image.at<uchar>(i + 2, j + 1)
+ gs_image.at<uchar>(i + 2, j + 2);
}
// 로버츠
else if (method == 2)
{
// 대각선 방향 (diagonal direction)
Gx_sum = -gs_image.at<uchar>(i, j + 2) + gs_image.at<uchar>(i + 1, j + 1);
// 역대각선 방향 (reverse diagonal direction)
Gy_sum = -gs_image.at<uchar>(i, j) + gs_image.at<uchar>(i + 1, j + 1);
}
ret_var = fabs(Gx_sum) + fabs(Gy_sum);
// hysteresis 수행
// 즉, lower, upper 경계값 처리 하는 것.(lower, upper boundary value)
// 여기서 upper를 255로 정하였다. (upper==255)
if (ret_var > 255.0) ret_var = 0.0;
else if (ret_var < (double)threshold) ret_var = 0.0;
dst_image.at<uchar>(i + (mask_height - 1) / 2, j + (mask_width - 1) / 2) = ret_var;
}
}
return dst_image;
}
// LoG(Laplacian of Gaussian) 에지 추출 : 공식 (Extraction LoG(Laplacian of Gaussian) edge: formula)
double **CEdge::GS_get_LoG_mask(double sigma, int *mask_height, int *mask_width)
{
int i, j;
double tmp;
// 마스크의 크기를 결정한다. (set the size of mask)
int size = (int)(3.35*sigma + 0.33);
int mask_length = 2 * size + 1;
CUtil cUtil;
double **mask = cUtil.GS_doubleAlloc2D(mask_length, mask_length);
for (i = 0; i<mask_length; i++)
{
for (j = 0; j<mask_length; j++)
{
tmp = GS_get_distance((double)i, (double)j, (double)size, (double)size);
mask[i][j] = GS_get_LoG_formular(tmp, sigma);
}
}
*mask_height = *mask_width = mask_length;
return mask;
}
double CEdge::GS_get_LoG_formular(double x, double sigma)
{
return (x*x - 2.0*sigma*sigma) / (sigma*sigma*sigma*sigma) * gauss(x, sigma);
}
double CEdge::norm(double x, double y)
{
return sqrt(x*x + y*y);
}
double CEdge::GS_get_distance(double a, double b, double c, double d)
{
return norm((a - c), (b - d));
}
double CEdge::gauss(double x, double sigma)
{
return exp((-x*x) / (2.0*sigma*sigma));
}
// LoG(Laplacian of Gaussian) : 마스크 (mask)
// 라플라시안 샤프닝 (Laplacian Sharpening)
// method - 0 : 4 방향 마스크 1 (mask 1 for 4 directions)
// - 1 : 4 방향 마스크 2 (mask 2 for 4 directions)
// - 2 : 8 방향 마스크 1 (mask 1 for 8 directions)
// - 3 : 8 방향 마스크 2 (mask 2 for 8 directions)
cv::Mat CEdge::GS_laplacian_sharpening(cv::Mat src_image, int method)
{
CUtil cUtil;
// 초기화 (Initialization)
cv::Mat dst_image = cUtil.GS_createImage(src_image.size(), src_image.channels());
if (dst_image.channels() != 1) return dst_image;
// 라플라시안 에지 추출(Extraction Laplacian edge)
cv::Mat laplacian_edge = GS_laplacian_edge(src_image, method);
// 덧셈 연산 (add)
cv::add(src_image, laplacian_edge, dst_image);
// 할당한 메모리 해제(memory deallocation)
laplacian_edge.release();
return dst_image;
}
// 라플라시안 에지 추출 : Laplacian() (Extraction Laplacian edge: Laplacian())
cv::Mat CEdge::GS_laplacian_edge_Laplacian(cv::Mat src_image)
{
CUtil cUtil;
// 초기화
cv::Mat dst_image = cUtil.GS_createImage(src_image.size(), src_image.channels());
if (dst_image.channels() != 1) return dst_image;
cv::Mat laplacian_edge = cUtil.GS_createImage(src_image.size(), CV_32F, src_image.channels());
// 4번째 인자가 1이면 아래의 라플라시안 마스크를 갖고 처리한다. (if the third argument is 1, use Laplacian mask below)
// |0 1 0|
// |1 -4 1|
// |0 1 0|
// https://docs.opencv.org/3.4.1/d4/d86/group__imgproc__filter.html#gad78703e4c8fe703d479c1860d76429e6
// 4 번째 인자가 1이 default로 선언되어있어 밑에서는 생략됨.
cv::Laplacian(src_image, laplacian_edge, laplacian_edge.depth());
laplacian_edge.convertTo(dst_image, CV_8UC1, 1, 0);
return dst_image;
}
// 라플라시안 에지 추출 : 마스크 (Extraction Laplacian edge: mask)
// method - 0 : 4 방향 마스크 1 (mask 1 for 4 directions)
// - 1 : 4 방향 마스크 2 (mask 2 for 4 directions)
// - 2 : 8 방향 마스크 1 (mask 1 for 8 directions)
// - 3 : 8 방향 마스크 2 (mask 2 for 8 directions)
cv::Mat CEdge::GS_laplacian_edge(cv::Mat src_image, int method)
{
int i, j, m, n;
int mask_height, mask_width;
double var, ret_var;
CUtil cUtil;
// 초기화 (Initialization)
int height = src_image.rows;
int width = src_image.cols;
cv::Mat dst_image = cUtil.GS_createImage(src_image.size(), src_image.channels());
if (dst_image.channels() != 1) return dst_image;
// 라플라시안 마스크 정의 (define Laplacian mask)
mask_height = mask_width = 3;
int mask_four1[3][3] = { { 0, -1, 0 },{ -1, 4, -1 },{ 0, -1, 0 } };
int mask_four2[3][3] = { { 0, 1, 0 },{ 1, -4, 1 },{ 0, 1, 0 } };
int mask_eight1[3][3] = { { -1, -1, -1 },{ -1, 8, -1 },{ -1, -1, -1 } };
int mask_eight2[3][3] = { { 1, -2, 1 },{ -2, 4, -2 },{ 1, -2, 1 } };
for (i = 0; i<height - mask_height + ((mask_height - 1) / 2); i++)
{
for (j = 0; j<width - mask_width + ((mask_width - 1) / 2); j++)
{
// 초기화 (Initialization)
ret_var = 0.0;
if ((i + mask_height > height) || (j + mask_width > width))
continue;
for (m = 0; m<mask_height; m++)
{
for (n = 0; n<mask_width; n++)
{
var = src_image.at<uchar>(i + m, j + n);
if (method == 0) ret_var += var * mask_four1[m][n];
else if (method == 1) ret_var += var * mask_four2[m][n];
else if (method == 2) ret_var += var * mask_eight1[m][n];
else if (method == 3) ret_var += var * mask_eight2[m][n];
}
}
ret_var = cUtil.GS_clamping(ret_var);
dst_image.at<uchar>(i + (mask_height - 1) / 2, j + (mask_width - 1) / 2) = ret_var;
}
}
return dst_image;
}
// 로버츠 에지 추출 (Extraction Roberts edge)
cv::Mat CEdge::GS_roberts_edge(cv::Mat src_image, int method)
{
int i, j, m, n;
int mask_height, mask_width;
double diagonal_var1, diagonal_var2, ret_var;
CUtil cUtil;
// 초기화 (Initialization)
int height = src_image.rows;
int width = src_image.cols;
cv::Mat dst_image = cUtil.GS_createImage(src_image.size(), src_image.channels());
if (dst_image.channels() != 1) return dst_image;
// 대각선 마스크 정의 (define diagonal mask)
mask_height = mask_width = 3;
int diagonal_mask1[3][3] = { { -1, 0, 0 },{ 0, 1, 0 },{ 0, 0, 0 }, };
int diagonal_mask2[3][3] = { { 0, 0, -1 },{ 0, 1, 0 },{ 0, 0, 0 }, };
for (i = 0; i<height - mask_height + ((mask_height - 1) / 2); i++)
{
for (j = 0; j<width - mask_width + ((mask_width - 1) / 2); j++)
{
// 초기화 (Initialization)
diagonal_var1 = diagonal_var2 = 0.0;
if ((i + mask_height > height) || (j + mask_width > width))
continue;
for (m = 0; m<mask_height; m++)
{
for (n = 0; n<mask_width; n++)
{
// 대각선 에지 1 (diagonal edge1)
diagonal_var1 += src_image.at<uchar>(i + m, j + n) * diagonal_mask1[m][n];
// 대각선 에지 2 (diagonal edge1)
diagonal_var2 += src_image.at<uchar>(i + m, j + n) * diagonal_mask2[m][n];
}
}
diagonal_var1 = fabs(diagonal_var1);
diagonal_var2 = fabs(diagonal_var2);
ret_var = diagonal_var1 + diagonal_var2;
if (method == 0)
{
dst_image.at<uchar>(i + (mask_height - 1) / 2, j + (mask_width - 1) / 2) = diagonal_var1;
}
else if (method == 1)
{
dst_image.at<uchar>(i + (mask_height - 1) / 2, j + (mask_width - 1) / 2) = diagonal_var2;
}
else if (method == 2)
{
dst_image.at<uchar>(i + (mask_height - 1) / 2, j + (mask_width - 1) / 2) = ret_var;
}
}
}
return dst_image;
}
// 서터캐스틱 에지 추출 (Extraction Stochastic edge)
cv::Mat CEdge::GS_stochastic_Edge(cv::Mat src_image, int method)
{
int i, j, m, n;
int mask_height, mask_width;
double vertical_var, horizontal_var, ret_var;
CUtil cUtil;
// 초기화 (Initialization)
int height = src_image.rows;
int width = src_image.cols;
cv::Mat dst_image = cUtil.GS_createImage(src_image.size(), src_image.channels());
if (dst_image.channels() != 1) return dst_image;
// 수직/수평 마스크 정의 (define vertical/horizontal mask)
mask_height = mask_width = 3;
double vertical_mask[3][3] = { { -0.97, 0, 0.97 },{ -1, 0, 1 },{ -0.97, 0, 0.97 } };
double horizontal_mask[3][3] = { { 0.97, 1, 0.97 },{ 0, 0, 0 },{ -0.97, -1, -0.97 } };
for (i = 0; i<height - mask_height + ((mask_height - 1) / 2); i++)
{
for (j = 0; j<width - mask_width + ((mask_width - 1) / 2); j++)
{
// 초기화 (Initialization)
vertical_var = horizontal_var = 0.0;
if ((i + mask_height > height) || (j + mask_width > width))
continue;
for (m = 0; m<mask_height; m++)
{
for (n = 0; n<mask_width; n++)
{
// 수직 에지 (vertical edge)
vertical_var += src_image.at<uchar>(i + m, j + n) * vertical_mask[m][n];
// 수평 에지 (horizontal edge)
horizontal_var += src_image.at<uchar>(i + m, j + n)*horizontal_mask[m][n];
}
}
vertical_var = fabs(vertical_var);
horizontal_var = fabs(horizontal_var);
ret_var = vertical_var + horizontal_var;
if (method == 0)
{
dst_image.at<uchar>(i + (mask_height - 1) / 2, j + (mask_width - 1) / 2) = vertical_var;
}
else if (method == 1)
{
dst_image.at<uchar>(i + (mask_height - 1) / 2, j + (mask_width - 1) / 2) = horizontal_var;
}
else if (method == 2)
{
dst_image.at<uchar>(i + (mask_height - 1) / 2, j + (mask_width - 1) / 2) = ret_var;
}
}
}
return dst_image;
}
// 프레이첸 에지 추출 (Extraction freichen edge)
cv::Mat CEdge::GS_freichen_edge(cv::Mat src_image, int method)
{
int i, j, m, n;
int mask_height, mask_width;
double vertical_var, horizontal_var, ret_var;
CUtil cUtil;
// 초기화 (Initialization)
int height = src_image.rows;
int width = src_image.cols;
cv::Mat dst_image = cUtil.GS_createImage(src_image.size(), src_image.channels());
if (dst_image.channels() != 1) return dst_image;
// 수직/수평 마스크 정의 (define vertical/horizontal mask)
mask_height = mask_width = 3;
double vertical_mask[3][3] = { { -1, 0, 1 },{ -sqrt(2), 0, sqrt(2) },{ -1, 0, 1 } };
double horizontal_mask[3][3] = { { 1, sqrt(2), 1 },{ 0, 0, 0 },{ -1, -sqrt(2), -1 } };
for (i = 0; i<height - mask_height + ((mask_height - 1) / 2); i++)
{
for (j = 0; j<width - mask_width + ((mask_width - 1) / 2); j++)
{
// 초기화 (Initialization)
vertical_var = horizontal_var = 0.0;
if ((i + mask_height > height) || (j + mask_width > width))
continue;
for (m = 0; m<mask_height; m++)
{
for (n = 0; n<mask_width; n++)
{
// 수직 에지 (vertical edge)
vertical_var += src_image.at<uchar>(i + m, j + n)*vertical_mask[m][n];
// 수평 에지 (horizontal edge)
horizontal_var += src_image.at<uchar>(i + m, j + n) * horizontal_mask[m][n];
}
}
vertical_var = fabs(vertical_var);
horizontal_var = fabs(horizontal_var);
ret_var = vertical_var + horizontal_var;
if (method == 0)
{
dst_image.at<uchar>(i + (mask_height - 1) / 2, j + (mask_width - 1) / 2) = vertical_var;
}
else if (method == 1)
{
dst_image.at<uchar>(i + (mask_height - 1) / 2, j + (mask_width - 1) / 2) = horizontal_var;
}
else if (method == 2)
{
dst_image.at<uchar>(i + (mask_height - 1) / 2, j + (mask_width - 1) / 2) = ret_var;
}
}
}
return dst_image;
}
// 프리윗 에지 추출 (Extraction prewitt edge)
cv::Mat CEdge::GS_prewitt_edge(cv::Mat src_image, int method)
{
int i, j, m, n;
int mask_height, mask_width;
double vertical_var, horizontal_var, ret_var;
CUtil cUtil;
// 초기화 (Initialization)
int height = src_image.rows;
int width = src_image.cols;
cv::Mat dst_image = cUtil.GS_createImage(src_image.size(), src_image.channels());
if (dst_image.channels() != 1) return dst_image;
// 수직/수평 마스크 정의 (define vertical/horizontal mask)
mask_height = mask_width = 3;
int vertical_mask[3][3] = { { -1, 0, 1 },{ -1, 0, 1 },{ -1, 0, 1 } };
int horizontal_mask[3][3] = { { 1, 1, 1 },{ 0, 0, 0 },{ -1, -1, -1 } };
for (i = 0; i<height - mask_height + ((mask_height - 1) / 2); i++)
{
for (j = 0; j<width - mask_width + ((mask_width - 1) / 2); j++)
{
// 초기화 (Initialization)
vertical_var = horizontal_var = 0.0;
if ((i + mask_height > height) || (j + mask_width > width))
continue;
for (m = 0; m<mask_height; m++)
{
for (n = 0; n<mask_width; n++)
{
// 수직 에지 (vertical edge)
vertical_var += src_image.at<uchar>(i + m, j + n) * vertical_mask[m][n];
// 수평 에지 (horizontal edge)
horizontal_var += src_image.at<uchar>(i + m, j + n) * horizontal_mask[m][n];
}
}
vertical_var = fabs(vertical_var);
horizontal_var = fabs(horizontal_var);
ret_var = vertical_var + horizontal_var;
if (method == 0)
{
dst_image.at<uchar>(i + (mask_height - 1) / 2, j + (mask_width - 1) / 2) = vertical_var;
}
else if (method == 1)
{
dst_image.at<uchar>(i + (mask_height - 1) / 2, j + (mask_width - 1) / 2) = horizontal_var;
}
else if (method == 2)
{
dst_image.at<uchar>(i + (mask_height - 1) / 2, j + (mask_width - 1) / 2) = ret_var;
}
}
}
return dst_image;
}
// 소벨 에지 추출 : 대각선 (Extraction sobel edge: diagonal)
cv::Mat CEdge::GS_sobel_diagonal_edge(cv::Mat src_image, int method)
{
int i, j, m, n;
int mask_height, mask_width;
double diagonal_var1, diagonal_var2, ret_var;
CUtil cUtil;
// 초기화 (Initialization)
int height = src_image.rows;
int width = src_image.cols;
cv::Mat dst_image = cUtil.GS_createImage(src_image.size(), src_image.channels());
if (dst_image.channels() != 1) return dst_image;
// 수직/수평 마스크 정의 (define vertical/horizontal mask)
mask_height = mask_width = 3;
int diagonal_mask1[3][3] = { { -2, -1, 0 },{ -1, 0, 1 },{ 0, 1, 2 } };
int diagonal_mask2[3][3] = { { 0, -1, -2 },{ 1, 0, -1 },{ 2, 1, 0 } };
for (i = 0; i<height - mask_height + ((mask_height - 1) / 2); i++)
{
for (j = 0; j<width - mask_width + ((mask_width - 1) / 2); j++)
{
// 초기화 (Initialization)
diagonal_var1 = diagonal_var2 = 0.0;
if ((i + mask_height > height) || (j + mask_width > width))
continue;
for (m = 0; m<mask_height; m++)
{
for (n = 0; n<mask_width; n++)
{
// 대각선 에지 1 (diagonal edge1)
diagonal_var1 += src_image.at<uchar>(i + m, j + n) * diagonal_mask1[m][n];
// 대각선 에지 2 (diagonal edge2)
diagonal_var2 += src_image.at<uchar>(i + m, j + n)*diagonal_mask2[m][n];
}
}
diagonal_var1 = fabs(diagonal_var1);
diagonal_var2 = fabs(diagonal_var2);
ret_var = diagonal_var1 + diagonal_var2;
if (method == 0)
{
dst_image.at<uchar>(i + (mask_height - 1) / 2, j + (mask_width - 1) / 2) = diagonal_var1;
}
else if (method == 1)
{
dst_image.at<uchar>(i + (mask_height - 1) / 2, j + (mask_width - 1) / 2) = diagonal_var2;
}
else if (method == 2)
{
dst_image.at<uchar>(i + (mask_height - 1) / 2, j + (mask_width - 1) / 2) = ret_var;
}
}
}
return dst_image;
}
// 소벨 에지 추출 : 마스크(7x7) (Extraction sobel edge : mask(7x7))
cv::Mat CEdge::GS_sobel_edge_7x7(cv::Mat src_image, int method)
{
int i, j, m, n;
int mask_height, mask_width;
double vertical_var, horizontal_var, ret_var;
CUtil cUtil;
// 초기화 (Initialization)
int height = src_image.rows;
int width = src_image.cols;
cv::Mat dst_image = cUtil.GS_createImage(src_image.size(), src_image.channels());
if (dst_image.channels() != 1) return dst_image;
// 수직/수평 마스크 정의 (define vertical/horizontal mask)
mask_height = mask_width = 7;
int vertical_mask[7][7] = { { -1, -1, -1, 0, 1, 1, 1 },
{ -1, -1, -1, 0, 1, 1, 1 },
{ -1, -1, -1, 0, 1, 1, 1 },
{ -2, -2, -2, 0, 2, 2, 2 },
{ -1, -1, -1, 0, 1, 1, 1 },
{ -1, -1, -1, 0, 1, 1, 1 },
{ -1, -1, -1, 0, 1, 1, 1 } };
int horizontal_mask[7][7] = { { 1, 1, 1, 2, 1, 1, 1 },
{ 1, 1, 1, 2, 1, 1, 1 },
{ 1, 1, 1, 2, 1, 1, 1 },
{ 0, 0, 0, 0, 0, 0, 0 },
{ -1, -1, -1, -2, -1, -1, -1 },
{ -1, -1, -1, -2, -1, -1, -1 },
{ -1, -1, -1, -2, -1, -1, -1 } };
for (i = 0; i<height - mask_height + ((mask_height - 1) / 2); i++)
{
for (j = 0; j<width - mask_width + ((mask_width - 1) / 2); j++)
{
// 초기화 (Initialization)
vertical_var = horizontal_var = 0.0;
if ((i + mask_height > height) || (j + mask_width > width))
continue;
for (m = 0; m<mask_height; m++)
{
for (n = 0; n<mask_width; n++)
{
// 수직 에지 (vertical edge)
vertical_var += src_image.at<uchar>(i + m, j + n) * vertical_mask[m][n];
// 수평 에지 (horizontal edge)
horizontal_var += src_image.at<uchar>(i + m, j + n) * horizontal_mask[m][n];
}
}
vertical_var = fabs(vertical_var);
horizontal_var = fabs(horizontal_var);
ret_var = vertical_var + horizontal_var;
if (method == 0)
{
dst_image.at<uchar>(i + (mask_height - 1) / 2, j + (mask_width - 1) / 2) = vertical_var;
}
else if (method == 1)
{
dst_image.at<uchar>(i + (mask_height - 1) / 2, j + (mask_width - 1) / 2) = horizontal_var;
}
else if (method == 2)
{
dst_image.at<uchar>(i + (mask_height - 1) / 2, j + (mask_width - 1) / 2) = ret_var;
}
}
}
return dst_image;
}
// 소벨 에지 추출 : cv::Sobel() 함수 이용(Extraction sobel edge : using cv::Sobel())
cv::Mat CEdge::GS_sobel_edge_Sobel(cv::Mat src_image, int method)
{
CUtil cUtil;
// 초기화 (Initialization)
cv::Mat dst_image = cUtil.GS_createImage(src_image.size(), src_image.channels());
if (dst_image.channels() != 1) return dst_image;
//
cv::Mat vertical_edge = cUtil.GS_createImage(src_image.size(), CV_32F, src_image.channels());
//
cv::Mat horizontal_edge = cUtil.GS_createImage(src_image.size(), CV_32F, src_image.channels());
// cv::Sobel() 함수 여섯 번째 인자값이 3 이면 가우시안 스무딩 적용
// if the third argument is 3, apply Gaussian smoothing
Sobel(src_image, vertical_edge, -1, 1, 0, 3); // 수직 (vertical)
Sobel(src_image, horizontal_edge, -1, 0, 1, 3); // 수평 (horizontal)
// cv::conertScaleAbs() 함수는 CV_32F를 CV_8U로 변환.
// 0 = 수직 엣지 1 = 수평 엣지 2 = 수직/수평 엣지
if (method == 0)
cv::convertScaleAbs(vertical_edge, dst_image);
else if (method == 1)
cv::convertScaleAbs(horizontal_edge, dst_image);
else if (method == 2)
{
cv::Mat tmp_image1 = dst_image.clone();
cv::Mat tmp_image2 = dst_image.clone();
cv::convertScaleAbs(vertical_edge, tmp_image1);
cv::convertScaleAbs(horizontal_edge, tmp_image2);
cv::add(tmp_image1, tmp_image2, dst_image);
tmp_image1.release();
tmp_image2.release();
}
vertical_edge.release();
horizontal_edge.release();
return dst_image;
}
// 소벨 에지 추출 : 마스크(3x3) (Extraction sobel edge : mask(3x3))
cv::Mat CEdge::GS_sobel_edge(cv::Mat src_image, int method)
{
int i, j, m, n;
int mask_height, mask_width;
double vertical_var, horizontal_var, ret_var;
CUtil cUtil;
// 초기화 (Initialization)
int height = src_image.rows;
int width = src_image.cols;
cv::Mat dst_image = cUtil.GS_createImage(src_image.size(), src_image.channels());
if (dst_image.channels() != 1) return dst_image;
// 수직/수평 마스크 정의 (define vertical/horizontal mask)
mask_height = mask_width = 3;
int vertical_mask[3][3] = { { -1, 0, 1 },{ -2, 0, 2 },{ -1, 0, 1 } };
int horizontal_mask[3][3] = { { 1, 2, 1 },{ 0, 0, 0 },{ -1, -2, -1 } };
for (i = 0; i<height - mask_height + ((mask_height - 1) / 2); i++)
{
for (j = 0; j<width - mask_width + ((mask_width - 1) / 2); j++)
{
// 초기화 (Initialization)
vertical_var = horizontal_var = 0.0;
if ((i + mask_height > height) || (j + mask_width > width))
continue;
for (m = 0; m<mask_height; m++)
{
for (n = 0; n<mask_width; n++)
{
// 수직 에지 (vertical edge)
vertical_var += src_image.at<uchar>(i + m, j + n) * vertical_mask[m][n];
// 수평 에지 (horizontal edge)
horizontal_var += src_image.at<uchar>(i + m, j + n) * horizontal_mask[m][n];
}
}
vertical_var = fabs(vertical_var);
horizontal_var = fabs(horizontal_var);
ret_var = vertical_var + horizontal_var;
if (method == 0)
{
dst_image.at<uchar>(i + (mask_height - 1) / 2, j + (mask_width - 1) / 2) = vertical_var;
}
else if (method == 1)
{
dst_image.at<uchar>(i + (mask_height - 1) / 2, j + (mask_width - 1) / 2) = horizontal_var;
}
else if (method == 2)
{
dst_image.at<uchar>(i + (mask_height - 1) / 2, j + (mask_width - 1) / 2) = ret_var;
}
}
}
return dst_image;
}