EyemLib.cs 60.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 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
using Microsoft.Win32.SafeHandles;
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Linq;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;

namespace eyemLib_Sharp
{
    public unsafe class EyemLib
    {

        #region 枚举

        //稳健估计方法
        public enum ROBUST_METHOD
        {
            EYEM_DIST_USER = -1,
            EYEM_DIST_L1 = 1,
            EYEM_DIST_L12 = 2,
            EYEM_DIST_FAIR = 3,
            EYEM_DIST_WELSCH = 4,
            EYEM_DIST_HUBER = 5,
            EYEM_DIST_BISQUARE = 6,
            EYEM_DIST_CAUCHY = 7,
            EYEM_DIST_LOGISTIC = 8,
            EYEM_DIST_ANDREWS = 9,
            EYEM_DIST_ATLWORTH = 10
        }

        //图像格式信息
        enum ColorConversionCodes
        {
            COLOR_BGR2BGRA = 0, //!< add alpha channel to RGB or BGR image
            COLOR_RGB2RGBA = COLOR_BGR2BGRA,

            COLOR_BGRA2BGR = 1, //!< remove alpha channel from RGB or BGR image
            COLOR_RGBA2RGB = COLOR_BGRA2BGR,

            COLOR_BGR2RGBA = 2, //!< convert between RGB and BGR color spaces (with or without alpha channel)
            COLOR_RGB2BGRA = COLOR_BGR2RGBA,

            COLOR_RGBA2BGR = 3,
            COLOR_BGRA2RGB = COLOR_RGBA2BGR,

            COLOR_BGR2RGB = 4,
            COLOR_RGB2BGR = COLOR_BGR2RGB,

            COLOR_BGRA2RGBA = 5,
            COLOR_RGBA2BGRA = COLOR_BGRA2RGBA,

            COLOR_BGR2GRAY = 6, //!< convert between RGB/BGR and grayscale, @ref color_convert_rgb_gray "color conversions"
            COLOR_RGB2GRAY = 7,
            COLOR_GRAY2BGR = 8,
            COLOR_GRAY2RGB = COLOR_GRAY2BGR,
            COLOR_GRAY2BGRA = 9,
            COLOR_GRAY2RGBA = COLOR_GRAY2BGRA,
            COLOR_BGRA2GRAY = 10,
            COLOR_RGBA2GRAY = 11,

            COLOR_BGR2BGR565 = 12, //!< convert between RGB/BGR and BGR565 (16-bit images)
            COLOR_RGB2BGR565 = 13,
            COLOR_BGR5652BGR = 14,
            COLOR_BGR5652RGB = 15,
            COLOR_BGRA2BGR565 = 16,
            COLOR_RGBA2BGR565 = 17,
            COLOR_BGR5652BGRA = 18,
            COLOR_BGR5652RGBA = 19,

            COLOR_GRAY2BGR565 = 20, //!< convert between grayscale to BGR565 (16-bit images)
            COLOR_BGR5652GRAY = 21,

            COLOR_BGR2BGR555 = 22,  //!< convert between RGB/BGR and BGR555 (16-bit images)
            COLOR_RGB2BGR555 = 23,
            COLOR_BGR5552BGR = 24,
            COLOR_BGR5552RGB = 25,
            COLOR_BGRA2BGR555 = 26,
            COLOR_RGBA2BGR555 = 27,
            COLOR_BGR5552BGRA = 28,
            COLOR_BGR5552RGBA = 29,

            COLOR_GRAY2BGR555 = 30, //!< convert between grayscale and BGR555 (16-bit images)
            COLOR_BGR5552GRAY = 31,

            COLOR_BGR2XYZ = 32, //!< convert RGB/BGR to CIE XYZ, @ref color_convert_rgb_xyz "color conversions"
            COLOR_RGB2XYZ = 33,
            COLOR_XYZ2BGR = 34,
            COLOR_XYZ2RGB = 35,

            COLOR_BGR2YCrCb = 36, //!< convert RGB/BGR to luma-chroma (aka YCC), @ref color_convert_rgb_ycrcb "color conversions"
            COLOR_RGB2YCrCb = 37,
            COLOR_YCrCb2BGR = 38,
            COLOR_YCrCb2RGB = 39,

            COLOR_BGR2HSV = 40, //!< convert RGB/BGR to HSV (hue saturation value), @ref color_convert_rgb_hsv "color conversions"
            COLOR_RGB2HSV = 41,

            COLOR_BGR2Lab = 44, //!< convert RGB/BGR to CIE Lab, @ref color_convert_rgb_lab "color conversions"
            COLOR_RGB2Lab = 45,

            COLOR_BGR2Luv = 50, //!< convert RGB/BGR to CIE Luv, @ref color_convert_rgb_luv "color conversions"
            COLOR_RGB2Luv = 51,
            COLOR_BGR2HLS = 52, //!< convert RGB/BGR to HLS (hue lightness saturation), @ref color_convert_rgb_hls "color conversions"
            COLOR_RGB2HLS = 53,

            COLOR_HSV2BGR = 54, //!< backward conversions to RGB/BGR
            COLOR_HSV2RGB = 55,

            COLOR_Lab2BGR = 56,
            COLOR_Lab2RGB = 57,
            COLOR_Luv2BGR = 58,
            COLOR_Luv2RGB = 59,
            COLOR_HLS2BGR = 60,
            COLOR_HLS2RGB = 61,

            COLOR_BGR2HSV_FULL = 66,
            COLOR_RGB2HSV_FULL = 67,
            COLOR_BGR2HLS_FULL = 68,
            COLOR_RGB2HLS_FULL = 69,

            COLOR_HSV2BGR_FULL = 70,
            COLOR_HSV2RGB_FULL = 71,
            COLOR_HLS2BGR_FULL = 72,
            COLOR_HLS2RGB_FULL = 73,

            COLOR_LBGR2Lab = 74,
            COLOR_LRGB2Lab = 75,
            COLOR_LBGR2Luv = 76,
            COLOR_LRGB2Luv = 77,

            COLOR_Lab2LBGR = 78,
            COLOR_Lab2LRGB = 79,
            COLOR_Luv2LBGR = 80,
            COLOR_Luv2LRGB = 81,

            COLOR_BGR2YUV = 82, //!< convert between RGB/BGR and YUV
            COLOR_RGB2YUV = 83,
            COLOR_YUV2BGR = 84,
            COLOR_YUV2RGB = 85,

            //! YUV 4:2:0 family to RGB
            COLOR_YUV2RGB_NV12 = 90,
            COLOR_YUV2BGR_NV12 = 91,
            COLOR_YUV2RGB_NV21 = 92,
            COLOR_YUV2BGR_NV21 = 93,
            COLOR_YUV420sp2RGB = COLOR_YUV2RGB_NV21,
            COLOR_YUV420sp2BGR = COLOR_YUV2BGR_NV21,

            COLOR_YUV2RGBA_NV12 = 94,
            COLOR_YUV2BGRA_NV12 = 95,
            COLOR_YUV2RGBA_NV21 = 96,
            COLOR_YUV2BGRA_NV21 = 97,
            COLOR_YUV420sp2RGBA = COLOR_YUV2RGBA_NV21,
            COLOR_YUV420sp2BGRA = COLOR_YUV2BGRA_NV21,

