-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmydiblib.h
1670 lines (1456 loc) · 38 KB
/
mydiblib.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
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
#include "dibapi.h"
#include <math.h>
#include <string>
#include <vector>
#include <fstream>
#include <stdio.h>
#define pi 3.1415926
#define M 15
#define N 24
using namespace std;
typedef struct
{
int id;
bool isWhite;
bool isVisited;
bool isEdge;
bool isSum;
}PelsInfo; //点的信息
typedef struct
{
bool isCircularity;
int area;
double perimeter;
int dec_code;
CString *bin_code;
int center_x;
int center_y;
int xsum;
int ysum;
int count;
}RegionInfo; //区域信息
//声明一些全局变量
HDIB g_hDIB;
RegionInfo *g_RegionInfo;
PelsInfo *g_PelsInfo;
CString strPathName;
CString strFileTitle;
bool isFileOpen;
int region_id=0; //区域编号
double average_radius=0;
bool is256=0;
int step=0;
int pic=0;
/********************************signction declaration*************************************/
void ClearAll(CDC* pDC);//清除屏幕
void DisplayDIB(CDC* pDC,HDIB hDIB);//在屏幕上显示位图
void GrayToWhiteBlack(HDIB hDIB);//将灰度图二值化
void Set256toGray(HDIB hDIB);//将256色位图转为灰度图
void RemoveNoise(HDIB hDIB); //去除离散噪声点
void RegionSign(HDIB hDIB); //图像分割
void EdgeIdentify(HDIB hDIB); //提取边缘
void AreaandPerimeter(HDIB hDIB); //求周长和面积
void GetCenter(HDIB hDIB); //求出中心坐标点
void CircularityIdentify(HDIB hDIB); //识别圆形标记并求圆平均半径
void DelErr(HDIB hDIB); //去掉错误中心圆
void Codejiema(HDIB hDIB); //编码点解码
void Sign(int i,int j,LONG lWidth,LONG lHeight); //标记一个白色区域中所有的点
void sum(int i,int j,LONG lWidth,LONG lHeight); //求椭圆边界所有像素点的x,y坐标和以及像素总数
int round(double f); //取整(4舍5入)
void ImageProcess(HDIB hDIB); //图像预处理
void CenterIdentify(HDIB hDIB); //中心圆识别
void Codematch(); //编码点的匹配
void GetCrossRatio(); //求交比不变量
int zhongjian(int c1,int c2,int aa[100][6]); //求2幅图片中匹配的非编码点,存于 aa[100][6]
int Match(int c1,int c2,int aa[100][6]); //求2幅图片中匹配的编码点,存于 aa[100][6]
void Incodematch(); //非编码点的匹配
vector <string> Getsubstr(string str); //分割字符串
/***********************************implementation*************************************/
void DisplayDIB(CDC* pDC,HDIB hDIB)
{
BYTE* lpDIB=(BYTE*)::GlobalLock((HGLOBAL)hDIB);
// 获取DIB宽度和高度
int cxDIB = ::DIBWidth((char*) lpDIB);
int cyDIB = ::DIBHeight((char*)lpDIB);
CRect rcDIB,rcDest;
rcDIB.top = rcDIB.left = 0;
rcDIB.right = cxDIB;
rcDIB.bottom = cyDIB;
//设置目标客户区输出大小尺寸
rcDest = rcDIB;
//CDC* pDC=GetDC();
ClearAll(pDC);
//在客户区显示图像
::PaintDIB(pDC->m_hDC,rcDest,hDIB,rcDIB,NULL);
::GlobalUnlock((HGLOBAL)hDIB);
}
void ClearAll(CDC *pDC)
{
CRect rect;
rect.left =0;rect.top =0;rect.right =2000;rect.bottom =1000;
CPen pen;
pen.CreatePen (PS_SOLID,1,RGB(255,255,255));
pDC->SelectObject (&pen);
pDC->Rectangle (&rect);
::DeleteObject (pen);
}
// 将256色位图转化为灰度图
void Set256toGray(HDIB hDIB)
{
if(!is256)
{
AfxMessageBox("非256色位图!");
return;
}
LPSTR lpDIB;
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)hDIB);// 由DIB句柄得到DIB指针并锁定DIB
LPSTR lpDIBBits;// 指向DIB象素数据区的指针
BYTE * lpSrc;// 指向DIB象素的指针
LONG lWidth; // 图像宽度
LONG lHeight;// 图像高度
LONG lLineBytes; // 图像每行的字节数
LPBITMAPINFO lpbmi; // 指向BITMAPINFO结构的指针(Win3.0)
LPBITMAPCOREINFO lpbmc;// 指向BITMAPCOREINFO结构的指针
lpbmi = (LPBITMAPINFO)lpDIB;// 获取指向BITMAPINFO结构的指针(Win3.0)
lpbmc = (LPBITMAPCOREINFO)lpDIB;// 获取指向BITMAPCOREINFO结构的指针
BYTE bMap[256];// 灰度映射表
// 计算灰度映射表(保存各个颜色的灰度值),并更新DIB调色板
int i,j;
for (i = 0; i < 256; i ++)
{
// 计算该颜色对应的灰度值
bMap[i] = (BYTE)(0.299 * lpbmi->bmiColors[i].rgbRed +
0.587 * lpbmi->bmiColors[i].rgbGreen +
0.114 * lpbmi->bmiColors[i].rgbBlue + 0.5);
// 更新DIB调色板红色分量
lpbmi->bmiColors[i].rgbRed = i;
// 更新DIB调色板绿色分量
lpbmi->bmiColors[i].rgbGreen = i;
// 更新DIB调色板蓝色分量
lpbmi->bmiColors[i].rgbBlue = i;
// 更新DIB调色板保留位
lpbmi->bmiColors[i].rgbReserved = 0;
}
// 找到DIB图像象素起始位置
lpDIBBits = ::FindDIBBits(lpDIB);
// 获取图像宽度
lWidth = ::DIBWidth(lpDIB);
// 获取图像高度
lHeight = ::DIBHeight(lpDIB);
// 计算图像每行的字节数
lLineBytes = WIDTHBYTES(lWidth * 8);
// 更换每个象素的颜色索引(即按照灰度映射表换成灰度值)
//逐行扫描
for(i = 0; i < lHeight; i++)
{
//逐列扫描
for(j = 0; j < lWidth; j++)
{
// 指向DIB第i行,第j个象素的指针
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * (lHeight - 1 - i) + j;
// 变换
*lpSrc = bMap[*lpSrc];
}
}
//解除锁定
::GlobalUnlock ((HGLOBAL)hDIB);
}
//二值化
void GrayToWhiteBlack(HDIB hDIB)
{
LPSTR lpDIB;// 指向DIB的指针
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)hDIB);// 由DIB句柄得到DIB指针并锁定DIB
LPSTR lpDIBBits;// 指向DIB象素数据区的指针
BYTE * lpSrc;// 指向DIB象素的指针
LONG lWidth;// 图像宽度
LONG lHeight;// 图像高度
LONG lLineBytes; // 图像每行的字节数
lpDIBBits = ::FindDIBBits(lpDIB);// 找到DIB图像象素起始位置
lWidth = ::DIBWidth(lpDIB); // 获取图像宽度
lHeight = ::DIBHeight(lpDIB);// 获取图像高度
lLineBytes = WIDTHBYTES(lWidth * 8);// 计算图像每行的字节数
int i,j;
//二值化
for(i = 0; i < lHeight; i++)
{
for(j = 0; j < lWidth; j++)
{
// 指向DIB第i行,第j个象素的指针
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * i + j;
// 二值化处理
//大于90,设置为255,即白点
if(*lpSrc>90) *lpSrc=255;
//否则设置为0,即黑点
else *lpSrc=0;
}
}
//解除锁定
::GlobalUnlock((HGLOBAL)hDIB);
}
void RemoveNoise(HDIB hDIB)
{
LPSTR lpDIB;// 指向DIB的指针
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)hDIB);// 由DIB句柄得到DIB指针并锁定DIB
LPSTR lpDIBBits;// 指向DIB象素数据区的指针
BYTE * lpSrc;// 指向DIB象素的指针
LONG lWidth;// 图像宽度
LONG lHeight;// 图像高度
LONG lLineBytes; // 图像每行的字节数
lpDIBBits = ::FindDIBBits(lpDIB);// 找到DIB图像象素起始位置
lWidth = ::DIBWidth(lpDIB); // 获取图像宽度
lHeight = ::DIBHeight(lpDIB);// 获取图像高度
lLineBytes = WIDTHBYTES(lWidth * 8);// 计算图像每行的字节数
int i,j;
//去噪
for(i = 0; i < lHeight; i++)
{
for(j = 0; j < lWidth; j++)
{
// 指向DIB第i行,第j个象素的指针
if((i!=0)&&(j!=0)&&(i!=lHeight-1)&&(j!=lWidth-1))
{
int num=0;
BYTE color=0;
lpSrc=( unsigned char*)lpDIBBits + lLineBytes * i + j;
color=*lpSrc;
lpSrc=( unsigned char*)lpDIBBits + lLineBytes * (i+1) + j;
if (*lpSrc != color)
{
num++;
}
lpSrc=( unsigned char*)lpDIBBits + lLineBytes * (i-1) + j;
if (*lpSrc != color)
{
num++;
}
lpSrc=(unsigned char*)lpDIBBits + lLineBytes * i + (j+1);
if (*lpSrc != color)
{
num++;
}
lpSrc=(unsigned char*)lpDIBBits + lLineBytes * i + (j-1);
if (*lpSrc != color)
{
num++;
}
if(num>=3)
{
lpSrc=( unsigned char*)lpDIBBits + lLineBytes * i + j;
if(color==0) *lpSrc=255;
else *lpSrc=0;
}
}
}
}
//解除锁定
::GlobalUnlock((HGLOBAL)hDIB);
}
void RegionSign(HDIB hDIB)
{
LPSTR lpDIB;// 指向DIB的指针
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)hDIB);// 由DIB句柄得到DIB指针并锁定DIB
LPSTR lpDIBBits;// 指向DIB象素数据区的指针
BYTE * lpSrc;// 指向DIB象素的指针
LONG lWidth;// 图像宽度
LONG lHeight;// 图像高度
LONG lLineBytes; // 图像每行的字节数
lpDIBBits = ::FindDIBBits(lpDIB);// 找到DIB图像象素起始位置
lWidth = ::DIBWidth(lpDIB); // 获取图像宽度
lHeight = ::DIBHeight(lpDIB);// 获取图像高度
lLineBytes = WIDTHBYTES(lWidth * 8);// 计算图像每行的字节数
int i,j;
//初始化g_PelsInfo
g_PelsInfo = (PelsInfo*)malloc(lWidth*lHeight*sizeof(PelsInfo));
for(i = 0; i < lHeight; i++)
{
for(j = 0; j < lWidth; j++)
{
// 指向DIB第i行,第j个象素的指针
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * i + j;
if(*lpSrc==255)
{
g_PelsInfo[i*lWidth+j].isWhite=1;
}
else
{
g_PelsInfo[i*lWidth+j].isWhite=0;
}
g_PelsInfo[i*lWidth+j].id=0;
g_PelsInfo[i*lWidth+j].isVisited=0;
g_PelsInfo[i*lWidth+j].isEdge=0;
g_PelsInfo[i*lWidth+j].isSum=0;
}
}
//图形分割
for(i = 0; i < lHeight; i++)
{
for(j = 0; j < lWidth; j++)
{
// 指向DIB第i行,第j个象素的指针
// lpSrc = (unsigned char*)lpDIBBits + lLineBytes * i + j;
if((g_PelsInfo[i*lWidth+j].isWhite)&&
!(g_PelsInfo[i*lWidth+j].isVisited))
{
region_id++;
Sign(i,j,lWidth,lHeight);
}
}
}
//解除锁定
::GlobalUnlock((HGLOBAL)hDIB);
}
void EdgeIdentify(HDIB hDIB)
{
// TODO: Add your command handler code here
LPSTR lpDIB;// 指向DIB的指针
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)hDIB);// 由DIB句柄得到DIB指针并锁定DIB
LPSTR lpDIBBits;// 指向DIB象素数据区的指针
BYTE * lpSrc;// 指向DIB象素的指针
LONG lWidth;// 图像宽度
LONG lHeight;// 图像高度
LONG lLineBytes; // 图像每行的字节数
lpDIBBits = ::FindDIBBits(lpDIB);// 找到DIB图像象素起始位置
lWidth = ::DIBWidth(lpDIB); // 获取图像宽度
lHeight = ::DIBHeight(lpDIB);// 获取图像高度
lLineBytes = WIDTHBYTES(lWidth * 8);// 计算图像每行的字节数
int i,j;
//求边界
for(i = 0; i < lHeight; i++)
{
for(j = 0; j < lWidth; j++)
{
if(g_PelsInfo[i*lWidth+j].isWhite)
{
bool isEdge=0;
if(i!=0)
{
if(!g_PelsInfo[(i-1)*lWidth+j].isWhite)
{
isEdge=1;
}
}
if(i!=lHeight-1)
{
if(!g_PelsInfo[(i+1)*lWidth+j].isWhite)
{
isEdge=1;
}
}
if(j!=0)
{
if(!g_PelsInfo[i*lWidth+j-1].isWhite)
{
isEdge=1;
}
}
if(j!=lHeight-1)
{
if(!g_PelsInfo[i*lWidth+j+1].isWhite)
{
isEdge=1;
}
}
if(isEdge)
{
lpSrc=(unsigned char*)lpDIBBits + lLineBytes * i + j;
*lpSrc=100;
g_PelsInfo[i*lWidth+j].isEdge=1;
}
}
}
}
//解除锁定
::GlobalUnlock((HGLOBAL)hDIB);
}
void GetCenter(HDIB hDIB)
{
LPSTR lpDIB;// 指向DIB的指针
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)hDIB);// 由DIB句柄得到DIB指针并锁定DIB
LPSTR lpDIBBits;// 指向DIB象素数据区的指针
BYTE * lpSrc;// 指向DIB象素的指针
LONG lWidth;// 图像宽度
LONG lHeight;// 图像高度
LONG lLineBytes; // 图像每行的字节数
lpDIBBits = ::FindDIBBits(lpDIB);// 找到DIB图像象素起始位置
lWidth = ::DIBWidth(lpDIB); // 获取图像宽度
lHeight = ::DIBHeight(lpDIB);// 获取图像高度
lLineBytes = WIDTHBYTES(lWidth * 8);// 计算图像每行的字节数
int i,j;
//初始化g_RegionInfo
g_RegionInfo = (RegionInfo*)malloc(region_id*sizeof(RegionInfo));
for(i=0;i<region_id;i++)
{
g_RegionInfo[i].area=0;
g_RegionInfo[i].perimeter=0;
g_RegionInfo[i].count=0;
g_RegionInfo[i].isCircularity=0;
g_RegionInfo[i].center_x=0;
g_RegionInfo[i].center_y=0;
g_RegionInfo[i].xsum=0;
g_RegionInfo[i].ysum=0;
}
//求中心
for(i = 0; i < lHeight; i++)
{
for(j = 0; j < lWidth; j++)
{
if(g_PelsInfo[i*lWidth+j].isEdge)
{
sum(i,j,lWidth,lHeight);
g_RegionInfo[g_PelsInfo[i*lWidth+j].id-1].center_x=round(g_RegionInfo[g_PelsInfo[i*lWidth+j].id-1].xsum/g_RegionInfo[g_PelsInfo[i*lWidth+j].id-1].count);
g_RegionInfo[g_PelsInfo[i*lWidth+j].id-1].center_y=round(g_RegionInfo[g_PelsInfo[i*lWidth+j].id-1].ysum/g_RegionInfo[g_PelsInfo[i*lWidth+j].id-1].count);
}
}
}
//解除锁定
::GlobalUnlock ((HGLOBAL)hDIB);
}
void sum(int i,int j,LONG lWidth,LONG lHeight)
{
g_RegionInfo[g_PelsInfo[i*lWidth+j].id-1].xsum=g_RegionInfo[g_PelsInfo[i*lWidth+j].id-1].xsum+j;
g_RegionInfo[g_PelsInfo[i*lWidth+j].id-1].ysum=g_RegionInfo[g_PelsInfo[i*lWidth+j].id-1].ysum+i;
g_PelsInfo[i*lWidth+j].isSum=1;
g_RegionInfo[g_PelsInfo[i*lWidth+j].id-1].count++;
bool b=0;
if(i!=lHeight-1 && i!=0 && j!=lWidth-1 && j!=0)
{
if((g_PelsInfo[(i+1)*lWidth+j].isEdge)&&
!(g_PelsInfo[(i+1)*lWidth+j].isSum))
{
b=1;
sum(i+1,j,lWidth,lHeight);
}
if((g_PelsInfo[(i-1)*lWidth+j].isEdge)&&
!(g_PelsInfo[(i-1)*lWidth+j].isSum))
{
b=1;
sum(i-1,j,lWidth,lHeight);
}
if((g_PelsInfo[i*lWidth+j+1].isEdge)&&
!(g_PelsInfo[i*lWidth+j+1].isSum))
{
b=1;
sum(i,j+1,lWidth,lHeight);
}
if((g_PelsInfo[i*lWidth+j-1].isEdge)&&
!(g_PelsInfo[i*lWidth+j-1].isSum))
{
b=1;
sum(i,j-1,lWidth,lHeight);
}
}
if(!b)
return;
}
void AreaandPerimeter(HDIB hDIB)
{
LPSTR lpDIB;// 指向DIB的指针
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)hDIB);// 由DIB句柄得到DIB指针并锁定DIB
LPSTR lpDIBBits;// 指向DIB象素数据区的指针
LONG lWidth;// 图像宽度
LONG lHeight;// 图像高度
LONG lLineBytes; // 图像每行的字节数
lpDIBBits = ::FindDIBBits(lpDIB);// 找到DIB图像象素起始位置
lWidth = ::DIBWidth(lpDIB); // 获取图像宽度
lHeight = ::DIBHeight(lpDIB);// 获取图像高度
lLineBytes = WIDTHBYTES(lWidth * 8);// 计算图像每行的字节数
int i,j;
for(i = 0; i < lHeight; i++)
{
for(j = 0; j < lWidth; j++)
{
if(g_PelsInfo[i*lWidth+j].isWhite)
{
//求面积
g_RegionInfo[g_PelsInfo[i*lWidth+j].id-1].area++;
//求周长
if(g_PelsInfo[i*lWidth+j].isEdge)
{
int adjacentpoint_num=0;
if(i!=0)
{
if(g_PelsInfo[(i-1)*lWidth+j].isEdge)
{
adjacentpoint_num++;
}
}
if(i!=lHeight-1)
{
if(g_PelsInfo[(i+1)*lWidth+j].isEdge)
{
adjacentpoint_num++;
}
}
if(j!=0)
{
if(g_PelsInfo[i*lWidth+j-1].isEdge)
{
adjacentpoint_num++;
}
}
if(j!=lWidth-1)
{
if(g_PelsInfo[i*lWidth+j+1].isEdge)
{
adjacentpoint_num++;
}
}
if(adjacentpoint_num==0)
{
g_RegionInfo[g_PelsInfo[i*lWidth+j].id-1].perimeter+=sqrt(2);
}
else if(adjacentpoint_num==1)
{
g_RegionInfo[g_PelsInfo[i*lWidth+j].id-1].perimeter+=(sqrt(2)+1)/2;
}
else if(adjacentpoint_num==2)
{
g_RegionInfo[g_PelsInfo[i*lWidth+j].id-1].perimeter+=1;
}
}
}
}
}
//解除锁定
::GlobalUnlock ((HGLOBAL)hDIB);
}
void CircularityIdentify(HDIB hDIB)
{
BYTE * lpSrc;
LPSTR lpDIB;// 指向DIB的指针
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)hDIB);// 由DIB句柄得到DIB指针并锁定DIB
LPSTR lpDIBBits;// 指向DIB象素数据区的指针
LONG lWidth;// 图像宽度
LONG lHeight;// 图像高度
LONG lLineBytes; // 图像每行的字节数
lpDIBBits = ::FindDIBBits(lpDIB);// 找到DIB图像象素起始位置
lWidth = ::DIBWidth(lpDIB); // 获取图像宽度
lHeight = ::DIBHeight(lpDIB);// 获取图像高度
lLineBytes = WIDTHBYTES(lWidth * 8);// 计算图像每行的字节数
int i;
//识别圆形标记并求圆平均半径
int circle_area=0;
int circle_num=0;
for(i=0;i<region_id;i++)
{
if(g_RegionInfo[i].perimeter*g_RegionInfo[i].perimeter/(pi*g_RegionInfo[i].area*4)<=1.3)
{
g_RegionInfo[i].isCircularity=1;
circle_area+=g_RegionInfo[i].area;
circle_num++;
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * g_RegionInfo[i].center_y + g_RegionInfo[i].center_x;
*lpSrc=0;
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * g_RegionInfo[i].center_y + g_RegionInfo[i].center_x+1;
*lpSrc=0;
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * g_RegionInfo[i].center_y + g_RegionInfo[i].center_x-1;
*lpSrc=0;
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * (g_RegionInfo[i].center_y+1) + g_RegionInfo[i].center_x;
*lpSrc=0;
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * (g_RegionInfo[i].center_y-1) + g_RegionInfo[i].center_x;
*lpSrc=0;
}
}
average_radius=sqrt(circle_area/circle_num/pi);
//解除锁定
::GlobalUnlock ((HGLOBAL)hDIB);
}
void DelErr(HDIB hDIB)
{
step=4;
LPSTR lpDIB;// 指向DIB的指针
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)hDIB);// 由DIB句柄得到DIB指针并锁定DIB
LPSTR lpDIBBits;// 指向DIB象素数据区的指针
LONG lWidth;// 图像宽度
BYTE * lpSrc;
LONG lHeight;// 图像高度
LONG lLineBytes; // 图像每行的字节数
lpDIBBits = ::FindDIBBits(lpDIB);// 找到DIB图像象素起始位置
lWidth = ::DIBWidth(lpDIB); // 获取图像宽度
lHeight = ::DIBHeight(lpDIB);// 获取图像高度
lLineBytes = WIDTHBYTES(lWidth * 8);// 计算图像每行的字节数
int i,j;
for(i=0;i<region_id;i++)
{
if(g_RegionInfo[i].isCircularity==1)
{
for(j=0;j<region_id;j++)
{
if(g_RegionInfo[j].isCircularity==1&&i!=j)
{
double distance=sqrt(
(g_RegionInfo[i].center_x-g_RegionInfo[j].center_x)*(g_RegionInfo[i].center_x-g_RegionInfo[j].center_x)+
(g_RegionInfo[i].center_y-g_RegionInfo[j].center_y)*(g_RegionInfo[i].center_y-g_RegionInfo[j].center_y)
);
if(distance<5*average_radius)
{
if(g_RegionInfo[i].area<g_RegionInfo[j].area)
{
g_RegionInfo[i].isCircularity=0;
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * g_RegionInfo[i].center_y + g_RegionInfo[i].center_x;
*lpSrc=255;
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * g_RegionInfo[i].center_y + g_RegionInfo[i].center_x+1;
*lpSrc=255;
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * g_RegionInfo[i].center_y + g_RegionInfo[i].center_x-1;
*lpSrc=255;
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * (g_RegionInfo[i].center_y+1) + g_RegionInfo[i].center_x;
*lpSrc=255;
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * (g_RegionInfo[i].center_y-1) + g_RegionInfo[i].center_x;
*lpSrc=255;
}
else
{
g_RegionInfo[j].isCircularity=0;
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * g_RegionInfo[j].center_y+ g_RegionInfo[j].center_x;
*lpSrc=255;
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * g_RegionInfo[j].center_y + g_RegionInfo[j].center_x+1;
*lpSrc=255;
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * g_RegionInfo[j].center_y + g_RegionInfo[j].center_x-1;
*lpSrc=255;
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * (g_RegionInfo[j].center_y+1) + g_RegionInfo[j].center_x;
*lpSrc=255;
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * (g_RegionInfo[j].center_y-1) + g_RegionInfo[j].center_x;
*lpSrc=255;
}
}
}
}
}
}
//解除锁定
::GlobalUnlock ((HGLOBAL)hDIB);
}
void ImageProcess(HDIB hDIB)
{
step=2;
Set256toGray(hDIB); //将256色位图转为灰度图
if(is256)
{
GrayToWhiteBlack(hDIB); //将灰度图二值化
RemoveNoise(hDIB);
RegionSign(hDIB);
EdgeIdentify(hDIB);
}
}
void CenterIdentify(HDIB hDIB)
{
step=3;
GetCenter(hDIB);
AreaandPerimeter(hDIB);
CircularityIdentify(hDIB); //识别圆形标记
}
void Codejiema(HDIB hDIB)
{
// TODO: Add your command handler code here
step=5;
LPSTR lpDIB;// 指向DIB的指针
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)hDIB);// 由DIB句柄得到DIB指针并锁定DIB
LPSTR lpDIBBits;// 指向DIB象素数据区的指针
BYTE * lpSrc;// 指向DIB象素的指针
LONG lWidth;// 图像宽度
LONG lHeight;// 图像高度
LONG lLineBytes; // 图像每行的字节数
lpDIBBits = ::FindDIBBits(lpDIB);// 找到DIB图像象素起始位置
lWidth = ::DIBWidth(lpDIB); // 获取图像宽度
lHeight = ::DIBHeight(lpDIB);// 获取图像高度
lLineBytes = WIDTHBYTES(lWidth * 8);// 计算图像每行的字节数
// 更换每个象素的颜色索引(即按照灰度映射表换成灰度值)
int k;
int i,j;
for(k=0;k<region_id;k++)
{
if(g_RegionInfo[k].isCircularity==1)
{
int radius;
int required_angle;
int angle;
int angle_n=0;
int cross_n=0;
bool isfind=0;
//寻找大概起始角度
for(angle=0;angle<360/N;angle++)
{
cross_n=0;
for(radius=round(1.5*sqrt(g_RegionInfo[k].area/pi));radius<4*average_radius;radius++)
{
j=round(radius*cos(N*angle*pi/180.0))+g_RegionInfo[k].center_x; //隔24度扫一次,水平朝右为0度
i=round(radius*sin(N*angle*pi/180.0))+g_RegionInfo[k].center_y;
if(i>0&&i<lHeight&&j>=0&&j<lWidth)
{
if((g_PelsInfo[(i+1)*lWidth+j].isEdge&&angle<180)||
(g_PelsInfo[(i-1)*lWidth+j].isEdge&&angle>180)||
g_PelsInfo[i*lWidth+j].isEdge)
{
cross_n++;
if(cross_n>0)
{
required_angle=N*angle;
isfind=1;
break;
}
}
}
else
{
break;
}
}
if(isfind)
{
break;
}
}
int bin_code[M]= //找到扇环开始记为1(白)
{
1,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0
};
if(angle==360/N) //没有找到扇环
bin_code[0]=0;
//寻找精确起始角度
for(angle=1;angle<360;angle++)
{
cross_n=0;
isfind=0;
for(radius=round(1.5*sqrt(g_RegionInfo[k].area/pi));radius<4*average_radius;radius++)
{
j=round(radius*cos((required_angle+angle)*pi/180.0))+g_RegionInfo[k].center_x;
i=round(radius*sin((required_angle+angle)*pi/180.0))+g_RegionInfo[k].center_y;
if(i>0&&i<lHeight-1&&j>0&&j<lWidth-1)
{
if((g_PelsInfo[(i+1)*lWidth+j].isEdge&&angle<180)||
(g_PelsInfo[(i-1)*lWidth+j].isEdge&&angle>180)||
g_PelsInfo[i*lWidth+j].isEdge)
{
cross_n++;
if(cross_n>0)
{
isfind=1;
break;
}
}
}
else
{
break;
}
}
if(!isfind) //找到扇环的边界
{
required_angle=required_angle+angle-1;
break;
}
}
//求二进制编码
for(angle=1;angle<M;angle++)
{
isfind=0;
cross_n=0;
for(radius=round(1.5*sqrt(g_RegionInfo[k].area/pi));radius<4*average_radius;radius++)
{
j=round(radius*cos((required_angle-angle*(360.0/M)-(180.0/M))*pi/180))+g_RegionInfo[k].center_x;
i=round(radius*sin((required_angle-angle*(360.0/M)-(180.0/M))*pi/180))+g_RegionInfo[k].center_y;
if(i>0&&i<lHeight-1&&j>0&&j<lWidth-1)
{
lpSrc = (unsigned char*)lpDIBBits + lLineBytes * i + j;
*lpSrc=255;
if((g_PelsInfo[(i+1)*lWidth+j].isEdge&&angle<180)||
(g_PelsInfo[(i-1)*lWidth+j].isEdge&&angle>180)||
g_PelsInfo[i*lWidth+j].isEdge)
{
cross_n++;
if(cross_n>0)
{
isfind=1;
break;
}
}
}
else
{
break;
}
}
if(isfind)
{
bin_code[angle]=1;
}
}
//求出最小十进制编码
int dec_code[M]=
{
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0
};
bool b=1;
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
dec_code[i]+=bin_code[(i+j)%M]*round(pow(2,M-j-1));
}
}
int min=32767;
int n;
for(i=0;i<M;i++)
{
if(dec_code[i]<min)
{
min=dec_code[i];
n=i;
}
}
//把编码存放到g_RegionInfo中
CString str;
CString coding;
for(i=0;i<M;i++)
{
str.Format("%d",bin_code[(i+n)%M]);
coding+=str;
}
g_RegionInfo[k].bin_code=new CString(coding);
g_RegionInfo[k].dec_code=min;
}
}
//将结果写入文件
CString str;
CStdioFile file;
int n=0;
for(k=0;k<region_id;k++)
{
if(g_RegionInfo[k].isCircularity==1)
{
n++;
}
}
str.Format("%s.txt",strFileTitle);
file.Open(str,CFile::modeWrite|CFile::modeCreate);
str.Format( "共有%d个编码点:\n",n);
file.WriteString(str);
str.Format( "编号: 坐标: 十进制编码:\n");
file.WriteString(str);
n=0;
j=1000;
for(k=0;k<region_id;k++)
{
if(g_RegionInfo[k].isCircularity==1)
{
if(g_RegionInfo[k].dec_code==0)
{
j++;
str.Format("%d",j);
file.WriteString(str);
file.WriteString(" ");
}
else
{
n++;
str.Format("%d",n);
file.WriteString(str);
file.WriteString(" ");
}
str.Format("%d",g_RegionInfo[k].center_x);
file.WriteString(str);
file.WriteString(" ");
str.Format("%d",g_RegionInfo[k].center_y);
file.WriteString(str);
file.WriteString(" ");
/* str=*g_RegionInfo[k].bin_code;
file.WriteString(str);
file.WriteString(" ");*/
str.Format("%d",g_RegionInfo[k].dec_code);
file.WriteString(str);
file.WriteString("\n");
}
}