2015年6月26日
C#实现验证码中粘连字符分割(七)
5月份发布了《C#实现验证码中粘连字符分割》的一系列文章,初步构建了粘连字符分割的软件框架。近期经过进一步的学习,修正了之前发布的代码中的BUG,字符分割的准确度提升了一点点,我选取了9组原图与分割图的组合。
这次发布的代码更新了如下内容:
1.更新了被判定为谷点的像素类型组合,新增了寻找下谷点的代码;
2.修正了水滴算法的滴水路径,新增了向上滴落的滴水算法;
3.修正了水滴算法以及根据最终路径分割字符中的部分BUG;
4.更新了谷点过滤算法,但仍然简单粗暴。
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 |
/* -------------------------------------------------------- * 作者:livezingy * * 博客:http://www.livezingy.com * * 开发环境: * Visual Studio V2012 * .NET Framework 4.5 * * * 版本历史: * V1.1 2015年06月26日 * 实现粘粘字符的分割 * 1.分别寻找上谷点与下谷点,先对上下谷点分别进行过滤,过滤原则仍根据相邻像素的距离,以及与边界的距离进行过滤; * 2.上下谷点再综合过滤,最终保留3个谷点,综合过滤原则:上下谷点距离在1/8距离以内时,上下谷点优先取上谷点; * 3.3个谷点确定后,上谷点采用向下滴落;下谷点采用向上滴落 * --------------------------------------------------------- */ using System; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Text; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; namespace ImageSegmention { /// <summary> /// 通常我们关注的字符并不会填满整张图片,找到我们所关注字符的边界并将其记录。 /// </summary> public class ImgBoundary { private int _heightMax = 0; public int heightMax { get { return _heightMax; } set { _heightMax = value; } } private int _heightMin = 0; public int heightMin { get { return _heightMin; } set { _heightMin = value; } } private int _widthMax = 0; public int widthMax { get { return _widthMax; } set { _widthMax = value; } } private int _widthMin = 0; public int widthMin { get { return _widthMin; } set { _widthMin = value; } } } /// <summary> /// 记录图片中顶部的谷点集合和底部的谷点集合 /// </summary> public class ImgValley { private ArrayList _upValley; public ArrayList upValley { get { return _upValley; } set { _upValley = value; } } private ArrayList _downValley; public ArrayList downValley { get { return _downValley; } set { _downValley = value; } } } public class PixPos { private int _widthPos = 0; public int widthPos { get { return _widthPos; } set { _widthPos = value; } } private int _heightPos = 0; public int heightPos { get { return _heightPos; } set { _heightPos = value; } } } public class SegmentFunction { /// <summary> /// 将输入的含粘粘字符的图片转换为分割后的字符图片 /// </summary> /// <param name="imageSrcPath">粘粘字符图片的路径</param> /// <param name="imageSrcPath">分割后图片的存储路径</param> /// <param name="segNum">分割路径的数量</param> /// <returns>字符分割后的图片为位图</returns> public static Bitmap ImageSegment(string imageSrcPath, string imageDestPath, int segNum) { Bitmap bmp = new Bitmap(imageSrcPath); int Threshold = 0; Byte[,] BinaryArray = ImageBinarization.ToBinaryArray(bmp, out Threshold); ImgBoundary Boundary = getImgBoundary(BinaryArray); ImgValley iniValley = getImgValley(BinaryArray, Boundary); ImgValley filterValley = filterImgValley(iniValley, Boundary, segNum); ArrayList segPathList = getSegmentPath(filterValley, Boundary, BinaryArray); Bitmap segmentBmp = getDivideImg(BinaryArray, segPathList); segmentBmp.Save(imageDestPath, System.Drawing.Imaging.ImageFormat.Jpeg); return segmentBmp; } /// <summary> /// 获取含粘粘字符图片的谷点 /// </summary> /// <param name="BinaryArray">原始图片的二值化数组</param> /// <returns>谷点列表</returns> //public static ImgValley getImgValley(Byte[,] BinaryArray) //public static Bitmap getImgValley(Byte[,] BinaryArray) public static ImgValley getImgValley(Byte[,] BinaryArray,ImgBoundary Boundary) { int imageHeight = BinaryArray.GetLength(0); int imageWidth = BinaryArray.GetLength(1); ArrayList upValley = new ArrayList(); ArrayList downValley = new ArrayList(); ImgValley Valley = new ImgValley(); Byte[,] m_DesImage = Thining.ThinPicture(BinaryArray); byte flg = 1; byte P0 = 0; byte P1 = 0; byte P2 = 0; byte P3 = 0; byte P4 = 0; byte P5 = 0; byte P6 = 0; byte P7 = 0; byte P8 = 0; byte P9 = 0; byte P10 = 0; byte P11 = 0; byte P12 = 0; byte P13 = 0; byte P14 = 0; //get the up valley point for (int j = 2; j < (imageWidth - 2); j++) { flg = 1; for (int i = 2; ((i < imageHeight) && (1 == flg)); i++) { if (0 == m_DesImage[i, j]) { flg = 0; P0 = m_DesImage[i - 2, j - 2]; P1 = m_DesImage[i - 2, j - 1]; P2 = m_DesImage[i - 2, j]; P3 = m_DesImage[i - 2, j + 1]; P4 = m_DesImage[i - 2, j + 2]; P5 = m_DesImage[i - 1, j - 2]; P6 = m_DesImage[i - 1, j - 1]; P7 = m_DesImage[i - 1, j]; P8 = m_DesImage[i - 1, j + 1]; P9 = m_DesImage[i - 1, j + 2]; P10 = m_DesImage[i, j - 2]; P11 = m_DesImage[i, j - 1]; P12 = m_DesImage[i, j]; P13 = m_DesImage[i, j + 1]; P14 = m_DesImage[i, j + 2]; if ((0 == P0) && (0 == P6) && (0 == P12) && (255 == P1) && (255 == P2) && (255 == P7) && ( ((0 == P13) && (0 == P14) && (255 == P3) && (255 == P8) && (255 == P4) && (255 == P9)) || ((255 == P3) && (0 == P8) && (0 == P4)) || ((0 == P13) && (0 == P8)) || ((0 == P3) && (0 == P8)) || ((0 == P13) && (0 == P9) && (255 == P8)) || ((0 == P8) && (0 == P9) && (255 == P3) && (255 == P4)))) { upValley.Add(j); } if ((0 == P4) && (0 == P8) && (0 == P12) && (255 == P3) && (255 == P2) && (255 == P7) && (((0 == P10) && (0 == P11) && (255 == P0) && (255 == P1) && (255 == P5) && (255 == P6)) || ((0 == P6) && (0 == P11)) || ((0 == P6) && (0 == P1)) || ((255 == P6) && (0 == P5) && (0 == P11)) || ((0 == P5) && (0 == P6) && (255 == P1) && (255 == P0)))) { upValley.Add(j); } if ((0 == P5) && (0 == P12)&& (0 == P9) && (255 == P1) && (255 == P2) && (255 == P3) && (255 == P7) && ( ((0 == P6)&& (0 == P8) ) ||((255 == P6)&& (0 == P8) && (0 == P11)) || ((0 == P6) && (255 == P8) && (0 == P13)))) { upValley.Add(j); } if((0 == P11) && (0 == P12) && (0 == P13) && (255 == P7) && (255 == P1) && (255 == P2) && (255 == P3) && (((255 == P6) && (0 == P8)) || ((0 == P6) && (255 == P8)))) { upValley.Add(j); } } } } //get the down valley point for (int j = 2; j < (imageWidth - 2); j++) { flg = 1; for (int i = imageHeight - 3; ((i > 0) && (1 == flg)); i--) { P0 = m_DesImage[i, j - 2]; P1 = m_DesImage[i, j - 1]; P2 = m_DesImage[i, j]; P3 = m_DesImage[i, j + 1]; P4 = m_DesImage[i, j + 2]; P5 = m_DesImage[i + 1, j - 2]; P6 = m_DesImage[i + 1, j - 1]; P7 = m_DesImage[i + 1, j]; P8 = m_DesImage[i + 1, j + 1]; P9 = m_DesImage[i + 1, j + 2]; P10 = m_DesImage[i + 2, j - 2]; P11 = m_DesImage[i + 2, j - 1]; P12 = m_DesImage[i + 2, j]; P13 = m_DesImage[i + 2, j + 1]; P14 = m_DesImage[i + 2, j + 2]; if (0 == m_DesImage[i, j]) { flg = 0; if ((0 == P2) && (0 == P6) && (0 == P10) && (255 == P7) && (255 == P11) && (255 == P12) && (((0 == P3) && (0 == P4) && (255 == P8) && (255 == P9) && (255 == P13) && (255 == P14)) || ((255 == P13) && (0 == P8) && (0 == P14)) || ((0 == P13) && (0 == P8)) || ((0 == P3) && (0 == P8)) || ((0 == P3) && (0 == P9) && (255 == P8)) || ((0 == P8) && (0 == P9) && (255 == P13) && (255 == P14)))) { downValley.Add(j); } if ((0 == P2) && (0 == P8) && (0 == P14) && (255 == P13) && (255 == P12) && (255 == P7) && (((0 == P0) && (0 == P1) && (255 == P10) && (255 == P11) && (255 == P5) && (255 == P6)) || ((0 == P6) && (0 == P11)) || ((0 == P6) && (0 == P1)) || ((0 == P1) && (0 == P5) && (255 == P6)) || ((0 == P5) && (0 == P6) && (255 == P1) && (255 == P0)))) { downValley.Add(j); } if ((0 == P5) && (0 == P2)&& (0 == P9) && (255 == P11) && (255 == P12) && (255 == P13) && (255 == P7) && (((0 == P6) && (0 == P8)) || ((255 == P6) && (0 == P8) && (0 == P1)) || ((0 == P6) && (255 == P8) && (0 == P3)))) { downValley.Add(j); } if ((0 == P1) && (0 == P2) && (0 == P3) && (255 == P7) && (255 == P11) && (255 == P12) && (255 == P13) && (((255 == P6) && (0 == P8)) || ((0 == P6) && (255 == P8)))) { downValley.Add(j); } } } } Valley.upValley = upValley; Valley.downValley = downValley; Bitmap valleyBmp = ImageBinarization.GrayArrayToGrayBitmap(m_DesImage); // return valleyBmp; return Valley; } /// <summary> ///在四字符的前提下对谷点进行过滤,过滤原则: ///1.与左右边界间隔距离小于1/8字符宽度的谷点淘汰; ///2.若两个谷点之间的宽度差距小于3个像素点,则靠左侧的像素点被淘汰 /// </summary> /// <param name="valleyCollection">谷点列表集合</param> /// <param name="Boundary">图片中字符所在区域的边界</param> /// <returns>过滤后的谷点列表</returns> public static ImgValley filterImgValley(ImgValley valleyCollection, ImgBoundary Boundary, int valleyNum) { ArrayList valley = new ArrayList();//valleyCollection.upValley; ArrayList filterUpValley = new ArrayList(); ArrayList filterDownValley = new ArrayList(); foreach (var widVal in valleyCollection.upValley) { valley.Add(widVal); } foreach(var widVal in valleyCollection.downValley) { if(!(valley.Contains(widVal))) { valley.Add(widVal); } } valley.Sort(); int count = valley.Count; int leftWidth = Boundary.widthMin; int rightWidth = Boundary.widthMax; int filterWidth = (rightWidth - leftWidth) >> 3; int tmpWidth0 = 0; int tmpWidth1 = 0; int i = count - 1;//循环用变量 bool bFlg = true;//记录是否是首次进入i = count - 1的条件 do { if (bFlg && ((count - 1) == i))////首先确定最靠右侧的点,与右侧边界的距离为优先判定原则 { tmpWidth0 = (int)valley[i]; if ((rightWidth - tmpWidth0) < filterWidth) { valley.RemoveAt(i); i--; } else { i = 0;//右侧点满足条件后,再确定左侧点 bFlg = false; } } else if(0 == i)//确定最左侧的点,以与左侧边界的距离为优先判定原则 { tmpWidth0 = (int)valley[i]; if ((tmpWidth0 - leftWidth) < filterWidth) { valley.RemoveAt(i); } else { i ++; } } else if (i < (count - 1)) { tmpWidth1 = (int)valley[i]; if (((tmpWidth1 - tmpWidth0) < filterWidth) || ((rightWidth - tmpWidth1) < filterWidth)) { valley.RemoveAt(i); } else { tmpWidth0 = tmpWidth1; i ++; } } count = valley.Count; } while (((i < (count - 1)) && (count > valleyNum)) || bFlg); if(count > valleyNum) { int tmpVal = 0; tmpWidth0 = (int)valley[0]; tmpWidth1 = (int)valley[count - 1]; valley.RemoveAt(count - 1); valley.RemoveAt(0); foreach(var widVal in valley) { tmpVal += (int)widVal; } tmpVal = tmpVal / valley.Count; valley.Clear(); valley.Add(tmpWidth0); valley.Add(tmpVal); valley.Add(tmpWidth1); } foreach(var widVal in valley) { if(valleyCollection.upValley.Contains(widVal)) { filterUpValley.Add(widVal); } else { filterDownValley.Add(widVal); } } ImgValley filterValley = new ImgValley(); filterValley.upValley = filterUpValley; filterValley.downValley = filterDownValley; return filterValley; } /// <summary> ///根据谷点与边界获取图片分割路径 /// </summary> /// <param name="valleyCollection">谷点列表</param> /// <param name="Boundary">图片中字符所在区域的边界</param> /// <param name="BinaryArray">二值化图片数组</param> /// <returns>返回粘粘图片的分割路径</returns> public static ArrayList getSegmentPath(ImgValley valleyCollection, ImgBoundary Boundary, Byte[,] BinaryArray) { ArrayList segPathList = new ArrayList(); int yIndex2 = Boundary.widthMax; int yIndex1 = Boundary.widthMin; int xIndex2 = Boundary.heightMax; int xIndex1 = Boundary.heightMin; int tmpWidth = 0; ArrayList valley = new ArrayList(); foreach (var widVal in valleyCollection.upValley) { valley.Add(widVal); } foreach (var widVal in valleyCollection.downValley) { valley.Add(widVal); } valley.Sort(); int valleyNum = valley.Count; //将图像最左侧的点所在直线作为第一条分割路径 ArrayList pathList = new ArrayList(); for (int a = xIndex1; a < xIndex2; a++) { PixPos newPos = new PixPos(); newPos.widthPos = yIndex1; newPos.heightPos = a; pathList.Add(newPos); } segPathList.Add(pathList); for (int i = 0; i < valleyNum; i++) { tmpWidth = (int)valley[i]; if(valleyCollection.upValley.Contains(tmpWidth)) { pathList = dropFallUp(tmpWidth, Boundary, BinaryArray); segPathList.Add(pathList); } else { pathList = dropFallDown(tmpWidth, Boundary, BinaryArray); segPathList.Add(pathList); } } //将图像最右侧的点所在直线作为最后一条分割路径 ArrayList pathListLast = new ArrayList(); for (int a = xIndex1; a < xIndex2; a++) { PixPos newPos = new PixPos(); newPos.widthPos = yIndex2; newPos.heightPos = a; pathListLast.Add(newPos); } segPathList.Add(pathListLast); return segPathList; } /// <summary> ///根据谷点获取图片的分割路径,水滴从上向下滴落 /// </summary> /// <param name="valleyCollection">谷点列表</param> /// <param name="Boundary">图片中字符所在区域的边界</param> /// <param name="BinaryArray">二值化图片数组</param> /// <returns>返回粘粘图片的分割路径</returns> public static ArrayList dropFallUp(int widVal, ImgBoundary Boundary, Byte[,] BinaryArray) { int yIndex2 = Boundary.widthMax; int yIndex1 = Boundary.widthMin; int xIndex2 = Boundary.heightMax; int xIndex1 = Boundary.heightMin; int imageHeight = BinaryArray.GetLength(0); int imageWidth = BinaryArray.GetLength(1); ArrayList pathList = new ArrayList(); int iniPointY = widVal; int iniPointX = xIndex1;//任一分割路径的高度起始点均为有效字符出现的第一个点 byte leftRightFlg = 0;//该标志位置位时,表示情况5与情况6可能会循环出现 while ((iniPointX <= xIndex2) && (iniPointY < yIndex2)) { PixPos newPos = new PixPos(); newPos.widthPos = iniPointY; newPos.heightPos = iniPointX; pathList.Add(newPos); if (((iniPointX + 1) >= imageHeight) || ((iniPointX - 1) <= 0) || ((iniPointY + 1) >= imageWidth) || ((iniPointY - 1) <= 0)) { break; } Byte pointLeft = BinaryArray[iniPointX, (iniPointY - 1)]; Byte pointRight = BinaryArray[iniPointX, (iniPointY + 1)]; Byte pointDown = BinaryArray[(iniPointX + 1), iniPointY]; Byte pointDownLeft = BinaryArray[(iniPointX + 1), (iniPointY - 1)]; Byte pointDownRight = BinaryArray[(iniPointX + 1), (iniPointY + 1)]; if (((0 == pointLeft) && (0 == pointRight) && (0 == pointDown) && (0 == pointDownLeft) && ((0 == pointDownRight)))// all black:11111 || ((255 == pointLeft) && (255 == pointRight) && (255 == pointDown) && (255 == pointDownLeft) && ((255 == pointDownRight))//all white:00000 || ((0 == pointDownLeft) && (255 == pointDown))))//情况1与情况3 // **10* {//down iniPointX = iniPointX + 1; leftRightFlg = 0; } else if (((0 == pointDown) && (255 == pointDownLeft) && (0 == pointDownRight))//**011 || ((255 == pointLeft) && (0 == pointDown) && (255 == pointDownLeft) && (255 == pointDownRight)))//0*010 {//left down //情况2 iniPointX = iniPointX + 1; iniPointY = iniPointY - 1; leftRightFlg = 0; } else if (((0 == pointDown) && (0 == pointDownLeft) && (255 == pointDownRight))//**110 || ((0 == pointLeft) && (255 == pointRight) && (0 == pointDown) && (255 == pointDownLeft) && (255 == pointDownRight)))//10010 {//情况4 iniPointX = iniPointX + 1; iniPointY = iniPointY + 1; leftRightFlg = 0; } else if ((255 == pointRight) && (0 == pointDown) && (0 == pointDownLeft) && (0 == pointDownRight))//*0111 {//情况5 if (0 == leftRightFlg) { iniPointY = iniPointY + 1; leftRightFlg = 1; } else//标志位为1时说明上一个点出现在情况6,而本次循环的点出现在情况5,此种情况将垂直渗透 { iniPointX = iniPointX + 1; leftRightFlg = 0; } } else if ((255 == pointLeft) && (0 == pointDown) && (0 == pointDownLeft) && (0 == pointDownRight))//01111 {//情况6 if (0 == leftRightFlg) { iniPointY = iniPointY - 1; leftRightFlg = 1; } else//标志位为1时说明上一个点出现在情况5,而本次循环的点出现在情况6,此种情况将垂直渗透 { iniPointX = iniPointX + 1; leftRightFlg = 0; } } else { iniPointX = iniPointX + 1; } } return pathList; } /// <summary> ///根据谷点获取图片的分割路径,水滴从下向上滴落 /// </summary> /// <param name="valleyCollection">谷点列表</param> /// <param name="Boundary">图片中字符所在区域的边界</param> /// <param name="BinaryArray">二值化图片数组</param> /// <returns>返回粘粘图片的分割路径</returns> public static ArrayList dropFallDown(int widVal, ImgBoundary Boundary, Byte[,] BinaryArray) { int yIndex2 = Boundary.widthMax; int yIndex1 = Boundary.widthMin; int xIndex2 = Boundary.heightMax; int xIndex1 = Boundary.heightMin; int imageHeight = BinaryArray.GetLength(0); int imageWidth = BinaryArray.GetLength(1); ArrayList pathList = new ArrayList(); int iniPointY = widVal; int iniPointX = xIndex2;//任一分割路径的高度起始点均为有效字符出现的第一个点 byte leftRightFlg = 0;//该标志位置位时,表示情况5与情况6可能会循环出现 while ((iniPointX >= xIndex1) && (iniPointY < yIndex2)) { PixPos newPos = new PixPos(); newPos.widthPos = iniPointY; newPos.heightPos = iniPointX; pathList.Add(newPos); if (((iniPointX - 1) <= 0) || ((iniPointY + 1) >= imageWidth) || ((iniPointY - 1) <= 0)) { break; } Byte pointLeft = BinaryArray[iniPointX, (iniPointY - 1)]; Byte pointRight = BinaryArray[iniPointX, (iniPointY + 1)]; Byte pointDown = BinaryArray[(iniPointX - 1), iniPointY]; Byte pointDownLeft = BinaryArray[(iniPointX - 1), (iniPointY - 1)]; Byte pointDownRight = BinaryArray[(iniPointX - 1), (iniPointY + 1)]; if (((0 == pointLeft) && (0 == pointRight) && (0 == pointDown) && (0 == pointDownLeft) && ((0 == pointDownRight)))// all black 11111 || ((255 == pointLeft) && (255 == pointRight) && (255 == pointDown) && (255 == pointDownLeft) && ((255 == pointDownRight))//all white 00000 || ((0 == pointDownLeft) && (255 == pointDown))))//情况1与情况3 //**10* {//down iniPointX = iniPointX - 1; leftRightFlg = 0; } else if (((0 == pointDown) && (255 == pointDownLeft) && (0 == pointDownRight))//**011 || ((255 == pointLeft) && (0 == pointDown) && (255 == pointDownLeft) && (255 == pointDownRight)))//0*010 {//left down //情况2 iniPointX = iniPointX - 1; iniPointY = iniPointY - 1; leftRightFlg = 0; } else if (((0 == pointDown) && (0 == pointDownLeft) && (255 == pointDownRight))//**110 || ((0 == pointLeft) && (255 == pointRight) && (0 == pointDown) && (255 == pointDownLeft) && (255 == pointDownRight)))//10010 {//情况4 iniPointX = iniPointX - 1; iniPointY = iniPointY + 1; leftRightFlg = 0; } else if ((255 == pointRight) && (0 == pointDown) && (0 == pointDownLeft) && (0 == pointDownRight))//*0111 {//情况5 if (0 == leftRightFlg) { iniPointY = iniPointY + 1; leftRightFlg = 1; } else//标志位为1时说明上一个点出现在情况6,而本次循环的点出现在情况5,此种情况将垂直渗透 { iniPointX = iniPointX - 1; leftRightFlg = 0; } } else if ((255 == pointLeft) && (0 == pointDown) && (0 == pointDownLeft) && (0 == pointDownRight))//01111 {//情况6 if (0 == leftRightFlg) { iniPointY = iniPointY - 1; leftRightFlg = 1; } else//标志位为1时说明上一个点出现在情况5,而本次循环的点出现在情况6,此种情况将垂直渗透 { iniPointX = iniPointX - 1; leftRightFlg = 0; } } else { iniPointX = iniPointX - 1; } } pathList.Reverse(); return pathList; } /// <summary> ///根据分割路径分割粘粘字符并另存为位图 /// </summary> /// <param name="valleyCollection">谷点列表</param> /// <param name="Boundary">图片中字符所在区域的边界</param> /// <param name="BinaryArray">二值化图片数组</param> /// <returns>返回粘粘图片的分割路径</returns> public static Bitmap getDivideImg(Byte[,] BinaryArray, ArrayList SegPathList) { int imageHeight = BinaryArray.GetLength(0); int imageWidth = BinaryArray.GetLength(1); int segCount = SegPathList.Count; int locationVal = imageWidth / (segCount - 1); int locationPos = 0; int iniWidthLeft = 0; Byte[,] divideArray = new Byte[imageHeight, imageWidth]; for (Int32 x = 0; x < imageHeight; x++) { for (Int32 y = 0; y < imageWidth; y++) { divideArray[x, y] = 255; } } PixPos divPosRight = new PixPos(); PixPos divPosLeft = new PixPos(); ArrayList pathListLeft = new ArrayList(); ArrayList pathListRight = new ArrayList(); int indexWidthRight = 0; int indexWidthLeft = 0; int indexHeightRight = 0; int indexHeightLeft = 0; int pointIndexLeft = 0; int pointIndexRight = 0; int posCountLeft = 0; int posCountRight = 0; for (int i = 0; i < segCount - 1; i++) { pathListLeft = (ArrayList)SegPathList[i]; pathListRight = (ArrayList)SegPathList[i + 1]; posCountLeft = pathListLeft.Count; posCountRight = pathListRight.Count; //遍历左侧路径中的点,找到左侧路径点中最小的宽度值,用于辅助分割后字符的定位 divPosRight = (PixPos)pathListRight[0]; iniWidthLeft = divPosRight.widthPos; for (pointIndexLeft = 0; pointIndexLeft < posCountLeft; pointIndexLeft++) { divPosLeft = (PixPos)pathListLeft[pointIndexLeft]; if (iniWidthLeft > divPosLeft.widthPos) { iniWidthLeft = divPosLeft.widthPos; } } locationPos = 5 + locationVal * i; pointIndexLeft = 0; pointIndexRight = 0; //目前所用的滴水算法下,同一高度值最多同时对应两个宽度值 while ((pointIndexLeft < posCountLeft) && (pointIndexRight < posCountRight)) { divPosRight = (PixPos)pathListRight[pointIndexRight]; divPosLeft = (PixPos)pathListLeft[pointIndexLeft]; pointIndexLeft++; pointIndexRight++; indexWidthLeft = divPosLeft.widthPos; indexWidthRight = divPosRight.widthPos; indexHeightRight = divPosLeft.heightPos; indexHeightLeft = divPosRight.heightPos; if(pointIndexLeft < posCountLeft) { divPosLeft = (PixPos)pathListLeft[pointIndexLeft]; if (indexHeightLeft == divPosLeft.heightPos)//若下一点高度值相同,索引值加1,宽度值更新 { pointIndexLeft++; indexWidthLeft = divPosLeft.widthPos; } } if (pointIndexRight < posCountRight) { divPosRight = (PixPos)pathListRight[pointIndexRight]; if (indexHeightRight == divPosRight.heightPos)//若下一点高度值相同,索引值加1,宽度值更新 { pointIndexRight++; indexWidthRight = divPosRight.widthPos; } } if ((indexWidthRight - iniWidthLeft + locationPos) >= imageWidth) { locationPos =imageWidth - indexWidthRight + iniWidthLeft; } for (Int32 y = indexWidthLeft; y < indexWidthRight; y++) { divideArray[indexHeightRight, (y - iniWidthLeft + locationPos)] = BinaryArray[indexHeightRight, y]; } } } Bitmap GrayBmp = ImageBinarization.BinaryArrayToBinaryBitmap(divideArray); return GrayBmp; } /// <summary> ///获取字符所在区域的边界 /// </summary> /// <param name="BinaryArray">二值化图片数组</param> /// <returns>返回字符所在区域的边界</returns> public static ImgBoundary getImgBoundary(Byte[,] BinaryArray) { ImgBoundary boundary = new ImgBoundary(); int imageHeight = BinaryArray.GetLength(0); int imageWidth = BinaryArray.GetLength(1); //记录图像出现的左右极限位置 int widthLeft = 0, widthRight = 0; //记录图像出现的上下极限位置 int heightUp = 0, heightDown = 0; byte loopFlg = 0;//当该变量置为1时将跳出双重循环 //外层循环从原点开始沿宽度方向值图像右侧 for (int x = 0; (x < imageWidth) && (0 == loopFlg); x++) { //内层循环从原点开始沿高度方向值图像下方 for (int y = 0; (y < imageHeight) && (0 == loopFlg); y++) { //遇到的第一个点为最接近原点宽度方向上的第一个点,左侧极限点 if (0 == BinaryArray[y, x]) { widthLeft = x; loopFlg = 1; } } } loopFlg = 0;//当该变量置为零时将跳出双重循环 //外层循环从图像右侧沿宽度方向到原点 for (int x = (imageWidth - 1); (x > 0) && (0 == loopFlg); x--) { //内层循环从原点开始沿高度方向到图像下方 for (int y = 0; (y < imageHeight) && (0 == loopFlg); y++) { //遇到的第一个点离原点最原的右侧极限点 if (0 == BinaryArray[y, x]) { widthRight = x; loopFlg = 1; } } } loopFlg = 0;//当该变量置为零时将跳出双重循环 //外层循环从图像下方开始向上接近原点方向,高度方向 for (int x = (imageHeight - 1); (x > 0) && (0 == loopFlg); x--) { //内层循环从原点开始,沿宽度方向 for (int y = 0; (y < imageWidth) && (0 == loopFlg); y++) { //遇到的第一个点为高度方向离原点最远的点,下方极限点 if (0 == BinaryArray[x, y]) { heightDown = x; loopFlg = 1; } } } loopFlg = 0;//当该变量置为零时将跳出双重循环 //外层循环从原点开始沿高度方向至图像下方 for (int x = 0; (x < imageHeight) && (0 == loopFlg); x++) { //内层循环从原点开始沿宽度方向至图像右侧 for (int y = 0; (y < imageWidth) && (0 == loopFlg); y++) { if (0 == BinaryArray[x, y])//遇到的第一个点为高度方向离原点最远的点,上方极限点 { heightUp = x; loopFlg = 1; } } } boundary.widthMin = widthLeft; boundary.widthMax = widthRight; boundary.heightMin = heightUp; boundary.heightMax = heightDown; return boundary; } } public static class Thining { //调用此函数即可实现提取图像骨架 public static void getThinPicture(string imageSrcPath, string imageDestPath) { Bitmap bmp = new Bitmap(imageSrcPath); int Threshold = 0; Byte[,] m_SourceImage = ImageBinarization.ToBinaryArray(bmp, out Threshold); Byte[,] m_DesImage = Thining.ThinPicture(m_SourceImage); Bitmap bmpThin = ImageBinarization.BinaryArrayToBinaryBitmap(m_DesImage); bmpThin.Save(imageDestPath, System.Drawing.Imaging.ImageFormat.Jpeg); } public static int B(Byte[,] picture, int x, int y) { return picture[x, y - 1] + picture[x + 1, y - 1] + picture[x + 1, y] + picture[x + 1, y + 1] + picture[x, y + 1] + picture[x - 1, y + 1] + picture[x - 1, y] + picture[x - 1, y - 1]; } public static int A(Byte[,] picture, int x, int y) { int counter = 0; if ((picture[x, y - 1] == 0) && (picture[x + 1, y - 1] == 1)) { counter++; } if ((picture[x + 1, y - 1] == 0) && (picture[x + 1, y] == 1)) { counter++; } if ((picture[x + 1, y] == 0) && (picture[x + 1, y + 1] == 1)) { counter++; } if ((picture[x + 1, y + 1] == 0) && (picture[x, y + 1] == 1)) { counter++; } if ((picture[x, y + 1] == 0) && (picture[x - 1, y + 1] == 1)) { counter++; } if ((picture[x - 1, y + 1] == 0) && (picture[x - 1, y] == 1)) { counter++; } if ((picture[x - 1, y] == 0) && (picture[x - 1, y - 1] == 1)) { counter++; } if ((picture[x - 1, y - 1] == 0) && (picture[x, y - 1] == 1)) { counter++; } return counter; } public static Byte[,] ThinPicture(Byte[,] newPicture) { Byte[,] picture = new Byte[newPicture.GetLength(0) + 2, newPicture.GetLength(1) + 2]; Byte[,] pictureToRemove = new Byte[newPicture.GetLength(0) + 2, newPicture.GetLength(1) + 2]; bool hasChanged; for (int i = 0; i < picture.GetLength(1); i++) { for (int j = 0; j < picture.GetLength(0); j++) { picture[j, i] = 255; pictureToRemove[j, i] = 0; } } for (int i = 0; i < newPicture.GetLength(1); i++) { for (int j = 0; j < newPicture.GetLength(0); j++) { picture[j + 1, i + 1] = newPicture[j, i]; } } for (int i = 0; i < picture.GetLength(1); i++) { for (int j = 0; j < picture.GetLength(0); j++) { picture[j, i] = picture[j, i] == 0 ? picture[j, i] = 1 : picture[j, i] = 0; } } do { hasChanged = false; for (int i = 0; i < newPicture.GetLength(1); i++) { for (int j = 0; j < newPicture.GetLength(0); j++) { if ((picture[j, i] == 1) && (2 <= B(picture, j, i)) && (B(picture, j, i) <= 6) && (A(picture, j, i) == 1) && (picture[j, i - 1] * picture[j + 1, i] * picture[j, i + 1] == 0) && (picture[j + 1, i] * picture[j, i + 1] * picture[j - 1, i] == 0)) { pictureToRemove[j, i] = 1; hasChanged = true; } } } for (int i = 0; i < newPicture.GetLength(1); i++) { for (int j = 0; j < newPicture.GetLength(0); j++) { if (pictureToRemove[j, i] == 1) { picture[j, i] = 0; pictureToRemove[j, i] = 0; } } } for (int i = 0; i < newPicture.GetLength(1); i++) { for (int j = 0; j < newPicture.GetLength(0); j++) { if ((picture[j, i] == 1) && (2 <= B(picture, j, i)) && (B(picture, j, i) <= 6) && (A(picture, j, i) == 1) && (picture[j, i - 1] * picture[j + 1, i] * picture[j - 1, i] == 0) && (picture[j, i - 1] * picture[j, i + 1] * picture[j - 1, i] == 0)) { pictureToRemove[j, i] = 1; hasChanged = true; } } } for (int i = 0; i < newPicture.GetLength(1); i++) { for (int j = 0; j < newPicture.GetLength(0); j++) { if (pictureToRemove[j, i] == 1) { picture[j, i] = 0; pictureToRemove[j, i] = 0; } } } } while (hasChanged); for (int i = 0; i < newPicture.GetLength(1); i++) { for (int j = 0; j < newPicture.GetLength(0); j++) { if ((picture[j, i] == 1) && (((picture[j, i - 1] * picture[j + 1, i] == 1) && (picture[j - 1, i + 1] != 1)) || ((picture[j + 1, i] * picture[j, i + 1] == 1) && (picture[j - 1, i - 1] != 1)) || //Небольшая модификцаия алгоритма для ещё большего утоньшения ((picture[j, i + 1] * picture[j - 1, i] == 1) && (picture[j + 1, i - 1] != 1)) || ((picture[j, i - 1] * picture[j - 1, i] == 1) && (picture[j + 1, i + 1] != 1)))) { picture[j, i] = 0; } } } for (int i = 0; i < picture.GetLength(1); i++) { for (int j = 0; j < picture.GetLength(0); j++) { // picture[j, i] = picture[j, i] == 0 ? 255 : 0; if (0 == picture[j, i]) { picture[j, i] = 255; } else { picture[j, i] = 0; } } } Byte[,] outPicture = new Byte[newPicture.GetLength(0), newPicture.GetLength(1)]; for (int i = 0; i < newPicture.GetLength(1); i++) { for (int j = 0; j < newPicture.GetLength(0); j++) { outPicture[j, i] = picture[j + 1, i + 1]; } } return outPicture; } } public static class ImageBinarization { /// <summary> /// 全局阈值图像二值化 /// </summary> /// <param name="bmp">原始图像</param> /// <param name="method">二值化方法</param> /// <param name="threshold">输出:全局阈值</param> /// <returns>二值化后的图像数组</returns> public static Byte[,] ToBinaryArray(this Bitmap bmp, out Int32 threshold) { // 位图转换为灰度数组 Byte[,] GrayArray = bmp.ToGrayArray(); // 计算全局阈值 threshold = OtsuThreshold(GrayArray); // 根据阈值进行二值化 Int32 PixelHeight = bmp.Height; Int32 PixelWidth = bmp.Width; Byte[,] BinaryArray = new Byte[PixelHeight, PixelWidth]; for (Int32 i = 0; i < PixelHeight; i++) { for (Int32 j = 0; j < PixelWidth; j++) { BinaryArray[i, j] = Convert.ToByte((GrayArray[i, j] > threshold) ? 255 : 0); } } return BinaryArray; } /// <summary> /// 将位图转换为灰度数组(256级灰度) /// </summary> /// <param name="bmp">原始位图</param> /// <returns>灰度数组</returns> public static Byte[,] ToGrayArray(this Bitmap bmp) { Int32 PixelHeight = bmp.Height; // 图像高度 Int32 PixelWidth = bmp.Width; // 图像宽度 Int32 Stride = ((PixelWidth * 3 + 3) >> 2) << 2; // 跨距宽度 Byte[] Pixels = new Byte[PixelHeight * Stride]; // 锁定位图到系统内存 BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, PixelWidth, PixelHeight), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); Marshal.Copy(bmpData.Scan0, Pixels, 0, Pixels.Length); // 从非托管内存拷贝数据到托管内存 bmp.UnlockBits(bmpData); // 从系统内存解锁位图 // 将像素数据转换为灰度数组 Byte[,] GrayArray = new Byte[PixelHeight, PixelWidth]; for (Int32 i = 0; i < PixelHeight; i++) { Int32 Index = i * Stride; for (Int32 j = 0; j < PixelWidth; j++) { GrayArray[i, j] = Convert.ToByte((Pixels[Index + 2] * 19595 + Pixels[Index + 1] * 38469 + Pixels[Index] * 7471 + 32768) >> 16); Index += 3; } } return GrayArray; } /// <summary> /// 大津法计算阈值 /// </summary> /// <param name="grayArray">灰度数组</param> /// <returns>二值化阈值</returns> public static Int32 OtsuThreshold(Byte[,] grayArray) { // 建立统计直方图 Int32[] Histogram = new Int32[256]; Array.Clear(Histogram, 0, 256); // 初始化 foreach (Byte b in grayArray) { Histogram[b]++; // 统计直方图 } // 总的质量矩和图像点数 Int32 SumC = grayArray.Length; // 总的图像点数 Double SumU = 0; // 双精度避免方差运算中数据溢出 for (Int32 i = 1; i < 256; i++) { SumU += i * Histogram[i]; // 总的质量矩 } // 灰度区间 Int32 MinGrayLevel = Array.FindIndex(Histogram, NonZero); // 最小灰度值 Int32 MaxGrayLevel = Array.FindLastIndex(Histogram, NonZero); // 最大灰度值 // 计算最大类间方差 Int32 Threshold = MinGrayLevel; Double MaxVariance = 0.0; // 初始最大方差 Double U0 = 0; // 初始目标质量矩 Int32 C0 = 0; // 初始目标点数 for (Int32 i = MinGrayLevel; i < MaxGrayLevel; i++) { if (Histogram[i] == 0) continue; // 目标的质量矩和点数 U0 += i * Histogram[i]; C0 += Histogram[i]; // 计算目标和背景的类间方差 Double Diference = U0 * SumC - SumU * C0; Double Variance = Diference * Diference / C0 / (SumC - C0); // 方差 if (Variance > MaxVariance) { MaxVariance = Variance; Threshold = i; } } // 返回类间方差最大阈值 return Threshold; } /// <summary> /// 检测非零值 /// </summary> /// <param name="value">要检测的数值</param> /// <returns> /// true:非零 /// false:零 /// </returns> private static Boolean NonZero(Int32 value) { return (value != 0) ? true : false; } /// <summary> /// 将二值化数组转换为二值化图像 /// </summary> /// <param name="binaryArray">二值化数组</param> /// <returns>二值化图像</returns> public static Bitmap BinaryArrayToBinaryBitmap(Byte[,] binaryArray) { // 将二值化数组转换为二值化数据 Int32 PixelHeight = binaryArray.GetLength(0); Int32 PixelWidth = binaryArray.GetLength(1); Int32 Stride = ((PixelWidth + 31) >> 5) << 2; Byte[] Pixels = new Byte[PixelHeight * Stride]; for (Int32 i = 0; i < PixelHeight; i++) { Int32 Base = i * Stride; for (Int32 j = 0; j < PixelWidth; j++) { if (binaryArray[i, j] != 0) { Pixels[Base + (j >> 3)] |= Convert.ToByte(0x80 >> (j & 0x7)); } } } // 创建黑白图像 Bitmap BinaryBmp = new Bitmap(PixelWidth, PixelHeight, PixelFormat.Format1bppIndexed); // 设置调色表 ColorPalette cp = BinaryBmp.Palette; cp.Entries[0] = Color.Black; // 黑色 cp.Entries[1] = Color.White; // 白色 BinaryBmp.Palette = cp; // 设置位图图像特性 BitmapData BinaryBmpData = BinaryBmp.LockBits(new Rectangle(0, 0, PixelWidth, PixelHeight), ImageLockMode.WriteOnly, PixelFormat.Format1bppIndexed); Marshal.Copy(Pixels, 0, BinaryBmpData.Scan0, Pixels.Length); BinaryBmp.UnlockBits(BinaryBmpData); return BinaryBmp; } /// <summary> /// 将灰度数组转换为灰度图像(256级灰度) /// </summary> /// <param name="grayArray">灰度数组</param> /// <returns>灰度图像</returns> public static Bitmap GrayArrayToGrayBitmap(Byte[,] grayArray) { // 将灰度数组转换为灰度数据 Int32 PixelHeight = grayArray.GetLength(0); // 图像高度 Int32 PixelWidth = grayArray.GetLength(1); // 图像宽度 Int32 Stride = ((PixelWidth + 3) >> 2) << 2; // 跨距宽度 Byte[] Pixels = new Byte[PixelHeight * Stride]; for (Int32 i = 0; i < PixelHeight; i++) { Int32 Index = i * Stride; for (Int32 j = 0; j < PixelWidth; j++) { Pixels[Index++] = grayArray[i, j]; } } // 创建灰度图像 Bitmap GrayBmp = new Bitmap(PixelWidth, PixelHeight, PixelFormat.Format8bppIndexed); // 设置调色表 ColorPalette cp = GrayBmp.Palette; for (int i = 0; i < 256; i++) cp.Entries[i] = Color.FromArgb(i, i, i); GrayBmp.Palette = cp; // 设置位图图像特性 BitmapData GrayBmpData = GrayBmp.LockBits(new Rectangle(0, 0, PixelWidth, PixelHeight), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); Marshal.Copy(Pixels, 0, GrayBmpData.Scan0, Pixels.Length); GrayBmp.UnlockBits(GrayBmpData); return GrayBmp; } } } |