            COLOR_YUV2RGB_YV12 = 98,
            COLOR_YUV2BGR_YV12 = 99,
            COLOR_YUV2RGB_IYUV = 100,
            COLOR_YUV2BGR_IYUV = 101,
            COLOR_YUV2RGB_I420 = COLOR_YUV2RGB_IYUV,
            COLOR_YUV2BGR_I420 = COLOR_YUV2BGR_IYUV,
            COLOR_YUV420p2RGB = COLOR_YUV2RGB_YV12,
            COLOR_YUV420p2BGR = COLOR_YUV2BGR_YV12,

            COLOR_YUV2RGBA_YV12 = 102,
            COLOR_YUV2BGRA_YV12 = 103,
            COLOR_YUV2RGBA_IYUV = 104,
            COLOR_YUV2BGRA_IYUV = 105,
            COLOR_YUV2RGBA_I420 = COLOR_YUV2RGBA_IYUV,
            COLOR_YUV2BGRA_I420 = COLOR_YUV2BGRA_IYUV,
            COLOR_YUV420p2RGBA = COLOR_YUV2RGBA_YV12,
            COLOR_YUV420p2BGRA = COLOR_YUV2BGRA_YV12,

            COLOR_YUV2GRAY_420 = 106,
            COLOR_YUV2GRAY_NV21 = COLOR_YUV2GRAY_420,
            COLOR_YUV2GRAY_NV12 = COLOR_YUV2GRAY_420,
            COLOR_YUV2GRAY_YV12 = COLOR_YUV2GRAY_420,
            COLOR_YUV2GRAY_IYUV = COLOR_YUV2GRAY_420,
            COLOR_YUV2GRAY_I420 = COLOR_YUV2GRAY_420,
            COLOR_YUV420sp2GRAY = COLOR_YUV2GRAY_420,
            COLOR_YUV420p2GRAY = COLOR_YUV2GRAY_420,

            //! YUV 4:2:2 family to RGB
            COLOR_YUV2RGB_UYVY = 107,
            COLOR_YUV2BGR_UYVY = 108,
            //COLOR_YUV2RGB_VYUY = 109,
            //COLOR_YUV2BGR_VYUY = 110,
            COLOR_YUV2RGB_Y422 = COLOR_YUV2RGB_UYVY,
            COLOR_YUV2BGR_Y422 = COLOR_YUV2BGR_UYVY,
            COLOR_YUV2RGB_UYNV = COLOR_YUV2RGB_UYVY,
            COLOR_YUV2BGR_UYNV = COLOR_YUV2BGR_UYVY,

            COLOR_YUV2RGBA_UYVY = 111,
            COLOR_YUV2BGRA_UYVY = 112,
            //COLOR_YUV2RGBA_VYUY = 113,
            //COLOR_YUV2BGRA_VYUY = 114,
            COLOR_YUV2RGBA_Y422 = COLOR_YUV2RGBA_UYVY,
            COLOR_YUV2BGRA_Y422 = COLOR_YUV2BGRA_UYVY,
            COLOR_YUV2RGBA_UYNV = COLOR_YUV2RGBA_UYVY,
            COLOR_YUV2BGRA_UYNV = COLOR_YUV2BGRA_UYVY,

            COLOR_YUV2RGB_YUY2 = 115,
            COLOR_YUV2BGR_YUY2 = 116,
            COLOR_YUV2RGB_YVYU = 117,
            COLOR_YUV2BGR_YVYU = 118,
            COLOR_YUV2RGB_YUYV = COLOR_YUV2RGB_YUY2,
            COLOR_YUV2BGR_YUYV = COLOR_YUV2BGR_YUY2,
            COLOR_YUV2RGB_YUNV = COLOR_YUV2RGB_YUY2,
            COLOR_YUV2BGR_YUNV = COLOR_YUV2BGR_YUY2,

            COLOR_YUV2RGBA_YUY2 = 119,
            COLOR_YUV2BGRA_YUY2 = 120,
            COLOR_YUV2RGBA_YVYU = 121,
            COLOR_YUV2BGRA_YVYU = 122,
            COLOR_YUV2RGBA_YUYV = COLOR_YUV2RGBA_YUY2,
            COLOR_YUV2BGRA_YUYV = COLOR_YUV2BGRA_YUY2,
            COLOR_YUV2RGBA_YUNV = COLOR_YUV2RGBA_YUY2,
            COLOR_YUV2BGRA_YUNV = COLOR_YUV2BGRA_YUY2,

            COLOR_YUV2GRAY_UYVY = 123,
            COLOR_YUV2GRAY_YUY2 = 124,
            //CV_YUV2GRAY_VYUY    = CV_YUV2GRAY_UYVY,
            COLOR_YUV2GRAY_Y422 = COLOR_YUV2GRAY_UYVY,
            COLOR_YUV2GRAY_UYNV = COLOR_YUV2GRAY_UYVY,
            COLOR_YUV2GRAY_YVYU = COLOR_YUV2GRAY_YUY2,
            COLOR_YUV2GRAY_YUYV = COLOR_YUV2GRAY_YUY2,
            COLOR_YUV2GRAY_YUNV = COLOR_YUV2GRAY_YUY2,

            //! alpha premultiplication
            COLOR_RGBA2mRGBA = 125,
            COLOR_mRGBA2RGBA = 126,

            //! RGB to YUV 4:2:0 family
            COLOR_RGB2YUV_I420 = 127,
            COLOR_BGR2YUV_I420 = 128,
            COLOR_RGB2YUV_IYUV = COLOR_RGB2YUV_I420,
            COLOR_BGR2YUV_IYUV = COLOR_BGR2YUV_I420,

            COLOR_RGBA2YUV_I420 = 129,
            COLOR_BGRA2YUV_I420 = 130,
            COLOR_RGBA2YUV_IYUV = COLOR_RGBA2YUV_I420,
            COLOR_BGRA2YUV_IYUV = COLOR_BGRA2YUV_I420,
            COLOR_RGB2YUV_YV12 = 131,
            COLOR_BGR2YUV_YV12 = 132,
            COLOR_RGBA2YUV_YV12 = 133,
            COLOR_BGRA2YUV_YV12 = 134,

            //! Demosaicing
            COLOR_BayerBG2BGR = 46,
            COLOR_BayerGB2BGR = 47,
            COLOR_BayerRG2BGR = 48,
            COLOR_BayerGR2BGR = 49,

            COLOR_BayerBG2RGB = COLOR_BayerRG2BGR,
            COLOR_BayerGB2RGB = COLOR_BayerGR2BGR,
            COLOR_BayerRG2RGB = COLOR_BayerBG2BGR,
            COLOR_BayerGR2RGB = COLOR_BayerGB2BGR,

            COLOR_BayerBG2GRAY = 86,
            COLOR_BayerGB2GRAY = 87,
            COLOR_BayerRG2GRAY = 88,
            COLOR_BayerGR2GRAY = 89,

