EyemLib.cs 28.7 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
using Microsoft.Win32.SafeHandles;
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Linq;
using System.Collections.Generic;

namespace eyemLib_Sharp
{
    #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
    }

    #endregion

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

    // 矩形定义
    [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 坐标
    }

    ///////////////////////////////////////////////////////////////////////////////
    // 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                        // 3次元空間内の直線の表現に使用
    {
        EyemOcsDXYZ tP;                             // 直線上の1点の座標
        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 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

    public unsafe class EyemLib
    {
        #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 ucpImage);
        //读取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 ucpImage);
        //保存图像
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemImageWrite(string filename, EyemImage ipImage);
        //释放图像资源
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern void eyemImageFree(IntPtr ipImage);

        #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 EyemImage tpDstImage, out EyemChainCode* tpContours, out EyemBinBlob* tpResult, out int ipNum);
        //释放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 bool eyemEdge1dGenMeasureFree(IntPtr hObject);
        #endregion

        [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);

        #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 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);
        #endregion

        public static void eyemReadImageTool(string fileName)
        {
            EyemImage ucpImage;
            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 ucpImage);
            if (flag != 0)
            {
                Console.WriteLine("读图失败!");
                return;
            }

            //ucpImage.iWidth = 3072; ucpImage.iHeight = 3072; ucpImage.iDepth = 2;
            //Bitmap bmp = new Bitmap(ucpImage.iWidth, ucpImage.iHeight, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
            //ColorPalette color_palette_gray_ = bmp.Palette;
            //for (int i = 0; i < 256; i++)
            //{
            //    color_palette_gray_.Entries[i] = System.Drawing.Color.FromArgb(i, i, i);
            //}
            //bmp.Palette = color_palette_gray_;
            //BitmapData bdd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
            //memcpy((byte*)bdd.Scan0.ToPointer(), (byte*)ucpImage.ucpImage.ToPointer(), bdd.Stride * bdd.Height);
            //bmp.UnlockBits(bdd);
            //bmp.Dispose();

            Stopwatch sw = new Stopwatch();
            sw.Restart();

            #region Test Blob
            //int ipNum;
            //BlobHandle hObject;
            //EyemBinBlob* eyemBlob;
            //EyemChainCode* eyemContours;
            //eyemBinBlob(ucpImage, out hObject, 100, out tpDstImg, out eyemContours, out eyemBlob, out ipNum);
            //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 = tpRoi.iYs = 0;
            tpRoi.iWidth = ucpImage.iWidth;
            tpRoi.iHeight = ucpImage.iHeight;
            //
            string pNumObj = "";
            string file = fileName.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries)[2];
            //"IP_SMALL_PARTS","IP_LARGE_PARTS","IP_LONG_PARTS",""
            //eyemCountObject(ucpImage, file.Replace(".png", ""), 35, 0, 100, 5, ref pNumObj, out tpDstImg);
            //eyemCountObjectIrregularParts(ucpImage, file.Replace(".png", ""), 0d, "IP_LONG_PARTS", 100, 5, ref pNumObj, out tpDstImg);
            int ipNum; EyemBarCode* tpResults;
            DataCodeHandle hObject;
            int iRes = eyemDetectAndDecode(ucpImage, tpRoi, file.Replace(".png", ""), "QR_CODE|DATA_MATRIX|CODE_128|CODE_39", out hObject, out tpResults, out ipNum, false, 11, 5, 128, 215, 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(ucpImage.ucpImage);
        }

        //计算最佳参数
        public static void eyemReadImageToolTest(string fileName)
        {
            EyemImage ucpImage;
            int flag = eyemImageRead(fileName, -1, out ucpImage);
            if (flag != 0)
            {
                Console.WriteLine("读图失败!");
                return;
            }
            EyemRect tpRoi = new EyemRect();
            tpRoi.iXs = tpRoi.iYs = 0;
            tpRoi.iWidth = ucpImage.iWidth;
            tpRoi.iHeight = ucpImage.iHeight;
            //
            System.Collections.Generic.Dictionary<int, int> dictPara = new System.Collections.Generic.Dictionary<int, int>();

            string file = fileName.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries)[2];
            for (int ii = 5; ii < 27; ii += 2)
            {
                int ipNum; EyemBarCode* tpResults;
                DataCodeHandle hObject;
                int iRes = eyemDetectAndDecode(ucpImage, tpRoi, file.Replace(".png", ""), "QR_CODE|DATA_MATRIX|CODE_128", out hObject, out tpResults, out ipNum, false, ii, 5, 128, 215, 1d);
                dictPara.Add(ii, ipNum);
                hObject.Dispose();
                //Console.WriteLine("数量:" + ipNum + ";" + "参数:" + ii.ToString());
            }
            var sortResult = from dic in dictPara orderby dic.Value descending select dic;
            foreach (KeyValuePair<int, int> kvp in sortResult)
            {
                int ipNum; EyemBarCode* tpResults;
                DataCodeHandle hObject;
                int iRes = eyemDetectAndDecode(ucpImage, tpRoi, file.Replace(".png", ""), "QR_CODE|DATA_MATRIX|CODE_128", out hObject, out tpResults, out ipNum, false, kvp.Key, 5, 128, 215, 1d);
                hObject.Dispose();
                Console.WriteLine("最佳参数:" + kvp.Key);
                break;
            }
            //free image
            eyemImageFree(ucpImage.ucpImage);
            return;
        }


        #region 结构转内存指针
        public static IntPtr teStructArray2IntPtr(EyemOcsDXY[] cvPoint2D32f, int iLength)
        {
            //分配结构体需要的内存
            IntPtr ptrTemp = (IntPtr)Marshal.AllocHGlobal(Marshal.SizeOf(typeof(EyemOcsDXY)) * iLength);
            for (int j = 0, addr = (int)ptrTemp; j < iLength; j++, addr += Marshal.SizeOf(typeof(EyemOcsDXY)))
            {
                Marshal.StructureToPtr(cvPoint2D32f[j], (IntPtr)addr, false);
            }
            return ptrTemp;
        }
        #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);
            }
        }

        #endregion
    }
}