-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathImage.cpp
6144 lines (4967 loc) · 132 KB
/
Image.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
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
// Image.cpp : implementation of the Image Processing Subroutines
//
#include "stdafx.h"
#include "ImageProcessing.h"
#include "MainFrm.h" // 추가
#include "ChildFrm.h" // 추가
#include "ImageProcessingDoc.h" // 추가
#include "ImageProcessingView.h" // 추가
#include <cmath> // 추가
#include "opencv2\core\core.hpp"
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv2\features2d\features2d.hpp"
#include "opencv2\nonfree\nonfree.hpp"
#include "opencv2\nonfree\features2d.hpp"
#include "opencv2\calib3d\calib3d.hpp"
#include "opencv2\video\tracking.hpp"
#include<vector>
using namespace cv;
// 거리 정렬을 위한 Class
class Distance{
public:
double distance;
int i;
double x_pre, y_pre;
double x, y;
Distance(double distance, int i, double x_pre, double y_pre, double x, double y){
this->distance = distance;
this->i = i;
this->x_pre = x_pre;
this->y_pre = y_pre;
this->x = x;
this->y = y;
}
bool operator <(const Distance &a) const {
return this->distance < a.distance;
}
};
// 2차원 메모리 할당
BYTE **cmatrix(int nH, int nW) {
BYTE **Temp;
Temp = new BYTE *[nH];
for(int y = 0 ; y < nH ; y++)
Temp[y] = new BYTE[nW];
return Temp;
}
// 2차원 메모리 해제
void free_cmatrix(BYTE **Image, int nH, int nW) {
for(int y = 0 ; y < nH ; y++)
delete [] Image[y];
delete [] Image;
}
// 2차원 메모리 할당
int **imatrix(int nH, int nW) {
int **Temp;
Temp = new int *[nH];
for(int y = 0 ; y < nH ; y++)
Temp[y] = new int[nW];
return Temp;
}
// 2차원 메모리 해제
void free_imatrix(int **Image, int nH, int nW) {
for(int y = 0 ; y < nH ; y++)
delete [] Image[y];
delete [] Image;
}
// 2차원 메모리 할당
double **dmatrix(int nH, int nW) {
double **Temp;
Temp = new double *[nH];
for(int y = 0 ; y < nH ; y++)
Temp[y] = new double[nW];
return Temp;
}
// 2차원 메모리 해제
void free_dmatrix(double **Image, int nH, int nW) {
for(int y = 0 ; y < nH ; y++)
delete [] Image[y];
delete [] Image;
}
// 24비트 비트맵 영상의 크기 계산
int GetBmp24Size(int nW, int nH) {
return (nW*3+3)/4*4 * nH;
}
// 24비트 비트맵 영상의 데이터 위치를 계산
int GetBmp24Pos(int nW, int nH, int x, int y) {
return (nW*3+3)/4*4 * (nH-1-y) + x*3;
}
// 비트맵 파일 읽기
BYTE *ReadBmp(LPCTSTR FileName, int *pW, int *pH) {
BITMAPFILEHEADER bmiFileHeader;
BITMAPINFOHEADER bmiHeader;
CFile cfile;
BOOL bOpen;
int nColors;
// 파일 열기
bOpen = cfile.Open(FileName, CFile::modeRead | CFile::typeBinary);
// 파일 열기 실패
if(!bOpen) return NULL;
// 파일 헤더 정보 읽기
cfile.Read(&bmiFileHeader, sizeof(BITMAPFILEHEADER));
// 식별자 인식
if(bmiFileHeader.bfType != 'M'*0x0100 + 'B') {
cfile.Close();
return NULL;
}
// 비트맵 정보 헤더 읽기
cfile.Read(&bmiHeader, sizeof(BITMAPINFOHEADER));
// 압축 여부 확인 - 압축되어 있다면 종료
if(bmiHeader.biCompression != BI_RGB) {
cfile.Close();
return NULL;
}
// 만일 Colors이 0이면 화소당 비트수에 대한 최대 컬러 개수
if(bmiHeader.biClrUsed == 0)
nColors = 1 << bmiHeader.biBitCount;
else
nColors = bmiHeader.biClrUsed;
// 팔레트
RGBQUAD *Palette = NULL;
// 팔레트 정보 읽기
switch (bmiHeader.biBitCount) {
case 24:
break;
case 1:
case 4:
case 8:
Palette = new RGBQUAD[nColors];
int i;
for(i = 0 ; i < nColors ; i++) {
BYTE r, g, b, temp;
cfile.Read(&b, sizeof(BYTE));
cfile.Read(&g, sizeof(BYTE));
cfile.Read(&r, sizeof(BYTE));
cfile.Read(&temp, sizeof(BYTE));
Palette[i].rgbRed = r;
Palette[i].rgbGreen = g;
Palette[i].rgbBlue = b;
}
break;
}
// 비트맵 데이터 위치로 이동
cfile.Seek(bmiFileHeader.bfOffBits, CFile::begin);
*pW = bmiHeader.biWidth;
*pH = bmiHeader.biHeight;
BYTE *Image1D = NULL;
// 1차원 메모리 할당
Image1D = new BYTE[GetBmp24Size(*pW, *pH)];
// 24비트 컬러
if(bmiHeader.biBitCount == 24)
cfile.Read(Image1D, *pH*((*pW*3+3)/4*4));
long Row, Col;
// 1, 4, 8 비트 컬러
for(Row = 0 ; Row < bmiHeader.biHeight ; Row++)
{
if(bmiHeader.biBitCount != 24)
{
int BitCount = 0;
UINT mask = (1 << bmiHeader.biBitCount) - 1;
BYTE ReadByte = 0;
int ReadByteCnt = 0;
for(Col = 0; Col < *pW ; Col++)
{
int PaletteIndex = 0;
if (BitCount <= 0) {
BitCount = 8;
cfile.Read(&ReadByte, sizeof(BYTE));
ReadByteCnt++;
}
BitCount -= bmiHeader.biBitCount;
// 팔레트 인덱스 번호 계산
PaletteIndex = (ReadByte >> BitCount) & mask;
int Pos;
// 24비트 컬러로 저장
Pos = (((*pW*3+3)/4*4) * Row) + Col*3;
Image1D[Pos++] = Palette[PaletteIndex].rgbBlue;
Image1D[Pos++] = Palette[PaletteIndex].rgbGreen;
Image1D[Pos] = Palette[PaletteIndex].rgbRed;
}
// 윗줄의 위치 결정
while (ReadByteCnt&3)
{
char temp;
cfile.Read(&temp, sizeof(char));
ReadByteCnt++;
}
}
}
if(Palette) delete [] Palette;
cfile.Close();
return Image1D;
}
// raw 파일 읽기
BYTE *ReadRaw(LPCTSTR FileName, int nW, int nH) {
BYTE *ReadBytes;
int x, y, Pos;
CFile cfile;
BOOL bOpen;
bOpen = cfile.Open(FileName, CFile::modeRead | CFile::typeBinary);
// 파일 열기 실패
if(!bOpen) return NULL;
BYTE *Image1D;
// 메모리 할당
Image1D = new BYTE[GetBmp24Size(nW, nH)];
ReadBytes = new BYTE[nW];
// 1차원 비트맵 구조로 회색조 raw 영상 읽기
for(y = 0 ; y < nH ; y++)
{
cfile.Read(ReadBytes, nW);
for(x = 0 ; x < nW ; x++)
{
Pos = GetBmp24Pos(nW, nH, x, y);
Image1D[Pos++] = ReadBytes[x];
Image1D[Pos++] = ReadBytes[x];
Image1D[Pos] = ReadBytes[x];
}
}
delete [] ReadBytes;
cfile.Close();
return Image1D;
}
// 비트맵 파일 저장(24비트 컬러)
bool SaveBmp(LPCTSTR FileName, BYTE *Image1D, int nW, int nH) {
unsigned long dwBitsSize;
unsigned long size;
size = GetBmp24Size(nW, nH);
dwBitsSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + size;
// 헤더 정보 저장
BITMAPINFOHEADER bmiHeader;
bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmiHeader.biWidth = nW;
bmiHeader.biHeight = nH;
bmiHeader.biPlanes = 1;
bmiHeader.biBitCount = 24;
bmiHeader.biCompression = BI_RGB;
bmiHeader.biSizeImage = size;
bmiHeader.biXPelsPerMeter = 2000;
bmiHeader.biYPelsPerMeter = 2000;
bmiHeader.biClrUsed = 0;
bmiHeader.biClrImportant = 0;
BITMAPFILEHEADER bmiFileHeader;
bmiFileHeader.bfType = 'M'*0x0100 + 'B';
bmiFileHeader.bfSize = dwBitsSize;
bmiFileHeader.bfReserved1 = 0;
bmiFileHeader.bfReserved2 = 0;
bmiFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
CFile cfile;
BOOL bOpen;
// 파일 열기
bOpen = cfile.Open(FileName, CFile::modeWrite | CFile::typeBinary | CFile::modeCreate);
// 파일 열기 실패
if(!bOpen) return false;
// 헤더 쓰기
cfile.Write(&bmiFileHeader, sizeof(BITMAPFILEHEADER));
cfile.Write(&bmiHeader, sizeof(BITMAPINFOHEADER));
// 비트맵 정보 쓰기
cfile.Write(Image1D, sizeof(BYTE)*size);
cfile.Close();
return true;
}
// 마지막 영상의 폭, 높이, 위치 읽기
bool GetCurrentImageInfo(int *pW, int *pH, int *pPosX, int *pPosY, int nIndex)
{
CMainFrame *pMain = (CMainFrame *)AfxGetMainWnd();
CChildFrame *pChild = (CChildFrame *)pMain->MDIGetActive();
if(!pChild) return false;
CImageProcessingView *pView = (CImageProcessingView *)pChild->GetActiveView();
if(!pView) return false;
return pView->GetCurrentImageInfo(pW, pH, pPosX, pPosY, nIndex);
}
// 마지막 영상을 2차원 회색조 정보로 읽기
bool GetCurrentImageGray(BYTE **ImageGray, int nIndex)
{
CMainFrame *pMain = (CMainFrame *)AfxGetMainWnd();
CChildFrame *pChild = (CChildFrame *)pMain->MDIGetActive();
if(!pChild) return false;
CImageProcessingView *pView = (CImageProcessingView *)pChild->GetActiveView();
if(!pView) return false;
return pView->GetCurrentImageGray(ImageGray, nIndex);
}
// 2차원 회색조 영상을 출력
bool DisplayCimage2D(BYTE **ImageGray, int nW, int nH, int nPosX, int nPosY, bool bErase, bool bDelete, int Rate)
{
CMainFrame *pMain = (CMainFrame *)AfxGetMainWnd();
CChildFrame *pChild = (CChildFrame *)pMain->MDIGetActive();
if(!pChild) return false;
CImageProcessingView *pView = (CImageProcessingView *)pChild->GetActiveView();
if(!pView) return false;
return pView->DisplayCimage2D(ImageGray, nW, nH, nPosX, nPosY, bErase, bDelete, Rate);
}
// 마지막 영상을 2차원 컬러 정보로 읽기
bool GetCurrentImageColor(BYTE **ImageRed, BYTE **ImageGreen, BYTE **ImageBlue, int nIndex)
{
CMainFrame *pMain = (CMainFrame *)AfxGetMainWnd();
CChildFrame *pChild = (CChildFrame *)pMain->MDIGetActive();
if(!pChild) return false;
CImageProcessingView *pView = (CImageProcessingView *)pChild->GetActiveView();
if(!pView) return false;
return pView->GetCurrentImageColor(ImageRed, ImageGreen, ImageBlue, nIndex);
}
bool GetCurrentImage1DColor(BYTE *Image1D, int nIndex)
{
CMainFrame *pMain = (CMainFrame *)AfxGetMainWnd();
CChildFrame *pChild = (CChildFrame *)pMain->MDIGetActive();
if(!pChild) return false;
CImageProcessingView *pView = (CImageProcessingView *)pChild->GetActiveView();
if(!pView) return false;
return pView->GetCurrentImage1DColor(Image1D, nIndex);
}
bool DisplayCimage1D(BYTE *Image1D, int nW, int nH, int nPosX, int nPosY, bool bErase, bool bDelete)
{
CMainFrame *pMain = (CMainFrame *)AfxGetMainWnd();
CChildFrame *pChild = (CChildFrame *)pMain->MDIGetActive();
if(!pChild) return false;
CImageProcessingView *pView = (CImageProcessingView *)pChild->GetActiveView();
if(!pView) return false;
return pView->DisplayCimage1D(Image1D, nW, nH,
nPosX, nPosY, bErase, bDelete);
}
// 2차원 컬러 영상을 출력
bool DisplayCimage2DColor(BYTE **ImageRed, BYTE **ImageGreen, BYTE **ImageBlue, int nW, int nH,
int nPosX, int nPosY, bool bErase, bool bDelete, int Rate)
{
CMainFrame *pMain = (CMainFrame *)AfxGetMainWnd();
CChildFrame *pChild = (CChildFrame *)pMain->MDIGetActive();
if(!pChild) return false;
CImageProcessingView *pView = (CImageProcessingView *)pChild->GetActiveView();
if(!pView) return false;
return pView->DisplayCimage2DColor(ImageRed, ImageGreen, ImageBlue, nW, nH,
nPosX, nPosY, bErase, bDelete, Rate);
}
bool DisplayIimage2D(int **ImageInt, int nW, int nH, int nPosX, int nPosY, bool bErase, bool bDelete)
{
CMainFrame *pMain = (CMainFrame *)AfxGetMainWnd();
CChildFrame *pChild = (CChildFrame *)pMain->MDIGetActive();
if(!pChild) return false;
CImageProcessingView *pView = (CImageProcessingView *)pChild->GetActiveView();
if(!pView) return false;
return pView->DisplayIimage2D(ImageInt, nW, nH, nPosX, nPosY, bErase, bDelete);
}
bool DisplayDimage2D(double **ImageDouble, int nW, int nH, int nPosX, int nPosY, bool bErase, bool bDelete)
{
CMainFrame *pMain = (CMainFrame *)AfxGetMainWnd();
CChildFrame *pChild = (CChildFrame *)pMain->MDIGetActive();
if(!pChild) return false;
CImageProcessingView *pView = (CImageProcessingView *)pChild->GetActiveView();
if(!pView) return false;
return pView->DisplayDimage2D(ImageDouble, nW, nH, nPosX, nPosY, bErase, bDelete);
}
void SetViewMsg(CString ViewMsg, bool bErase)
{
CMainFrame *pMain = (CMainFrame *)AfxGetMainWnd();
CChildFrame *pChild = (CChildFrame *)pMain->MDIGetActive();
if(!pChild) return;
CImageProcessingView *pView = (CImageProcessingView *)pChild->GetActiveView();
if(!pView) return;
pView->m_ViewMsg = ViewMsg;
pView->Invalidate(bErase);
}
void Move(BYTE **ImageGray, BYTE **OutputGray, int nW, int nH, int x0, int y0)
{
int x, y;
int xx, yy;
for(y = 0 ; y < nH ; y++)
for(x = 0 ; x < nW ; x++)
{
xx = x - x0;
yy = y - y0;
if(xx < 0 || xx >= nW || yy < 0 || yy >= nH)
OutputGray[y][x] = 0;
else
OutputGray[y][x] = ImageGray[yy][xx];
}
}
void ScaleX2(BYTE **ImageGray, BYTE **OutputGray, int nW, int nH)
{
int x, y;
int nOutW = nW*2, nOutH = nH*2; // nW, nH: 원영상의 폭과 높이
// nOutW, nOutH: 변환된 영상의 폭과 높이
for(y = 0 ; y < nOutH ; y++)
for(x = 0 ; x < nOutW ; x++)
{
OutputGray[y][x] = ImageGray[y/2][x/2];
}
}
void Rotate(BYTE **ImageGray, BYTE **OutputGray, int nW, int nH, int nOutW, int nOutH, double dDeg)
{
int x,y;
int xx, yy;
double dAng = dDeg * acos(-1.) / 180.; // 각도를 라디안으로 수정
for(y = 0 ; y < nOutH ; y++)
for(x = 0 ; x < nOutW ; x++)
{
xx = (int)((x-nOutW/2)*cos(dAng)+(y-nOutH/2)*sin(dAng));
yy = (int)(-(x-nOutW/2)*sin(dAng)+(y-nOutH/2)*cos(dAng));
xx += nW/2;
yy += nH/2;
if(xx < 0 || xx >= nW || yy < 0 || yy >= nH)
OutputGray[y][x] = 0;
else
OutputGray[y][x] = ImageGray[yy][xx];
}
}
void HistogramEqualization(BYTE **ImageGray, BYTE **OutputGray, int nW, int nH)
{
BYTE Table[GRAY_CNT];
int CumHisto[GRAY_CNT], Histogram[GRAY_CNT] ={0};
int x, y, i;
BYTE gray;
for(y = 0 ; y < nH ; y++)
for(x = 0 ; x < nW ; x++)
Histogram[ImageGray[y][x]]++;
int nSum = 0;
for (i=0; i< GRAY_CNT ; i++) {
nSum += Histogram[i];
CumHisto[i] = nSum;
}
for (i=0; i< GRAY_CNT ; i++)
Table[i] = (BYTE)((CumHisto[i] * (GRAY_CNT-1)) / (nW*nH));
for(y = 0 ; y < nH ; y++)
for(x = 0 ; x < nW ; x++)
{
gray = Table[ImageGray[y][x]];
if (gray < 0) gray = 0;
else if (gray >= GRAY_CNT) gray = GRAY_CNT-1;
OutputGray[y][x] = gray;
}
}
void MeanFilter(BYTE **ImageGray, BYTE **OutputGray, int nW, int nH, int nWinSize)
{
int x, y;
int dx, dy;
int nSum;
int xx, yy;
if(nWinSize <= 0) return;
double nRange = nWinSize/2.;
for(y = 0 ; y < nH ; y++)
for(x = 0 ; x < nW ; x++)
{
nSum = 0;
for(dy = -(int)nRange ; dy < nRange ; dy++)
for(dx = -(int)nRange ; dx < nRange ; dx++)
{
xx = x+dx;
yy = y+dy;
if(xx < 0) xx = 0;
if(xx >= nW) xx = nW-1;
if(yy < 0) yy = 0;
if(yy >= nH) yy = nH-1;
nSum += ImageGray[yy][xx];
}
OutputGray[y][x] = nSum/(nWinSize*nWinSize);
}
}
void MedianFilter(BYTE **ImageGray, BYTE **OutputGray, int nW, int nH, int nWinSize)
{
int x, y;
int dx, dy;
int xx, yy;
if(nWinSize <= 0) return;
double nRange = nWinSize/2.;
BYTE *Sort, nInsert;
int nIndex, i;
Sort = new BYTE[nWinSize*nWinSize];
for(y = 0 ; y < nH ; y++)
for(x = 0 ; x < nW ; x++)
{
nIndex = 0;
for(dy = -(int)nRange ; dy < nRange ; dy++)
for(dx = -(int)nRange ; dx < nRange ; dx++)
{
xx = x+dx;
yy = y+dy;
if(xx < 0) xx = 0;
if(xx >= nW) xx = nW-1;
if(yy < 0) yy = 0;
if(yy >= nH) yy = nH-1;
nInsert = ImageGray[yy][xx];
// 삽입 정렬
if(nIndex == 0) Sort[nIndex] = nInsert;
else
{
for(i = nIndex - 1 ; i >= 0 && Sort[i] > nInsert ; i--)
Sort[i+1] = Sort[i];
Sort[i+1] = nInsert;
}
nIndex++;
}
OutputGray[y][x] = Sort[nWinSize*nWinSize/2];
}
delete [] Sort;
}
void Sobel(BYTE **ImageGray, BYTE **OutputGray, int nW, int nH)
{
int x, y;
int nGx, nGy;
for(y = 0 ; y < nH ; y++)
for(x = 0 ; x < nW ; x++)
{
if(x <= 0 || x >= nW-1 || y <= 0 || y >= nH-1)
OutputGray[y][x] = 0;
else
{
nGx = ImageGray[y-1][x+1] + 2*ImageGray[y][x+1] + ImageGray[y+1][x+1]
- ImageGray[y-1][x-1] - 2*ImageGray[y][x-1] - ImageGray[y+1][x-1];
nGy = ImageGray[y+1][x-1] + 2*ImageGray[y+1][x] + ImageGray[y+1][x+1]
- ImageGray[y-1][x-1] - 2*ImageGray[y-1][x] - ImageGray[y-1][x+1];
OutputGray[y][x] = (abs(nGx) + abs(nGy))/8;
}
}
}
void SobelXY(BYTE **ImageGray, double **OutputGrayX, double **OutputGrayY, int nW, int nH)
{
int x, y;
for(y = 0 ; y < nH ; y++)
for(x = 0 ; x < nW ; x++)
{
if(x <= 0 || x >= nW-1 || y <= 0 || y >= nH-1)
{
OutputGrayX[y][x] = 0;
OutputGrayY[y][x] = 0;
}
else
{
OutputGrayX[y][x] = ImageGray[y-1][x+1] + 2*ImageGray[y][x+1] + ImageGray[y+1][x+1]
- ImageGray[y-1][x-1] - 2*ImageGray[y][x-1] - ImageGray[y+1][x-1];
OutputGrayY[y][x] = ImageGray[y+1][x-1] + 2*ImageGray[y+1][x] + ImageGray[y+1][x+1]
- ImageGray[y-1][x-1] - 2*ImageGray[y-1][x] - ImageGray[y-1][x+1];
}
}
}
void Laplacian(BYTE **ImageGray, BYTE **OutputGray, int nW, int nH)
{
int x, y;
int nSum;
for(y = 0 ; y < nH ; y++)
for(x = 0 ; x < nW ; x++)
{
if(x <= 0 || x >= nW-1 || y <= 0 || y >= nH-1)
OutputGray[y][x] = 0;
else
{
nSum = 4*ImageGray[y][x] - ImageGray[y-1][x] - ImageGray[y][x-1]
- ImageGray[y][x+1] - ImageGray[y+1][x];
nSum = (nSum+GRAY_CNT)/2;
if(nSum < 0) nSum = 0;
else if(nSum >= GRAY_CNT) nSum = GRAY_CNT-1;
OutputGray[y][x] = nSum;
}
}
}
void LaplacianSharp(BYTE **ImageGray, BYTE **OutputGray, int nW, int nH)
{
int x, y;
int nSum;
for(y = 0 ; y < nH ; y++)
for(x = 0 ; x < nW ; x++)
{
if(x <= 0 || x >= nW-1 || y <= 0 || y >= nH-1)
OutputGray[y][x] = 0;
else
{
nSum = 4*ImageGray[y][x] - ImageGray[y-1][x] - ImageGray[y][x-1]
- ImageGray[y][x+1] - ImageGray[y+1][x];
nSum = ImageGray[y][x] + nSum;
if(nSum < 0) nSum = 0;
else if(nSum >= GRAY_CNT) nSum = GRAY_CNT-1;
OutputGray[y][x] = nSum;
}
}
}
int NeighborMatch(BYTE **ImageGray, int x, int y, int pat)
{
switch (pat) {
case 0:
if ((ImageGray[y-1][x-1] != 0 || ImageGray[y-1][x] != 0 || ImageGray[y-1][x+1] != 0) &&
(ImageGray[y+1][x-1] != 0 || ImageGray[y+1][x] != 0 || ImageGray[y+1][x+1] != 0) &&
(ImageGray[y][x-1] == 0 && ImageGray[y][x+1] == 0) )
return (TRUE);
break;
case 1:
if ((ImageGray[y-1][x-1] != 0 || ImageGray[y][x-1] != 0 || ImageGray[y+1][x-1] != 0) &&
(ImageGray[y-1][x+1] != 0 || ImageGray[y][x+1] != 0 || ImageGray[y+1][x+1] != 0) &&
(ImageGray[y+1][x] == 0 && ImageGray[y-1][x] == 0) )
return (TRUE);
break;
case 2:
if ((ImageGray[y-1][x+1] != 0 || ImageGray[y-1][x] != 0 || ImageGray[y-1][x-1] != 0 ||
ImageGray[y][x-1] != 0 || ImageGray[y+1][x-1] != 0) &&
(ImageGray[y][x+1] == 0 && ImageGray[y+1][x] == 0 && ImageGray[y+1][x+1] == 2) )
return (TRUE);
break;
case 3:
if ((ImageGray[y-1][x-1] != 0 || ImageGray[y][x-1] != 0 || ImageGray[y+1][x-1] != 0 ||
ImageGray[y+1][x] != 0 || ImageGray[y+1][x+1] != 0) &&
(ImageGray[y][x+1] == 0 && ImageGray[y-1][x] == 0 && ImageGray[y-1][x+1] == 2) )
return (TRUE);
break;
case 4:
if ((ImageGray[y+1][x-1] != 0 || ImageGray[y+1][x] != 0 || ImageGray[y+1][x+1] != 0 ||
ImageGray[y][x+1] != 0 || ImageGray[y-1][x+1] != 0) &&
(ImageGray[y-1][x] == 0 && ImageGray[y][x-1] == 0 && ImageGray[y-1][x-1] == 2) )
return (TRUE);
break;
case 5:
if ((ImageGray[y+1][x+1] != 0 || ImageGray[y][x+1] != 0 || ImageGray[y-1][x+1] != 0 ||
ImageGray[y-1][x] != 0 || ImageGray[y-1][x-1] != 0) &&
(ImageGray[y][x-1] == 0 && ImageGray[y+1][x] == 0 && ImageGray[y+1][x-1] == 2) )
return (TRUE);
break;
default:
break;
}
return (FALSE);
}
void Thinning(BYTE **ImageGray, BYTE **OutputGray, int nW, int nH)
{
int x, y;
int FourNY[4] = {0, -1, 0, 1};
int FourNX[4] = {1, 0, -1, 0};
for(y = 0 ; y < nH ; y++)
for(x = 0 ; x < nW; x++)
{
if(ImageGray[y][x] > GRAY_CNT/2) OutputGray[y][x] = 1;
else OutputGray[y][x] = 0;
}
BOOL Remain = TRUE;
int j, Pattern;
BOOL bSkeleton;
while(Remain)
{
Remain = FALSE;
for(j = 0 ; j < 4; j++)
{
for(y = 1 ; y < nH-1 ; y++)
for(x = 1 ; x < nW-1 ; x++)
{
if(OutputGray[y][x] == 1 && OutputGray[y+FourNY[j]][x+FourNX[j]] == 0) {
bSkeleton = FALSE;
for(Pattern = 0 ; Pattern < 6; Pattern++) {
if (NeighborMatch(OutputGray, x, y, Pattern)) {
bSkeleton = TRUE;
break;
}
}
if(bSkeleton) OutputGray[y][x] = 2;
else {
OutputGray[y][x] = 3;
Remain = TRUE;
}
}
}
for(y = 1 ; y < nH-1 ; y++)
for(x = 1 ; x < nW-1 ; x++)
{
if(OutputGray[y][x] == 3) OutputGray[y][x] = 0;
}
}
}
for(y = 0 ; y < nH ; y++)
for(x = 0 ; x < nW ; x++)
{
if(OutputGray[y][x] == 2) OutputGray[y][x] = GRAY_CNT-1;
else OutputGray[y][x] = 0;
}
}
int Labeling(BYTE **ImageGray, int **Label, int nW, int nH, int nConnNumThre)
{
int x, y, num, left, top, k;
int *r, *area;
r = new int[nW*nH];
area = new int[nW*nH];
for(y = 0 ; y < nH ; y++)
for(x = 0 ; x < nW ; x++)
if(ImageGray[y][x] > 128) Label[y][x] = 0;
else Label[y][x] = -1;
for (x = 0 ; x < nW ; x++) {
Label[0][x] = -1;
Label[nH-1][x] = -1;
}
for (y = 0 ; y < nH ; y++) {
Label[y][0] = -1;
Label[y][nW-1] = -1;
}
num = -1;
for (y = 0; y < nH; y++) {
for (x = 0; x < nW; x++) {
if (y > 0 && x > 0) {
if (Label[y][x] >= 0) {
left = Label[y][x-1];
top = Label[y-1][x];
if (left == -1 && top != -1) {
Label[y][x] = r[top];
}
else if (left != -1 && top == -1) {
Label[y][x] = r[left];
}
else if (left == -1 && top == -1) {
num++;
if (num >= nW*nH) {
delete [] r;
delete [] area;
return 0;
}
r[num] = num;
Label[y][x] = r[num];
}
else if (left != -1 && top != -1) {
if (r[left] == r[top]) {
Label[y][x] = r[left];
}
else if (r[left] > r[top]) {
Label[y][x] = r[top];
r[left] = r[top];
}
else {
Label[y][x] = r[left];
r[top] = r[left];
}
}
}
}
}
}
for (k = 0; k <= num; k++) {
if (k != r[k]) r[k] = r[r[k]];
area[k] = 0;
}
for(y = 0 ; y < nH; y++)
for(x = 0 ; x < nW ; x++) {
if(Label[y][x] > -1) {
Label[y][x] = r[Label[y][x]];
area[Label[y][x]]++;
}
}
int cnt=0;
for (k = 0; k <= num; k++) {
if (area[k] > nConnNumThre) r[k] = cnt++;
else r[k] = -1;
}
for(y = 0 ; y < nH; y++)
for(x = 0 ; x < nW ; x++) {
if(Label[y][x] >= 0)
Label[y][x] = r[Label[y][x]];
}
delete [] r;
delete [] area;
return cnt;
}
int Labeling(BYTE **ImageGray, int **Label, int nW, int nH, int nConnNumThre, int *Area)
{
int x, y, num, left, top, k;
int *r, *rev;
r = new int[nW*nH];
rev = new int[nW*nH];
for(y = 0 ; y < nH ; y++)
for(x = 0 ; x < nW ; x++)
if(ImageGray[y][x] > 128) Label[y][x] = 0;
else Label[y][x] = -1;
for (x = 0 ; x < nW ; x++) {
Label[0][x] = -1;
Label[nH-1][x] = -1;
}
for (y = 0 ; y < nH ; y++) {
Label[y][0] = -1;
Label[y][nW-1] = -1;
}
num = -1;
for (y = 0; y < nH; y++) {
for (x = 0; x < nW; x++) {
if (y > 0 && x > 0) {
if (Label[y][x] >= 0) {
left = Label[y][x-1];
top = Label[y-1][x];
if (left == -1 && top != -1) {
Label[y][x] = r[top];
}
else if (left != -1 && top == -1) {
Label[y][x] = r[left];
}
else if (left == -1 && top == -1) {
num++;
if (num >= nW*nH) {
delete [] r;
return 0;
}
r[num] = num;
Label[y][x] = r[num];
}
else if (left != -1 && top != -1) {
if (r[left] == r[top]) {
Label[y][x] = r[left];
}
else if (r[left] > r[top]) {
Label[y][x] = r[top];
r[left] = r[top];