EyemLib.cs 48.8 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
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;

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;                       // 码内容
        }
        #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);

        #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, 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, 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, string fileName, ref string pNumObj, out EyemImage tpDstImg);
        //异型器件(新版本)
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemCountObjectIrregularPartsE(EyemImage tpImage, string fileName, string ccSubType, string ccTplName, double dMinScore, ref string pNumObj, out EyemImage tpDstImg);
        //创建模板匹配模型
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemCreateTemplateModel(EyemImage tpImage, EyemRect tpRoi, string ccTplName);
        //读码程序
        [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);
        #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 = 1231; tpRoi.iYs = 433;
            ////tpRoi.iWidth = 41;
            ////tpRoi.iHeight = 14;

            EyemRect tpRoi = new EyemRect();
            tpRoi.iXs = 0; tpRoi.iYs = 0;
            tpRoi.iWidth = image.iWidth;
            tpRoi.iHeight = image.iHeight;

            //创建模板匹配模型
            //flag = eyemCreateTemplateModel(image, tpRoi, "D://批量测试图像2//template.tpl");

            //
            string pNumObj = "";
            string file = fileName.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries)[2];
            //"IP_SMALL_PARTS","IP_LARGE_PARTS","IP_LONG_PARTS",""
            //eyemCountObject(image, file.Replace(".png", ""), 35, 0, 100, 5, ref pNumObj, out tpDstImg);
            //eyemCountObjectIrregularParts(image, file.Replace(".png", ""), 0.1, "IP_LARGE_PARTS", 100, 7, ref pNumObj, out tpDstImg);
            //eyemCountObjectE(image, fileName, ref pNumObj, out tpDstImg);
            eyemCountObjectIrregularPartsE(image, file.Replace(".png", ""), "IP_LARGE_PARTS", "D://批量测试图像2//template.png", 0.6, ref pNumObj, out tpDstImg);

            Bitmap bmp = eyemCvtToBitmap(tpDstImg);

            bmp.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|CODE_39|CODE_128|CODE_93", out hObject, out tpResults, out ipNum, false, 5, 5, 128, 256, 1d);
            //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);
            //free image
            eyemImageFree(ref tpDstImg);
            eyemImageFree(ref image);
        }



        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];

            //类似于实时采集的图像
            for (int i = 1; i < ipNum; i++)
            {
                //存放结果
                int[] iArrRes = new int[tpRois.Count];
                //fixed (int* ipResults = &iArrRes[0])
                //{
                int iRet = eyemTrackFeature(tpImages[0], tpImages[i], hGlobal, tpRois.Count, Marshal.UnsafeAddrOfPinnedArrayElement(iArrRes, 0)/* new IntPtr((void*)ipResults)*/);
                //}
                //过滤出有效信号(连续存在数帧以上才算,防止误触发),或者直接用iArrRes的值作为信号
                toSingleFilter(iArrRes, ref bitSingle);
                for (int j = 0; j < bitSingle.Length; j++)
                {
                    //设定信号持续帧数阈值
                    if (bitSingle[j] > 15)
                    {
                        //检测到信号
                        Console.WriteLine("检测到:" + j + "处有信号");
                    }
                }
            }
            //释放资源
            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)
        {
            //统一将图像转换成8位显示
            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;
            }
            //如果图不是8位的就转成8位显示
            if (tpImage.iDepth != 0)
                eyemCvtImageType(tpImage, "uint8_t", 1.0, 0.0, ref tpImage);

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

            BitmapData data = bitmap.LockBits(
                new Rectangle(Point.Empty, new Size(tpImage.iWidth, tpImage.iHeight)),
                ImageLockMode.WriteOnly,
                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;
            }

            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
    }
}