            //! Demosaicing using Variable Number of Gradients
            COLOR_BayerBG2BGR_VNG = 62,
            COLOR_BayerGB2BGR_VNG = 63,
            COLOR_BayerRG2BGR_VNG = 64,
            COLOR_BayerGR2BGR_VNG = 65,

            COLOR_BayerBG2RGB_VNG = COLOR_BayerRG2BGR_VNG,
            COLOR_BayerGB2RGB_VNG = COLOR_BayerGR2BGR_VNG,
            COLOR_BayerRG2RGB_VNG = COLOR_BayerBG2BGR_VNG,
            COLOR_BayerGR2RGB_VNG = COLOR_BayerGB2BGR_VNG,

            //! Edge-Aware Demosaicing
            COLOR_BayerBG2BGR_EA = 135,
            COLOR_BayerGB2BGR_EA = 136,
            COLOR_BayerRG2BGR_EA = 137,
            COLOR_BayerGR2BGR_EA = 138,

            COLOR_BayerBG2RGB_EA = COLOR_BayerRG2BGR_EA,
            COLOR_BayerGB2RGB_EA = COLOR_BayerGR2BGR_EA,
            COLOR_BayerRG2RGB_EA = COLOR_BayerBG2BGR_EA,
            COLOR_BayerGR2RGB_EA = COLOR_BayerGB2BGR_EA,

            //! Demosaicing with alpha channel
            COLOR_BayerBG2BGRA = 139,
            COLOR_BayerGB2BGRA = 140,
            COLOR_BayerRG2BGRA = 141,
            COLOR_BayerGR2BGRA = 142,

            COLOR_BayerBG2RGBA = COLOR_BayerRG2BGRA,
            COLOR_BayerGB2RGBA = COLOR_BayerGR2BGRA,
            COLOR_BayerRG2RGBA = COLOR_BayerBG2BGRA,
            COLOR_BayerGR2RGBA = COLOR_BayerGB2BGRA,

            COLOR_COLORCVT_MAX = 143
        };

        #endregion

        #region 结构体
        // 图像信息
        [StructLayout(LayoutKind.Sequential)]
        public struct EyemImage
        {
            public IntPtr vpImage;                     // 地址
            public int iWidth;                         // 图像内存 x 方向大小
            public int iHeight;                        // 图像内存 y 方向大小
            public int iDepth;                         // 图像位深度(详见说明)
            public int iChannels;                      // 图像通道数
        }

