-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1110 lines (1003 loc) · 35.4 KB
/
main.cpp
File metadata and controls
1110 lines (1003 loc) · 35.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
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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// histogram.cpp
// BMP
//
// Created by Phil on 2022/03/27.
//
#pragma pack(2)
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#pragma warning(disable:4996)
#include "Header.hpp"
//
//#pragma warning(disable:4996)
//#include <stdio.h>
//#include <stdlib.h>
//#include <Windows.h>
//#include <math.h>
//
// 2차원 배열 동적할당 위함
unsigned char** imageMatrix;
// 이진영상에서
unsigned char blankPixel = 255, imagePixel = 0;
typedef struct {
int row, col;
}pixel;
int getBlackNeighbours(int row, int col) {
int i, j, sum = 0;
for (i = -1; i <= 1; i++) {
for (j = -1; j <= 1; j++) {
if (i != 0 || j != 0)
sum += (imageMatrix[row + i][col + j] == imagePixel);
}
}
return sum;
}
int getBWTransitions(int row, int col) {
return ((imageMatrix[row - 1][col] == blankPixel && imageMatrix[row - 1][col + 1] == imagePixel)
+ (imageMatrix[row - 1][col + 1] == blankPixel && imageMatrix[row][col + 1] == imagePixel)
+ (imageMatrix[row][col + 1] == blankPixel && imageMatrix[row + 1][col + 1] == imagePixel)
+ (imageMatrix[row + 1][col + 1] == blankPixel && imageMatrix[row + 1][col] == imagePixel)
+ (imageMatrix[row + 1][col] == blankPixel && imageMatrix[row + 1][col - 1] == imagePixel)
+ (imageMatrix[row + 1][col - 1] == blankPixel && imageMatrix[row][col - 1] == imagePixel)
+ (imageMatrix[row][col - 1] == blankPixel && imageMatrix[row - 1][col - 1] == imagePixel)
+ (imageMatrix[row - 1][col - 1] == blankPixel && imageMatrix[row - 1][col] == imagePixel));
}
int zhangSuenTest1(int row, int col) {
int neighbours = getBlackNeighbours(row, col);
return ((neighbours >= 2 && neighbours <= 6)
&& (getBWTransitions(row, col) == 1)
&& (imageMatrix[row - 1][col] == blankPixel || imageMatrix[row][col + 1] == blankPixel || imageMatrix[row + 1][col] == blankPixel)
&& (imageMatrix[row][col + 1] == blankPixel || imageMatrix[row + 1][col] == blankPixel || imageMatrix[row][col - 1] == blankPixel));
}
int zhangSuenTest2(int row, int col) {
int neighbours = getBlackNeighbours(row, col);
return ((neighbours >= 2 && neighbours <= 6)
&& (getBWTransitions(row, col) == 1)
&& (imageMatrix[row - 1][col] == blankPixel || imageMatrix[row][col + 1] == blankPixel || imageMatrix[row][col - 1] == blankPixel)
&& (imageMatrix[row - 1][col] == blankPixel || imageMatrix[row + 1][col] == blankPixel || imageMatrix[row][col + 1] == blankPixel));
}
void zhangSuen(unsigned char* image, unsigned char* output, int rows, int cols) {
int startRow = 1, startCol = 1, endRow, endCol, i, j, count, processed;
pixel* markers;
imageMatrix = (unsigned char**)malloc(rows * sizeof(unsigned char*));
for (i = 0; i < rows; i++) {
imageMatrix[i] = (unsigned char*)malloc((cols + 1) * sizeof(unsigned char));
for (int k = 0; k < cols; k++) imageMatrix[i][k] = image[i * cols + k];
}
endRow = rows - 2;
endCol = cols - 2;
do {
markers = (pixel*)malloc((endRow - startRow + 1) * (endCol - startCol + 1) * sizeof(pixel));
count = 0;
for (i = startRow; i <= endRow; i++) {
for (j = startCol; j <= endCol; j++) {
if (imageMatrix[i][j] == imagePixel && zhangSuenTest1(i, j) == 1) {
markers[count].row = i;
markers[count].col = j;
count++;
}
}
}
processed = (count > 0);
for (i = 0; i < count; i++) {
imageMatrix[markers[i].row][markers[i].col] = blankPixel;
}
free(markers);
markers = (pixel*)malloc((endRow - startRow + 1) * (endCol - startCol + 1) * sizeof(pixel));
count = 0;
for (i = startRow; i <= endRow; i++) {
for (j = startCol; j <= endCol; j++) {
if (imageMatrix[i][j] == imagePixel && zhangSuenTest2(i, j) == 1) {
markers[count].row = i;
markers[count].col = j;
count++;
}
}
}
if (processed == 0)
processed = (count > 0);
for (i = 0; i < count; i++) {
imageMatrix[markers[i].row][markers[i].col] = blankPixel;
}
free(markers);
} while (processed == 1);
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
output[i * cols + j] = imageMatrix[i][j];
}
}
}
void InverseImage(BYTE* Img, BYTE *Out, int W, int H)
{
int ImgSize = W * H;
for (int i = 0; i < ImgSize; i++)
{
Out[i] = 255 - Img[i];
}
}
void BrightnessAdj(BYTE* Img, BYTE* Out, int W, int H, int Val)
{
int ImgSize = W * H;
for (int i = 0; i < ImgSize; i++)
{
if (Img[i] + Val > 255)
{
Out[i] = 255;
}
else if (Img[i] + Val < 0)
{
Out[i] = 0;
}
else Out[i] =Img[i] + Val;
}
}
void ContrastAdj(BYTE* Img, BYTE* Out, int W, int H, double Val)
{
int ImgSize = W * H;
for (int i = 0; i < ImgSize; i++)
{
if (Img[i] * Val > 255.0)
{
Out[i] = 255;
}
else Out[i] = (BYTE)(Img[i] * Val);
}
}
void ObtainHistogram(BYTE* Img, int* Histo, int W, int H)
{
int ImgSize = W * H;
for (int i = 0; i < ImgSize; i++) {
Histo[Img[i]]++;
}
}
void ObtainAHistogram(int* Histo, int* AHisto) //누적합
{
for (int i = 0; i < 256; i++) {
for (int j = 0; j <= i; j++) {
AHisto[i] += Histo[j];
}
}
}
void HistogramStretching(BYTE * Img, BYTE * Out, int * Histo, int W, int H)
{
int ImgSize = W * H;
BYTE Low = 0, High = 0;
for (int i = 0; i < 256; i++) {// 가장작은값의 밝기값 low
if (Histo[i] != 0) {
Low = i;
break;
}
}
for (int i = 255; i >= 0; i--) {//가장 큰값의 밝기
if (Histo[i] != 0) {
High = i;
break;
}
}
for (int i = 0; i < ImgSize; i++) {
Out[i] = (BYTE)((Img[i] - Low) / (double)(High - Low) * 255.0); //백분율 같은 느낌 255%만점으로 바꾸는 과정이라 생각하셈
}
}
void HistogramEqualization(BYTE* Img, BYTE* Out, int* AHisto, int W, int H)
{
int ImgSize = W * H;
int Nt = W * H, Gmax = 255;
double Ratio = Gmax / (double)Nt;// 0이랑 1사이의 비율값을 가짐
BYTE NormSum[256];// 정규화된 누적 히스토그램
for (int i = 0; i < 256; i++) {
NormSum[i] = (BYTE)(Ratio * AHisto[i]);// 누적합을 정규화 시켜주는 과정
}
for (int i = 0; i < ImgSize; i++)
{
Out[i] = NormSum[Img[i]];
}
}
void Binarization(BYTE * Img, BYTE * Out, int W, int H, BYTE Threshold)
{
int ImgSize = W * H;
for (int i = 0; i < ImgSize; i++) {
if (Img[i] < Threshold) Out[i] = 0;
else Out[i] = 255;
}
}
int GonzalezBinThresh(BYTE * Output, int * Histo, int ImgSize )
{
int T;
int eps = 3; //diffrence of T and T_next under eps -> exit
int diff = eps + 1;//First setting
int Low = 0 , High = 0;
for (int i = 0; i < 256; i++) {
if (Histo[i] != 0) {
Low = i;
break;
}
}
for (int i = 255; i>= 0; i--) {
if (Histo[i] != 0) {
High = i;
break;
}
}
T = Low + High / 2;
printf("First Thres : %d\n",T);
while (diff > eps){
int T_next;
int sumlow=0;
int sumhigh=0;
int countl = 0;
int counth = 0;
for ( int i=0; i< 256 ; i++){
if (i<T){
sumlow += Histo[i] * i;
countl += Histo[i];
}
else{
sumhigh += Histo[i]*i;
counth += Histo[i];
}
}
T_next = (int)((double)sumlow/countl + (double)sumhigh/counth)/2 ;
printf("Next Thres : %d\n",T_next);
diff = abs(T - T_next);
T = T_next;
}
return T;
}
void AverageConv(BYTE* Img, BYTE* Out, int W, int H) // 박스평활화
{
double Kernel[3][3] = {0.11111, 0.11111, 0.11111,
0.11111, 0.11111, 0.11111,
0.11111, 0.11111, 0.11111 };
double SumProduct = 0.0;
for (int i = 1; i < H-1; i++) { // Y좌표 (행)
for (int j = 1; j < W-1; j++) { // X좌표 (열)
for (int m = -1; m <= 1; m++) { // Kernel 행
for (int n = -1; n <= 1; n++) { // Kernel 열
SumProduct += Img[(i+m)*W + (j+n)] * Kernel[m+1][n+1];
}
}
Out[i * W + j] = (BYTE)SumProduct;
SumProduct = 0.0;
}
}
}
void GaussAvrConv(BYTE* Img, BYTE* Out, int W, int H) // 가우시안평활화
{
double Kernel[3][3] = {0.0625, 0.125, 0.0625,
0.125, 0.25, 0.125,
0.0625, 0.125, 0.0625 };
double SumProduct = 0.0;
for (int i = 1; i < H - 1; i++) { // Y좌표 (행)
for (int j = 1; j < W - 1; j++) { // X좌표 (열)
for (int m = -1; m <= 1; m++) { // Kernel 행
for (int n = -1; n <= 1; n++) { // Kernel 열
SumProduct += Img[(i + m) * W + (j + n)] * Kernel[m + 1][n + 1];
}
}
Out[i * W + j] = (BYTE)SumProduct;
SumProduct = 0.0;
}
}
}
void Prewitt_X_Conv(BYTE* Img, BYTE* Out, int W, int H) // Prewitt 마스크 X
{
double Kernel[3][3] = { -1.0, 0.0, 1.0,
-1.0, 0.0, 1.0,
-1.0, 0.0, 1.0 };
double SumProduct = 0.0;
for (int i = 1; i < H - 1; i++) { // Y좌표 (행)
for (int j = 1; j < W - 1; j++) { // X좌표 (열)
for (int m = -1; m <= 1; m++) { // Kernel 행
for (int n = -1; n <= 1; n++) { // Kernel 열
SumProduct += Img[(i + m) * W + (j + n)] * Kernel[m + 1][n + 1];
}
}
// 0 ~ 765 =====> 0 ~ 255
Out[i * W + j] = abs((long)SumProduct) / 3;
SumProduct = 0.0;
}
}
}
void Prewitt_Y_Conv(BYTE* Img, BYTE* Out, int W, int H) // Prewitt 마스크 X
{
double Kernel[3][3] = { -1.0, -1.0, -1.0,
0.0, 0.0, 0.0,
1.0, 1.0, 1.0 };
double SumProduct = 0.0;
for (int i = 1; i < H - 1; i++) { // Y좌표 (행)
for (int j = 1; j < W - 1; j++) { // X좌표 (열)
for (int m = -1; m <= 1; m++) { // Kernel 행
for (int n = -1; n <= 1; n++) { // Kernel 열
SumProduct += Img[(i + m) * W + (j + n)] * Kernel[m + 1][n + 1];
}
}
// 0 ~ 765 =====> 0 ~ 255
Out[i * W + j] = abs((long)SumProduct) / 3;
SumProduct = 0.0;
}
}
}
void Sobel_X_Conv(BYTE* Img, BYTE* Out, int W, int H) // Prewitt 마스크 X
{
double Kernel[3][3] = { -1.0, 0.0, 1.0,
-2.0, 0.0, 2.0,
-1.0, 0.0, 1.0 };
double SumProduct = 0.0;
for (int i = 1; i < H - 1; i++) { // Y좌표 (행)
for (int j = 1; j < W - 1; j++) { // X좌표 (열)
for (int m = -1; m <= 1; m++) { // Kernel 행
for (int n = -1; n <= 1; n++) { // Kernel 열
SumProduct += Img[(i + m) * W + (j + n)] * Kernel[m + 1][n + 1];
}
}
// 0 ~ 1020 =====> 0 ~ 255
Out[i * W + j] = abs((long)SumProduct) / 4;
SumProduct = 0.0;
}
}
}
void Sobel_Y_Conv(BYTE* Img, BYTE* Out, int W, int H) // Prewitt 마스크 X
{
double Kernel[3][3] = { -1.0, -2.0, -1.0,
0.0, 0.0, 0.0,
1.0, 2.0, 1.0 };
double SumProduct = 0.0;
for (int i = 1; i < H - 1; i++) { // Y좌표 (행)
for (int j = 1; j < W - 1; j++) { // X좌표 (열)
for (int m = -1; m <= 1; m++) { // Kernel 행
for (int n = -1; n <= 1; n++) { // Kernel 열
SumProduct += Img[(i + m) * W + (j + n)] * Kernel[m + 1][n + 1];
}
}
// 0 ~ 765 =====> 0 ~ 255
Out[i * W + j] = abs((long)SumProduct) / 4;
SumProduct = 0.0;
}
}
}
void Laplace_Conv(BYTE* Img, BYTE* Out, int W, int H) // Prewitt 마스크 X
{
double Kernel[3][3] = { -1.0, -1.0, -1.0,
-1.0, 8.0, -1.0,
-1.0, -1.0, -1.0 };
double SumProduct = 0.0;
for (int i = 1; i < H - 1; i++) { // Y좌표 (행)
for (int j = 1; j < W - 1; j++) { // X좌표 (열)
for (int m = -1; m <= 1; m++) { // Kernel 행
for (int n = -1; n <= 1; n++) { // Kernel 열
SumProduct += Img[(i + m) * W + (j + n)] * Kernel[m + 1][n + 1];
}
}
// 0 ~ 2040 =====> 0 ~ 255
Out[i * W + j] = abs((long)SumProduct) / 8;
SumProduct = 0.0;
}
}
}
void Laplace_Conv_DC(BYTE* Img, BYTE* Out, int W, int H) // Prewitt 마스크 X
{
double Kernel[3][3] = { -1.0, -1.0, -1.0,
-1.0, 9.0, -1.0,
-1.0, -1.0, -1.0 };
double SumProduct = 0.0;
for (int i = 1; i < H - 1; i++) { // Y좌표 (행)
for (int j = 1; j < W - 1; j++) { // X좌표 (열)
for (int m = -1; m <= 1; m++) { // Kernel 행
for (int n = -1; n <= 1; n++) { // Kernel 열
SumProduct += Img[(i + m) * W + (j + n)] * Kernel[m + 1][n + 1];
}
}
//Out[i * W + j] = abs((long)SumProduct) / 8;
if (SumProduct > 255.0) Out[i * W + j] = 255;
else if (SumProduct < 0.0) Out[i * W + j] = 0;
else Out[i * W + j] = (BYTE)SumProduct;
SumProduct = 0.0;
}
}
}
void Prewitt_BOTH_Conv(BYTE* Img,BYTE* temp, BYTE* Out, int W, int H)
{
for( int i = 1; i < W * H; i++){
if ( Img[i] < temp[i]){
Out[i] = temp[i];
}
else Out[i] = Img[i];
}
}
void swap(BYTE* a, BYTE* b)
{
BYTE temp = *a;
*a = *b;
*b = temp;
}
BYTE Median(BYTE* arr, int size)
{
const int S = size;
for (int i = 0; i < size - 1; i++) // pivot index
{
for (int j = i + 1; j < size; j++) // object index
{
if (arr[i] > arr[j]) swap(&arr[i], &arr[j]);
}
}
return arr[S/2];
}
BYTE MaxPooling(BYTE* arr, int size)
{
const int S = size;
for (int i = 0; i < size - 1; i++) // pivot index
{
for (int j = i + 1; j < size; j++)
{
if (arr[i] > arr[j]) swap(&arr[i], &arr[j]);
}
}
return arr[S-1];
}
BYTE MinPooling(BYTE* arr, int size)
{
for (int i = 0; i < size - 1; i++) // pivot index
{
for (int j = i + 1; j < size; j++)
{
if (arr[i] > arr[j]) swap(&arr[i], &arr[j]);
}
}
return arr[0];
}
int push(short* stackx, short* stacky, int arr_size, short vx, short vy, int* top)
{
if (*top >= arr_size) return(-1);
(*top)++;
stackx[*top] = vx;
stacky[*top] = vy;
return(1);
}
int pop(short* stackx, short* stacky, short* vx, short* vy, int* top)
{
if (*top == 0) return(-1);
*vx = stackx[*top];
*vy = stacky[*top];
(*top)--;
return(1);
}
// GlassFire 알고리즘을 이용한 라벨링 함수
void m_BlobColoring(BYTE* CutImage, int height, int width) // gㅣassfire algorythm 스택형. 구조는 알고있되 외울 필요는없다.
{
int i, j, m, n, top, area, Out_Area, index, BlobArea[1000];
long k;
short curColor = 0, r, c;
// BYTE** CutImage2;
Out_Area = 1;
// 스택으로 사용할 메모리 할당
short* stackx = new short[height * width];
short* stacky = new short[height * width];
short* coloring = new short[height * width];
int arr_size = height * width;
// 라벨링된 픽셀을 저장하기 위해 메모리 할당
for (k = 0; k < height * width; k++) coloring[k] = 0; // 메모리 초기화
for (i = 0; i < height; i++)
{
index = i * width;
for (j = 0; j < width; j++)
{
// 이미 방문한 점이거나 픽셀값이 255가 아니라면 처리 안함
if (coloring[index + j] != 0 || CutImage[index + j] != 255) continue;
r = i; c = j; top = 0; area = 1;
curColor++;
while (1)
{
GRASSFIRE:
for (m = r - 1; m <= r + 1; m++)
{
index = m * width;
for (n = c - 1; n <= c + 1; n++)
{
//관심 픽셀이 영상경계를 벗어나면 처리 안함
if (m < 0 || m >= height || n < 0 || n >= width) continue;
if ((int)CutImage[index + n] == 255 && coloring[index + n] == 0)
{
coloring[index + n] = curColor; // 현재 라벨로 마크
if (push(stackx, stacky, arr_size, (short)m, (short)n, &top) == -1) continue;
r = m; c = n; area++;
goto GRASSFIRE;
}
}
}
if (pop(stackx, stacky, &r, &c, &top) == -1) break;
}
if (curColor < 1000) BlobArea[curColor] = area;
}
}
float grayGap = 255.0f / (float)curColor;
// 가장 면적이 넓은 영역을 찾아내기 위함
for (i = 1; i <= curColor; i++)
{
if (BlobArea[i] >= BlobArea[Out_Area]) Out_Area = i;// 가장 넓은 영역에 해당하는 레이블넘버가 들어감
}
// CutImage 배열 클리어~ㄱ하얗게
for (k = 0; k < width * height; k++) CutImage[k] = 255;
// coloring에 저장된 라벨링 결과중 (Out_Area에 저장된) 영역이 가장 큰 것만 CutImage에 저장
for (k = 0; k < width * height; k++)
{
// 용도에 맞게 3개중에 하나만 선택
if (coloring[k] == Out_Area) CutImage[k] = 0; // 가장 큰 것만 저장 (size filtering)
//if (BlobArea[coloring[k]] > 500) CutImage[k] = 0; // 특정 면적이상되는 영역만 출력
//CutImage[k] = (unsigned char)(coloring[k] * grayGap);
}
delete[] coloring;
delete[] stackx;
delete[] stacky;
}
// 라벨링 후 가장 넓은 영역에 대해서만 뽑아내는 코드 포함
void BinaryImageEdgeDetection(BYTE* Bin, BYTE* Out, int W, int H)
{
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (Bin[i * W + j] == 0) // 전경화소라면
{
if (!(Bin[(i - 1) * W + j] == 0 && Bin[(i + 1) * W + j] == 0 &&
Bin[i * W + j - 1] == 0 && Bin[i * W + j + 1] == 0)) // 4방향 화소 중 하나라도 전경이 아니라면
Out[i * W + j] = 255;
}
}
}
}
BYTE DetermThGonzalez(int* H)
{
BYTE ep = 3;
BYTE Low, High;
BYTE Th, NewTh;
int G1 = 0, G2 = 0, cnt1 = 0, cnt2 = 0, mu1, mu2;
for (int i = 0; i < 256; i++) {
if (H[i] != 0) {
Low = i;
break;
}
}
for (int i = 255; i >= 0; i--) {
if (H[i] != 0) {
High = i;
break;
}
}
Th = (Low + High) / 2;
//printf("%d\n", Th);
while(1)
{
for (int i = Th + 1; i <= High; i++) {
G1 += (H[i] * i);
cnt1 += H[i];
}
for (int i = Low; i <= Th; i++) {
G2 += (H[i] * i);
cnt2 += H[i];
}
if (cnt1 == 0) cnt1 = 1;
if (cnt2 == 0) cnt2 = 1;
mu1 = G1 / cnt1;
mu2 = G2 / cnt2;
NewTh = (mu1 + mu2) / 2;
if (abs(NewTh - Th) < ep)
{
Th = NewTh;
break;
}
else
{
Th = NewTh;
}
G1 = G2 = cnt1 = cnt2 = 0;
//printf("%d\n", Th);
}
return Th;
}
void VerticalFlip(BYTE * Img, int W, int H)
{
for (int i = 0; i < H / 2; i++) { // y좌표
for (int j = 0; j < W; j++) { // x좌표
swap(&Img[i*W + j], &Img[(H-1-i)*W + j]);
}
}
}
void HorizontalFlip(BYTE* Img, int W, int H)
{
for (int i = 0; i < W / 2; i++) { // x좌표
for (int j = 0; j < H; j++) { // y좌표
swap(&Img[j * W + i], &Img[j * W + (W-1-i)]);
}
}
}
void Translation(BYTE* Image, BYTE* Output, int W, int H, int Tx, int Ty)
{
// Translation
Ty *= -1;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if ((i + Ty < H && i + Ty >= 0) && (j + Tx < W && j + Tx >= 0))
Output[(i + Ty) * W + (j + Tx)] = Image[i * W + j];
}
}
}
void Scaling(BYTE* Image, BYTE* Output, int W, int H, double SF_X, double SF_Y)
{
// Scaling
int tmpX, tmpY;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
tmpX = (int)(j / SF_X);
tmpY = (int)(i / SF_Y);
if (tmpY < H && tmpX < W)
Output[i * W + j] = Image[tmpY * W + tmpX];
}
}
}
void Rotation(BYTE* Image, BYTE* Output, int W, int H, int Angle)
{
int tmpX, tmpY;
double Radian = Angle * 3.141592 / 180.0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
tmpX = (int)(cos(Radian) * j + sin(Radian) * i);
tmpY = (int)(-sin(Radian) * j + cos(Radian) * i);
if ((tmpY < H && tmpY >= 0) && (tmpX < W && tmpX >= 0))
Output[i * W + j] = Image[tmpY * W + tmpX];
}
}
}
void MedianFiltering(BYTE* Image, BYTE* Output, int W, int H, int size)
{
int Length = size; // 마스크의 한 변의 길이
int Margin = Length / 2;
int WSize = Length * Length;
BYTE* temp = (BYTE*)malloc(sizeof(BYTE) * WSize);
int i, j, m, n;
for (i = Margin; i < H - Margin; i++) {
for (j = Margin; j < W - Margin; j++) {
for (m = -Margin; m <= Margin; m++) {
for (n = -Margin; n <= Margin; n++) {
temp[(m + Margin) * Length + (n + Margin)] = Image[(i + m) * W + j + n];
}
}
Output[i * W + j] = Median(temp, WSize);
}
}
free(temp);
}
void FillColor(BYTE* Image, int X, int Y, int W, int H, BYTE R, BYTE G, BYTE B)
{
Image[Y * W * 3 + X * 3] = B; // Blue 성분
Image[Y * W * 3 + X * 3 + 1] = G; // Green 성분
Image[Y * W * 3 + X * 3 + 2] = R; // Red 성분
}
// Img: 사각형을 그릴 이미지배열, W: 영상 가로사이즈, H: 영상 세로사이즈,
// LU_X: 사각형의 좌측상단 X좌표, LU_Y: 사각형의 좌측상단 Y좌표,
// RD_X: 사각형의 우측하단 X좌표, LU_Y: 사각형의 우측하단 Y좌표.
void DrawRectOutline(BYTE* Img, int W, int H, int LU_X, int LU_Y, int RD_X, int RD_Y)
{
for (int i = LU_X; i < RD_X; i++) FillColor(Img, i, LU_Y, W, H, 255, 0, 0);
for (int i = LU_X; i < RD_X; i++) FillColor(Img, i, RD_Y, W, H, 255, 0, 0);
for (int i = LU_Y; i < RD_Y; i++) FillColor(Img, LU_X, i, W, H, 255, 0, 0);
for (int i = LU_Y; i < RD_Y; i++) FillColor(Img, RD_X, i, W, H, 255, 0, 0);
}
// Img: 가로/세로 라인을 그릴 이미지배열, W: 영상 가로사이즈, H: 영상 세로사이즈,
// Cx: 가로/세로 라인이 교차되는 지점의 X좌표
// Cy: 가로/세로 라인이 교차되는 지점의 Y좌표
void DrawCrossLine(BYTE* Img, int W, int H, int Cx, int Cy)
{
for (int i = 0; i < W - 1; i++) // horizontal line
Img[Cy * W + i] = 255;
for (int i = 0; i < H - 1; i++) // vertical line
Img[i * W + Cx] = 255;
}
void Obtain2DCenter(BYTE* Image, int W, int H, int* Cx, int* Cy)
{
int SumX = 0, SumY = 0;
int cnt = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (Image[i * W + j] == 0) // 동공영역이면
{
SumX += j;
SumY += i;
cnt++;
}
}
}
if (cnt == 0) cnt = 1;
*Cx = SumX / cnt;
*Cy = SumY / cnt;
//printf("%d %d\n", Cx, Cy);
}
void Obtain2DBoundingBox(BYTE* Image, int W, int H, int* LUX, int* LUY, int* RDX, int* RDY)
{
int flag = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (Image[i*3 * W + j*3] != 255) {
*LUY = i;
flag = 1;
break;
}
}
if (flag == 1) break;
}
flag = 0;
for (int i = H - 1; i >= 0; i--) {
for (int j = 0; j < W; j++) {
if (Image[i*3 * W + j*3] != 255) {
*RDY = i;
flag = 1;
break;
}
}
if (flag == 1) break;
}
flag = 0;
for (int j = 0; j < W; j++) {
for (int i = 0; i < H; i++) {
if (Image[i * W + j] != 255) {
*LUX = j;
flag = 1;
break;
}
}
if (flag == 1) break;
}
flag = 0;
for (int j = W - 1; j >= 0; j--) {
for (int i = 0; i < H; i++) {
if (Image[i * W + j] != 255) {
*RDX = j;
flag = 1;
break;
}
}
if (flag == 1) break;
}
}
void RGB2YCbCr(BYTE* Image, BYTE* Y, BYTE* Cb, BYTE* Cr, int W, int H)
{
for (int i = 0; i < H; i++) { // Y좌표
for (int j = 0; j < W; j++) { // X좌표
Y[i * W + j] = (BYTE)(0.299 * Image[i * W * 3 + j * 3 + 2] + 0.587 * Image[i * W * 3 + j * 3 + 1] + 0.114 * Image[i * W * 3 + j * 3]);
Cb[i * W + j] = (BYTE)(-0.16874 * Image[i * W * 3 + j * 3 + 2] -0.3313 * Image[i * W * 3 + j * 3 + 1] + 0.5 * Image[i * W * 3 + j * 3] + 128.0);
Cr[i * W + j] = (BYTE)(0.5 * Image[i * W * 3 + j * 3 + 2] - 0.4187 * Image[i * W * 3 + j * 3 + 1] - 0.0813 * Image[i * W * 3 + j * 3] + 128.0);
}
}
}
void Erosion(BYTE* Image, BYTE* Output, int W, int H)
{
for (int i = 1; i < H - 1; i++) {
for (int j = 1; j < W - 1; j++) {
if (Image[i * W + j] == 255) // 전경화소라면
{
if (!(Image[(i - 1) * W + j] == 255 &&
Image[(i + 1) * W + j] == 255 &&
Image[i * W + j - 1] == 255 &&
Image[i * W + j + 1] == 255)) // 4주변화소가 모두 전경화소가 아니라면
Output[i * W + j] = 0;
else Output[i * W + j] = 255;
}
else Output[i * W + j] = 0;
}
}
}
void Dilation(BYTE* Image, BYTE* Output, int W, int H)
{
for (int i = 1; i < H - 1; i++) {
for (int j = 1; j < W - 1; j++) {
if (Image[i * W + j] == 0) // 배경화소라면
{
if (!(Image[(i - 1) * W + j] == 0 &&
Image[(i + 1) * W + j] == 0 &&
Image[i * W + j - 1] == 0 &&
Image[i * W + j + 1] == 0)) // 4주변화소가 모두 배경화소가 아니라면
Output[i * W + j] = 255;
else Output[i * W + j] = 0;
}
else Output[i * W + j] = 255;
}
}
}
void FeatureExtractThinImage(BYTE * Image, BYTE * Output, int W, int H){//parameter -> Image: 세선화된 이미지,Output:분기점과 끝점 표시된 이미지
for (int i = 1; i < H - 1; i++) {
for (int j = 1; j < W - 1; j++) {
if (Image[i * W + j] == 0) // 전경화소라면
{ //해당화소의 8주변화소 배경->전경 변화 check
int count = 0;
if (Image[(i - 1) * W + j - 1] != Image[i * W + j - 1]) count++;
if (Image[i * W + j - 1] != Image[(i + 1) * W + j - 1]) count++;
if (Image[(i+1) * W + j - 1] != Image[(i+1) * W + j ]) count++;
if (Image[(i+1) * W + j ] != Image[(i + 1) * W + j + 1]) count++;
if (Image[(i+1) * W + j + 1] != Image[i * W + j + 1]) count++;
if (Image[i * W + j + 1] != Image[(i - 1) * W + j + 1]) count++;
if (Image[(i-1) * W + j + 1] != Image[(i - 1) * W + j ]) count++;
if (Image[(i-1) * W + j ] != Image[(i - 1) * W + j - 1]) count++;
if ((count/2)%2 == 1) Output[i * W + j] = 128; // 변화 횟수가 홀수일때 분기점 혹은 끝점
}
else Output[i * W + j] = 255;// 배경
}
}
}
void SaveBMPFile(BITMAPFILEHEADER hf, BITMAPINFOHEADER hInfo,
RGBQUAD* hRGB, BYTE* Output, int W, int H, const char* FileName)
{
FILE * fp = fopen(FileName, "wb");
if (hInfo.biBitCount == 24) {
fwrite(&hf, sizeof(BYTE), sizeof(BITMAPFILEHEADER), fp);
fwrite(&hInfo, sizeof(BYTE), sizeof(BITMAPINFOHEADER), fp);
fwrite(Output, sizeof(BYTE), W * H * 3, fp);
}
else if (hInfo.biBitCount == 8) {
fwrite(&hf, sizeof(BYTE), sizeof(BITMAPFILEHEADER), fp);
fwrite(&hInfo, sizeof(BYTE), sizeof(BITMAPINFOHEADER), fp);
fwrite(hRGB, sizeof(RGBQUAD), 256, fp);
fwrite(Output, sizeof(BYTE), W * H, fp);
}
fclose(fp);
}
int main()
{
BITMAPFILEHEADER hf; // 14바이트
BITMAPINFOHEADER hInfo; // 40바이트
RGBQUAD hRGB[256]; // 1024바이트
FILE* fp;
fp = fopen("dilation.bmp", "rb");
if (fp == NULL) {
printf("File not found!\n");
return -1;
}
fread(&hf, sizeof(BITMAPFILEHEADER), 1, fp);
fread(&hInfo, sizeof(BITMAPINFOHEADER), 1, fp);
int ImgSize = hInfo.biWidth * hInfo.biHeight;
int H = hInfo.biHeight, W = hInfo.biWidth;
BYTE* Image;
BYTE* Output;
if (hInfo.biBitCount == 24) { // 트루컬러
Image = (BYTE*)malloc(ImgSize * 3);
Output = (BYTE*)malloc(ImgSize * 3);
fread(Image, sizeof(BYTE), ImgSize * 3, fp);
}
else { // 인덱스(그레이)
fread(hRGB, sizeof(RGBQUAD), 256, fp);
Image = (BYTE*)malloc(ImgSize);
Output = (BYTE*)malloc(ImgSize);
fread(Image, sizeof(BYTE), ImgSize, fp);
}
fclose(fp);
// int Histo[256] = { 0 };
// int AHisto[256] = { 0 };
// (50, 40)위치를 특정 색상으로
/*for (int i = 0; i < W; i++) {
FillColor(Image, i, 200, W, H, 0, 255, 255);
}*/
// (50, 100) ~ (300, 400) 박스 채우기
/*for (int i = 100; i <= 400; i++) {
for (int j = 50; j <= 300; j++) {
FillColor(Image, j, i, W, H, 255, 0, 255);
}
}*/
// 가로 띠 만들기
/*
// 초기화
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
Image[i * W * 3 + j * 3] = 0;
Image[i * W * 3 + j * 3 + 1] = 0;
Image[i * W * 3 + j * 3 + 2] = 0;
}
}
// y좌표 기준 0~239 (Red)
for (int i = 0; i < 240; i++) {
for (int j = 0; j < W; j++) {
Image[i*W*3 + j*3 + 2] = 255;
}
}
// y좌표 기준 120 ~ 359 (Green)
for (int i = 120; i < 360; i++) {
for (int j = 0; j < W; j++) {
Image[i * W * 3 + j * 3 + 1] = 255;
}
}
// y좌표 기준 240 ~ 479 (Blue)
for (int i = 240; i < 480; i++) {