原文 C# 验证识别基类
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
|
网上找了些代码 自己又改了下 先放出来了 处理简单的验证码足够了 001 using
System;
002 using
System.Collections.Generic;
003 using
System.Linq;
004 using
System.Text;
005 using
System.Drawing;
006 using
System.Drawing.Imaging;
007 using
System.Runtime.InteropServices;
008 009 namespace
验证码处理
010 { 011 class
VerifyCode
012 {
013 public
Bitmap bmpobj;
014 015 public
VerifyCode(Bitmap pic)
016 {
017 bmpobj = new
Bitmap(pic); //转换为Format32bppRgb
018 }
019 020 /// <summary>
021 /// 根据RGB,计算灰度值
022 /// </summary>
023 /// <param name="posClr">Color值</param>
024 /// <returns>灰度值,整型</returns>
025 private
int
GetGrayNumColor(System.Drawing.Color posClr)
026 {
027 return
(posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16;
028 }
029 030 /// <summary>
031 /// 灰度转换,逐点方式
032 /// </summary>
033 public
void
GrayByPixels()
034 {
035 for
( int
i = 0; i < bmpobj.Height; i++)
036 {
037 for
( int
j = 0; j < bmpobj.Width; j++)
038 {
039 int
tmpValue = GetGrayNumColor(bmpobj.GetPixel(j, i));
040 bmpobj.SetPixel(j, i, Color.FromArgb(tmpValue, tmpValue, tmpValue));
041 }
042 }
043 }
044 045 /// <summary>
046 /// 去图形边框
047 /// </summary>
048 /// <param name="borderWidth"></param>
049 public
void
ClearPicBorder( int
borderWidth)
050 {
051 for
( int
i = 0; i < bmpobj.Height; i++)
052 {
053 for
( int
j = 0; j < bmpobj.Width; j++)
054 {
055 if
(i < borderWidth || j < borderWidth || j > bmpobj.Width - 1 - borderWidth || i > bmpobj.Height - 1 - borderWidth)
056 bmpobj.SetPixel(j, i, Color.FromArgb(255, 255, 255));
057 }
058 }
059 }
060 061 /// <summary>
062 /// 灰度转换,逐行方式
063 /// </summary>
064 public
void
GrayByLine()
065 {
066 Rectangle rec = new
Rectangle(0, 0, bmpobj.Width, bmpobj.Height);
067 BitmapData bmpData = bmpobj.LockBits(rec, ImageLockMode.ReadWrite, bmpobj.PixelFormat); // PixelFormat.Format32bppPArgb);
068 // bmpData.PixelFormat = PixelFormat.Format24bppRgb;
069 IntPtr scan0 = bmpData.Scan0;
070 int
len = bmpobj.Width * bmpobj.Height;
071 int [] pixels = new
int [len];
072 Marshal.Copy(scan0, pixels, 0, len);
073 074 //对图片进行处理
075 int
GrayValue = 0;
076 for
( int
i = 0; i < len; i++)
077 {
078 GrayValue = GetGrayNumColor(Color.FromArgb(pixels[i]));
079 pixels[i] = ( byte )(Color.FromArgb(GrayValue, GrayValue, GrayValue)).ToArgb(); //Color转byte
080 }
081 082 bmpobj.UnlockBits(bmpData);
083 084 ////输出
085 //GCHandle gch = GCHandle.Alloc(pixels, GCHandleType.Pinned);
086 //bmpOutput = new Bitmap(bmpobj.Width, bmpobj.Height, bmpData.Stride, bmpData.PixelFormat, gch.AddrOfPinnedObject());
087 //gch.Free();
088 }
089 090 /// <summary>
091 /// 得到有效图形并调整为可平均分割的大小
092 /// </summary>
093 /// <param name="dgGrayValue">灰度背景分界值</param>
094 /// <param name="CharsCount">有效字符数</param>
095 /// <returns></returns>
096 public
void
GetPicValidByValue( int
dgGrayValue, int
CharsCount)
097 {
098 int
posx1 = bmpobj.Width; int
posy1 = bmpobj.Height;
099 int
posx2 = 0; int
posy2 = 0;
100 for
( int
i = 0; i < bmpobj.Height; i++) //找有效区
101 {
102 for
( int
j = 0; j < bmpobj.Width; j++)
103 {
104 int
pixelValue = bmpobj.GetPixel(j, i).R;
105 if
(pixelValue < dgGrayValue) //根据灰度值
106 {
107 if
(posx1 > j) posx1 = j;
108 if
(posy1 > i) posy1 = i;
109 110 if
(posx2 < j) posx2 = j;
111 if
(posy2 < i) posy2 = i;
112 };
113 };
114 };
115 // 确保能整除
116 int
Span = CharsCount - (posx2 - posx1 + 1) % CharsCount; //可整除的差额数
117 if
(Span < CharsCount)
118 {
119 int
leftSpan = Span / 2; //分配到左边的空列 ,如span为单数,则右边比左边大1
120 if
(posx1 > leftSpan)
121 posx1 = posx1 - leftSpan;
122 if
(posx2 + Span - leftSpan < bmpobj.Width)
123 posx2 = posx2 + Span - leftSpan;
124 }
125 //复制新图
126 Rectangle cloneRect = new
Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);
127 bmpobj = bmpobj.Clone(cloneRect, bmpobj.PixelFormat);
128 }
129 130 /// <summary>
131 /// 得到有效图形,图形为类变量
132 /// </summary>
133 /// <param name="dgGrayValue">灰度背景分界值</param>
134 /// <param name="CharsCount">有效字符数</param>
135 /// <returns></returns>
136 public
void
GetPicValidByValue( int
dgGrayValue)
137 {
138 int
posx1 = bmpobj.Width; int
posy1 = bmpobj.Height;
139 int
posx2 = 0; int
posy2 = 0;
140 for
( int
i = 0; i < bmpobj.Height; i++) //找有效区
141 {
142 for
( int
j = 0; j < bmpobj.Width; j++)
143 {
144 int
pixelValue = bmpobj.GetPixel(j, i).R;
145 if
(pixelValue < dgGrayValue) //根据灰度值
146 {
147 if
(posx1 > j) posx1 = j;
148 if
(posy1 > i) posy1 = i;
149 150 if
(posx2 < j) posx2 = j;
151 if
(posy2 < i) posy2 = i;
152 };
153 };
154 };
155 //复制新图
156 Rectangle cloneRect = new
Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);
157 bmpobj = bmpobj.Clone(cloneRect, bmpobj.PixelFormat);
158 }
159 160 /// <summary>
161 /// 得到有效图形,图形由外面传入
162 /// </summary>
163 /// <param name="dgGrayValue">灰度背景分界值</param>
164 /// <param name="CharsCount">有效字符数</param>
165 /// <returns></returns>
166 public
Bitmap GetPicValidByValue(Bitmap singlepic, int
dgGrayValue)
167 {
168 int
posx1 = singlepic.Width; int
posy1 = singlepic.Height;
169 int
posx2 = 0; int
posy2 = 0;
170 for
( int
i = 0; i < singlepic.Height; i++) //找有效区
171 {
172 for
( int
j = 0; j < singlepic.Width; j++)
173 {
174 int
pixelValue = singlepic.GetPixel(j, i).R;
175 if
(pixelValue < dgGrayValue) //根据灰度值
176 {
177 if
(posx1 > j) posx1 = j;
178 if
(posy1 > i) posy1 = i;
179 180 if
(posx2 < j) posx2 = j;
181 if
(posy2 < i) posy2 = i;
182 };
183 };
184 };
185 //复制新图
186 Rectangle cloneRect = new
Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);
187 return
singlepic.Clone(cloneRect, singlepic.PixelFormat);
188 }
189 190 /// <summary>
191 /// 平均分割图片
192 /// </summary>
193 /// <param name="RowNum">水平上分割数</param>
194 /// <param name="ColNum">垂直上分割数</param>
195 /// <returns>分割好的图片数组</returns>
196 public
Bitmap [] GetSplitPics( int
RowNum, int
ColNum)
197 {
198 if
(RowNum == 0 || ColNum == 0)
199 return
null ;
200 int
singW = bmpobj.Width / RowNum;
201 int
singH = bmpobj.Height / ColNum;
202 Bitmap [] PicArray= new
Bitmap[RowNum*ColNum];
203 204 Rectangle cloneRect;
205 for
( int
i = 0; i < ColNum; i++) //找有效区
206 {
207 for
( int
j = 0; j < RowNum; j++)
208 {
209 cloneRect = new
Rectangle(j*singW, i*singH, singW , singH);
210 PicArray[i*RowNum+j]=bmpobj.Clone(cloneRect, bmpobj.PixelFormat); //复制小块图
211 }
212 }
213 return
PicArray;
214 }
215 216 /// <summary>
217 /// 返回灰度图片的点阵描述字串,1表示灰点,0表示背景
218 /// </summary>
219 /// <param name="singlepic">灰度图</param>
220 /// <param name="dgGrayValue">背前景灰色界限</param>
221 /// <returns></returns>
222 public
string
GetSingleBmpCode(Bitmap singlepic, int
dgGrayValue)
223 {
224 Color piexl;
225 string
code = "" ;
226 for
( int
posy = 0; posy < singlepic.Height; posy++)
227 for
( int
posx = 0; posx < singlepic.Width; posx++)
228 {
229 piexl = singlepic.GetPixel(posx, posy);
230 if
(piexl.R < dgGrayValue) // Color.Black )
231 code = code + "1" ;
232 else
233 code = code + "0" ;
234 }
235 return
code;
236 }
237 238 /// <summary>
239 /// 得到灰度图像前景背景的临界值 最大类间方差法
240 /// </summary>
241 /// <returns>前景背景的临界值</returns>
242 public
int
GetDgGrayValue()
243 {
244 int [] pixelNum = new
int [256]; //图象直方图,共256个点
245 int
n, n1, n2;
246 int
total; //total为总和,累计值
247 double
m1, m2, sum, csum, fmax, sb; //sb为类间方差,fmax存储最大方差值
248 int
k, t, q;
249 int
threshValue = 1; // 阈值
250 //生成直方图
251 for
( int
i = 0; i < bmpobj.Width; i++)
252 {
253 for
( int
j = 0; j < bmpobj.Height; j++)
254 {
255 //返回各个点的颜色,以RGB表示
256 pixelNum[bmpobj.GetPixel(i, j).R]++; //相应的直方图加1
257 }
258 }
259 //直方图平滑化
260 for
(k = 0; k <= 255; k++)
261 {
262 total = 0;
263 for
(t = -2; t <= 2; t++) //与附近2个灰度做平滑化,t值应取较小的值
264 {
265 q = k + t;
266 if
(q < 0) //越界处理
267 q = 0;
268 if
(q > 255)
269 q = 255;
270 total = total + pixelNum[q]; //total为总和,累计值
271 }
272 pixelNum[k] = ( int )(( float )total / 5.0 + 0.5); //平滑化,左边2个+中间1个+右边2个灰度,共5个,所以总和除以5,后面加0.5是用修正值
273 }
274 //求阈值
275 sum = csum = 0.0;
276 n = 0;
277 //计算总的图象的点数和质量矩,为后面的计算做准备
278 for
(k = 0; k <= 255; k++)
279 {
280 sum += ( double )k * ( double )pixelNum[k]; //x*f(x)质量矩,也就是每个灰度的值乘以其点数(归一化后为概率),sum为其总和
281 n += pixelNum[k]; //n为图象总的点数,归一化后就是累积概率
282 }
283 284 fmax = -1.0; //类间方差sb不可能为负,所以fmax初始值为-1不影响计算的进行
285 n1 = 0;
286 for
(k = 0; k < 256; k++) //对每个灰度(从0到255)计算一次分割后的类间方差sb
287 {
288 n1 += pixelNum[k]; //n1为在当前阈值遍前景图象的点数
289 if
(n1 == 0) { continue ; } //没有分出前景后景
290 n2 = n - n1; //n2为背景图象的点数
291 if
(n2 == 0) { break ; } //n2为0表示全部都是后景图象,与n1=0情况类似,之后的遍历不可能使前景点数增加,所以此时可以退出循环
292 csum += ( double )k * pixelNum[k]; //前景的“灰度的值*其点数”的总和
293 m1 = csum / n1; //m1为前景的平均灰度
294 m2 = (sum - csum) / n2; //m2为背景的平均灰度
295 sb = ( double )n1 * ( double )n2 * (m1 - m2) * (m1 - m2); //sb为类间方差
296 if
(sb > fmax) //如果算出的类间方差大于前一次算出的类间方差
297 {
298 fmax = sb; //fmax始终为最大类间方差(otsu)
299 threshValue = k; //取最大类间方差时对应的灰度的k就是最佳阈值
300 }
301 }
302 return
threshValue;
303 }
304 305 /// <summary>
306 /// 去掉杂点(适合杂点/杂线粗为1)
307 /// </summary>
308 /// <param name="dgGrayValue">背前景灰色界限</param>
309 /// <returns></returns>
310 public
void
ClearNoise( int
dgGrayValue, int
MaxNearPoints)
311 {
312 Color piexl;
313 int
nearDots = 0;
314 //逐点判断
315 for
( int
i = 0; i < bmpobj.Width; i++)
316 for
( int
j = 0; j < bmpobj.Height; j++)
317 {
318 piexl = bmpobj.GetPixel(i, j);
319 if
(piexl.R < dgGrayValue)
320 {
321 nearDots = 0;
322 //判断周围8个点是否全为空
323 if
(i == 0 || i == bmpobj.Width - 1 || j == 0 || j == bmpobj.Height - 1) //边框全去掉
324 {
325 bmpobj.SetPixel(i, j, Color.FromArgb(255, 255, 255));
326 }
327 else
328 {
329 if
(bmpobj.GetPixel(i - 1, j - 1).R < dgGrayValue) nearDots++;
330 if
(bmpobj.GetPixel(i, j - 1).R < dgGrayValue) nearDots++;
331 if
(bmpobj.GetPixel(i + 1, j - 1).R < dgGrayValue) nearDots++;
332 if
(bmpobj.GetPixel(i - 1, j).R < dgGrayValue) nearDots++;
333 if
(bmpobj.GetPixel(i + 1, j).R < dgGrayValue) nearDots++;
334 if
(bmpobj.GetPixel(i - 1, j + 1).R < dgGrayValue) nearDots++;
335 if
(bmpobj.GetPixel(i, j + 1).R < dgGrayValue) nearDots++;
336 if
(bmpobj.GetPixel(i + 1, j + 1).R < dgGrayValue) nearDots++;
337 }
338 339 if
(nearDots < MaxNearPoints)
340 bmpobj.SetPixel(i, j, Color.FromArgb(255, 255, 255)); //去掉单点 && 粗细小3邻边点
341 }
342 else
//背景
343 bmpobj.SetPixel(i, j, Color.FromArgb(255, 255, 255));
344 }
345 }
346 347 /// <summary>
348 /// 3×3中值滤波除杂
349 /// </summary>
350 /// <param name="dgGrayValue"></param>
351 public
void
ClearNoise( int
dgGrayValue)
352 {
353 int
x, y;
354 byte [] p = new
byte [9]; //最小处理窗口3*3
355 byte
s;
356 //byte[] lpTemp=new BYTE[nByteWidth*nHeight];
357 int
i, j;
358 //--!!!!!!!!!!!!!!下面开始窗口为3×3中值滤波!!!!!!!!!!!!!!!!
359 for
(y = 1; y < bmpobj.Height - 1; y++) //--第一行和最后一行无法取窗口
360 {
361 for
(x = 1; x < bmpobj.Width - 1; x++)
362 {
363 //取9个点的值
364 p[0] = bmpobj.GetPixel(x - 1, y - 1).R;
365 p[1] = bmpobj.GetPixel(x, y - 1).R;
366 p[2] = bmpobj.GetPixel(x + 1, y - 1).R;
367 p[3] = bmpobj.GetPixel(x - 1, y).R;
368 p[4] = bmpobj.GetPixel(x, y).R;
369 p[5] = bmpobj.GetPixel(x + 1, y).R;
370 p[6] = bmpobj.GetPixel(x - 1, y + 1).R;
371 p[7] = bmpobj.GetPixel(x, y + 1).R;
372 p[8] = bmpobj.GetPixel(x + 1, y + 1).R;
373 //计算中值
374 for
(j = 0; j < 5; j++)
375 {
376 for
(i = j + 1; i < 9; i++)
377 {
378 if
(p[j] > p[i])
379 {
380 s = p[j];
381 p[j] = p[i];
382 p[i] = s;
383 }
384 }
385 }
386 // if (bmpobj.GetPixel(x, y).R < dgGrayValue)
387 bmpobj.SetPixel(x, y, Color.FromArgb(p[4], p[4], p[4])); //给有效值付中值
388 }
389 }
390 }
391 392 /// <summary>
393 /// 该函数用于对图像进行腐蚀运算。结构元素为水平方向或垂直方向的三个点,
394 /// 中间点位于原点;或者由用户自己定义3×3的结构元素。
395 /// </summary>
396 /// <param name="dgGrayValue">前后景临界值</param>
397 /// <param name="nMode">腐蚀方式:0表示水平方向,1垂直方向,2自定义结构元素。</param>
398 /// <param name="structure"> 自定义的3×3结构元素</param>
399 public
void
ErosionPic( int
dgGrayValue, int
nMode, bool [,] structure)
400 {
401 int
lWidth = bmpobj.Width;
402 int
lHeight = bmpobj.Height;
403 Bitmap newBmp = new
Bitmap(lWidth, lHeight);
404 405 int
i, j, n, m; //循环变量
406 407 if
(nMode == 0)
408 {
409 //使用水平方向的结构元素进行腐蚀
410 // 由于使用1×3的结构元素,为防止越界,所以不处理最左边和最右边
411 // 的两列像素
412 for
(j = 0; j < lHeight; j++)
413 {
414 for
(i = 1; i < lWidth - 1; i++)
415 {
416 //目标图像中的当前点先赋成黑色
417 newBmp.SetPixel(i, j, Color.Black);
418 419 //如果源图像中当前点自身或者左右有一个点不是黑色,
420 //则将目标图像中的当前点赋成白色
421 if
(bmpobj.GetPixel(i - 1, j).R > dgGrayValue ||
422 bmpobj.GetPixel(i, j).R > dgGrayValue ||
423 bmpobj.GetPixel(i + 1, j).R > dgGrayValue)
424 newBmp.SetPixel(i, j, Color.White);
425 }
426 }
427 }
428 else
if
(nMode == 1)
429 {
430 //使用垂真方向的结构元素进行腐蚀
431 // 由于使用3×1的结构元素,为防止越界,所以不处理最上边和最下边
432 // 的两行像素
433 for
(j = 1; j < lHeight - 1; j++)
434 {
435 for
(i = 0; i < lWidth; i++)
436 {
437 //目标图像中的当前点先赋成黑色
438 newBmp.SetPixel(i, j, Color.Black);
439 440 //如果源图像中当前点自身或者左右有一个点不是黑色,
441 //则将目标图像中的当前点赋成白色
442 if
(bmpobj.GetPixel(i, j - 1).R > dgGrayValue ||
443 bmpobj.GetPixel(i, j).R > dgGrayValue ||
444 bmpobj.GetPixel(i, j + 1).R > dgGrayValue)
445 newBmp.SetPixel(i, j, Color.White);
446 }
447 }
448 }
449 else
450 {
451 if
(structure.Length != 9) //检查自定义结构
452 return ;
453 //使用自定义的结构元素进行腐蚀
454 // 由于使用3×3的结构元素,为防止越界,所以不处理最左边和最右边
455 // 的两列像素和最上边和最下边的两列像素
456 for
(j = 1; j < lHeight - 1; j++)
457 {
458 for
(i = 1; i < lWidth - 1; i++)
459 {
460 //目标图像中的当前点先赋成黑色
461 newBmp.SetPixel(i, j, Color.Black);
462 //如果原图像中对应结构元素中为黑色的那些点中有一个不是黑色,
463 //则将目标图像中的当前点赋成白色
464 for
(m = 0; m < 3; m++)
465 {
466 for
(n = 0; n < 3; n++)
467 {
468 if
(!structure[m, n])
469 continue ;
470 if
(bmpobj.GetPixel(i + m - 1, j + n - 1).R > dgGrayValue)
471 {
472 newBmp.SetPixel(i, j, Color.White);
473 break ;
474 }
475 }
476 }
477 }
478 }
479 }
480 bmpobj = newBmp;
481 }
482 483 /// <summary>
484 /// 该函数用于对图像进行细化运算。要求目标图像为灰度图像
485 /// </summary>
486 /// <param name="dgGrayValue"></param>
487 public
void
ThiningPic( int
dgGrayValue)
488 {
489 int
lWidth = bmpobj.Width;
490 int
lHeight = bmpobj.Height;
491 // Bitmap newBmp = new Bitmap(lWidth, lHeight);
492 493 bool
bModified; //脏标记
494 int
i, j, n, m; //循环变量
495 496 //四个条件
497 bool
bCondition1;
498 bool
bCondition2;
499 bool
bCondition3;
500 bool
bCondition4;
501 502 int
nCount; //计数器
503 int [,] neighbour = new
int [5, 5]; //5×5相邻区域像素值
504 505 506 507 bModified = true ;
508 while
(bModified)
509 {
510 bModified = false ;
511 512 //由于使用5×5的结构元素,为防止越界,所以不处理外围的几行和几列像素
513 for
(j = 2; j < lHeight - 2; j++)
514 {
515 for
(i = 2; i < lWidth - 2; i++)
516 {
517 bCondition1 = false ;
518 bCondition2 = false ;
519 bCondition3 = false ;
520 bCondition4 = false ;
521 522 if
(bmpobj.GetPixel(i, j).R > dgGrayValue)
523 {
524 if
(bmpobj.GetPixel(i, j).R < 255)
525 bmpobj.SetPixel(i, j, Color.White);
526 continue ;
527 }
528 529 //获得当前点相邻的5×5区域内像素值,白色用0代表,黑色用1代表
530 for
(m = 0; m < 5; m++)
531 {
532 for
(n = 0; n < 5; n++)
533 {
534 neighbour[m, n] = bmpobj.GetPixel(i + m - 2, j + n - 2).R < dgGrayValue ? 1 : 0;
535 }
536 }
537 538 //逐个判断条件。
539 //判断2<=NZ(P1)<=6
540 nCount = neighbour[1, 1] + neighbour[1, 2] + neighbour[1, 3]
541 + neighbour[2, 1] + neighbour[2, 3] +
542 +neighbour[3, 1] + neighbour[3, 2] + neighbour[3, 3];
543 if
(nCount >= 2 && nCount <= 6)
544 {
545 bCondition1 = true ;
546 }
547 548 //判断Z0(P1)=1
549 nCount = 0;
550 if
(neighbour[1, 2] == 0 && neighbour[1, 1] == 1)
551 nCount++;
552 if
(neighbour[1, 1] == 0 && neighbour[2, 1] == 1)
553 nCount++;
554 if
(neighbour[2, 1] == 0 && neighbour[3, 1] == 1)
555 nCount++;
556 if
(neighbour[3, 1] == 0 && neighbour[3, 2] == 1)
557 nCount++;
558 if
(neighbour[3, 2] == 0 && neighbour[3, 3] == 1)
559 nCount++;
560 if
(neighbour[3, 3] == 0 && neighbour[2, 3] == 1)
561 nCount++;
562 if
(neighbour[2, 3] == 0 && neighbour[1, 3] == 1)
563 nCount++;
564 if
(neighbour[1, 3] == 0 && neighbour[1, 2] == 1)
565 nCount++;
566 if
(nCount == 1)
567 bCondition2 = true ;
568 569 //判断P2*P4*P8=0 or Z0(p2)!=1
570 if
(neighbour[1, 2] * neighbour[2, 1] * neighbour[2, 3] == 0)
571 {
572 bCondition3 = true ;
573 }
574 else
575 {
576 nCount = 0;
577 if
(neighbour[0, 2] == 0 && neighbour[0, 1] == 1)
578 nCount++;
579 if
(neighbour[0, 1] == 0 && neighbour[1, 1] == 1)
580 nCount++;
581 if
(neighbour[1, 1] == 0 && neighbour[2, 1] == 1)
582 nCount++;
583 if
(neighbour[2, 1] == 0 && neighbour[2, 2] == 1)
584 nCount++;
585 if
(neighbour[2, 2] == 0 && neighbour[2, 3] == 1)
586 nCount++;
587 if
(neighbour[2, 3] == 0 && neighbour[1, 3] == 1)
588 nCount++;
589 if
(neighbour[1, 3] == 0 && neighbour[0, 3] == 1)
590 nCount++;
591 if
(neighbour[0, 3] == 0 && neighbour[0, 2] == 1)
592 nCount++;
593 if
(nCount != 1)
594 bCondition3 = true ;
595 }
596 597 //判断P2*P4*P6=0 or Z0(p4)!=1
598 if
(neighbour[1, 2] * neighbour[2, 1] * neighbour[3, 2] == 0)
599 {
600 bCondition4 = true ;
601 }
602 else
603 {
604 nCount = 0;
605 if
(neighbour[1, 1] == 0 && neighbour[1, 0] == 1)
606 nCount++;
607 if
(neighbour[1, 0] == 0 && neighbour[2, 0] == 1)
608 nCount++;
609 if
(neighbour[2, 0] == 0 && neighbour[3, 0] == 1)
610 nCount++;
611 if
(neighbour[3, 0] == 0 && neighbour[3, 1] == 1)
612 nCount++;
613 if
(neighbour[3, 1] == 0 && neighbour[3, 2] == 1)
614 nCount++;
615 if
(neighbour[3, 2] == 0 && neighbour[2, 2] == 1)
616 nCount++;
617 if
(neighbour[2, 2] == 0 && neighbour[1, 2] == 1)
618 nCount++;
619 if
(neighbour[1, 2] == 0 && neighbour[1, 1] == 1)
620 nCount++;
621 if
(nCount != 1)
622 bCondition4 = true ;
623 }
624 625 if
(bCondition1 && bCondition2 && bCondition3 && bCondition4)
626 {
627 bmpobj.SetPixel(i, j, Color.White);
628 bModified = true ;
629 }
630 else
631 {
632 bmpobj.SetPixel(i, j, Color.Black);
633 }
634 }
635 }
636 }
637 // 复制细化后的图像
638 // bmpobj = newBmp;
639 }
640 641 /// <summary>
642 /// 锐化要启用不安全代码编译
643 /// </summary>
644 /// <param name="val">锐化程度。取值[0,1]。值越大锐化程度越高</param>
645 /// <returns>锐化后的图像</returns>
646 public
void
Sharpen( float
val)
647 {
648 int
w = bmpobj.Width;
649 int
h = bmpobj.Height;
650 Bitmap bmpRtn = new
Bitmap(w, h, PixelFormat.Format24bppRgb);
651 BitmapData srcData = bmpobj.LockBits( new
Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
652 BitmapData dstData = bmpRtn.LockBits( new
Rectangle(0, 0, w, h), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
653 unsafe
654 {
655 byte * pIn = ( byte *)srcData.Scan0.ToPointer();
656 byte * pOut = ( byte *)dstData.Scan0.ToPointer();
657 int
stride = srcData.Stride;
658 byte * p;
659 660 for
( int
y = 0; y < h; y++)
661 {
662 for
( int
x = 0; x < w; x++)
663 {
664 //取周围9点的值。位于边缘上的点不做改变。
665 if
(x == 0 || x == w - 1 || y == 0 || y == h - 1)
666 {
667 //不做
668 pOut[0] = pIn[0];
669 pOut[1] = pIn[1];
670 pOut[2] = pIn[2];
671 }
672 else
673 {
674 int
r1, r2, r3, r4, r5, r6, r7, r8, r0;
675 int
g1, g2, g3, g4, g5, g6, g7, g8, g0;
676 int
b1, b2, b3, b4, b5, b6, b7, b8, b0;
677 678 float
vR, vG, vB;
679 680 //左上
681 p = pIn - stride - 3;
682 r1 = p[2];
683 g1 = p[1];
684 b1 = p[0];
685 686 //正上
687 p = pIn - stride;
688 r2 = p[2];
689 g2 = p[1];
690 b2 = p[0];
691 692 //右上
693 p = pIn - stride + 3;
694 r3 = p[2];
695 g3 = p[1];
696 b3 = p[0];
697 698 //左侧
699 p = pIn - 3;
700 r4 = p[2];
701 g4 = p[1];
702 b4 = p[0];
703 704 //右侧
705 p = pIn + 3;
706 r5 = p[2];
707 g5 = p[1];
708 b5 = p[0];
709 710 //右下
711 p = pIn + stride - 3;
712 r6 = p[2];
713 g6 = p[1];
714 b6 = p[0];
715 716 //正下
717 p = pIn + stride;
718 r7 = p[2];
719 g7 = p[1];
720 b7 = p[0];
721 722 //右下
723 p = pIn + stride + 3;
724 r8 = p[2];
725 g8 = p[1];
726 b8 = p[0];
727 728 //自己
729 p = pIn;
730 r0 = p[2];
731 g0 = p[1];
732 b0 = p[0];
733 734 vR = ( float )r0 - ( float )(r1 + r2 + r3 + r4 + r5 + r6 + r7 + r8) / 8;
735 vG = ( float )g0 - ( float )(g1 + g2 + g3 + g4 + g5 + g6 + g7 + g8) / 8;
736 vB = ( float )b0 - ( float )(b1 + b2 + b3 + b4 + b5 + b6 + b7 + b8) / 8;
737 738 vR = r0 + vR * val;
739 vG = g0 + vG * val;
740 vB = b0 + vB * val;
741 742 if
(vR > 0)
743 {
744 vR = Math.Min(255, vR);
745 }
746 else
747 {
748 vR = Math.Max(0, vR);
749 }
750 751 if
(vG > 0)
752 {
753 vG = Math.Min(255, vG);
754 }
755 else
756 {
757 vG = Math.Max(0, vG);
758 }
759 760 if
(vB > 0)
761 {
762 vB = Math.Min(255, vB);
763 }
764 else
765 {
766 vB = Math.Max(0, vB);
767 }
768 769 pOut[0] = ( byte )vB;
770 pOut[1] = ( byte )vG;
771 pOut[2] = ( byte )vR;
772 }
773 pIn += 3;
774 pOut += 3;
775 } // end of x
776 pIn += srcData.Stride - w * 3;
777 pOut += srcData.Stride - w * 3;
778 } // end of y
779 }
780 bmpobj.UnlockBits(srcData);
781 bmpRtn.UnlockBits(dstData);
782 bmpobj = bmpRtn;
783 }
784 785 /// <summary>
786 /// 图片二值化
787 /// </summary>
788 /// <param name="hsb"></param>
789 public
void
BitmapTo1Bpp(Double hsb)
790 {
791 int
w = bmpobj.Width;
792 int
h = bmpobj.Height;
793 Bitmap bmp = new
Bitmap(w, h, PixelFormat.Format1bppIndexed);
794 BitmapData data = bmp.LockBits( new
Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);
795 for
( int
y = 0; y < h; y++)
796 {
797 byte [] scan = new
byte [(w + 7) / 8];
798 for
( int
x = 0; x < w; x++)
799 {
800 Color c = bmpobj.GetPixel(x, y);
801 if
(c.GetBrightness() >= hsb) scan[x / 8] |= ( byte )(0x80 >> (x % 8));
802 }
803 Marshal.Copy(scan, 0, (IntPtr)(( int )data.Scan0 + data.Stride * y), scan.Length);
804 }
805 bmp.UnlockBits(data);
806 bmpobj = bmp;
807 }
808 }
809 } |