        // 矩形定义
        [StructLayout(LayoutKind.Sequential)]
        public struct EyemRect
        {
            public int iXs;                            // 起始点(左上角) x 坐标
            public int iYs;                            // 起始点(左上角) y 坐标
            public int iWidth;                         // x 方向大小(宽度)
            public int iHeight;                        // y 方向大小(高度)
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct EyemRect2
        {
            int iXs;                            // 起始点(左上角) x 坐标
            int iYs;                            // 起始点(左上角) y 坐标
            int iXe;                            // 端点(右下) x 坐标
            int iYe;                            // 端点(右下) y 坐标
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct EyemRect3
        {
            public int iXs;                            // 起始点(左上角) x 坐标
            public int iYs;                            // 起始点(左上角) y 坐标
            public int iWidth;                         // x 方向大小(宽度)
            public int iHeight;                        // y 方向大小(高度)
            public double dVar;                        // 某种可能会使用的值
        }

        ///////////////////////////////////////////////////////////////////////////////
        // Orthogonal Coordinate System

        /////////////////////
        // int type
        //
        [StructLayout(LayoutKind.Sequential)]
        public struct EyemOcsIXY
        {
            int iX;                             // X坐标
            int iY;                             // Y坐标
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct EyemOcsIXYZ
        {
            int iX;                             // X坐标
            int iY;                             // Y坐标
            int iZ;                             // Z坐标
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct EyemOcsIXYQ
        {
            int iX;                             // X坐标
            int iY;                             // Y坐标
            int iQ;                             // θ
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct EyemOcsIXYR               // 用于表示圆
        {
            int iX;                             // X坐标
            int iY;                             // Y坐标
            int iR;                             // 半径
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct EyemOcsIABC               // 用于表示直线(一般形式)
        {
            int iA;                             // a
            int iB;                             // b
            int iC;                             // c
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct EyemOcsIRQ                // 用于表示直线(黑森标准形式)或矢量
        {
            int iR;                             // ρ
            int iQ;                             // θ
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct EyemOcsIXYQS
        {
            int iX;                             // X坐标(单位:像素)
            int iY;                             // Y坐标(单位:像素)
            int iQ;                             // 斜率(単位:rad)
            int iS;                             // 刻度
        }


        /////////////////////
        // float type
        //
        [StructLayout(LayoutKind.Sequential)]
        public struct EyemOcsFXY
        {
            float fX;                               // X坐标
            float fY;                               // Y坐标
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct EyemOcsFXYZ
        {
            float fX;                               // X坐标
            float fY;                               // Y坐标
            float fZ;                               // Z坐标
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct EyemOcsFXYQ
        {
            float fX;                               // X坐标
            float fY;                               // Y坐标
            float fQ;                               // θ
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct EyemOcsFXYR                   // 用于表示圆
        {
            float fX;                               // X坐标
            float fY;                               // Y坐标
            float fR;                               // 半径
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct EyemOcsFABC                   // 用于表示直线(一般形式)
        {
            float fA;                               // a
            float fB;                               // b
            float fC;                               // c
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct EyemOcsFRQ                    // 用于表示直线(黑森标准形式)或矢量
        {
            float fR;                               // ρ
            float fQ;                               // θ
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct EyemOcsFXYQS
        {
            float fX;                               // X坐标(単位:像素)
            float fY;                               // Y坐标(単位:像素)
            float fQ;                               // 斜率(単位:rad)
            float fS;                               // 刻度
        }


        /////////////////////
        // double type
        //
        [StructLayout(LayoutKind.Sequential)]
        public struct EyemOcsDXY
        {
            public double dX;                              // X坐标
            public double dY;                              // Y坐标
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct EyemOcsDXYZ
        {
            double dX;                              // X坐标
            double dY;                              // Y坐标
            double dZ;                              // Z坐标
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct EyemOcsDXYQ
        {
            double dX;                              // X坐标
            double dY;                              // Y坐标
            double dQ;                              // θ
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct EyemOcsDXYR                   // 用于表示圆
        {
            double dX;                              // 中心的X坐标
            double dY;                              // 中心的Y坐标
            double dR;                              // 半径
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct EyemOcsDABC                   // 直线(一般形)的表现形式
        {
            double dA;                              // a
            double dB;                              // b
            double dC;                              // c
        }


        [StructLayout(LayoutKind.Sequential)]
        public struct EyemOcsDRQ                    // 用于表示直线(黑森标准形状)和矢量
        {
            double dR;                              // ρ
            double dQ;                              // θ
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct EyemOcsDXYQS
        {
            double dX;                              // X坐标
            double dY;                              // Y坐标
            double dQ;                              // 旋转角度(単位:rad)
            double dS;                              // 规模
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct EyemOcsDABCD                  // 用于表示平面(一般形式)
        {
            double dA;                              // a
            double dB;                              // b
            double dC;                              // c
            double dD;                              // d
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct EyemOcsDXYLSQ                 // 用于表示椭圆
        {
            double dXo;                             // 中心X坐标
            double dYo;                             // 中心Y坐标
            double dL;                              // 长轴半径
            double dS;                              // 短轴半径
            double dQ;                              // 长轴倾斜角(単位:rad)
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct EyemOcsDPV                        // 用于表示三维空间中的直线
        {
            EyemOcsDXYZ tP;                             // 直线上一点的坐标
            EyemOcsDXYZ tV;                             // 直线方向矢量
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct EyemOcsDCRUVW                     // 用于表示椭圆体
        {
            EyemOcsDXYZ tC;                             // 椭圆体中心
            EyemOcsDXYZ tR;                             // 轴半径(dX:长轴、dY:中轴、dZ:短轴)
            double dU;                                  // 长轴投影到 XY 平面与 X 轴的角(单位:rad)
            double dV;                                  // 长轴与XY平面之角(单位:rad)
            double dW;                                  // 绕长轴旋转角度(单位:rad)
        }

        // Blob 分析结果
        [StructLayout(LayoutKind.Sequential)]
        public struct EyemBinBlob
        {
            public int iLabel;                          // 标签
            public int iArea;                           // 面积
            public double dCenterX;                     // 重心x坐标
            public double dCenterY;                     // 重心y坐标
            public int iXs, iYs, iXe, iYe;              // 外接矩形(始点,终点)
            public int iWidth, iHeight;                 // 外接矩形(x 方向大小(宽度),y 方向大小(高度))
            public double dTheta;                       // 主轴倾斜角(rad)
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct EyemChainCode
        {
            int iLabel;                         // 标签
            double dX;                          // x坐标
            double dY;                          // y坐标
            double dVx, dVy;                    // 向量
        }

        // 条码 解码结果
        [StructLayout(LayoutKind.Sequential)]
        public struct EyemBarCode
        {
            public double dAngle;                      // 角度
            public int iCenterX;                       // x坐标
            public int iCenterY;                       // y坐标
            public IntPtr hType;                       // 码类型
            public IntPtr hText;                       // 码内容
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct EyemModelID
        {
            public IntPtr vpImage;                      // 地址
            public int iXs;                             // 图像X坐标
            public int iYs;                             // 图像Y坐标
            public int iWidth;                          // 图像内存X方向大小
            public int iHeight;                         // 图像内存Y方向大小
            public double dMatchDeg;                    // 匹配度
            public IntPtr lpszName;                     // 名称
        }
        #endregion

        #region 通用

        // Win32 memory copy function
        [DllImport("ntdll.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern byte* memcpy(byte* dst, byte* src, int count);
        //读取图像,支持彩色与多深度
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemImageRead(string filename, int iFalgs, out EyemImage tpImage);
        //读取视频,支持彩色
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemVideoCapture(string filename, out VideoHandle hObject, out EyemImage* tpImages, out int ipNum);
        //释放视频句柄
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern bool eyemVideoCaptureFree(IntPtr hObject);
        //读取Raw格式图像,仅支持8/16位
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemImageReadRaw(string filename, int iWidth, int iHeight, int iDepth, out EyemImage tpImage);
        //创建图像(uint8_t、int8_t、uint16_t、int16_t、int32_t、float_t、double_t)
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemImageMalloc(int iWidth, int iHeight, int iChannels, string ccSubType, out EyemImage tpImage);
        //拷贝图像(必须预分配空间)
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemImageCopy(EyemImage tpImage, ref EyemImage tpDstImg);
        //图像颜色空间转换
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemCvtImageColor(EyemImage tpImage, ColorConversionCodes iCCodes, ref EyemImage tpDstImg);
        //图像数据格式转换(uint8_t、int8_t、uint16_t、int16_t、int32_t、float_t、double_t)
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemCvtImageType(EyemImage tpImage, string ccSubType, double alpha, double beta, ref EyemImage tpDstImg);
        //图像相加
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemImageAdd(EyemImage tpImage1, EyemImage tpImage2, ref EyemImage tpDstImg);
        //图像相减
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemImageSub(EyemImage tpImageMinuend, EyemImage tpImageSubtrahend, ref EyemImage tpDstImg);
        //图像相除
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemImageDiv(EyemImage tpImage1, EyemImage tpImage2, ref EyemImage tpDstImg);
        //图像取绝对值
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemImageAbs(EyemImage tpImage, ref EyemImage tpDstImg);
        //释放图像资源
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern void eyemImageFree(ref EyemImage tpImage);
        // 设定日志回调
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern void setLogCallback(TCallBack cb);

        //测试用
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern void eyemOpenWindow(string winname);

        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern void eyemCloseWindow(string winname);

        #endregion

        #region 2  blob 分析

        //二值化图像
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemBinAutoThreshold(EyemImage tpImage, double dSigma, int iLightDark, int binMethod, out EyemImage tpDstImg);
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemBinThreshold(EyemImage tpSrsImg, int iLightDark, int binMethod, out EyemImage tpDstImg);
        //Sauvola二值化
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemBinNiBlack(EyemImage tpSrcImg, out EyemImage tpDstImg, int iType, int iWinSize, double dK, int binarizationMethod, double r);
        //动态阈值
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemBinDynThreshold(EyemImage tpSrcImg, EyemImage tpThresholdImg, int iOffset, int iLightDark, out EyemImage tpDstImg);
        //二值Blob分析
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemBinBlob(EyemImage tpImage, out BlobHandle hObject, int iAreaThrs, out EyemBinBlob* tpResult, out int ipNum, out EyemImage tpDstImage);
        //释放Blob资源
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern bool eyemBinFree(IntPtr hObject);

        #endregion

        #region 矩阵运算

        //创建二维矩阵
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern IntPtr eyemMatMallocMatrix(int iRows, int iCols, [In]double[] dpData);
        //释放二维矩阵
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern void eyemMatFreeMatrix(IntPtr intPtr);
        //初始化矩阵
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern void eyemMatZero(IntPtr vpA);
        //复制矩阵
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern void eyemMatCopy(IntPtr vpDst, IntPtr vpSrc);
        //矩阵相加
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern void eyemMatAdd(IntPtr vpA, IntPtr vpB, IntPtr vpC);

        #endregion

        #region  一维边缘测量

        //边缘测量
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemEdge1dGenMeasureRect(EyemImage tpImage, EyemOcsDXY tpLineSt, EyemOcsDXY tpLineEd, int iWhRoi, string strSubType, int iTransition, double dSigma, double dAmpThresh, out MeasureHandle hObject);
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        //边缘查找
        private static extern int eyemEdge1dGenPosRect(EyemImage tpImage, EyemOcsDXY tpLineSt, EyemOcsDXY tpLineEd, int iWhRoi, int iTransition, double dSigma, double dAmpThresh, out MeasureHandle hObject);
        //findLine
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemEdge1dFindLine(EyemImage tpImage, EyemOcsDXY tpLineSt, EyemOcsDXY tpLineEd, int iWhRoi, int iTransition, out MeasureHandle hObject);
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemSkeleton(EyemImage tpImage);
        //边缘
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemEdgesPixel(EyemImage tpImage, double dThresh);
        //释放工具所使用句柄
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern bool eyemEdge1dGenMeasureFree(IntPtr hObject);
        #endregion

        #region 稳健估计
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemRobustFitLine(int iPtnNum, IntPtr taPoint, int iCalcMode, double dRobustCoef, ref EyemOcsDABC tpLine);

        #endregion

        #region 项目
        //普通器件
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemCountObject(EyemImage tpImage, EyemRect tpRoi, string fileName, double dOffset, int iMinArea, int iMaxArea, int iWinSize, ref string pNumObj, out EyemImage tpDstImg);
        //异型器件
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemCountObjectIrregularParts(EyemImage tpImage, EyemRect tpRoi, string fileName, double dOffset, string strType, int iMaxArea, int iWinSize, ref string pNumObj, out EyemImage tpDstImg);
        //普通器件(新版本)
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemCountObjectE(EyemImage tpImage, EyemRect tpRoi, string fileName, ref string pNumObj, out EyemImage tpDstImg);
        //异型器件(新版本)
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemCountObjectIrregularPartsE(EyemImage tpImage, EyemRect tpRoi, string fileName, string ccTplName, IntPtr hModelID, ref string pNumObj, out EyemImage tpDstImg);
        //创建模板匹配模型
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemCreateTemplateModel(EyemImage tpImage, EyemRect tpRoi, double dMinScore, string ccTplName);
        //获取模板图像
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemAchvTemplateImage(EyemImage tpImage, EyemRect tpRoi, out EyemImage tpDstImg);
        //选取最匹配模板
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemMatchTemplateModel(EyemImage tpImage, IntPtr hModelID, ref string lpszTplName);
        //加载模板到内存
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemInitModel(string ccTplName, out IntPtr hModelID);
        //通过名称获取模板
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemAchvModelByName(string ccTplName, IntPtr hModelID, ref EyemModelID tpModelID);
        //插入模板
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemInsertModel(IntPtr hModelID, string ccTplName);
        //通过名称移除模板
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemRemoveModelByName(IntPtr hModelID, string ccTplName);
        //释放模板内存
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemReleaseModel(ref IntPtr hModelID);
        //读码程序
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemDetectAndDecode(EyemImage tpImage, EyemRect tpRoi, string fileName, string strCodeType, out DataCodeHandle hObject, out EyemBarCode* tpResults, out int ipNum, bool bUseNiBlack, int iBlockSize, int iRangeC, int iSymbolMin, int iSymbolMax, double dScaleUpAndDown = 0.5, double dToleErr = 0.5, double dMinorStep = 1.0);
        //释放解码句柄
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern bool eyemDetectAndDecodeFree(IntPtr hObject);
        //背景变化跟踪
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemTrackFeature(EyemImage tpRefImg, EyemImage tpNextImg, IntPtr tpArray, int iArraySize, IntPtr ipResults, out EyemImage tpDstImg);
        //插件机
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemAOIForTSAV(EyemImage tpRefImg, EyemImage tpNextImg, IntPtr tpArray, int iArraySize);
        //跳过程序执行
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int setSkipProcessID(int pid);
        #endregion

        #region 日志功能
        // 日志回调
        public delegate void TCallBack(string msg);
        public static TCallBack sld = new TCallBack(TLogCallback);

        public static event TCallBack OnNewLogCallback;

        public static void TLogCallback(string msg)
        {
            OnNewLogCallback?.Invoke(msg);
        }

        public static log4cpp.LogManager logcpp = null;

        public static void Init()
        {
            logcpp = new log4cpp.LogManager("D:\\日志\\", "_运行日志", log4cpp.GenerateMode.ByEveryDay);

            setLogCallback(sld);

            OnNewLogCallback += new TCallBack(EyemLib_OnNewLogCallback);
        }

        public static void Free()
        {
            setLogCallback(null); sld = null;
        }

        //记录日志
        private static void EyemLib_OnNewLogCallback(string msg)
        {
            logcpp.WriteLog("[" + Thread.CurrentThread.ManagedThreadId.ToString("X16") + "]" + msg);
        }

        //设置跳过程序执行
        public static void setSkipProcess(int pid)
        {
            setSkipProcessID(pid);
        }

        #endregion

        public static void eyemReadImageTool(string fileName)
        {
            Stopwatch sw = new Stopwatch();
            sw.Restart();

            EyemImage image;
            EyemImage tpDstImg = new EyemImage();
            //int flag = eyemImageRead("D:\\图片及统计结果\\图片及统计结果\\data\\6D551\\6D551-R014212020040601587-20200624140637.png", 0, out ucpImage);
            //int flag = eyemImageReadRaw(fileName, 3072, 3072, 2, out ucpImage);
            int flag = eyemImageRead(fileName, -1, out image);
            if (flag != 0)
            {
                Console.WriteLine("读图失败!");
                return;
            }

            //flag = eyemImageMalloc(image.iWidth, image.iHeight, 1, "uint16_t", out image1);
            //flag = eyemImageMalloc(image.iWidth, image.iHeight, 1, "int8_t", out image2);

            //flag = eyemImageAdd(image1, image2, ref tpDstImg);

            //flag = eyemCvtImageColor(image, (int)ColorConversionCodes.COLOR_BGR2RGB, ref tpDstImg);

            //flag = eyemCvtImageType(image2, "uint16_t", 1.0, 128, ref tpDstImg);

            //flag = eyemImageSub(image1, image2, ref tpDstImg);

            //flag = eyemImageAbs(image1, ref tpDstImg);


            #region Test Blob
            //int ipNum;
            //BlobHandle hObject;
            //EyemBinBlob* eyemBlob;
            //eyemBinBlob(image, out hObject, 100, out eyemBlob, out ipNum, out tpDstImg);
            //for (int i = 0; i < ipNum; i++)
            //{
            //    //tpResult.Add(eyemBlob[i]);
            //}
            //hObject.Dispose();
            #endregion

            #region Test 1DEdge
            //EyemOcsDXY tpLineSt = new EyemOcsDXY();
            //tpLineSt.dX = 947;
            //tpLineSt.dY = 1;
            //EyemOcsDXY tpLineEd = new EyemOcsDXY();
            //tpLineEd.dX = 149;
            //tpLineEd.dY = 548;

            //MeasureHandle hObject;
            //eyemEdge1dGenMeasureRect(ucpImage, tpLineSt, tpLineEd, 10, "all", 0, 0.9, 30, out hObject);
            //eyemEdge1dGenPosRect(ucpImage, tpLineSt, tpLineEd, 50, 0, 0.9, 30, out hObject);
            //eyemEdge1dFindLine(ucpImage, tpLineSt, tpLineEd, 15, 0, out hObject);
            ///hObject.Dispose();
            #endregion

            #region  Test Matrix

            //double[] dpData = new double[9] { 1, 0, 1, 2, 2, 2, 3, 3, 3 };
            //IntPtr vpA = eyemMatMallocMatrix(3, 3, dpData);

            //eyemMatZero(vpA);

            //double[] dpData2 = new double[9] { 1, 0, 1, 2, 0, 2, 3, 0, 3 };

            //IntPtr vpB = eyemMatMallocMatrix(3, 3, dpData);

            //IntPtr vpC = IntPtr.Zero;
            //eyemMatAdd(vpA, vpB, vpC);

            //eyemMatFreeMatrix(vpA);

            //eyemMatFreeMatrix(vpB);


            #endregion

            #region Test Binary
            //eyemBinAutoThreshold(ucpImage, 1.8, 1, 6, out tpDstImg);
            //eyemSkeleton(ucpImage);
            //eyemBinBinaryImage(ucpImage, 0, 3, out tpDtImg);
            //eyemBinDynThreshold(ucpImage, ucpImage, 3, 1, out tpDstImg);
            //eyemBinNiBlack(ucpImage, out tpDstImg, 0, 11, 0.5, 3, 128);
            #endregion

            #region Test Sauvola

            //eyemBinNiBlack(ucpImage, out tpDstImg, 0, 5, 0.001, 3, 5);

            #endregion

            #region Test RobustFitLine

            //EyemOcsDABC tpLine = new EyemOcsDABC();
            //EyemOcsDXY taPoint = new EyemOcsDXY();
            //List<EyemOcsDXY> taPoints = new List<EyemOcsDXY>();

            //double k = 2.0, b = 1.0;//y=2x+1
            //for (int i = 0; i < 100; i += 5)
            //{
            //    taPoint.dX = i;
            //    taPoint.dY = k * taPoint.dX + b;
            //    taPoints.Add(taPoint);
            //}
            //taPoint.dX = 50;
            //taPoint.dY = 45;
            //taPoints.Add(taPoint);

            //taPoint.dX = 60;
            //taPoint.dY = 76;
            //taPoints.Add(taPoint);

            //taPoint.dX = 38;
            //taPoint.dY = 58;
            //taPoints.Add(taPoint);


            //taPoint.dX = 71;
            //taPoint.dY = 37;
            //taPoints.Add(taPoint);

            //taPoint.dX = 15;
            //taPoint.dY = 50;
            //taPoints.Add(taPoint);

            //taPoint.dX = 36;
            //taPoint.dY = 56;
            //taPoints.Add(taPoint);

            //taPoint.dX = 18;
            //taPoint.dY = 8;
            //taPoints.Add(taPoint);


            //taPoint.dX = 100;
            //taPoint.dY = 100;
            //taPoints.Add(taPoint);

            //taPoint.dX = 16;
            //taPoint.dY = 16;
            //taPoints.Add(taPoint);

            //taPoint.dX = 18;
            //taPoint.dY = 28;
            //taPoints.Add(taPoint);


            //taPoint.dX = 17;
            //taPoint.dY = 37;
            //taPoints.Add(taPoint);

            //taPoint.dX = 28;
            //taPoint.dY = 48;
            //taPoints.Add(taPoint);


            //IntPtr tpPoint = teStructArray2IntPtr(taPoints.ToArray(), taPoints.Count);
            //eyemRobustFitLine(taPoints.Count, tpPoint, 6, 0, ref tpLine);
            //Marshal.FreeHGlobal(tpPoint);
            #endregion

            #region Test WriteImage
            //eyemImageWrite("D:\\Matlab测试图像\\xxx.bmp", tpDstImg);
            #endregion

            #region Test Edge
            //eyemEdgesPixel(ucpImage, 15);
            #endregion


            EyemRect tpRoi = new EyemRect();
            tpRoi.iXs = 200; tpRoi.iYs = 200;
            tpRoi.iWidth = image.iWidth - 400;
            tpRoi.iHeight = image.iHeight - 400;

            //
            string pNumObj = "";
            string file = fileName.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries)[2];

            //获取用于制作模板的图像
            flag = eyemAchvTemplateImage(image, tpRoi, out tpDstImg);

            //Bitmap bitmap = eyemCvtToBitmap(tpDstImg);

            //if (bitmap != null)
            //{
            //    bitmap.Save(System.Windows.Forms.Application.StartupPath + "\\ResOut\\" + file);
            //}

            //创建模板匹配模型
            EyemRect tpRoi2 = new EyemRect();
            tpRoi2.iXs = 1553; tpRoi2.iYs = 733;
            tpRoi2.iWidth = 28;
            tpRoi2.iHeight = 9;

            //double matchDeg = 0.80;
            //flag = eyemCreateTemplateModel(tpDstImg, tpRoi2, matchDeg, "D:\\模板文件\\" + file.Replace(".png", ".tpl"));

            //加载模板到内存
            IntPtr hModelID = IntPtr.Zero;

            flag = eyemInitModel("D:\\模板文件", out hModelID);

            string selectModel = "";
            flag = eyemMatchTemplateModel(tpDstImg, hModelID, ref selectModel);

            //插入模板
            flag = eyemInsertModel(hModelID, "D:\\模板文件及图像\\df871193-6632-48f9-abfe-540c3fc49c3f.tpl");

            string[] tpModels = selectModel.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            if (tpModels.Length <= 0)
            {
                logcpp.WriteLog("选择模板少于0");
                return;
            }

            //根据名称获取模板
            EyemModelID tpModelID = new EyemModelID();
            eyemAchvModelByName(tpModels[0], hModelID, ref tpModelID);

            //EyemImage tpModeImg = new EyemImage();
            //tpModeImg.iChannels = 1; tpModeImg.iDepth = 0;
            //tpModeImg.iWidth = tpModelID.iWidth; tpModeImg.iHeight = tpModelID.iHeight; tpModeImg.vpImage = tpModelID.vpImage;

            //Bitmap bitmap = eyemCvtToBitmap(tpModeImg);

            //if (bitmap != null)
            //{
            //    bitmap.Save(System.Windows.Forms.Application.StartupPath + "\\ResOut\\" + file);
            //}

            //最好释放掉,如果对象供其他接口使用要先释放
            eyemImageFree(ref tpDstImg);

            //"IP_SMALL_PARTS","IP_LARGE_PARTS","IP_LONG_PARTS",""
            //eyemCountObject(image, tpRoi, file.Replace(".png", ""), 35, 0, 100, 5, ref pNumObj, out tpDstImg);
            //eyemCountObjectIrregularParts(image, tpRoi, file.Replace(".png", ""), 0.1, "IP_LARGE_PARTS", 100, 7, ref pNumObj, out tpDstImg);
            //eyemCountObjectE(image, tpRoi, fileName, ref pNumObj, out tpDstImg);
            eyemCountObjectIrregularPartsE(image, tpRoi, file.Replace(".png", ""), tpModels[0], hModelID, ref pNumObj, out tpDstImg);
            //eyemCountObjectIrregularPartsE(image, tpRoi, file.Replace(".png", ""), "D:\\模板文件\\" + /*file.Replace(".png", ".tpl")*/"74d571ed-9fd4-4959-85dd-3195261e4b48.tpl", ref pNumObj, out tpDstImg);

            //移除模板
            flag = eyemRemoveModelByName(hModelID, "D:\\模板文件及图像\\df871193-6632-48f9-abfe-540c3fc49c3f.tpl");

            //Bitmap bitmap = eyemCvtToBitmap(tpDstImg);

            //if (bitmap != null)
            //{
            //    bitmap.Save(System.Windows.Forms.Application.StartupPath + "\\ResOut\\" + file);
            //}

            ////<解码测试
            //int ipNum; EyemBarCode* tpResults;
            //DataCodeHandle hObject;
            //int iRes = eyemDetectAndDecode(image, tpRoi, file.Replace(".png", ""), "QR_CODE|DATA_MATRIX", out hObject, out tpResults, out ipNum, false, 11, 5, 128, 256);
            //for (int i = 0; i < ipNum; i++)
            //{
            //    Console.WriteLine("类型:" + Marshal.PtrToStringAnsi(tpResults[i].hType) + ";坐标" + "[" + tpResults[i].iCenterX.ToString() + "," + tpResults[i].iCenterY.ToString() + "]" + ";角度:" + tpResults[i].dAngle.ToString("F4") + "," + ";内容:" + Marshal.PtrToStringAnsi(tpResults[i].hText) + "");
            //    Marshal.FreeCoTaskMem(tpResults[i].hText); Marshal.FreeCoTaskMem(tpResults[i].hType);
            //}
            //hObject.Dispose();

            sw.Stop();
            Console.WriteLine("耗时:" + sw.ElapsedMilliseconds.ToString() + ",结果:" + pNumObj);

            //在关闭程序时释放
            eyemReleaseModel(ref hModelID);
            //free image
            eyemImageFree(ref tpDstImg);
            eyemImageFree(ref image);
        }

        public static void eyemReadImageToolE(string fileName, IntPtr hModelID)
        {
            Stopwatch sw = new Stopwatch();
            sw.Restart();

            EyemImage image;
            EyemImage tpDstImg = new EyemImage();

            int flag = eyemImageRead(fileName, -1, out image);
            if (flag != 0)
            {
                Console.WriteLine("读图失败!");
                return;
            }

            EyemRect tpRoi = new EyemRect();
            tpRoi.iXs = 50; tpRoi.iYs = 50;
            tpRoi.iWidth = image.iWidth - 100;
            tpRoi.iHeight = image.iHeight - 100;

            //
            string pNumObj = "";
            string file = fileName.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries)[2];

            //获取用于制作模板的图像
            flag = eyemAchvTemplateImage(image, tpRoi, out tpDstImg);

            //测试插入模板
            //eyemInsertModel(hModelID, "D:\\模板文件及图像\\df871193-6632-48f9-abfe-540c3fc49c3f.tpl");

            ////测试获取模板
            //EyemModelID tpModelID0 = new EyemModelID();
            //eyemAchvModelByName("D:\\模板文件及图像\\df871193-6632-48f9-abfe-540c3fc49c3f.tpl", hModelID, ref tpModelID0);

            string selectModel = "";
            flag = eyemMatchTemplateModel(tpDstImg, hModelID, ref selectModel);

            //根据名称获取模板
            EyemModelID tpModelID = new EyemModelID();
            eyemAchvModelByName(selectModel.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0], hModelID, ref tpModelID);

            EyemImage tpModeImg = new EyemImage();
            tpModeImg.iChannels = 1; tpModeImg.iDepth = 0;
            tpModeImg.iWidth = tpModelID.iWidth; tpModeImg.iHeight = tpModelID.iHeight; tpModeImg.vpImage = tpModelID.vpImage;

            string str = Marshal.PtrToStringAnsi(tpModelID.lpszName);

            Bitmap bitmap = eyemCvtToBitmap(tpModeImg);

            bitmap.Dispose();

            //if (bitmap != null)
            //{
            //    bitmap.Save(System.Windows.Forms.Application.StartupPath + "\\ResOut\\" + file);
            //}

            //最好释放掉,如果对象供其他接口使用要先释放
            eyemImageFree(ref tpDstImg);

            //点料
            eyemCountObjectIrregularPartsE(image, tpRoi, file.Replace(".png", ""), selectModel.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0], hModelID, ref pNumObj, out tpDstImg);

            //测试移除模板
            //eyemRemoveModelByName(hModelID, "D:\\模板文件及图像\\df871193-6632-48f9-abfe-540c3fc49c3f.tpl");

            //free image
            eyemImageFree(ref tpDstImg);
            eyemImageFree(ref image);

            sw.Stop();
            Console.WriteLine("耗时:" + sw.ElapsedMilliseconds.ToString() + ",结果:" + pNumObj + ",所用模板:" + str);
        }

        public static void eyemReadProcssedImage(string fileName)
        {
            EyemImage image;
            EyemImage tpDstImg = new EyemImage();

            int flag = eyemImageRead(fileName, -1, out image);
            if (flag != 0)
            {
                Console.WriteLine("读图失败!");
                return;
            }

            EyemRect tpRoi = new EyemRect();
            tpRoi.iXs = 200; tpRoi.iYs = 200;
            tpRoi.iWidth = image.iWidth - 400;
            tpRoi.iHeight = image.iHeight - 400;

            //
            string file = fileName.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries)[2];

            //获取用于制作模板的图像
            flag = eyemAchvTemplateImage(image, tpRoi, out tpDstImg);

            Bitmap bitmap = eyemCvtToBitmap(tpDstImg);

            if (bitmap != null)
            {
                bitmap.Save("D:\\ResOut\\" + file);
            }

            //建议释放掉
            bitmap.Dispose();
        }

        public static int eyemInitModelE(out IntPtr hModelID)
        {
            return eyemInitModel("D:\\模板文件", out hModelID);
        }

        public static int eyemReleaseModelE(ref IntPtr hModelID)
        {
            return eyemReleaseModel(ref hModelID);
        }

        public static void eyemTestVideoCapture(string fileName)
        {
            List<EyemRect3> tpRois = new List<EyemRect3>();
            EyemRect3 roi1 = new EyemRect3();
            roi1.iXs = 0; roi1.iYs = 400; roi1.iWidth = 100; roi1.iHeight = 100; roi1.dVar = 0.35;
            EyemRect3 roi2 = new EyemRect3();
            roi2.iXs = 101; roi2.iYs = 400; roi2.iWidth = 100; roi2.iHeight = 100; roi2.dVar = 0.35;
            EyemRect3 roi3 = new EyemRect3();
            roi3.iXs = 202; roi3.iYs = 400; roi3.iWidth = 100; roi3.iHeight = 100; roi3.dVar = 0.35;
            EyemRect3 roi4 = new EyemRect3();
            roi4.iXs = 303; roi4.iYs = 400; roi4.iWidth = 100; roi4.iHeight = 100; roi4.dVar = 0.35;

            //需要监控的位置信息
            tpRois.Add(roi1); tpRois.Add(roi2); tpRois.Add(roi3); tpRois.Add(roi4);

            //结构体转内存指针
            IntPtr hGlobal = eyemStructArray2IntPtr(tpRois.ToArray());

            //读取视频接口
            int ipNum; EyemImage* tpImages;
            VideoHandle hObject;
            eyemVideoCapture(fileName, out hObject, out tpImages, out ipNum);

            //信号值,用于后续处理
            int[] bitSingle = new int[tpRois.Count];

            //
            eyemOpenWindow("eyemLib");

            //类似于实时采集的图像
            for (int i = 1; i < ipNum; i++)
            {
                EyemImage tpDstImg;
                //存放结果
                int[] iArrRes = new int[tpRois.Count];
                //int iRet = eyemAOIForTSAV(tpImages[0], tpImages[i], hGlobal, tpRois.Count);
                int iRet = eyemTrackFeature(tpImages[0], tpImages[i], hGlobal, tpRois.Count, Marshal.UnsafeAddrOfPinnedArrayElement(iArrRes, 0), out tpDstImg);
                //过滤出有效信号(连续存在数帧以上才算,防止误触发),或者直接用iArrRes的值作为信号
                //toSingleFilter(iArrRes, ref bitSingle);
                //for (int j = 0; j < bitSingle.Length; j++)
                //{
                //    //设定信号持续帧数阈值
                //    if (bitSingle[j] > 15)
                //    {
                //        //检测到信号
                //        Console.WriteLine("检测到:" + j + "处有信号");
                //    }
                //}
                eyemImageFree(ref tpDstImg);
                System.Threading.Thread.Sleep(80);
            }
            //释放资源
            Marshal.FreeHGlobal(hGlobal);
            hObject.Dispose();
        }

        public static void toSingleFilter(int[] iArrRes, ref int[] bitSingle)
        {
            for (int i = 0; i < iArrRes.Length; i++)
            {
                bitSingle[i] += iArrRes[i];
                if (bitSingle[i] >= 1)//信号起始
                {
                    //如果有一帧没检测到就重新累计
                    if (iArrRes[i] == 0)
                    {
                        bitSingle[i] = 0;
                    }
                }
            }
        }

        public static void eyemTest(string[] fileNames)
        {
            int iRet = 0;

            EyemImage imageFormer = new EyemImage();
            EyemImage imageDiff = new EyemImage();

            //首次需要为图像申请内存,EyemImage之间直接用=赋值是不行的
            iRet = eyemImageMalloc(1080, 1440, 4, "int8_t", out imageFormer);

            iRet = eyemImageMalloc(1080, 1440, 1, "uint8_t", out imageDiff);

            int nFrmNum = 0;
            for (int i = 1; i < fileNames.Length; i++)
            {
                EyemImage imageNext = new EyemImage();
                iRet = eyemImageRead(fileNames[i], -1, out imageNext);
                if (iRet != 0)
                {
                    Console.WriteLine("读图失败!");
                    return;
                }
                //转灰度图像
                eyemCvtImageColor(imageNext, ColorConversionCodes.COLOR_BGR2GRAY, ref imageNext);

                nFrmNum++;
                if (nFrmNum > 0)
                {
                    iRet = eyemImageSub(imageNext, imageFormer, ref imageDiff);

                    iRet = eyemImageAbs(imageDiff, ref imageDiff);
                }
                //前一帧,(直接赋值是不可以的,要先对其释放,拷贝时必须具有相同类型)
                eyemImageCopy(imageNext, ref imageFormer);

                //释放资源
                eyemImageFree(ref imageNext);

                System.Threading.Thread.Sleep(350);
            }
            //释放资源
            eyemImageFree(ref imageFormer);
            eyemImageFree(ref imageDiff);
        }

        #region EyemImage转换成Bitmap
        public static Bitmap eyemCvtToBitmap(EyemImage tpImage)
        {
            if (tpImage.vpImage == IntPtr.Zero || tpImage.iDepth != 0)
                return null;

            PixelFormat format;

            switch (tpImage.iChannels)
            {
                case 1:
                    format = PixelFormat.Format8bppIndexed;
                    break;
                case 3:
                    format = PixelFormat.Format24bppRgb;
                    break;
                case 4:
                    format = PixelFormat.Format32bppArgb;
                    break;
                default:
                    return null;
            }

            Bitmap bitmap = new Bitmap(tpImage.iWidth, tpImage.iHeight, format);

            //对于输出灰度图像
            if (format == PixelFormat.Format8bppIndexed)
            {
                ColorPalette palette = bitmap.Palette;
                for (int i = 0; i < 256; i++)
                {
                    palette.Entries[i] = Color.FromArgb(i, i, i);
                }
                bitmap.Palette = palette;
            }

            //锁定数据区
            BitmapData bd = bitmap.LockBits(new Rectangle(0, 0, tpImage.iWidth, tpImage.iHeight),
                ImageLockMode.WriteOnly, format);

            try
            {
                int pd = ((tpImage.iWidth * tpImage.iChannels) + 3) / 4 * 4;

                long bytesToCopy = tpImage.iWidth * tpImage.iChannels;

                for (int y = 0; y < tpImage.iHeight; y++)
                {
                    long offsetSrc = (y * tpImage.iWidth * tpImage.iChannels);
                    long offsetDst = (y * pd);

                    Buffer.MemoryCopy((byte*)(tpImage.vpImage.ToPointer()) + offsetSrc, (byte*)(bd.Scan0.ToPointer()) + offsetDst, bytesToCopy, bytesToCopy);
                }
            }
            finally
            {
                bitmap.UnlockBits(bd);
            }
            return bitmap;
        }
        #endregion

        #region 结构体转内存指针
        public static IntPtr eyemStructArray2IntPtr<T>(T[] tpArray)
        {
            if (tpArray == null)
                throw new ArgumentNullException(typeof(T).Name.ToString());
            //分配结构体需要的内存,需要释放
            IntPtr hGlobal = Marshal.AllocHGlobal(checked(Marshal.SizeOf(typeof(T)) * tpArray.Length));
            for (int index = 0; index < tpArray.Length; index++)
            {
                Marshal.StructureToPtr(tpArray[index], (IntPtr)(checked((long)hGlobal + index * Marshal.SizeOf(typeof(T)))), false);
            }
            return hGlobal;
        }
        #endregion

        #region 释放句柄
        //释放Blob句柄
        public class BlobHandle : SafeHandleZeroOrMinusOneIsInvalid
        {
            public BlobHandle() : base(true) { }
            protected override bool ReleaseHandle()
            {
                return eyemBinFree(handle);
            }
        }
        //释放测量句柄
        public class MeasureHandle : SafeHandleZeroOrMinusOneIsInvalid
        {
            public MeasureHandle() : base(true) { }
            protected override bool ReleaseHandle()
            {
                return eyemEdge1dGenMeasureFree(handle);
            }
        }
        //释放解码句柄
        public class DataCodeHandle : SafeHandleZeroOrMinusOneIsInvalid
        {
            public DataCodeHandle() : base(true) { }
            protected override bool ReleaseHandle()
            {
                return eyemDetectAndDecodeFree(handle);
            }
        }
        //释放视频句柄
        public class VideoHandle : SafeHandleZeroOrMinusOneIsInvalid
        {
            public VideoHandle() : base(true) { }
            protected override bool ReleaseHandle()
            {
                return eyemVideoCaptureFree(handle);
            }
        }

        #endregion
    }
}