예제 #1
0
        /// <summary>
        /// 初始化後才能叫用 Action
        /// </summary>
        public MeasureResult Action()
        {
            #region 輸出結果, CircleResult or Distance Result
            CircleResult mResult = null;
            #endregion

            // Local iconic variables

            HObject ho_R1_Circle = null;
            HObject ho_R1_ROI_Image = null, ho_R1_Region = null, ho_R1_ImageReduced = null;
            HObject ho_R1_Edges = null, ho_R1_ContoursSplit = null, ho_R1_SingleSegment = null;
            //HObject ho_R1_ContEllipse = null;

            // Local control variables

            HTuple hv_msgOffsetY, hv_msgOffsetX;
            //HTuple hv_AllModelId;
            HTuple hv_STD_Row;
            HTuple hv_STD_Col, hv_Img_Row, hv_Img_Col, hv_Img_Rotate_Angle;
            HTuple hv_OffsetRow, hv_OffsetCol, hv_STD_R1_Row, hv_STD_R1_Col;
            HTuple hv_STD_R1_V_Row, hv_STD_R1_V_Col, hv_R1_X, hv_R1_Y;
            HTuple hv_R1_Pos_Row, hv_R1_Pos_Col, hv_R1_R;
            HTuple hv_alpha = new HTuple(), hv_R1_low = new HTuple(), hv_R1_high = new HTuple();
            HTuple hv_R1_NumSegments = new HTuple(), hv_NumCircles = new HTuple();
            HTuple hv_Num_Circle_Point = new HTuple(), hv_R1 = new HTuple();
            HTuple hv_R1_limit = new HTuple(), hv_i = new HTuple(), hv_Attrib = new HTuple();
            HTuple hv_R1_Row = new HTuple(), hv_R1_Column = new HTuple();
            HTuple hv_R1_Radius = new HTuple(), hv_R1_StartPhi = new HTuple();
            HTuple hv_R1_EndPhi = new HTuple(), hv_R1_PointOrder = new HTuple();
            HTuple hv_R1_MinDist = new HTuple(), hv_R1_MaxDist = new HTuple();
            HTuple hv_R1_AvgDist = new HTuple(), hv_R1_SigmaDist = new HTuple();
            HTuple hv_ResultText = new HTuple(), hv_MeasureReasult;

            // Initialize local and output iconic variables
            //HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_R1_Circle);
            HOperatorSet.GenEmptyObj(out ho_R1_ROI_Image);
            HOperatorSet.GenEmptyObj(out ho_R1_Region);
            HOperatorSet.GenEmptyObj(out ho_R1_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_R1_Edges);
            HOperatorSet.GenEmptyObj(out ho_R1_ContoursSplit);
            HOperatorSet.GenEmptyObj(out ho_R1_SingleSegment);
            //HOperatorSet.GenEmptyObj(out ho_R1_ContEllipse);

            //Measure: SDMS_R1
            //Author: John Hsieh
            //Date: 2012
            //ho_Image.Dispose();
            //HOperatorSet.ReadImage(out ho_Image, "D:/Projects/Halcon/SDMS/SDMS_Measure/Images/E-1/E1-1.bmp");
            //dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle);
            //dev_update_off ()
            // dev_update_window(...); only in hdevelop
            HOperatorSet.SetSystem("border_shape_models", "false");

            //****Message Args
            hv_msgOffsetY = 100;
            hv_msgOffsetX = 100;

            //****Model All
            //HOperatorSet.ReadShapeModel("D:/Projects/Halcon/SDMS/SDMS_Measure/Model/MatchingAll.shm",
            //	out hv_AllModelId);
            //ho_AllModelContours.Dispose();
            //HOperatorSet.GetShapeModelContours(out ho_AllModelContours, hv_AllModelId, 1);
            //HOperatorSet.FindShapeModel(ho_Image, hv_AllModelId, (new HTuple(0)).TupleRad()
            //	, (new HTuple(360)).TupleRad(), 0.5, 1, 0.5, "least_squares", 6, 0.75, out hv_AllModelRow,
            //	out hv_AllModelColumn, out hv_AllModelAngle, out modelScore);

            //****Model Args

            //STD 中心點
            hv_STD_Row = 772;
            hv_STD_Col = 1003;

            //目前圖形 中心點
            hv_Img_Row = hv_AllModelRow.Clone();
            hv_Img_Col = hv_AllModelColumn.Clone();

            //目前圖形 Rotate Angle
            hv_Img_Rotate_Angle = hv_AllModelAngle.Clone();

            //目前圖形偏移量
            hv_OffsetRow = hv_Img_Row - hv_STD_Row;
            hv_OffsetCol = hv_Img_Col - hv_STD_Col;

            //****R1
            //STD R1_ 位置
            hv_STD_R1_Row = 589;
            hv_STD_R1_Col = 705;

            //STD 向量 STD_R1_
            hv_STD_R1_V_Row = hv_STD_R1_Row - hv_STD_Row;
            hv_STD_R1_V_Col = hv_STD_R1_Col - hv_STD_Col;

            //R1_X, R1_Y 分量
            hv_R1_X = (hv_STD_R1_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_R1_V_Row * (hv_Img_Rotate_Angle.TupleSin()
                ));
            hv_R1_Y = (hv_STD_R1_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_R1_V_Col * (hv_Img_Rotate_Angle.TupleSin()
                ));

            //目前圖形 R1_ 位置
            hv_R1_Pos_Row = (hv_STD_Row + hv_R1_Y) + hv_OffsetRow;
            hv_R1_Pos_Col = (hv_STD_Col + hv_R1_X) + hv_OffsetCol;

            hv_R1_R = 20;
            //*ROI
            ho_R1_Circle.Dispose();
            HOperatorSet.GenCircle(out ho_R1_Circle, hv_R1_Pos_Row, hv_R1_Pos_Col, hv_R1_R);
            ho_R1_ROI_Image.Dispose();
            HOperatorSet.ReduceDomain(ho_Image, ho_R1_Circle, out ho_R1_ROI_Image);
            ho_R1_Region.Dispose();
            HOperatorSet.FastThreshold(ho_R1_ROI_Image, out ho_R1_Region, 100, 255, 20);
            ho_R1_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_R1_ROI_Image, ho_R1_Region, out ho_R1_ImageReduced
                );

            //sobel_fast 具有較寬的選擇範圍,搭配 alpha 參數 (alpha 越大, 容錯範圍大)
            hv_alpha = 0.9;
            hv_R1_low = 20;
            hv_R1_high = 40;
            ho_R1_Edges.Dispose();
            HOperatorSet.EdgesSubPix(ho_R1_ImageReduced, out ho_R1_Edges, "sobel_fast",
                hv_alpha, hv_R1_low, hv_R1_high);
            //stop ()
            //*所有的數值越小,表示容錯範圍大,反之亦然
            ho_R1_ContoursSplit.Dispose();
            HOperatorSet.SegmentContoursXld(ho_R1_Edges, out ho_R1_ContoursSplit, "lines_circles",
                17, 1, 1);
            //Display the results
            //===========================================================
            HOperatorSet.CountObj(ho_R1_ContoursSplit, out hv_R1_NumSegments);
            hv_NumCircles = 0;
            hv_Num_Circle_Point = 5;
            hv_R1 = 999;
            hv_R1_limit = 10;
            for (hv_i = 1; hv_i.Continue(hv_R1_NumSegments, 1); hv_i = hv_i.TupleAdd(1))
            {
                ho_R1_SingleSegment.Dispose();
                HOperatorSet.SelectObj(ho_R1_ContoursSplit, out ho_R1_SingleSegment, hv_i);
                HOperatorSet.GetContourGlobalAttribXld(ho_R1_SingleSegment, "cont_approx", out hv_Attrib);

                if ((int)(new HTuple(hv_Attrib.TupleEqual(1))) != 0)
                {
                    HOperatorSet.FitCircleContourXld(ho_R1_SingleSegment, "atukey", -1, 2,
                        hv_Num_Circle_Point, 5, 2, out hv_R1_Row, out hv_R1_Column, out hv_R1_Radius,
                        out hv_R1_StartPhi, out hv_R1_EndPhi, out hv_R1_PointOrder);

                    //ho_R1_ContEllipse.Dispose();
                    //HOperatorSet.GenEllipseContourXld(out ho_R1_ContEllipse, hv_R1_Row, hv_R1_Column,
                    //	0, hv_R1_Radius, hv_R1_Radius, 0, (new HTuple(360)).TupleRad(), "positive",
                    //	1.0);

                    //HOperatorSet.DistEllipseContourXld(ho_R1_SingleSegment, "algebraic", -1,
                    //	0, hv_R1_Row, hv_R1_Column, 0, hv_R1_Radius, hv_R1_Radius, out hv_R1_MinDist,
                    //	out hv_R1_MaxDist, out hv_R1_AvgDist, out hv_R1_SigmaDist);

                    hv_NumCircles = hv_NumCircles + 1;
                    if ((int)(new HTuple(hv_R1.TupleGreater(hv_R1_Radius))) != 0)
                    {
                        hv_R1 = hv_R1_Radius.Clone();
                        //hv_ResultText = (((("C" + hv_NumCircles) + ": Radius = ") + (hv_R1_Radius.TupleString(
                        //	".3"))) + " / MaxDeviation: ") + (hv_R1_MaxDist.TupleString(".3"));
                        //HOperatorSet.SetTposition(hv_WindowHandle, hv_R1_Pos_Row - hv_msgOffsetY,
                        //	hv_R1_Pos_Col - hv_msgOffsetX);
                        //HOperatorSet.WriteString(hv_WindowHandle, "R1");

                        #region 組合結果
                        mResult = new CircleResult(new HTuple(hv_R1_Row)
                                                , new HTuple(hv_R1_Column)
                                                , new HTuple(hv_R1_Radius)
                                                , new HTuple(hv_R1_StartPhi)
                                                , new HTuple(hv_R1_EndPhi)
                                                , new HTuple(hv_R1_PointOrder)) { };
                        #endregion
                    }
                }
            }
            hv_MeasureReasult = hv_R1.Clone();
            //****R1 End
            //ho_Image.Dispose();
            ho_R1_Circle.Dispose();
            ho_R1_ROI_Image.Dispose();
            ho_R1_Region.Dispose();
            ho_R1_ImageReduced.Dispose();
            ho_R1_Edges.Dispose();
            ho_R1_ContoursSplit.Dispose();
            ho_R1_SingleSegment.Dispose();
            //ho_R1_ContEllipse.Dispose();

            return mResult;
        }
예제 #2
0
    // Procedures
    public void EnhanceEdgeArea(HObject ho_InputImage, out HObject ho_EnhancedImage,
                                HTuple hv_LineStartPoint_Row, HTuple hv_LineStartPoint_Column, HTuple hv_LineEndPoint_Row,
                                HTuple hv_LineEndPoint_Column, HTuple hv_RoiWidthLen, HTuple hv_EmpMaskWidth,
                                HTuple hv_EmpMaskHeight, HTuple hv_EmpMaskFactor, HTuple hv_MeanMaskWidth, HTuple hv_MeanMaskHeight,
                                HTuple hv_IterationCount, HTuple hv_MinGray, HTuple hv_MaxGray, HTuple hv_Grayval,
                                HTuple hv_ClosingWidth, HTuple hv_ClosingHeight, HTuple hv_DilationRadius)
    {
        // Stack for temporary objects
        HObject[] OTemp = new HObject[20];

        // Local iconic variables

        HObject ho_Rectangle, ho_RegionDilation, ho_MeanImage = null;
        HObject ho_ImageEmphasize = null, ho_Seg, ho_ConnectedRegions;
        HObject ho_SelectedRegions, ho_RegionClosing, ho_RegionBorder;
        HObject ho_RegionBorder2;


        // Local control variables

        HTuple hv_TmpCtrl_Row = null, hv_TmpCtrl_Column = null;
        HTuple hv_TmpCtrl_Dr = null, hv_TmpCtrl_Dc = null, hv_TmpCtrl_Phi = null;
        HTuple hv_TmpCtrl_Len1 = null, hv_TmpCtrl_Len2 = null;
        HTuple hv_Index = null;

        // Initialize local and output iconic variables
        HOperatorSet.GenEmptyObj(out ho_EnhancedImage);
        HOperatorSet.GenEmptyObj(out ho_Rectangle);
        HOperatorSet.GenEmptyObj(out ho_RegionDilation);
        HOperatorSet.GenEmptyObj(out ho_MeanImage);
        HOperatorSet.GenEmptyObj(out ho_ImageEmphasize);
        HOperatorSet.GenEmptyObj(out ho_Seg);
        HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
        HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
        HOperatorSet.GenEmptyObj(out ho_RegionClosing);
        HOperatorSet.GenEmptyObj(out ho_RegionBorder);
        HOperatorSet.GenEmptyObj(out ho_RegionBorder2);

        //
        //init
        //FilterAlpha := 6
        //Measure 01: Convert coordinates to rectangle2 type
        hv_TmpCtrl_Row    = 0.5 * (hv_LineStartPoint_Row + hv_LineEndPoint_Row);
        hv_TmpCtrl_Column = 0.5 * (hv_LineStartPoint_Column + hv_LineEndPoint_Column);
        hv_TmpCtrl_Dr     = hv_LineStartPoint_Row - hv_LineEndPoint_Row;
        hv_TmpCtrl_Dc     = hv_LineEndPoint_Column - hv_LineStartPoint_Column;
        hv_TmpCtrl_Phi    = hv_TmpCtrl_Dr.TupleAtan2(hv_TmpCtrl_Dc);
        hv_TmpCtrl_Len1   = 0.5 * ((((hv_TmpCtrl_Dr * hv_TmpCtrl_Dr) + (hv_TmpCtrl_Dc * hv_TmpCtrl_Dc))).TupleSqrt()
                                   );
        hv_TmpCtrl_Len2 = hv_RoiWidthLen.Clone();
        ho_Rectangle.Dispose();
        HOperatorSet.GenRectangle2(out ho_Rectangle, hv_TmpCtrl_Row, hv_TmpCtrl_Column,
                                   hv_TmpCtrl_Phi, hv_TmpCtrl_Len1, hv_TmpCtrl_Len2);

        ho_RegionDilation.Dispose();
        HOperatorSet.DilationRectangle1(ho_Rectangle, out ho_RegionDilation, 100, 100);

        ho_EnhancedImage.Dispose();
        HOperatorSet.ReduceDomain(ho_InputImage, ho_RegionDilation, out ho_EnhancedImage
                                  );

        HTuple end_val17  = hv_IterationCount;
        HTuple step_val17 = 1;

        for (hv_Index = 1; hv_Index.Continue(end_val17, step_val17); hv_Index = hv_Index.TupleAdd(step_val17))
        {
            ho_MeanImage.Dispose();
            HOperatorSet.MeanImage(ho_EnhancedImage, out ho_MeanImage, hv_MeanMaskWidth,
                                   hv_MeanMaskHeight);
            ho_ImageEmphasize.Dispose();
            HOperatorSet.Emphasize(ho_MeanImage, out ho_ImageEmphasize, hv_EmpMaskWidth,
                                   hv_EmpMaskHeight, hv_EmpMaskFactor);
            ho_EnhancedImage.Dispose();
            HOperatorSet.MeanImage(ho_ImageEmphasize, out ho_EnhancedImage, 2, 2);
            //median_separate (ImageEmphasize, EnhancedImage, MeanMaskWidth, MeanMaskHeight, 'continued')
        }


        ho_Seg.Dispose();
        HOperatorSet.FastThreshold(ho_EnhancedImage, out ho_Seg, hv_MinGray, hv_MaxGray,
                                   150);
        ho_ConnectedRegions.Dispose();
        HOperatorSet.Connection(ho_Seg, out ho_ConnectedRegions);
        ho_SelectedRegions.Dispose();
        HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area",
                                 "and", 1000, 9999999);
        //opening_rectange1(SelectedRegions, RegionOpening, ClosingWidth, ClosingHeight)
        ho_RegionClosing.Dispose();
        HOperatorSet.ClosingRectangle1(ho_SelectedRegions, out ho_RegionClosing, hv_ClosingWidth,
                                       hv_ClosingHeight);

        ho_RegionBorder.Dispose();
        HOperatorSet.Boundary(ho_RegionClosing, out ho_RegionBorder, "inner");
        ho_RegionDilation.Dispose();
        HOperatorSet.DilationCircle(ho_RegionClosing, out ho_RegionDilation, hv_DilationRadius);
        ho_RegionBorder2.Dispose();
        HOperatorSet.Boundary(ho_RegionDilation, out ho_RegionBorder2, "inner");


        HOperatorSet.PaintRegion(ho_RegionDilation, ho_EnhancedImage, out OTemp[0], hv_Grayval,
                                 "fill");
        ho_EnhancedImage.Dispose();
        ho_EnhancedImage = OTemp[0];
        //region_to_bin (RegionDilation, EnhancedImage, 255, 0, 512, 512)

        ho_Rectangle.Dispose();
        ho_RegionDilation.Dispose();
        ho_MeanImage.Dispose();
        ho_ImageEmphasize.Dispose();
        ho_Seg.Dispose();
        ho_ConnectedRegions.Dispose();
        ho_SelectedRegions.Dispose();
        ho_RegionClosing.Dispose();
        ho_RegionBorder.Dispose();
        ho_RegionBorder2.Dispose();

        return;
    }
예제 #3
0
        private void action()
        {
            // Stack for temporary objects
            HObject[] OTemp = new HObject[20];

            // Local iconic variables

            HObject ho_Rectangle, ho_ImageReduced;
            HObject ho_Region, ho_RegionLines, ho_RegionUnion1, ho_RegionUnion2;
            HObject ho_RegionUnion3, ho_Region1, ho_ConnectedRegions;
            HObject ho_RegionFillUp, ho_SelectedRegions, ho_SortedRegions;
            HObject ho_ObjectSelected = null, ho_Rectangle1 = null, ho_ImageReduced1 = null;
            HObject ho_Circle1 = null, ho_Circle2 = null, ho_Circle3 = null;
            HObject ho_Circle4 = null, ho_RegionUnionc1 = null, ho_RegionUnionc2 = null;
            HObject ho_RegionUnionc3 = null, ho_ConnectedRegionsc = null;
            HObject ho_SortedRegions1 = null, ho_ContoursL = null, ho_RegionLinesL = null;
            HObject ho_RegionLinesL1 = null, ho_RegionUnionL = null, ho_ConnectedRegionsL = null;
            HObject ho_SelectedRegionsL = null, ho_RegionFillUpL = null;
            HObject ho_ContoursSplitL = null, ho_SelectedContoursL = null;
            HObject ho_UnionContoursL = null, ho_RegionL = null, ho_RegionDifferenceL = null;
            HObject ho_ContoursR = null, ho_RegionLinesR = null, ho_RegionLinesR1 = null;
            HObject ho_RegionUnionR = null, ho_ConnectedRegionsR = null;
            HObject ho_SelectedRegionsR = null, ho_RegionFillUpR = null;
            HObject ho_ContoursSplitR = null, ho_SelectedContoursR = null;
            HObject ho_UnionContoursR = null, ho_RegionR = null, ho_RegionDifferenceR = null;
            HObject ho_ConnectedRegions1, ho_ObjectSelected1 = null;

            // Local control variables

            HTuple hv_Width = null, hv_Height = null, hv_Row11 = null;
            HTuple hv_Column11 = null, hv_Row21 = null, hv_Column21 = null;
            HTuple hv_Row12 = null, hv_Column12 = null, hv_Row22 = null;
            HTuple hv_Column22 = null, hv_Number = null, hv_h = null;
            HTuple hv_w = null, hv_i = null, hv_Row = new HTuple();
            HTuple hv_Column = new HTuple(), hv_Phi = new HTuple();
            HTuple hv_Length1 = new HTuple(), hv_Length2 = new HTuple();
            HTuple hv_Cos = new HTuple(), hv_Sin = new HTuple(), hv_RT_X = new HTuple();
            HTuple hv_RT_Y = new HTuple(), hv_AX = new HTuple(), hv_AY = new HTuple();
            HTuple hv_RB_X = new HTuple(), hv_RB_Y = new HTuple();
            HTuple hv_BX = new HTuple(), hv_BY = new HTuple(), hv_LB_X = new HTuple();
            HTuple hv_LB_Y = new HTuple(), hv_CX = new HTuple(), hv_CY = new HTuple();
            HTuple hv_LT_X = new HTuple(), hv_LT_Y = new HTuple();
            HTuple hv_DX = new HTuple(), hv_DY = new HTuple(), hv_Number1 = new HTuple();
            HTuple hv_Area = new HTuple(), hv_Row1 = new HTuple();
            HTuple hv_Column1 = new HTuple(), hv_Distance = new HTuple();
            HTuple hv_Distance1 = new HTuple(), hv_shapeParam = new HTuple();
            HTuple hv_MetrologyHandleL = new HTuple(), hv_Index = new HTuple();
            HTuple hv_RowL = new HTuple(), hv_ColumnL = new HTuple();
            HTuple hv_l = new HTuple(), hv_NumberL = new HTuple();
            HTuple hv_MetrologyHandleR = new HTuple(), hv_RowR = new HTuple();
            HTuple hv_ColumnR = new HTuple(), hv_r = new HTuple();
            HTuple hv_NumberR = new HTuple(), hv_AreaL = new HTuple();
            HTuple hv_AreaR = new HTuple(), hv_Area1 = null, hv_Row2 = null;
            HTuple hv_Column2 = null, hv_Area2 = null, hv_Row3 = null;
            HTuple hv_Column3 = null, hv_hm = null, hv_wm = null, hv_Row13 = null;
            HTuple hv_Column13 = null, hv_Row23 = null, hv_Column23 = null;
            HTuple hv_zh = null, hv_w1 = new HTuple(), hv_Row4 = new HTuple(), hv_Column4 = new HTuple();
            HTuple hv_Phi1 = new HTuple(), hv_Length11 = new HTuple(), hv_Length21 = new HTuple(), hv_Concat = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_RegionLines);
            HOperatorSet.GenEmptyObj(out ho_RegionUnion1);
            HOperatorSet.GenEmptyObj(out ho_RegionUnion2);
            HOperatorSet.GenEmptyObj(out ho_RegionUnion3);
            HOperatorSet.GenEmptyObj(out ho_Region1);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUp);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SortedRegions);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected);
            HOperatorSet.GenEmptyObj(out ho_Rectangle1);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced1);
            HOperatorSet.GenEmptyObj(out ho_Circle1);
            HOperatorSet.GenEmptyObj(out ho_Circle2);
            HOperatorSet.GenEmptyObj(out ho_Circle3);
            HOperatorSet.GenEmptyObj(out ho_Circle4);
            HOperatorSet.GenEmptyObj(out ho_RegionUnionc1);
            HOperatorSet.GenEmptyObj(out ho_RegionUnionc2);
            HOperatorSet.GenEmptyObj(out ho_RegionUnionc3);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegionsc);
            HOperatorSet.GenEmptyObj(out ho_SortedRegions1);
            HOperatorSet.GenEmptyObj(out ho_ContoursL);
            HOperatorSet.GenEmptyObj(out ho_RegionLinesL);
            HOperatorSet.GenEmptyObj(out ho_RegionLinesL1);
            HOperatorSet.GenEmptyObj(out ho_RegionUnionL);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegionsL);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegionsL);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUpL);
            HOperatorSet.GenEmptyObj(out ho_ContoursSplitL);
            HOperatorSet.GenEmptyObj(out ho_SelectedContoursL);
            HOperatorSet.GenEmptyObj(out ho_UnionContoursL);
            HOperatorSet.GenEmptyObj(out ho_RegionL);
            HOperatorSet.GenEmptyObj(out ho_RegionDifferenceL);
            HOperatorSet.GenEmptyObj(out ho_ContoursR);
            HOperatorSet.GenEmptyObj(out ho_RegionLinesR);
            HOperatorSet.GenEmptyObj(out ho_RegionLinesR1);
            HOperatorSet.GenEmptyObj(out ho_RegionUnionR);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegionsR);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegionsR);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUpR);
            HOperatorSet.GenEmptyObj(out ho_ContoursSplitR);
            HOperatorSet.GenEmptyObj(out ho_SelectedContoursR);
            HOperatorSet.GenEmptyObj(out ho_UnionContoursR);
            HOperatorSet.GenEmptyObj(out ho_RegionR);
            HOperatorSet.GenEmptyObj(out ho_RegionDifferenceR);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);

            HOperatorSet.GenEmptyObj(out ho_ObjectSelected1);


            HOperatorSet.Union1(algorithm.Region, out RegionToDisp);
            try
            {
                ho_Rectangle.Dispose();
                HOperatorSet.GenRectangle1(out ho_Rectangle, DRow1m, DCol1m, DRow2m, DCol2m);
                ho_ImageReduced.Dispose();
                HOperatorSet.ReduceDomain(Image, ho_Rectangle, out ho_ImageReduced);
                ho_Region.Dispose();
                HOperatorSet.GetImageSize(Image, out hv_Width, out hv_Height);
                HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, 128, 255);
                HOperatorSet.SmallestRectangle1(ho_Region, out hv_Row12, out hv_Column12, out hv_Row22,
                                                out hv_Column22);

                ho_RegionLines.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLines, (hv_Row12 + hv_Row22) / 2, hv_Column12,
                                           ((hv_Row12 + hv_Row22) / 2) + 1, hv_Column12);

                ho_RegionUnion1.Dispose();
                HOperatorSet.Union1(ho_RegionLines, out ho_RegionUnion1);

                ho_RegionUnion2.Dispose();
                HOperatorSet.Union1(ho_RegionLines, out ho_RegionUnion2);

                ho_RegionUnion3.Dispose();
                HOperatorSet.Union1(ho_RegionLines, out ho_RegionUnion3);


                ho_Region1.Dispose();
                HOperatorSet.Threshold(ho_ImageReduced, out ho_Region1, 128, 255);
                ho_ConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_Region1, out ho_ConnectedRegions);

                ho_RegionFillUp.Dispose();
                HOperatorSet.FillUp(ho_ConnectedRegions, out ho_RegionFillUp);

                ho_SelectedRegions.Dispose();
                HOperatorSet.SelectShape(ho_RegionFillUp, out ho_SelectedRegions, "area", "and",
                                         500, 99999000);

                ho_SortedRegions.Dispose();
                HOperatorSet.SortRegion(ho_SelectedRegions, out ho_SortedRegions, "upper_left",
                                        "true", "column");

                HOperatorSet.CountObj(ho_SortedRegions, out hv_Number);

                // stop(); only in hdevelop
                // stop(); only in hdevelop
                hv_h  = new HTuple();
                hv_w  = new HTuple();
                hv_w1 = new HTuple();
                ho_ObjectSelected1.Dispose();
                HOperatorSet.SelectObj(ho_SortedRegions, out ho_ObjectSelected1, 1);
                HOperatorSet.SmallestRectangle2(ho_ObjectSelected1, out hv_Row4, out hv_Column4,
                                                out hv_Phi1, out hv_Length11, out hv_Length21);
                hv_w1 = hv_w1.TupleConcat(hv_Length21 * 2);
                HTuple end_val40  = hv_Number - 1;
                HTuple step_val40 = 1;
                for (hv_i = 2; hv_i.Continue(end_val40, step_val40); hv_i = hv_i.TupleAdd(step_val40))
                {
                    ho_ObjectSelected.Dispose();
                    HOperatorSet.SelectObj(ho_SortedRegions, out ho_ObjectSelected, hv_i);
                    HOperatorSet.SmallestRectangle2(ho_ObjectSelected, out hv_Row, out hv_Column,
                                                    out hv_Phi, out hv_Length1, out hv_Length2);
                    //if (Length1>(Row22-Row12)*0.25)
                    ho_Rectangle1.Dispose();
                    HOperatorSet.GenRectangle2(out ho_Rectangle1, hv_Row, hv_Column, hv_Phi,
                                               hv_Length1 * 0.85, hv_Length2);
                    ho_ImageReduced1.Dispose();
                    HOperatorSet.ReduceDomain(ho_ImageReduced, ho_Rectangle1, out ho_ImageReduced1
                                              );
                    //*矩形4个角点
                    HOperatorSet.TupleCos(hv_Phi, out hv_Cos);
                    HOperatorSet.TupleSin(hv_Phi, out hv_Sin);
                    //*dev_set_color('green')
                    hv_RT_X = (((-hv_Length1) * 0.85) * hv_Cos) - (hv_Length2 * hv_Sin);
                    hv_RT_Y = (((-hv_Length1) * 0.85) * hv_Sin) + (hv_Length2 * hv_Cos);
                    ho_Circle1.Dispose();
                    HOperatorSet.GenCircle(out ho_Circle1, hv_Row - hv_RT_Y, hv_Column + hv_RT_X,
                                           0.5);
                    //*最小矩形的顶点A
                    hv_AX = (((-hv_Length1) * 0.85) * hv_Cos) - (hv_Length2 * hv_Sin);
                    hv_AY = (((-hv_Length1) * 0.85) * hv_Sin) + (hv_Length2 * hv_Cos);
                    //*dev_set_color('red')
                    hv_RB_X = ((hv_Length1 * 0.85) * hv_Cos) - (hv_Length2 * hv_Sin);
                    hv_RB_Y = ((hv_Length1 * 0.85) * hv_Sin) + (hv_Length2 * hv_Cos);
                    ho_Circle2.Dispose();
                    HOperatorSet.GenCircle(out ho_Circle2, hv_Row - hv_RB_Y, hv_Column + hv_RB_X,
                                           0.5);
                    //*最小矩形的顶点B
                    hv_BX = ((hv_Length1 * 0.85) * hv_Cos) - (hv_Length2 * hv_Sin);
                    hv_BY = ((hv_Length1 * 0.85) * hv_Sin) + (hv_Length2 * hv_Cos);

                    //*dev_set_color('yellow')
                    hv_LB_X = ((hv_Length1 * 0.85) * hv_Cos) + (hv_Length2 * hv_Sin);
                    hv_LB_Y = ((hv_Length1 * 0.85) * hv_Sin) - (hv_Length2 * hv_Cos);
                    ho_Circle3.Dispose();
                    HOperatorSet.GenCircle(out ho_Circle3, hv_Row - hv_LB_Y, hv_Column + hv_LB_X,
                                           0.5);
                    //*最小矩形的顶点C
                    hv_CX = ((hv_Length1 * 0.85) * hv_Cos) + (hv_Length2 * hv_Sin);
                    hv_CY = ((hv_Length1 * 0.85) * hv_Sin) - (hv_Length2 * hv_Cos);

                    //*dev_set_color('pink')
                    hv_LT_X = (((-hv_Length1) * 0.85) * hv_Cos) + (hv_Length2 * hv_Sin);
                    hv_LT_Y = (((-hv_Length1) * 0.85) * hv_Sin) - (hv_Length2 * hv_Cos);
                    ho_Circle4.Dispose();
                    HOperatorSet.GenCircle(out ho_Circle4, hv_Row - hv_LT_Y, hv_Column + hv_LT_X,
                                           0.5);
                    //*最小矩形的顶点D
                    hv_DX = (((-hv_Length1) * 0.85) * hv_Cos) + (hv_Length2 * hv_Sin);
                    hv_DY = (((-hv_Length1) * 0.85) * hv_Sin) - (hv_Length2 * hv_Cos);

                    //stop ()

                    ho_RegionUnionc1.Dispose();
                    HOperatorSet.Union2(ho_Circle1, ho_Circle2, out ho_RegionUnionc1);
                    ho_RegionUnionc2.Dispose();
                    HOperatorSet.Union2(ho_RegionUnionc1, ho_Circle3, out ho_RegionUnionc2);
                    ho_RegionUnionc3.Dispose();
                    HOperatorSet.Union2(ho_RegionUnionc2, ho_Circle4, out ho_RegionUnionc3);
                    ho_ConnectedRegionsc.Dispose();
                    HOperatorSet.Connection(ho_RegionUnionc3, out ho_ConnectedRegionsc);
                    //count_obj (RegionUnionc3, Number1)
                    ho_SortedRegions1.Dispose();
                    HOperatorSet.SortRegion(ho_ConnectedRegionsc, out ho_SortedRegions1, "upper_left",
                                            "true", "row");

                    HOperatorSet.AreaCenter(ho_SortedRegions1, out hv_Area, out hv_Row1, out hv_Column1);
                    HOperatorSet.DistancePp(hv_Row1.TupleSelect(0), hv_Column1.TupleSelect(
                                                0), hv_Row1.TupleSelect(1), hv_Column1.TupleSelect(1), out hv_Distance);
                    HOperatorSet.DistancePl(hv_Row1.TupleSelect(0), hv_Column1.TupleSelect(
                                                0), hv_Row1.TupleSelect(2), hv_Column1.TupleSelect(2), hv_Row1.TupleSelect(
                                                3), hv_Column1.TupleSelect(3), out hv_Distance1);
                    //if (i>1)
                    hv_h = hv_h.TupleConcat((hv_Distance1 * 4) / 3);
                    //endif
                    hv_w = hv_w.TupleConcat(hv_Distance);

                    //gen_region_line (RegionLines2, (Row1[0]+Row1[1])/2, (Column1[0]+Column1[1])/2, (Row1[2]+Row1[3])/2, (Column1[2]+Column1[3])/2)
                    hv_shapeParam = new HTuple();
                    hv_shapeParam = hv_shapeParam.TupleConcat(((hv_Row1.TupleSelect(
                                                                    0)) + (hv_Row1.TupleSelect(1))) / 2);
                    hv_shapeParam = hv_shapeParam.TupleConcat(((hv_Column1.TupleSelect(
                                                                    0)) + (hv_Column1.TupleSelect(1))) / 2);
                    hv_shapeParam = hv_shapeParam.TupleConcat(((hv_Row1.TupleSelect(
                                                                    2)) + (hv_Row1.TupleSelect(3))) / 2);
                    hv_shapeParam = hv_shapeParam.TupleConcat(((hv_Column1.TupleSelect(
                                                                    2)) + (hv_Column1.TupleSelect(3))) / 2);

                    //shapeParam := [Row12,(Column22+Column12)/2,Row22,(Column22+Column12)/2]
                    //找左侧点
                    HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandleL);
                    HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandleL, hv_Width,
                                                            hv_Height);
                    HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandleL, "line", hv_shapeParam,
                                                           hv_Distance / 2, 1, 1, 60, new HTuple(), new HTuple(), out hv_Index);
                    //设置参数
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "measure_transition",
                                                         "negative");
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "num_measures",
                                                         150);
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "num_instances",
                                                         1);
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "measure_select",
                                                         "last");

                    //开始找边缘
                    HOperatorSet.ApplyMetrologyModel(ho_ImageReduced1, hv_MetrologyHandleL);
                    ho_ContoursL.Dispose();
                    HOperatorSet.GetMetrologyObjectMeasures(out ho_ContoursL, hv_MetrologyHandleL,
                                                            "all", "all", out hv_RowL, out hv_ColumnL);

                    if (HDevWindowStack.IsOpen())
                    {
                        //dev_set_color ('red')
                    }
                    //disp_cross (3600, RowL, ColumnL, 6, Phi)

                    //左侧点,用一Row,只取最左边点
                    //gen_cross_contour_xld (CrossL, RowL, ColumnL, 6, 0)
                    ///释放测量句柄
                    HOperatorSet.ClearMetrologyModel(hv_MetrologyHandleL);
                    //gen_region_line (RegionLines, Row11, Column11, Row11+1, Column11)
                    //union1 (RegionLines, RegionUnion1)

                    for (hv_l = 0; (int)hv_l <= (int)((new HTuple(hv_RowL.TupleLength())) - 2); hv_l = (int)hv_l + 1)
                    {
                        ho_RegionLinesL.Dispose();
                        HOperatorSet.GenRegionLine(out ho_RegionLinesL, hv_RowL.TupleSelect(hv_l),
                                                   hv_ColumnL.TupleSelect(hv_l), hv_RowL.TupleSelect(hv_l + 1), hv_ColumnL.TupleSelect(
                                                       hv_l + 1));
                        {
                            HObject ExpTmpOutVar_0;
                            HOperatorSet.Union2(ho_RegionUnion1, ho_RegionLinesL, out ExpTmpOutVar_0
                                                );
                            ho_RegionUnion1.Dispose();
                            ho_RegionUnion1 = ExpTmpOutVar_0;
                        }
                    }

                    ho_RegionLinesL1.Dispose();
                    HOperatorSet.GenRegionLine(out ho_RegionLinesL1, hv_RowL.TupleSelect(0),
                                               hv_ColumnL.TupleSelect(0), hv_RowL.TupleSelect((new HTuple(hv_RowL.TupleLength()
                                                                                                          )) - 1), hv_ColumnL.TupleSelect((new HTuple(hv_RowL.TupleLength())) - 1));
                    ho_RegionUnionL.Dispose();
                    HOperatorSet.Union2(ho_RegionUnion1, ho_RegionLinesL1, out ho_RegionUnionL
                                        );
                    ho_ConnectedRegionsL.Dispose();
                    HOperatorSet.Connection(ho_RegionUnionL, out ho_ConnectedRegionsL);
                    ho_SelectedRegionsL.Dispose();
                    HOperatorSet.SelectShapeStd(ho_ConnectedRegionsL, out ho_SelectedRegionsL,
                                                "max_area", 70);
                    ho_RegionFillUpL.Dispose();
                    HOperatorSet.FillUp(ho_SelectedRegionsL, out ho_RegionFillUpL);
                    ho_ContoursL.Dispose();
                    HOperatorSet.GenContourRegionXld(ho_RegionFillUpL, out ho_ContoursL, "border");
                    ho_ContoursSplitL.Dispose();
                    HOperatorSet.SegmentContoursXld(ho_ContoursL, out ho_ContoursSplitL, "lines_circles",
                                                    5, 8, 3.5);
                    ho_SelectedContoursL.Dispose();
                    HOperatorSet.SelectContoursXld(ho_ContoursSplitL, out ho_SelectedContoursL,
                                                   "contour_length", 70, 2000, -0.5, 0.5);
                    //***
                    ho_UnionContoursL.Dispose();
                    HOperatorSet.UnionAdjacentContoursXld(ho_SelectedContoursL, out ho_UnionContoursL,
                                                          10, 1, "attr_keep");
                    ho_RegionL.Dispose();
                    HOperatorSet.GenRegionContourXld(ho_UnionContoursL, out ho_RegionL, "filled");

                    ho_RegionDifferenceL.Dispose();
                    HOperatorSet.Difference(ho_RegionFillUpL, ho_RegionL, out ho_RegionDifferenceL
                                            );
                    HOperatorSet.CountObj(ho_RegionDifferenceL, out hv_NumberL);

                    //*
                    //找右侧点
                    HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandleR);
                    HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandleR, hv_Width,
                                                            hv_Height);
                    HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandleR, "line", hv_shapeParam,
                                                           hv_Distance / 2, 1, 1, 60, new HTuple(), new HTuple(), out hv_Index);
                    //设置参数
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleR, "all", "measure_transition",
                                                         "positive");
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleR, "all", "num_measures",
                                                         100);
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleR, "all", "num_instances",
                                                         1);
                    HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleR, "all", "measure_select",
                                                         "first");

                    //开始找边缘
                    HOperatorSet.ApplyMetrologyModel(ho_ImageReduced1, hv_MetrologyHandleR);
                    ho_ContoursR.Dispose();
                    HOperatorSet.GetMetrologyObjectMeasures(out ho_ContoursR, hv_MetrologyHandleR,
                                                            "all", "all", out hv_RowR, out hv_ColumnR);

                    //disp_obj (Image, 3600)
                    if (HDevWindowStack.IsOpen())
                    {
                        //dev_set_color ('red')
                    }
                    //disp_cross (3600, RowR, ColumnR, 6, Phi)
                    //*右侧点,用一Row,只取最右边点
                    //gen_cross_contour_xld (CrossR, RowR, ColumnR, 6, 0)

                    ///释放测量句柄
                    HOperatorSet.ClearMetrologyModel(hv_MetrologyHandleR);
                    //close_measure (MeasureHandle1)
                    //stop ()

                    //gen_region_line (RegionLines, Row11, Column11, Row11+1, Column11)
                    //union1 (RegionLines, RegionUnion1)

                    for (hv_r = 0; (int)hv_r <= (int)((new HTuple(hv_RowR.TupleLength())) - 2); hv_r = (int)hv_r + 1)
                    {
                        ho_RegionLinesR.Dispose();
                        HOperatorSet.GenRegionLine(out ho_RegionLinesR, hv_RowR.TupleSelect(hv_r),
                                                   hv_ColumnR.TupleSelect(hv_r), hv_RowR.TupleSelect(hv_r + 1), hv_ColumnR.TupleSelect(
                                                       hv_r + 1));
                        {
                            HObject ExpTmpOutVar_0;
                            HOperatorSet.Union2(ho_RegionUnion2, ho_RegionLinesR, out ExpTmpOutVar_0
                                                );
                            ho_RegionUnion2.Dispose();
                            ho_RegionUnion2 = ExpTmpOutVar_0;
                        }
                    }

                    ho_RegionLinesR1.Dispose();
                    HOperatorSet.GenRegionLine(out ho_RegionLinesR1, hv_RowR.TupleSelect(0),
                                               hv_ColumnR.TupleSelect(0), hv_RowR.TupleSelect((new HTuple(hv_RowR.TupleLength()
                                                                                                          )) - 1), hv_ColumnR.TupleSelect((new HTuple(hv_RowR.TupleLength())) - 1));
                    ho_RegionUnionR.Dispose();
                    HOperatorSet.Union2(ho_RegionUnion2, ho_RegionLinesR1, out ho_RegionUnionR
                                        );
                    ho_ConnectedRegionsR.Dispose();
                    HOperatorSet.Connection(ho_RegionUnionR, out ho_ConnectedRegionsR);
                    ho_SelectedRegionsR.Dispose();
                    HOperatorSet.SelectShapeStd(ho_ConnectedRegionsR, out ho_SelectedRegionsR,
                                                "max_area", 70);
                    ho_RegionFillUpR.Dispose();
                    HOperatorSet.FillUp(ho_SelectedRegionsR, out ho_RegionFillUpR);
                    ho_ContoursR.Dispose();
                    HOperatorSet.GenContourRegionXld(ho_RegionFillUpR, out ho_ContoursR, "border");
                    ho_ContoursSplitR.Dispose();
                    HOperatorSet.SegmentContoursXld(ho_ContoursR, out ho_ContoursSplitR, "lines_circles",
                                                    5, 8, 3.5);
                    ho_SelectedContoursR.Dispose();
                    HOperatorSet.SelectContoursXld(ho_ContoursSplitR, out ho_SelectedContoursR,
                                                   "contour_length", 70, 2000, -0.5, 0.5);
                    ho_UnionContoursR.Dispose();
                    HOperatorSet.UnionAdjacentContoursXld(ho_SelectedContoursR, out ho_UnionContoursR,
                                                          10, 1, "attr_keep");
                    ho_RegionR.Dispose();
                    HOperatorSet.GenRegionContourXld(ho_UnionContoursR, out ho_RegionR, "filled");

                    ho_RegionDifferenceR.Dispose();
                    HOperatorSet.Difference(ho_RegionFillUpR, ho_RegionR, out ho_RegionDifferenceR
                                            );
                    HOperatorSet.CountObj(ho_RegionDifferenceR, out hv_NumberR);

                    //stop ()

                    if ((int)(new HTuple(hv_NumberL.TupleGreater(0))) != 0)
                    {
                        HOperatorSet.AreaCenter(ho_RegionDifferenceL, out hv_AreaL, out hv_RowL,
                                                out hv_ColumnL);
                        {
                            HObject ExpTmpOutVar_0;
                            HOperatorSet.Union2(ho_RegionUnion3, ho_RegionDifferenceL, out ExpTmpOutVar_0
                                                );
                            ho_RegionUnion3.Dispose();
                            ho_RegionUnion3 = ExpTmpOutVar_0;
                        }
                    }
                    if ((int)(new HTuple(hv_NumberR.TupleGreater(0))) != 0)
                    {
                        HOperatorSet.AreaCenter(ho_RegionDifferenceR, out hv_AreaR, out hv_RowR,
                                                out hv_ColumnR);
                        {
                            HObject ExpTmpOutVar_0;
                            HOperatorSet.Union2(ho_RegionUnion3, ho_RegionDifferenceR, out ExpTmpOutVar_0
                                                );
                            ho_RegionUnion3.Dispose();
                            ho_RegionUnion3 = ExpTmpOutVar_0;
                        }
                    }
                    //union2 (RegionDifferenceL, RegionDifferenceR, RegionUnionLR)
                    //union2 (RegionUnion3, RegionUnionLR, RegionUnion3)
                    // stop(); only in hdevelop
                    //endif
                }

                HOperatorSet.AreaCenter(ho_RegionUnion3, out hv_Area1, out hv_Row2, out hv_Column2);
                ho_ConnectedRegions1.Dispose();
                HOperatorSet.Connection(ho_RegionUnion3, out ho_ConnectedRegions1);
                HOperatorSet.AreaCenter(ho_ConnectedRegions1, out hv_Area2, out hv_Row3, out hv_Column3);
                HOperatorSet.TupleConcat(hv_w1, hv_w, out hv_Concat);
                hv_hm = (hv_h.TupleMin()) / (hv_h.TupleMean());
                hv_wm = (hv_Concat.TupleMax()) / (hv_Concat.TupleMean());
                ho_ObjectSelected.Dispose();
                HOperatorSet.SelectObj(ho_SortedRegions, out ho_ObjectSelected, hv_Number);
                HOperatorSet.SmallestRectangle1(ho_ObjectSelected, out hv_Row13, out hv_Column13,
                                                out hv_Row23, out hv_Column23);
                hv_zh = hv_Column23 - hv_Column13;
                HOperatorSet.Union1(ho_ConnectedRegions1, out RegionToDisp);

                HTuple hv_result = GetHv_result();
                hv_result = hv_result.TupleConcat("最短比值");
                hv_result = hv_result.TupleConcat(hv_hm.D);
                hv_result = hv_result.TupleConcat("最宽比值");
                hv_result = hv_result.TupleConcat(hv_wm.D);
                hv_result = hv_result.TupleConcat("缺陷总面积");
                hv_result = hv_result.TupleConcat(hv_Area1.D);
                hv_result = hv_result.TupleConcat("最大缺陷面积");
                hv_result = hv_result.TupleConcat(hv_Area2.D);
                hv_result = hv_result.TupleConcat("宽度");
                hv_result = hv_result.TupleConcat(hv_zh.D);


                result = hv_result.Clone();
                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region.Dispose();
                ho_RegionLines.Dispose();
                ho_RegionUnion1.Dispose();
                ho_RegionUnion2.Dispose();
                ho_RegionUnion3.Dispose();
                ho_Region1.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_RegionFillUp.Dispose();
                ho_SelectedRegions.Dispose();
                ho_SortedRegions.Dispose();
                ho_ObjectSelected.Dispose();
                ho_Rectangle1.Dispose();
                ho_ImageReduced1.Dispose();
                ho_Circle1.Dispose();
                ho_Circle2.Dispose();
                ho_Circle3.Dispose();
                ho_Circle4.Dispose();
                ho_RegionUnionc1.Dispose();
                ho_RegionUnionc2.Dispose();
                ho_RegionUnionc3.Dispose();
                ho_ConnectedRegionsc.Dispose();
                ho_SortedRegions1.Dispose();
                ho_ContoursL.Dispose();
                ho_RegionLinesL.Dispose();
                ho_RegionLinesL1.Dispose();
                ho_RegionUnionL.Dispose();
                ho_ConnectedRegionsL.Dispose();
                ho_SelectedRegionsL.Dispose();
                ho_RegionFillUpL.Dispose();
                ho_ContoursSplitL.Dispose();
                ho_SelectedContoursL.Dispose();
                ho_UnionContoursL.Dispose();
                ho_RegionL.Dispose();
                ho_RegionDifferenceL.Dispose();
                ho_ContoursR.Dispose();
                ho_RegionLinesR.Dispose();
                ho_RegionLinesR1.Dispose();
                ho_RegionUnionR.Dispose();
                ho_ConnectedRegionsR.Dispose();
                ho_SelectedRegionsR.Dispose();
                ho_RegionFillUpR.Dispose();
                ho_ContoursSplitR.Dispose();
                ho_SelectedContoursR.Dispose();
                ho_UnionContoursR.Dispose();
                ho_RegionR.Dispose();
                ho_RegionDifferenceR.Dispose();
                ho_ConnectedRegions1.Dispose();

                ho_ObjectSelected1.Dispose();
                algorithm.Region.Dispose();
            }
            catch
            {
                HTuple hv_result = GetHv_result();
                hv_result = hv_result.TupleConcat("最短比值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("最宽比值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("缺陷总面积");
                hv_result = hv_result.TupleConcat(999999);
                hv_result = hv_result.TupleConcat("最大缺陷面积");
                hv_result = hv_result.TupleConcat(999999);
                hv_result = hv_result.TupleConcat("宽度");
                hv_result = hv_result.TupleConcat(999999);
                result    = hv_result.Clone();
                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region.Dispose();
                ho_RegionLines.Dispose();
                ho_RegionUnion1.Dispose();
                ho_RegionUnion2.Dispose();
                ho_RegionUnion3.Dispose();
                ho_Region1.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_RegionFillUp.Dispose();
                ho_SelectedRegions.Dispose();
                ho_SortedRegions.Dispose();
                ho_ObjectSelected.Dispose();
                ho_Rectangle1.Dispose();
                ho_ImageReduced1.Dispose();
                ho_Circle1.Dispose();
                ho_Circle2.Dispose();
                ho_Circle3.Dispose();
                ho_Circle4.Dispose();
                ho_RegionUnionc1.Dispose();
                ho_RegionUnionc2.Dispose();
                ho_RegionUnionc3.Dispose();
                ho_ConnectedRegionsc.Dispose();
                ho_SortedRegions1.Dispose();
                ho_ContoursL.Dispose();
                ho_RegionLinesL.Dispose();
                ho_RegionLinesL1.Dispose();
                ho_RegionUnionL.Dispose();
                ho_ConnectedRegionsL.Dispose();
                ho_SelectedRegionsL.Dispose();
                ho_RegionFillUpL.Dispose();
                ho_ContoursSplitL.Dispose();
                ho_SelectedContoursL.Dispose();
                ho_UnionContoursL.Dispose();
                ho_RegionL.Dispose();
                ho_RegionDifferenceL.Dispose();
                ho_ContoursR.Dispose();
                ho_RegionLinesR.Dispose();
                ho_RegionLinesR1.Dispose();
                ho_RegionUnionR.Dispose();
                ho_ConnectedRegionsR.Dispose();
                ho_SelectedRegionsR.Dispose();
                ho_RegionFillUpR.Dispose();
                ho_ContoursSplitR.Dispose();
                ho_SelectedContoursR.Dispose();
                ho_UnionContoursR.Dispose();
                ho_RegionR.Dispose();
                ho_RegionDifferenceR.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_ObjectSelected1.Dispose();
                algorithm.Region.Dispose();
            }
            finally
            {
                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region.Dispose();
                ho_RegionLines.Dispose();
                ho_RegionUnion1.Dispose();
                ho_RegionUnion2.Dispose();
                ho_RegionUnion3.Dispose();
                ho_Region1.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_RegionFillUp.Dispose();
                ho_SelectedRegions.Dispose();
                ho_SortedRegions.Dispose();
                ho_ObjectSelected.Dispose();
                ho_Rectangle1.Dispose();
                ho_ImageReduced1.Dispose();
                ho_Circle1.Dispose();
                ho_Circle2.Dispose();
                ho_Circle3.Dispose();
                ho_Circle4.Dispose();
                ho_RegionUnionc1.Dispose();
                ho_RegionUnionc2.Dispose();
                ho_RegionUnionc3.Dispose();
                ho_ConnectedRegionsc.Dispose();
                ho_SortedRegions1.Dispose();
                ho_ContoursL.Dispose();
                ho_RegionLinesL.Dispose();
                ho_RegionLinesL1.Dispose();
                ho_RegionUnionL.Dispose();
                ho_ConnectedRegionsL.Dispose();
                ho_SelectedRegionsL.Dispose();
                ho_RegionFillUpL.Dispose();
                ho_ContoursSplitL.Dispose();
                ho_SelectedContoursL.Dispose();
                ho_UnionContoursL.Dispose();
                ho_RegionL.Dispose();
                ho_RegionDifferenceL.Dispose();
                ho_ContoursR.Dispose();
                ho_RegionLinesR.Dispose();
                ho_RegionLinesR1.Dispose();
                ho_RegionUnionR.Dispose();
                ho_ConnectedRegionsR.Dispose();
                ho_SelectedRegionsR.Dispose();
                ho_RegionFillUpR.Dispose();
                ho_ContoursSplitR.Dispose();
                ho_SelectedContoursR.Dispose();
                ho_UnionContoursR.Dispose();
                ho_RegionR.Dispose();
                ho_RegionDifferenceR.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_ObjectSelected1.Dispose();
                algorithm.Region.Dispose();
            }
        }
예제 #4
0
        public static void dev_display_shape_matching_results(HTuple hv_WindowHandle, HTuple hv_ModelID, HTuple hv_Color, HTuple hv_Row, HTuple hv_Column, HTuple hv_Angle, HTuple hv_ScaleR, HTuple hv_ScaleC, HTuple hv_Model)
        {
            if (hv_WindowHandle == null)
            {
                return;
            }
            // Local iconic variables
            HObject ho_ModelContours = null, ho_ContoursAffinTrans = null;
            // Local control variables
            HTuple hv_NumMatches, hv_Index = new HTuple();
            HTuple hv_Match = new HTuple(), hv_HomMat2DIdentity = new HTuple();
            HTuple hv_HomMat2DScale = new HTuple(), hv_HomMat2DRotate = new HTuple();
            HTuple hv_HomMat2DTranslate = new HTuple();

            HTuple hv_Model_COPY_INP_TMP  = hv_Model.Clone();
            HTuple hv_ScaleC_COPY_INP_TMP = hv_ScaleC.Clone();
            HTuple hv_ScaleR_COPY_INP_TMP = hv_ScaleR.Clone();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_ModelContours);
            HOperatorSet.GenEmptyObj(out ho_ContoursAffinTrans);

            try
            {
                //This procedure displays the results of Shape-Based Matching.
                //
                hv_NumMatches = new HTuple(hv_Row.TupleLength());
                if ((int)(new HTuple(hv_NumMatches.TupleGreater(0))) != 0)
                {
                    if ((int)(new HTuple((new HTuple(hv_ScaleR_COPY_INP_TMP.TupleLength())).TupleEqual(
                                             1))) != 0)
                    {
                        HOperatorSet.TupleGenConst(hv_NumMatches, hv_ScaleR_COPY_INP_TMP, out hv_ScaleR_COPY_INP_TMP);
                    }
                    if ((int)(new HTuple((new HTuple(hv_ScaleC_COPY_INP_TMP.TupleLength())).TupleEqual(
                                             1))) != 0)
                    {
                        HOperatorSet.TupleGenConst(hv_NumMatches, hv_ScaleC_COPY_INP_TMP, out hv_ScaleC_COPY_INP_TMP);
                    }
                    if ((int)(new HTuple((new HTuple(hv_Model_COPY_INP_TMP.TupleLength())).TupleEqual(
                                             0))) != 0)
                    {
                        HOperatorSet.TupleGenConst(hv_NumMatches, 0, out hv_Model_COPY_INP_TMP);
                    }
                    else if ((int)(new HTuple((new HTuple(hv_Model_COPY_INP_TMP.TupleLength()
                                                          )).TupleEqual(1))) != 0)
                    {
                        HOperatorSet.TupleGenConst(hv_NumMatches, hv_Model_COPY_INP_TMP, out hv_Model_COPY_INP_TMP);
                    }
                    for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_ModelID.TupleLength()
                                                                          )) - 1); hv_Index = (int)hv_Index + 1)
                    {
                        ho_ModelContours.Dispose();
                        HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelID.TupleSelect(
                                                               hv_Index), 1);
                        if (hv_WindowHandle > 0)
                        {
                            HOperatorSet.SetColor(hv_WindowHandle, hv_Color.TupleSelect(
                                                      hv_Index % (new HTuple(hv_Color.TupleLength()))));
                        }
                        for (hv_Match = 0; hv_Match.Continue(hv_NumMatches - 1, 1); hv_Match = hv_Match.TupleAdd(1))
                        {
                            if ((int)(new HTuple(hv_Index.TupleEqual(hv_Model_COPY_INP_TMP.TupleSelect(
                                                                         hv_Match)))) != 0)
                            {
                                HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity);
                                HOperatorSet.HomMat2dScale(hv_HomMat2DIdentity, hv_ScaleR_COPY_INP_TMP.TupleSelect(
                                                               hv_Match), hv_ScaleC_COPY_INP_TMP.TupleSelect(hv_Match), 0, 0,
                                                           out hv_HomMat2DScale);
                                HOperatorSet.HomMat2dRotate(hv_HomMat2DScale, hv_Angle.TupleSelect(
                                                                hv_Match), 0, 0, out hv_HomMat2DRotate);
                                HOperatorSet.HomMat2dTranslate(hv_HomMat2DRotate, hv_Row.TupleSelect(
                                                                   hv_Match), hv_Column.TupleSelect(hv_Match), out hv_HomMat2DTranslate);
                                ho_ContoursAffinTrans.Dispose();
                                HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_ContoursAffinTrans,
                                                                   hv_HomMat2DTranslate);
                                HOperatorSet.DispObj(ho_ContoursAffinTrans, hv_WindowHandle);
                            }
                        }
                    }
                }
                ho_ModelContours.Dispose();
                ho_ContoursAffinTrans.Dispose();

                return;
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_ModelContours.Dispose();
                ho_ContoursAffinTrans.Dispose();

                throw HDevExpDefaultException;
            }
        }
        public MeasureResult Action()
        {
            #region 輸出結果
            CircleResult mResult = null;
            #endregion

            // Local iconic variables

            HObject ho_R17_Circle = null;
            HObject ho_R17_ROI_Image = null, ho_R17_Region = null, ho_R17_ImageReduced = null;
            HObject ho_R17_Edges = null, ho_R17_ContoursSplit = null, ho_R17_SingleSegment = null;
            HObject ho_R17_ContEllipse = null;


            // Local control variables

            HTuple hv_msgOffsetY, hv_msgOffsetX;
            HTuple hv_STD_Row;
            HTuple hv_STD_Col, hv_Img_Row, hv_Img_Col, hv_Img_Rotate_Angle;
            HTuple hv_OffsetRow, hv_OffsetCol, hv_STD_R17_Row, hv_STD_R17_Col;
            HTuple hv_STD_R17_V_Row, hv_STD_R17_V_Col, hv_R17_X, hv_R17_Y;
            HTuple hv_R17_Pos_Row, hv_R17_Pos_Col, hv_R17_R;
            HTuple hv_alpha = new HTuple(), hv_R17_low = new HTuple();
            HTuple hv_R17_high = new HTuple(), hv_R17_NumSegments = new HTuple();
            HTuple hv_NumCircles = new HTuple(), hv_Num_Circle_Point = new HTuple();
            HTuple hv_R17 = new HTuple(), hv_i = new HTuple(), hv_Attrib = new HTuple();
            HTuple hv_R17_Row = new HTuple(), hv_R17_Column = new HTuple();
            HTuple hv_R17_Radius = new HTuple(), hv_R17_StartPhi = new HTuple();
            HTuple hv_R17_EndPhi = new HTuple(), hv_R17_PointOrder = new HTuple();
            HTuple hv_R17_MinDist = new HTuple(), hv_R17_MaxDist = new HTuple();
            HTuple hv_R17_AvgDist = new HTuple(), hv_R17_SigmaDist = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_R17_Circle);
            HOperatorSet.GenEmptyObj(out ho_R17_ROI_Image);
            HOperatorSet.GenEmptyObj(out ho_R17_Region);
            HOperatorSet.GenEmptyObj(out ho_R17_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_R17_Edges);
            HOperatorSet.GenEmptyObj(out ho_R17_ContoursSplit);
            HOperatorSet.GenEmptyObj(out ho_R17_SingleSegment);
            HOperatorSet.GenEmptyObj(out ho_R17_ContEllipse);

            //Measure: SDMS_R17
            //Author: John Hsieh
            //Date: 2012
            // dev_update_off(...); only in hdevelop
            HOperatorSet.SetSystem("border_shape_models", "false");

            //****Message Args
            hv_msgOffsetY = 100;
            hv_msgOffsetX = 100;


            //****Model Args

            //STD 中心點
            hv_STD_Row = 772;
            hv_STD_Col = 1003;

            //目前圖形 中心點
            hv_Img_Row = hv_AllModelRow.Clone();
            hv_Img_Col = hv_AllModelColumn.Clone();

            //目前圖形 Rotate Angle
            hv_Img_Rotate_Angle = hv_AllModelAngle.Clone();

            //目前圖形偏移量
            hv_OffsetRow = hv_Img_Row - hv_STD_Row;
            hv_OffsetCol = hv_Img_Col - hv_STD_Col;

            //****Display
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.ClearWindow(HDevWindowStack.GetActive());
            }
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
            }
            //*****R17
            //STD R17_ 位置
            hv_STD_R17_Row = 802;
            hv_STD_R17_Col = 660;

            //STD 向量 STD_R17_
            hv_STD_R17_V_Row = hv_STD_R17_Row - hv_STD_Row;
            hv_STD_R17_V_Col = hv_STD_R17_Col - hv_STD_Col;


            //R17_X, R17_Y 分量
            hv_R17_X = (hv_STD_R17_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_R17_V_Row * (hv_Img_Rotate_Angle.TupleSin()
                                                                                                    ));
            hv_R17_Y = (hv_STD_R17_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_R17_V_Col * (hv_Img_Rotate_Angle.TupleSin()
                                                                                                    ));


            //目前圖形 R17_ 位置
            hv_R17_Pos_Row = (hv_STD_Row + hv_R17_Y) + hv_OffsetRow;
            hv_R17_Pos_Col = (hv_STD_Col + hv_R17_X) + hv_OffsetCol;

            hv_R17_R = 17;

            ho_R17_Circle.Dispose();
            HOperatorSet.GenCircle(out ho_R17_Circle, hv_R17_Pos_Row, hv_R17_Pos_Col, hv_R17_R);
            if (HDevWindowStack.IsOpen())
            {
                //dev_display (R17_Circle)
            }
            //stop ()

            ho_R17_ROI_Image.Dispose();
            HOperatorSet.ReduceDomain(ho_Image, ho_R17_Circle, out ho_R17_ROI_Image);
            ho_R17_Region.Dispose();
            HOperatorSet.FastThreshold(ho_R17_ROI_Image, out ho_R17_Region, 100, 255, 15);
            ho_R17_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_R17_ROI_Image, ho_R17_Region, out ho_R17_ImageReduced
                                      );
            //stop ()
            //sobel_fast 具有較寬的選擇範圍,搭配 alpha 參數 (alpha 越大, 容錯範圍大)
            hv_alpha    = 0.9;
            hv_R17_low  = 10;
            hv_R17_high = 60;
            ho_R17_Edges.Dispose();
            HOperatorSet.EdgesSubPix(ho_R17_ImageReduced, out ho_R17_Edges, "sobel_fast",
                                     hv_alpha, hv_R17_low, hv_R17_high);
            //stop ()
            //*所有的數值越小,表示容錯範圍大,反之亦然
            ho_R17_ContoursSplit.Dispose();
            HOperatorSet.SegmentContoursXld(ho_R17_Edges, out ho_R17_ContoursSplit, "lines_circles",
                                            17, 1, 1);
            //Display the results
            //===========================================================
            HOperatorSet.CountObj(ho_R17_ContoursSplit, out hv_R17_NumSegments);
            hv_NumCircles       = 0;
            hv_Num_Circle_Point = 0;
            hv_R17 = 999;
            for (hv_i = 1; hv_i.Continue(hv_R17_NumSegments, 1); hv_i = hv_i.TupleAdd(1))
            {
                ho_R17_SingleSegment.Dispose();
                HOperatorSet.SelectObj(ho_R17_ContoursSplit, out ho_R17_SingleSegment, hv_i);
                HOperatorSet.GetContourGlobalAttribXld(ho_R17_SingleSegment, "cont_approx",
                                                       out hv_Attrib);
                if ((int)(new HTuple(hv_Attrib.TupleEqual(1))) != 0)
                {
                    HOperatorSet.FitCircleContourXld(ho_R17_SingleSegment, "atukey", -1, 2,
                                                     hv_Num_Circle_Point, 5, 2, out hv_R17_Row, out hv_R17_Column, out hv_R17_Radius,
                                                     out hv_R17_StartPhi, out hv_R17_EndPhi, out hv_R17_PointOrder);
                    ho_R17_ContEllipse.Dispose();
                    HOperatorSet.GenEllipseContourXld(out ho_R17_ContEllipse, hv_R17_Row, hv_R17_Column,
                                                      0, hv_R17_Radius, hv_R17_Radius, 0, (new HTuple(360)).TupleRad(), "positive",
                                                      1.0);
                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.DispObj(ho_R17_ContEllipse, HDevWindowStack.GetActive());
                    }
                    HOperatorSet.DistEllipseContourXld(ho_R17_SingleSegment, "algebraic", -1,
                                                       0, hv_R17_Row, hv_R17_Column, 0, hv_R17_Radius, hv_R17_Radius, out hv_R17_MinDist,
                                                       out hv_R17_MaxDist, out hv_R17_AvgDist, out hv_R17_SigmaDist);
                    hv_NumCircles = hv_NumCircles + 1;
                    if ((int)(new HTuple(hv_R17.TupleGreater(hv_R17_Radius))) != 0)
                    {
                        hv_R17  = hv_R17_Radius.Clone();
                        mResult = new CircleResult()
                        {
                            Row        = new HTuple(hv_R17_Row),
                            Col        = new HTuple(hv_R17_Column),
                            Radius     = new HTuple(hv_R17_Radius),
                            StartPhi   = new HTuple(hv_R17_StartPhi),
                            EndPhi     = new HTuple(hv_R17_EndPhi),
                            PointOrder = new HTuple(hv_R17_PointOrder),
                        };
                    }
                    //stop ()
                }
            }

            ho_R17_Circle.Dispose();
            ho_R17_ROI_Image.Dispose();
            ho_R17_Region.Dispose();
            ho_R17_ImageReduced.Dispose();
            ho_R17_Edges.Dispose();
            ho_R17_ContoursSplit.Dispose();
            ho_R17_SingleSegment.Dispose();
            ho_R17_ContEllipse.Dispose();


            return(mResult);
        }
예제 #6
0
        /// <summary>
        /// 传入原图 传出分离通道后concat一起的图
        /// </summary>
        /// <param name="ho_i_Image"></param>
        /// <param name="ho_o_Image"></param>
        /// <returns></returns>
        public static HObject Separat_Image(HObject ho_i_Image, out HObject ho_o_Image)
        {
            HObject ho_ObjectSelected = null, ho_Image1 = null;
            HObject ho_Image2 = null, ho_Image3 = null, ho_ObjectsConcat = null;

            // Local control variables

            HTuple hv_Number = null, hv_Index = null, hv_Channels = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_o_Image);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected);
            HOperatorSet.GenEmptyObj(out ho_Image1);
            HOperatorSet.GenEmptyObj(out ho_Image2);
            HOperatorSet.GenEmptyObj(out ho_Image3);
            HOperatorSet.GenEmptyObj(out ho_ObjectsConcat);

            HOperatorSet.CountObj(ho_i_Image, out hv_Number);
            ho_o_Image.Dispose();
            HOperatorSet.GenEmptyObj(out ho_o_Image);
            HTuple end_val3  = hv_Number;
            HTuple step_val3 = 1;

            for (hv_Index = 1; hv_Index.Continue(end_val3, step_val3); hv_Index = hv_Index.TupleAdd(step_val3))
            {
                ho_ObjectSelected.Dispose();
                HOperatorSet.SelectObj(ho_i_Image, out ho_ObjectSelected, hv_Index);
                HOperatorSet.CountChannels(ho_ObjectSelected, out hv_Channels);
                if ((int)(new HTuple(hv_Channels.TupleEqual(3))) != 0)
                {
                    ho_Image1.Dispose(); ho_Image2.Dispose(); ho_Image3.Dispose();
                    HOperatorSet.Decompose3(ho_ObjectSelected, out ho_Image1, out ho_Image2,
                                            out ho_Image3);
                    ho_ObjectsConcat.Dispose();
                    HOperatorSet.ConcatObj(ho_Image1, ho_Image2, out ho_ObjectsConcat);
                    {
                        HObject ExpTmpOutVar_0;
                        HOperatorSet.ConcatObj(ho_ObjectsConcat, ho_Image3, out ExpTmpOutVar_0);
                        ho_ObjectsConcat.Dispose();
                        ho_ObjectsConcat = ExpTmpOutVar_0;
                    }
                }
                else
                {
                    ho_ObjectsConcat.Dispose();
                    ho_ObjectsConcat = ho_ObjectSelected.CopyObj(1, -1);
                }

                {
                    HObject ExpTmpOutVar_0;
                    HOperatorSet.ConcatObj(ho_o_Image, ho_ObjectsConcat, out ExpTmpOutVar_0);
                    ho_o_Image.Dispose();
                    ho_o_Image = ExpTmpOutVar_0;
                }
            }

            ho_ObjectSelected.Dispose();
            ho_Image1.Dispose();
            ho_Image2.Dispose();
            ho_Image3.Dispose();
            ho_ObjectsConcat.Dispose();

            return(ho_o_Image);
        }
예제 #7
0
        // Main procedure
        private void RunPick(bool leftpallet, bool lastlayer)
        {
            // Stack for temporary objects
            HObject[] OTemp = new HObject[20];
            // Local iconic variables
            HObject ho_Image, ho_ImageRectified = null, ho_FOV;
            HObject ho_Box, ho_ImageReduced, ho_Objects, ho_Cross = null;
            HObject ho_GripPoint = null, ho_ImageReducedGripPoint = null;
            HObject ho_ImageMedian = null, ho_ObjectGripPoint = null, ho_Cross1 = null;
            HObject ho_Arrow = null, ho_Cross2 = null, ho_ContCircle;
            // Local control variables
            HTuple hv_AcqHandle = null, hv_pi = null, hv_angle = null;
            HTuple hv_CamParam = null, hv_CamPose = null, hv_GMMHandle = null;
            HTuple hv_Width = null, hv_Height = null, hv_CamParamOut = null;
            HTuple hv_Classes1 = null, hv_Row1 = null, hv_Column1 = null;
            HTuple hv_Row2 = null, hv_Column2 = null, hv_Diameter = null;
            HTuple hv_Area1 = null, hv_Row3 = null, hv_Column3 = null;
            HTuple hv_len_area = null, hv_MAX = new HTuple(), hv_index = new HTuple();
            HTuple hv_out = new HTuple(), hv_Length = new HTuple();
            HTuple hv_i = new HTuple(), hv_INDEX = new HTuple(), hv_j = new HTuple();
            HTuple hv_pom = new HTuple(), hv_DevDia = new HTuple();
            HTuple hv_joint = new HTuple(), hv_x_ = new HTuple(), hv_y_ = new HTuple();
            HTuple hv_w_ = new HTuple(), hv_a = new HTuple(), hv_b = new HTuple();
            HTuple hv_k = new HTuple(), hv_x_cross = new HTuple();
            HTuple hv_y_cross = new HTuple(), hv_WorldPose = new HTuple();
            HTuple hv_HomMat3D = new HTuple(), hv_HomMat3DRotate = new HTuple();
            HTuple hv_Area2 = new HTuple();
            HTuple hv_Row5 = new HTuple(), hv_Column5 = new HTuple();
            HTuple hv_SumX = new HTuple(), hv_SumY = new HTuple();
            HTuple hv_GripPointX = new HTuple(), hv_GripPointY = new HTuple();
            HTuple hv_maxdist = new HTuple(), hv_distgrippoints = new HTuple();
            HTuple hv_indexgrip = new HTuple(), hv_distgrip = new HTuple();
            HTuple hv_OrientX = new HTuple(), hv_OrientY = new HTuple();
            HTuple hv_maxX = new HTuple(), hv_diff01 = new HTuple();
            HTuple hv_diff02 = new HTuple(), hv_diff12 = new HTuple();
            HTuple hv_mindiff = new HTuple(), hv_GX = new HTuple();
            HTuple hv_GY = new HTuple(), hv_OX = new HTuple(), hv_OY = new HTuple();
            HTuple hv_DY = new HTuple(), hv_DX = new HTuple(), hv_theta = new HTuple();
            HTuple hv_anglerad = new HTuple();
            //
            HTuple hv_Pixel1Y = new HTuple(), hv_Pixel2X = new HTuple(), hv_Pixel1X = new HTuple();
            HTuple hv_Pixel2Y = new HTuple(), hv_distance = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_ImageRectified);
            HOperatorSet.GenEmptyObj(out ho_FOV);
            HOperatorSet.GenEmptyObj(out ho_Box);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Objects);
            HOperatorSet.GenEmptyObj(out ho_Cross);
            HOperatorSet.GenEmptyObj(out ho_GripPoint);
            HOperatorSet.GenEmptyObj(out ho_ImageReducedGripPoint);
            HOperatorSet.GenEmptyObj(out ho_ImageMedian);
            HOperatorSet.GenEmptyObj(out ho_ObjectGripPoint);
            HOperatorSet.GenEmptyObj(out ho_Cross1);
            HOperatorSet.GenEmptyObj(out ho_Arrow);
            HOperatorSet.GenEmptyObj(out ho_Cross2);
            HOperatorSet.GenEmptyObj(out ho_ContCircle);

            // Set Default Variables
            hv_angledeg = 0;
            // Wait for CAM4 thread to be closed
            _waitHandleCam1.WaitOne();
            // Close te thread DOOR
            _waitHandleCam1.Reset();
            //Image Acquisition
            try
            {
                HOperatorSet.OpenFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1, "default", -1, "false", "default", "RobotPick", 0, -1, out hv_AcqHandleCam1);
                HOperatorSet.SetFramegrabberParam(hv_AcqHandleCam1, "ExposureAuto", "Continuous");
            }
            catch (HalconException exceptionCameraNotConnected)
            {
            }
            // Constants
            hv_pi    = 3.14159265359;
            hv_angle = (0 * hv_pi) / 180;

            // Pallet calibration selection
            if (leftpallet)
            {
                // Relative path to file
                string IntrinsicsFileName = "CamParametersLeft.cal";
                string IntrinsicsPath     = Path.Combine(Environment.CurrentDirectory, @"CamPar", IntrinsicsFileName);
                //
                string ExtrinsicsFileName = "CamPoseLeft.dat";
                string ExtrinsicsPath     = Path.Combine(Environment.CurrentDirectory, @"CamPar", ExtrinsicsFileName);
                HOperatorSet.ReadCamPar(IntrinsicsPath, out hv_CamParam);
                HOperatorSet.ReadPose(ExtrinsicsPath, out hv_CamPose);
            }
            else
            {
                // Relative path to file
                string IntrinsicsFileName = "CamParametersRight.cal";
                string IntrinsicsPath     = Path.Combine(Environment.CurrentDirectory, @"CamPar", IntrinsicsFileName);
                //
                string ExtrinsicsFileName = "CamPoseRight.dat";
                string ExtrinsicsPath     = Path.Combine(Environment.CurrentDirectory, @"CamPar", ExtrinsicsFileName);
                HOperatorSet.ReadCamPar(IntrinsicsPath, out hv_CamParam);
                HOperatorSet.ReadPose(ExtrinsicsPath, out hv_CamPose);
            }
            //
            string TrainegGmmFileName = "traineg_gmm_pick_1.4.ggc";
            string TrainegGmmPath     = Path.Combine(Environment.CurrentDirectory, @"Train", TrainegGmmFileName);

            // Camera intrinsics and extrinsics call

            HOperatorSet.ReadClassGmm(TrainegGmmPath, out hv_GMMHandle);
            // Grab operator
            ho_Image.Dispose();
            HOperatorSet.GrabImage(out ho_Image, hv_AcqHandleCam1);
            HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
            ho_FOV.Dispose();
            HOperatorSet.GenRectangle1(out ho_FOV, 0, 0, hv_Height, hv_Width);
            ho_Box.Dispose();
            if (leftpallet)
            {
                HOperatorSet.GenRectangle1(out ho_Box, 190, 160, 870, 1130);
            }
            else
            {
                HOperatorSet.GenRectangle1(out ho_Box, 190, 160, 870, 1130);
            }
            HOperatorSet.ChangeRadialDistortionCamPar("adaptive", hv_CamParam, 0, out hv_CamParamOut);
            ho_ImageRectified.Dispose();
            HOperatorSet.ChangeRadialDistortionImage(ho_Image, ho_FOV, out ho_ImageRectified, hv_CamParam, hv_CamParamOut);
            ho_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_ImageRectified, ho_Box, out ho_ImageReduced);
            ho_ImageRectified.Dispose();
            HOperatorSet.Illuminate(ho_ImageReduced, out ho_ImageRectified, 230, 230, 1.15);
            ho_ImageMedian.Dispose();
            HOperatorSet.MedianImage(ho_ImageRectified, out ho_ImageRectified, "circle", 3, "mirrored");
            ho_ObjectGripPoint.Dispose();
            //segmentacija i klasifikacija
            ho_Objects.Dispose();
            segment(ho_ImageRectified, out ho_Objects);
            classify(ho_Objects, hv_GMMHandle, out hv_Classes1);
            disp_obj_class(ho_Objects, hv_Classes1);
            {
                HObject ExpTmpOutVar_0;
                HOperatorSet.Connection(ho_Objects, out ExpTmpOutVar_0);
                ho_Objects.Dispose();
                ho_Objects = ExpTmpOutVar_0;
            }

            //prefiltriranje klasificiranih predmeta
            {
                HObject ExpTmpOutVar_0;
                HOperatorSet.SelectShape(ho_Objects, out ExpTmpOutVar_0, "circularity", "and", 0.5, 1.0);
                ho_Objects.Dispose();
                ho_Objects = ExpTmpOutVar_0;
            }

            {
                HObject ExpTmpOutVar_0;
                HOperatorSet.SelectShape(ho_Objects, out ExpTmpOutVar_0, "area", "and", 1000, 100000);
                ho_Objects.Dispose();
                ho_Objects = ExpTmpOutVar_0;
            }

            //** Carolija ***
            HOperatorSet.DiameterRegion(ho_Objects, out hv_Row1, out hv_Column1, out hv_Row2, out hv_Column2, out hv_Diameter);
            HOperatorSet.AreaCenter(ho_Objects, out hv_Area1, out hv_Row3, out hv_Column3);
            HOperatorSet.TupleLength(hv_Area1, out hv_len_area);
            //* If objects exist sort them and find their 2D position
            if ((int)(new HTuple(hv_len_area.TupleGreater(0))) != 0)
            {
                hv_MAX   = 0;
                hv_index = 0;
                hv_out   = 0;
                HOperatorSet.TupleLength(hv_Diameter, out hv_Length);

                HOperatorSet.DispObj(ho_ImageRectified, hv_ExpDefaultWinHandle);
                HOperatorSet.DispLine(hv_ExpDefaultWinHandle, hv_Height / 2, 0, hv_Height / 2, hv_Width);
                HOperatorSet.DispLine(hv_ExpDefaultWinHandle, 0, hv_Width / 2, hv_Height, hv_Width / 2);

                hv_i = 1;
                HOperatorSet.TupleGenSequence(0, hv_Length - 1, 1, out hv_INDEX);
                while ((int)(new HTuple(hv_i.TupleLess(hv_Length))) != 0)
                {
                    hv_j = hv_i.Clone();
                    while ((int)(new HTuple(((hv_Diameter.TupleSelect(hv_j - 1))).TupleLess(hv_Diameter.TupleSelect(hv_j)))) != 0)
                    {
                        hv_pom = hv_Diameter.TupleSelect(hv_j);
                        if (hv_Diameter == null)
                        {
                            hv_Diameter = new HTuple();
                        }
                        hv_Diameter[hv_j] = hv_Diameter.TupleSelect(hv_j - 1);
                        if (hv_Diameter == null)
                        {
                            hv_Diameter = new HTuple();
                        }
                        hv_Diameter[hv_j - 1] = hv_pom;
                        hv_pom = hv_INDEX.TupleSelect(hv_j);
                        if (hv_INDEX == null)
                        {
                            hv_INDEX = new HTuple();
                        }
                        hv_INDEX[hv_j] = hv_INDEX.TupleSelect(hv_j - 1);
                        if (hv_INDEX == null)
                        {
                            hv_INDEX = new HTuple();
                        }
                        hv_INDEX[hv_j - 1] = hv_pom;
                        hv_j = hv_j - 1;
                        if ((int)(new HTuple(hv_j.TupleEqual(0))) != 0)
                        {
                            break;
                        }
                    }
                    hv_i = hv_i + 1;
                }

                HOperatorSet.TupleMedian(hv_Diameter, out hv_MAX);
                HOperatorSet.TupleDeviation(hv_Diameter, out hv_DevDia);

                HTuple end_val75  = hv_Length;
                HTuple step_val75 = 1;
                for (hv_i = 1; hv_i.Continue(end_val75, step_val75); hv_i = hv_i.TupleAdd(step_val75))
                {
                    if ((int)((new HTuple(((hv_Diameter.TupleSelect(hv_i - 1))).TupleGreaterEqual(
                                              hv_MAX - (5 * hv_DevDia)))).TupleAnd(new HTuple(((hv_Diameter.TupleSelect(
                                                                                                    hv_i - 1))).TupleLessEqual(hv_MAX + (5 * hv_DevDia))))) != 0)
                    {
                        if ((int)(new HTuple(hv_MAX.TupleGreaterEqual(2 * 300))) != 0)
                        {
                            hv_joint = 1;
                        }
                        hv_index = hv_index + 1;
                    }
                }

                //* w_ => weight factor for L1 norm             **
                //* row component weighting a => sort by column **
                //* column component weighting b => sort by row **
                hv_x_ = new HTuple();
                hv_y_ = new HTuple();
                hv_w_ = new HTuple();
                hv_a  = 0.2;
                hv_b  = 1.0;

                HTuple end_val93  = hv_index;
                HTuple step_val93 = 1;
                for (hv_k = 1; hv_k.Continue(end_val93, step_val93); hv_k = hv_k.TupleAdd(step_val93))
                {
                    if (hv_x_ == null)
                    {
                        hv_x_ = new HTuple();
                    }
                    hv_x_[hv_k - 1] = hv_Row3.TupleSelect(hv_k - 1);
                    if (hv_y_ == null)
                    {
                        hv_y_ = new HTuple();
                    }
                    hv_y_[hv_k - 1] = hv_Column3.TupleSelect(hv_k - 1);
                    if (hv_w_ == null)
                    {
                        hv_w_ = new HTuple();
                    }
                    hv_w_[hv_k - 1] = ((hv_x_.TupleSelect(hv_k - 1)) * hv_a) + ((hv_y_.TupleSelect(hv_k - 1)) * hv_b);
                }

                //* Sort objects by cost value **
                hv_i = 1;
                while ((int)(new HTuple(hv_i.TupleLess(hv_index))) != 0)
                {
                    hv_j = hv_i.Clone();
                    while ((int)(new HTuple(((hv_w_.TupleSelect(hv_j - 1))).TupleGreater(hv_w_.TupleSelect(hv_j)))) != 0)
                    {
                        hv_pom = hv_x_.TupleSelect(hv_j);
                        if (hv_x_ == null)
                        {
                            hv_x_ = new HTuple();
                        }
                        hv_x_[hv_j] = hv_x_.TupleSelect(hv_j - 1);
                        if (hv_x_ == null)
                        {
                            hv_x_ = new HTuple();
                        }
                        hv_x_[hv_j - 1] = hv_pom;
                        hv_pom          = hv_y_.TupleSelect(hv_j);
                        if (hv_y_ == null)
                        {
                            hv_y_ = new HTuple();
                        }
                        hv_y_[hv_j] = hv_y_.TupleSelect(hv_j - 1);
                        if (hv_y_ == null)
                        {
                            hv_y_ = new HTuple();
                        }
                        hv_y_[hv_j - 1] = hv_pom;
                        hv_pom          = hv_w_.TupleSelect(hv_j);
                        if (hv_w_ == null)
                        {
                            hv_w_ = new HTuple();
                        }
                        hv_w_[hv_j] = hv_w_.TupleSelect(hv_j - 1);
                        if (hv_w_ == null)
                        {
                            hv_w_ = new HTuple();
                        }
                        hv_w_[hv_j - 1] = hv_pom;
                        hv_j            = hv_j - 1;
                        if ((int)(new HTuple(hv_j.TupleEqual(0))) != 0)
                        {
                            break;
                        }
                    }
                    hv_i = hv_i + 1;
                }

                //* Display detected objects **
                HTuple end_val124  = hv_index;
                HTuple step_val124 = 1;
                for (hv_k = 1; hv_k.Continue(end_val124, step_val124); hv_k = hv_k.TupleAdd(step_val124))
                {
                    hv_x_cross = hv_x_.TupleSelect(hv_k - 1);
                    hv_y_cross = hv_y_.TupleSelect(hv_k - 1);
                    HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green");
                    ho_Cross.Dispose();
                    HOperatorSet.GenCrossContourXld(out ho_Cross, hv_x_cross, hv_y_cross, 200, 0);
                    HOperatorSet.DispObj(ho_Cross, hv_ExpDefaultWinHandle);
                    HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "red");
                    HOperatorSet.SetTposition(hv_ExpDefaultWinHandle, hv_x_cross, hv_y_cross);
                    HOperatorSet.WriteString(hv_ExpDefaultWinHandle, hv_k);
                }

                HOperatorSet.SetOriginPose(hv_CamPose, 0, 0, 0.00, out hv_WorldPose);
                HOperatorSet.PoseToHomMat3d(hv_WorldPose, out hv_HomMat3D);
                HOperatorSet.HomMat3dRotateLocal(hv_HomMat3D, hv_angle, "z", out hv_HomMat3DRotate);
                HOperatorSet.HomMat3dToPose(hv_HomMat3DRotate, out hv_WorldPose);
                HOperatorSet.ImagePointsToWorldPlane(hv_CamParamOut, hv_WorldPose, hv_x_.TupleSelect(0), hv_y_.TupleSelect(0), "mm", out hv_X, out hv_Y);

                // Diameter check // Still not working
                //HOperatorSet.ImagePointsToWorldPlane(hv_CamParamOut, hv_WorldPose, hv_Row1, hv_Column1, "mm", out hv_Pixel1X, out hv_Pixel1Y);
                //HOperatorSet.ImagePointsToWorldPlane(hv_CamParamOut, hv_WorldPose, hv_Row2, hv_Column2, "mm", out hv_Pixel2X, out hv_Pixel2Y);
                //HOperatorSet.DistancePp(hv_Pixel1X, hv_Pixel1Y, hv_Pixel2X, hv_Pixel2Y, out hv_distance);
                //HOperatorSet.TupleMean(hv_distance, out hv_distance_mean);

                HOperatorSet.SetTposition(hv_ExpDefaultWinHandle, 100, 100);
                HOperatorSet.WriteString(hv_ExpDefaultWinHandle, hv_a);
                HOperatorSet.WriteString(hv_ExpDefaultWinHandle, ", ");
                HOperatorSet.WriteString(hv_ExpDefaultWinHandle, hv_b);
                HOperatorSet.SetTposition(hv_ExpDefaultWinHandle, 20, 20);
                HOperatorSet.WriteString(hv_ExpDefaultWinHandle, hv_Length);
            }
            //* If objects not found check for layer holes
            else if ((int)(new HTuple(hv_len_area.TupleGreater(0))) == 0 && lastlayer)
            {
                hv_X        = 0;
                hv_Y        = 0;
                hv_angledeg = 0;
                HOperatorSet.ClearWindow(hv_ExpDefaultWinHandle);
                HOperatorSet.DispObj(ho_ImageReduced, hv_ExpDefaultWinHandle);
                HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "red");
                HOperatorSet.SetTposition(hv_ExpDefaultWinHandle, 120, 450);
                HOperatorSet.WriteString(hv_ExpDefaultWinHandle, "NA ZADNJEM SLOJU NISU PRONADENI KOMADI! ROBOT IDE NA IDUCU ULAZNU PALETU");
                App.PaletaPrazna();
            }
            else
            {
                try
                {
                    ho_GripPoint.Dispose();
                    HOperatorSet.GenRectangle1(out ho_GripPoint, 420, 520, 620, 770);
                    ho_ImageReducedGripPoint.Dispose();
                    HOperatorSet.ReduceDomain(ho_ImageRectified, ho_GripPoint, out ho_ImageReducedGripPoint);
                    ho_ImageMedian.Dispose();
                    HOperatorSet.MedianImage(ho_ImageReducedGripPoint, out ho_ImageMedian, "circle", 3, "mirrored");
                    ho_ObjectGripPoint.Dispose();
                    segment(ho_ImageMedian, out ho_ObjectGripPoint);
                    {
                        HObject ExpTmpOutVar_0;
                        HOperatorSet.Connection(ho_ObjectGripPoint, out ExpTmpOutVar_0);
                        ho_ObjectGripPoint.Dispose();
                        ho_ObjectGripPoint = ExpTmpOutVar_0;
                    }
                    {
                        HObject ExpTmpOutVar_0;
                        HOperatorSet.SelectShape(ho_ObjectGripPoint, out ExpTmpOutVar_0, "circularity", "and", 0.5,
                                                 1.0);
                        ho_ObjectGripPoint.Dispose();
                        ho_ObjectGripPoint = ExpTmpOutVar_0;
                    }
                    {
                        HObject ExpTmpOutVar_0;
                        HOperatorSet.SelectShape(ho_ObjectGripPoint, out ExpTmpOutVar_0, "area", "and", 100, 1000);
                        ho_ObjectGripPoint.Dispose();
                        ho_ObjectGripPoint = ExpTmpOutVar_0;
                    }
                    HOperatorSet.AreaCenter(ho_ObjectGripPoint, out hv_Area2, out hv_Row5, out hv_Column5);
                    HOperatorSet.TupleSum(hv_Row5, out hv_SumX);
                    HOperatorSet.TupleSum(hv_Column5, out hv_SumY);
                    hv_GripPointX = hv_SumX / 9;
                    hv_GripPointY = hv_SumY / 9;
                    ho_Cross1.Dispose();
                    HOperatorSet.GenCrossContourXld(out ho_Cross1, hv_GripPointX, hv_GripPointY, 10, 0);
                    hv_maxdist        = 0;
                    hv_distgrippoints = new HTuple();
                    hv_indexgrip      = 0;
                    for (hv_i = 1; (int)hv_i <= 9; hv_i = (int)hv_i + 1)
                    {
                        HOperatorSet.DistancePp(hv_Row5.TupleSelect(hv_i - 1), hv_Column5.TupleSelect(hv_i - 1),
                                                hv_GripPointX, hv_GripPointY, out hv_distgrip);
                        HOperatorSet.TupleConcat(hv_distgrippoints, hv_distgrip, out hv_distgrippoints);
                        if ((int)(new HTuple(hv_distgrip.TupleGreater(hv_maxdist))) != 0)
                        {
                            hv_indexgrip = hv_i - 1;
                            hv_maxdist   = hv_distgrip.Clone();
                        }
                    }

                    hv_indexgrip = new HTuple();
                    for (hv_i = 1; (int)hv_i <= 9; hv_i = (int)hv_i + 1)
                    {
                        if ((int)(new HTuple(
                                      ((hv_distgrippoints.TupleSelect(hv_i - 1))).TupleGreater(0.9 * hv_maxdist))) != 0)
                        {
                            HOperatorSet.TupleConcat(hv_indexgrip, hv_i - 1, out hv_indexgrip);
                        }
                    }

                    hv_OrientX = 0;
                    hv_OrientY = 0;
                    hv_maxX    = 0;
                    HOperatorSet.TupleAbs(
                        (hv_Row5.TupleSelect(hv_indexgrip.TupleSelect(0))) -
                        (hv_Row5.TupleSelect(hv_indexgrip.TupleSelect(1))), out hv_diff01);
                    HOperatorSet.TupleAbs(
                        (hv_Row5.TupleSelect(hv_indexgrip.TupleSelect(0))) -
                        (hv_Row5.TupleSelect(hv_indexgrip.TupleSelect(2))), out hv_diff02);
                    HOperatorSet.TupleAbs(
                        (hv_Row5.TupleSelect(hv_indexgrip.TupleSelect(1))) -
                        (hv_Row5.TupleSelect(hv_indexgrip.TupleSelect(2))), out hv_diff12);
                    HOperatorSet.TupleMin(((hv_diff01.TupleConcat(hv_diff02))).TupleConcat(hv_diff12), out hv_mindiff);
                    if ((int)(new HTuple(hv_mindiff.TupleEqual(hv_diff01))) != 0)
                    {
                        hv_OrientX = hv_Row5.TupleSelect(hv_indexgrip.TupleSelect(2));
                        hv_OrientY = hv_Column5.TupleSelect(hv_indexgrip.TupleSelect(2));
                    }
                    else if ((int)(new HTuple(hv_mindiff.TupleEqual(hv_diff02))) != 0)
                    {
                        hv_OrientX = hv_Row5.TupleSelect(hv_indexgrip.TupleSelect(1));
                        hv_OrientY = hv_Column5.TupleSelect(hv_indexgrip.TupleSelect(1));
                    }
                    else if ((int)(new HTuple(hv_mindiff.TupleEqual(hv_diff12))) != 0)
                    {
                        hv_OrientX = hv_Row5.TupleSelect(hv_indexgrip.TupleSelect(0));
                        hv_OrientY = hv_Column5.TupleSelect(hv_indexgrip.TupleSelect(0));
                    }

                    HOperatorSet.DispObj(ho_ImageReduced, hv_ExpDefaultWinHandle);
                    HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "red");
                    ho_Arrow.Dispose();
                    gen_arrow_contour_xld(out ho_Arrow, hv_GripPointX, hv_GripPointY, hv_OrientX, hv_OrientY, 15, 15);
                    HOperatorSet.DispObj(ho_Arrow, hv_ExpDefaultWinHandle);
                    HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green");
                    ho_Cross2.Dispose();
                    HOperatorSet.GenCrossContourXld(out ho_Cross2, hv_GripPointX, hv_GripPointY, 30, 0);
                    //HOperatorSet.DispObj(ho_Cross2, hv_ExpDefaultWinHandle);
                    HOperatorSet.SetOriginPose(hv_CamPose, 0, 0, 0.00, out hv_WorldPose);
                    HOperatorSet.ImagePointsToWorldPlane(hv_CamParamOut, hv_WorldPose, hv_GripPointX, hv_GripPointY, "mm", out hv_GX, out hv_GY);
                    HOperatorSet.ImagePointsToWorldPlane(hv_CamParamOut, hv_WorldPose, hv_OrientX, hv_OrientY, "mm", out hv_OX, out hv_OY);
                    hv_DY = hv_OY - hv_GY;
                    hv_DX = hv_OX - hv_GX;
                    HOperatorSet.TupleAtan2(hv_DY, hv_DX, out hv_theta);
                    hv_theta    = hv_theta.Clone();
                    hv_X        = hv_GX;
                    hv_Y        = hv_GY;
                    hv_angledeg = (hv_theta * 180) / hv_pi;
                }
                catch (HalconException exceptionLayerNotFound)
                {
                    hv_X        = 0;
                    hv_Y        = 0;
                    hv_angledeg = 0;
                    HOperatorSet.ClearWindow(hv_ExpDefaultWinHandle);
                    HOperatorSet.DispObj(ho_ImageReduced, hv_ExpDefaultWinHandle);
                    HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "red");
                    HOperatorSet.SetTposition(hv_ExpDefaultWinHandle, 120, 512);
                    HOperatorSet.WriteString(hv_ExpDefaultWinHandle, "NA PALETI NISU PRONADENI KOMADI NI SLOJEVI!!!");
                    App.LayerNijePronaden();
                }
            }
            // HOperatorSet.ClearWindow(hv_ExpDefaultWinHandle);
            HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "red");
            ho_ContCircle.Dispose();
            HOperatorSet.GenCircleContourXld(out ho_ContCircle, hv_Height / 2, hv_Width / 2, 100, 0, 6.28318, "positive", 10);
            ho_Image.Dispose();
            ho_ImageRectified.Dispose();
            ho_FOV.Dispose();
            ho_Box.Dispose();
            ho_ImageReduced.Dispose();
            ho_Objects.Dispose();
            ho_Cross.Dispose();
            ho_GripPoint.Dispose();
            ho_ImageReducedGripPoint.Dispose();
            ho_ImageMedian.Dispose();
            ho_ObjectGripPoint.Dispose();
            ho_Cross1.Dispose();
            ho_Arrow.Dispose();
            ho_Cross2.Dispose();
            ho_ContCircle.Dispose();
            HOperatorSet.CloseFramegrabber(hv_AcqHandleCam1);
            // Open the thread DOOR
            _waitHandleCam1.Set();
        }
예제 #8
0
    // Chapter: Graphics / Text
    // Short Description: This procedure writes a text message.
    public void disp_message(HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem,
                             HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box)
    {
        // Local iconic variables

        // Local control variables

        HTuple hv_M = null, hv_N = null, hv_Red = null;
        HTuple hv_Green = null, hv_Blue = null, hv_RowI1Part = null;
        HTuple hv_ColumnI1Part = null, hv_RowI2Part = null, hv_ColumnI2Part = null;
        HTuple hv_RowIWin = null, hv_ColumnIWin = null, hv_WidthWin = null;
        HTuple hv_HeightWin = null, hv_I = null, hv_RowI = new HTuple();
        HTuple hv_ColumnI = new HTuple(), hv_StringI = new HTuple();
        HTuple hv_MaxAscent = new HTuple(), hv_MaxDescent = new HTuple();
        HTuple hv_MaxWidth = new HTuple(), hv_MaxHeight = new HTuple();
        HTuple hv_R1 = new HTuple(), hv_C1 = new HTuple(), hv_FactorRowI = new HTuple();
        HTuple hv_FactorColumnI = new HTuple(), hv_UseShadow = new HTuple();
        HTuple hv_ShadowColor = new HTuple(), hv_Exception = new HTuple();
        HTuple hv_Width = new HTuple(), hv_Index = new HTuple();
        HTuple hv_Ascent = new HTuple(), hv_Descent = new HTuple();
        HTuple hv_W = new HTuple(), hv_H = new HTuple(), hv_FrameHeight = new HTuple();
        HTuple hv_FrameWidth = new HTuple(), hv_R2 = new HTuple();
        HTuple hv_C2 = new HTuple(), hv_DrawMode = new HTuple();
        HTuple hv_CurrentColor        = new HTuple();
        HTuple hv_Box_COPY_INP_TMP    = hv_Box.Clone();
        HTuple hv_Color_COPY_INP_TMP  = hv_Color.Clone();
        HTuple hv_Column_COPY_INP_TMP = hv_Column.Clone();
        HTuple hv_Row_COPY_INP_TMP    = hv_Row.Clone();
        HTuple hv_String_COPY_INP_TMP = hv_String.Clone();

        // Initialize local and output iconic variables
        //This procedure displays text in a graphics window.
        //
        //Input parameters:
        //WindowHandle: The WindowHandle of the graphics window, where
        //   the message should be displayed
        //String: A tuple of strings containing the text message to be displayed
        //CoordSystem: If set to 'window', the text position is given
        //   with respect to the window coordinate system.
        //   If set to 'image', image coordinates are used.
        //   (This may be useful in zoomed images.)
        //Row: The row coordinate of the desired text position
        //   If set to -1, a default value of 12 is used.
        //   A tuple of values is allowed to display text at different
        //   positions.
        //Column: The column coordinate of the desired text position
        //   If set to -1, a default value of 12 is used.
        //   A tuple of values is allowed to display text at different
        //   positions.
        //Color: defines the color of the text as string.
        //   If set to [], '' or 'auto' the currently set color is used.
        //   If a tuple of strings is passed, the colors are used cyclically...
        //   - if |Row| == |Column| == 1: for each new textline
        //   = else for each text position.
        //Box: If Box[0] is set to 'true', the text is written within an orange box.
        //     If set to' false', no box is displayed.
        //     If set to a color string (e.g. 'white', '#FF00CC', etc.),
        //       the text is written in a box of that color.
        //     An optional second value for Box (Box[1]) controls if a shadow is displayed:
        //       'true' -> display a shadow in a default color
        //       'false' -> display no shadow
        //       otherwise -> use given string as color string for the shadow color
        //
        //It is possible to display multiple text strings in a single call.
        //In this case, some restrictions apply:
        //- Multiple text positions can be defined by specifying a tuple
        //  with multiple Row and/or Column coordinates, i.e.:
        //  - |Row| == n, |Column| == n
        //  - |Row| == n, |Column| == 1
        //  - |Row| == 1, |Column| == n
        //- If |Row| == |Column| == 1,
        //  each element of String is display in a new textline.
        //- If multiple positions or specified, the number of Strings
        //  must match the number of positions, i.e.:
        //  - Either |String| == n (each string is displayed at the
        //                          corresponding position),
        //  - or     |String| == 1 (The string is displayed n times).
        //
        if ((int)(new HTuple(hv_Color_COPY_INP_TMP.TupleEqual(new HTuple()))) != 0)
        {
            hv_Color_COPY_INP_TMP = "";
        }
        if ((int)(new HTuple(hv_Box_COPY_INP_TMP.TupleEqual(new HTuple()))) != 0)
        {
            hv_Box_COPY_INP_TMP = "false";
        }
        //
        //
        //Check conditions
        //
        hv_M = (new HTuple(hv_Row_COPY_INP_TMP.TupleLength())) * (new HTuple(hv_Column_COPY_INP_TMP.TupleLength()
                                                                             ));
        hv_N = new HTuple(hv_Row_COPY_INP_TMP.TupleLength());
        if ((int)((new HTuple(hv_M.TupleEqual(0))).TupleOr(new HTuple(hv_String_COPY_INP_TMP.TupleEqual(
                                                                          new HTuple())))) != 0)
        {
            return;
        }
        if ((int)(new HTuple(hv_M.TupleNotEqual(1))) != 0)
        {
            //Multiple positions
            //
            //Expand single parameters
            if ((int)(new HTuple((new HTuple(hv_Row_COPY_INP_TMP.TupleLength())).TupleEqual(
                                     1))) != 0)
            {
                hv_N = new HTuple(hv_Column_COPY_INP_TMP.TupleLength());
                HOperatorSet.TupleGenConst(hv_N, hv_Row_COPY_INP_TMP, out hv_Row_COPY_INP_TMP);
            }
            else if ((int)(new HTuple((new HTuple(hv_Column_COPY_INP_TMP.TupleLength()
                                                  )).TupleEqual(1))) != 0)
            {
                HOperatorSet.TupleGenConst(hv_N, hv_Column_COPY_INP_TMP, out hv_Column_COPY_INP_TMP);
            }
            else if ((int)(new HTuple((new HTuple(hv_Column_COPY_INP_TMP.TupleLength()
                                                  )).TupleNotEqual(new HTuple(hv_Row_COPY_INP_TMP.TupleLength())))) != 0)
            {
                throw new HalconException("Number of elements in Row and Column does not match.");
            }
            if ((int)(new HTuple((new HTuple(hv_String_COPY_INP_TMP.TupleLength())).TupleEqual(
                                     1))) != 0)
            {
                HOperatorSet.TupleGenConst(hv_N, hv_String_COPY_INP_TMP, out hv_String_COPY_INP_TMP);
            }
            else if ((int)(new HTuple((new HTuple(hv_String_COPY_INP_TMP.TupleLength()
                                                  )).TupleNotEqual(hv_N))) != 0)
            {
                throw new HalconException("Number of elements in Strings does not match number of positions.");
            }
            //
        }
        //
        //Prepare window
        HOperatorSet.GetRgb(hv_WindowHandle, out hv_Red, out hv_Green, out hv_Blue);
        HOperatorSet.GetPart(hv_WindowHandle, out hv_RowI1Part, out hv_ColumnI1Part,
                             out hv_RowI2Part, out hv_ColumnI2Part);
        HOperatorSet.GetWindowExtents(hv_WindowHandle, out hv_RowIWin, out hv_ColumnIWin,
                                      out hv_WidthWin, out hv_HeightWin);
        HOperatorSet.SetPart(hv_WindowHandle, 0, 0, hv_HeightWin - 1, hv_WidthWin - 1);
        //
        //Loop over all positions
        HTuple end_val89  = hv_N - 1;
        HTuple step_val89 = 1;

        for (hv_I = 0; hv_I.Continue(end_val89, step_val89); hv_I = hv_I.TupleAdd(step_val89))
        {
            hv_RowI    = hv_Row_COPY_INP_TMP.TupleSelect(hv_I);
            hv_ColumnI = hv_Column_COPY_INP_TMP.TupleSelect(hv_I);
            //Allow multiple strings for a single position.
            if ((int)(new HTuple(hv_N.TupleEqual(1))) != 0)
            {
                hv_StringI = hv_String_COPY_INP_TMP.Clone();
            }
            else
            {
                //In case of multiple positions, only single strings
                //are allowed per position.
                //For line breaks, use \n in this case.
                hv_StringI = hv_String_COPY_INP_TMP.TupleSelect(hv_I);
            }
            //Default settings
            //-1 is mapped to 12.
            if ((int)(new HTuple(hv_RowI.TupleEqual(-1))) != 0)
            {
                hv_RowI = 12;
            }
            if ((int)(new HTuple(hv_ColumnI.TupleEqual(-1))) != 0)
            {
                hv_ColumnI = 12;
            }
            //
            //Split string into one string per line.
            hv_StringI = ((("" + hv_StringI) + "")).TupleSplit("\n");
            //
            //Estimate extentions of text depending on font size.
            HOperatorSet.GetFontExtents(hv_WindowHandle, out hv_MaxAscent, out hv_MaxDescent,
                                        out hv_MaxWidth, out hv_MaxHeight);
            if ((int)(new HTuple(hv_CoordSystem.TupleEqual("window"))) != 0)
            {
                hv_R1 = hv_RowI.Clone();
                hv_C1 = hv_ColumnI.Clone();
            }
            else
            {
                //Transform image to window coordinates.
                hv_FactorRowI    = (1.0 * hv_HeightWin) / ((hv_RowI2Part - hv_RowI1Part) + 1);
                hv_FactorColumnI = (1.0 * hv_WidthWin) / ((hv_ColumnI2Part - hv_ColumnI1Part) + 1);
                hv_R1            = (((hv_RowI - hv_RowI1Part) + 0.5) * hv_FactorRowI) - 0.5;
                hv_C1            = (((hv_ColumnI - hv_ColumnI1Part) + 0.5) * hv_FactorColumnI) - 0.5;
            }
            //
            //Display text box depending on text size.
            hv_UseShadow   = 1;
            hv_ShadowColor = "gray";
            if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(0))).TupleEqual("true"))) != 0)
            {
                if (hv_Box_COPY_INP_TMP == null)
                {
                    hv_Box_COPY_INP_TMP = new HTuple();
                }
                hv_Box_COPY_INP_TMP[0] = "#fce9d4";
                hv_ShadowColor         = "#f28d26";
            }
            if ((int)(new HTuple((new HTuple(hv_Box_COPY_INP_TMP.TupleLength())).TupleGreater(
                                     1))) != 0)
            {
                if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(1))).TupleEqual("true"))) != 0)
                {
                    //Use default ShadowColor set above
                }
                else if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(1))).TupleEqual(
                                              "false"))) != 0)
                {
                    hv_UseShadow = 0;
                }
                else
                {
                    hv_ShadowColor = hv_Box_COPY_INP_TMP.TupleSelect(1);
                    //Valid color?
                    try
                    {
                        HOperatorSet.SetColor(hv_WindowHandle, hv_Box_COPY_INP_TMP.TupleSelect(
                                                  1));
                    }
                    // catch (Exception)
                    catch (HalconException HDevExpDefaultException1)
                    {
                        HDevExpDefaultException1.ToHTuple(out hv_Exception);
                        hv_Exception = new HTuple("Wrong value of control parameter Box[1] (must be a 'true', 'false', or a valid color string)");
                        throw new HalconException(hv_Exception);
                    }
                }
            }
            if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(0))).TupleNotEqual("false"))) != 0)
            {
                //Valid color?
                try
                {
                    HOperatorSet.SetColor(hv_WindowHandle, hv_Box_COPY_INP_TMP.TupleSelect(
                                              0));
                }
                // catch (Exception)
                catch (HalconException HDevExpDefaultException1)
                {
                    HDevExpDefaultException1.ToHTuple(out hv_Exception);
                    hv_Exception = new HTuple("Wrong value of control parameter Box[0] (must be a 'true', 'false', or a valid color string)");
                    throw new HalconException(hv_Exception);
                }
                //Calculate box extents
                hv_StringI = (" " + hv_StringI) + " ";
                hv_Width   = new HTuple();
                for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_StringI.TupleLength()
                                                                      )) - 1); hv_Index = (int)hv_Index + 1)
                {
                    HOperatorSet.GetStringExtents(hv_WindowHandle, hv_StringI.TupleSelect(hv_Index),
                                                  out hv_Ascent, out hv_Descent, out hv_W, out hv_H);
                    hv_Width = hv_Width.TupleConcat(hv_W);
                }
                hv_FrameHeight = hv_MaxHeight * (new HTuple(hv_StringI.TupleLength()));
                hv_FrameWidth  = (((new HTuple(0)).TupleConcat(hv_Width))).TupleMax();
                hv_R2          = hv_R1 + hv_FrameHeight;
                hv_C2          = hv_C1 + hv_FrameWidth;
                //Display rectangles
                HOperatorSet.GetDraw(hv_WindowHandle, out hv_DrawMode);
                HOperatorSet.SetDraw(hv_WindowHandle, "fill");
                //Set shadow color
                HOperatorSet.SetColor(hv_WindowHandle, hv_ShadowColor);
                if ((int)(hv_UseShadow) != 0)
                {
                    HOperatorSet.DispRectangle1(hv_WindowHandle, hv_R1 + 1, hv_C1 + 1, hv_R2 + 1,
                                                hv_C2 + 1);
                }
                //Set box color
                HOperatorSet.SetColor(hv_WindowHandle, hv_Box_COPY_INP_TMP.TupleSelect(0));
                HOperatorSet.DispRectangle1(hv_WindowHandle, hv_R1, hv_C1, hv_R2, hv_C2);
                HOperatorSet.SetDraw(hv_WindowHandle, hv_DrawMode);
            }
            //Write text.
            for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_StringI.TupleLength())) - 1); hv_Index = (int)hv_Index + 1)
            {
                //Set color
                if ((int)(new HTuple(hv_N.TupleEqual(1))) != 0)
                {
                    //Wiht a single text position, each text line
                    //may get a different color.
                    hv_CurrentColor = hv_Color_COPY_INP_TMP.TupleSelect(hv_Index % (new HTuple(hv_Color_COPY_INP_TMP.TupleLength()
                                                                                               )));
                }
                else
                {
                    //With multiple text positions, each position
                    //gets a single color for all text lines.
                    hv_CurrentColor = hv_Color_COPY_INP_TMP.TupleSelect(hv_I % (new HTuple(hv_Color_COPY_INP_TMP.TupleLength()
                                                                                           )));
                }
                if ((int)((new HTuple(hv_CurrentColor.TupleNotEqual(""))).TupleAnd(new HTuple(hv_CurrentColor.TupleNotEqual(
                                                                                                  "auto")))) != 0)
                {
                    //Valid color?
                    try
                    {
                        HOperatorSet.SetColor(hv_WindowHandle, hv_CurrentColor);
                    }
                    // catch (Exception)
                    catch (HalconException HDevExpDefaultException1)
                    {
                        HDevExpDefaultException1.ToHTuple(out hv_Exception);
                        hv_Exception = ((("Wrong value of control parameter Color[" + (hv_Index % (new HTuple(hv_Color_COPY_INP_TMP.TupleLength()
                                                                                                              )))) + "] == '") + hv_CurrentColor) + "' (must be a valid color string)";
                        throw new HalconException(hv_Exception);
                    }
                }
                else
                {
                    HOperatorSet.SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue);
                }
                //Finally display text
                hv_RowI = hv_R1 + (hv_MaxHeight * hv_Index);
                HOperatorSet.SetTposition(hv_WindowHandle, hv_RowI, hv_C1);
                HOperatorSet.WriteString(hv_WindowHandle, hv_StringI.TupleSelect(hv_Index));
            }
        }
        //Reset changed window settings
        HOperatorSet.SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue);
        HOperatorSet.SetPart(hv_WindowHandle, hv_RowI1Part, hv_ColumnI1Part, hv_RowI2Part,
                             hv_ColumnI2Part);

        return;
    }
예제 #9
0
        private void action()
        {
            HObject ho_Rectangle, ho_ImageReduced;

            HTuple hv_Number = null;

            HObject ho_ImageCED = null, ho_Edges = null;
            HObject ho_ContoursSplit1 = null, ho_EmptyObject = null, ho_ObjectSelected = null;
            HObject ho_Region = null;
            HTuple  hv_i = new HTuple();
            HTuple  hv_Attrib = new HTuple(), hv_Row1 = new HTuple(), hv_Number1 = new HTuple();
            HTuple  hv_Column = new HTuple(), hv_Radius = new HTuple();
            HTuple  hv_StartPhi = new HTuple(), hv_EndPhi = new HTuple();
            HTuple  hv_PointOrder = new HTuple(), hv_Row = new HTuple();
            HTuple  hv_Col = new HTuple(), RR = new HTuple(), CC = new HTuple();

            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_ImageCED);
            HOperatorSet.GenEmptyObj(out ho_Edges);
            HOperatorSet.GenEmptyObj(out ho_ContoursSplit1);
            HOperatorSet.GenEmptyObj(out ho_EmptyObject);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.Union1(algorithm.Region, out RegionToDisp);
            try
            {
                ho_Rectangle.Dispose();
                HOperatorSet.GenRectangle2(out ho_Rectangle, hv_centerRowm, hv_centerColumnm, hv_Phim, hv_Length1m,
                                           hv_Length2m);
                ho_ImageReduced.Dispose();
                HOperatorSet.ReduceDomain(Image, ho_Rectangle, out ho_ImageReduced);
                ho_ImageCED.Dispose();
                HOperatorSet.CoherenceEnhancingDiff(ho_ImageReduced, out ho_ImageCED, 0.1,
                                                    3, 0.5, 2);

                ho_Edges.Dispose();
                HOperatorSet.EdgesSubPix(ho_ImageCED, out ho_Edges, "canny", 1, 5, 20);
                ho_ContoursSplit1.Dispose();
                HOperatorSet.SegmentContoursXld(ho_Edges, out ho_ContoursSplit1, "lines_circles",
                                                7, 7, 2);
                HOperatorSet.CountObj(ho_ContoursSplit1, out hv_Number1);
                hv_Number = 0;
                ho_EmptyObject.Dispose();
                HOperatorSet.GenEmptyObj(out ho_EmptyObject);
                HTuple end_val13  = hv_Number1;
                HTuple step_val13 = 1;
                for (hv_i = 1; hv_i.Continue(end_val13, step_val13); hv_i = hv_i.TupleAdd(step_val13))
                {
                    ho_ObjectSelected.Dispose();
                    HOperatorSet.SelectObj(ho_ContoursSplit1, out ho_ObjectSelected, hv_i);
                    HOperatorSet.GetContourGlobalAttribXld(ho_ObjectSelected, "cont_approx",
                                                           out hv_Attrib);
                    if ((int)(new HTuple(hv_Attrib.TupleGreater(-1))) != 0)
                    {
                        HOperatorSet.FitCircleContourXld(ho_ObjectSelected, "algebraic", -1, 0,
                                                         0, 3, 2, out hv_Row1, out hv_Column, out hv_Radius, out hv_StartPhi,
                                                         out hv_EndPhi, out hv_PointOrder);
                        if ((int)((new HTuple(hv_Column.TupleLess(hv_centerRowm - hv_Length1m / 10))).TupleAnd(new HTuple(hv_Radius.TupleGreater(
                                                                                                                              hv_Length1m * 0.3)))) != 0)
                        {
                            HOperatorSet.GetContourXld(ho_ObjectSelected, out RR, out CC);
                            hv_Number = hv_Number + RR.TupleLength();
                            HOperatorSet.GetContourXld(ho_ObjectSelected, out hv_Row, out hv_Col);
                            ho_Region.Dispose();
                            HOperatorSet.GenRegionPoints(out ho_Region, hv_Row, hv_Col);
                            {
                                HObject ExpTmpOutVar_0;
                                HOperatorSet.ConcatObj(ho_EmptyObject, ho_Region, out ExpTmpOutVar_0);
                                ho_EmptyObject.Dispose();
                                ho_EmptyObject = ExpTmpOutVar_0;
                            }
                        }
                    }
                }
                HOperatorSet.Union1(ho_EmptyObject, out RegionToDisp);
                HTuple hv_result = GetHv_result();
                hv_result = hv_result.TupleConcat("螺纹数量");
                hv_result = hv_result.TupleConcat(hv_Number.I);
                result    = hv_result.Clone();
            }
            catch
            {
                HTuple hv_result = GetHv_result();
                hv_result = hv_result.TupleConcat("螺纹数量");
                hv_result = hv_result.TupleConcat(0);
                result    = hv_result.Clone();
            }
            finally
            {
                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
                ho_ImageCED.Dispose();
                ho_Edges.Dispose();
                ho_ContoursSplit1.Dispose();
                ho_EmptyObject.Dispose();
                ho_ObjectSelected.Dispose();
                ho_Region.Dispose();
                algorithm.Region.Dispose();
            }
        }
예제 #10
0
        public void scale_image_range(HObject ho_Image, out HObject ho_ImageScaled, HTuple hv_Min,
                                      HTuple hv_Max)
        {
            // Stack for temporary objects
            HObject[] OTemp = new HObject[20];

            // Local iconic variables

            HObject ho_SelectedChannel = null, ho_LowerRegion = null;
            HObject ho_UpperRegion = null;

            // Local copy input parameter variables
            HObject ho_Image_COPY_INP_TMP;

            ho_Image_COPY_INP_TMP = ho_Image.CopyObj(1, -1);


            HTuple hv_LowerLimit = new HTuple(), hv_UpperLimit = new HTuple();
            HTuple hv_Mult = null, hv_Add = null, hv_Channels = null;
            HTuple hv_Index = null, hv_MinGray = new HTuple(), hv_MaxGray = new HTuple();
            HTuple hv_Range            = new HTuple();
            HTuple hv_Max_COPY_INP_TMP = hv_Max.Clone();
            HTuple hv_Min_COPY_INP_TMP = hv_Min.Clone();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_ImageScaled);
            HOperatorSet.GenEmptyObj(out ho_SelectedChannel);
            HOperatorSet.GenEmptyObj(out ho_LowerRegion);
            HOperatorSet.GenEmptyObj(out ho_UpperRegion);

            //
            if ((int)(new HTuple((new HTuple(hv_Min_COPY_INP_TMP.TupleLength())).TupleEqual(
                                     2))) != 0)
            {
                hv_LowerLimit       = hv_Min_COPY_INP_TMP[1];
                hv_Min_COPY_INP_TMP = hv_Min_COPY_INP_TMP[0];
            }
            else
            {
                hv_LowerLimit = 0.0;
            }
            if ((int)(new HTuple((new HTuple(hv_Max_COPY_INP_TMP.TupleLength())).TupleEqual(
                                     2))) != 0)
            {
                hv_UpperLimit       = hv_Max_COPY_INP_TMP[1];
                hv_Max_COPY_INP_TMP = hv_Max_COPY_INP_TMP[0];
            }
            else
            {
                hv_UpperLimit = 255.0;
            }
            //
            //Calculate scaling parameters
            hv_Mult = (((hv_UpperLimit - hv_LowerLimit)).TupleReal()) / (hv_Max_COPY_INP_TMP - hv_Min_COPY_INP_TMP);
            hv_Add  = ((-hv_Mult) * hv_Min_COPY_INP_TMP) + hv_LowerLimit;
            //
            //Scale image
            {
                HObject ExpTmpOutVar_0;
                HOperatorSet.ScaleImage(ho_Image_COPY_INP_TMP, out ExpTmpOutVar_0, hv_Mult, hv_Add);
                ho_Image_COPY_INP_TMP.Dispose();
                ho_Image_COPY_INP_TMP = ExpTmpOutVar_0;
            }
            //
            //Clip gray values if necessary
            //This must be done for each channel separately
            HOperatorSet.CountChannels(ho_Image_COPY_INP_TMP, out hv_Channels);
            HTuple end_val48  = hv_Channels;
            HTuple step_val48 = 1;

            for (hv_Index = 1; hv_Index.Continue(end_val48, step_val48); hv_Index = hv_Index.TupleAdd(step_val48))
            {
                ho_SelectedChannel.Dispose();
                HOperatorSet.AccessChannel(ho_Image_COPY_INP_TMP, out ho_SelectedChannel, hv_Index);
                HOperatorSet.MinMaxGray(ho_SelectedChannel, ho_SelectedChannel, 0, out hv_MinGray,
                                        out hv_MaxGray, out hv_Range);
                ho_LowerRegion.Dispose();
                HOperatorSet.Threshold(ho_SelectedChannel, out ho_LowerRegion, ((hv_MinGray.TupleConcat(
                                                                                     hv_LowerLimit))).TupleMin(), hv_LowerLimit);
                ho_UpperRegion.Dispose();
                HOperatorSet.Threshold(ho_SelectedChannel, out ho_UpperRegion, hv_UpperLimit,
                                       ((hv_UpperLimit.TupleConcat(hv_MaxGray))).TupleMax());
                {
                    HObject ExpTmpOutVar_0;
                    HOperatorSet.PaintRegion(ho_LowerRegion, ho_SelectedChannel, out ExpTmpOutVar_0,
                                             hv_LowerLimit, "fill");
                    ho_SelectedChannel.Dispose();
                    ho_SelectedChannel = ExpTmpOutVar_0;
                }
                {
                    HObject ExpTmpOutVar_0;
                    HOperatorSet.PaintRegion(ho_UpperRegion, ho_SelectedChannel, out ExpTmpOutVar_0,
                                             hv_UpperLimit, "fill");
                    ho_SelectedChannel.Dispose();
                    ho_SelectedChannel = ExpTmpOutVar_0;
                }
                if ((int)(new HTuple(hv_Index.TupleEqual(1))) != 0)
                {
                    ho_ImageScaled.Dispose();
                    HOperatorSet.CopyObj(ho_SelectedChannel, out ho_ImageScaled, 1, 1);
                }
                else
                {
                    {
                        HObject ExpTmpOutVar_0;
                        HOperatorSet.AppendChannel(ho_ImageScaled, ho_SelectedChannel, out ExpTmpOutVar_0
                                                   );
                        ho_ImageScaled.Dispose();
                        ho_ImageScaled = ExpTmpOutVar_0;
                    }
                }
            }
            ho_Image_COPY_INP_TMP.Dispose();
            ho_SelectedChannel.Dispose();
            ho_LowerRegion.Dispose();
            ho_UpperRegion.Dispose();

            return;
        }
예제 #11
0
    // Procedures
#if !NO_EXPORT_MAIN
    // Main procedure
    private void action()
    {
        // Local iconic variables

        HObject ho_Image, ho_ImageRotate, ho_ROI_0;
        HObject ho_ImageReduced, ho_Regions, ho_RegionFillUp, ho_ConnectedRegions;
        HObject ho_SelectedRegions, ho_BinImage, ho_Characters;
        HObject ho_Image2, ho_ImageRotate2, ho_ROI_02, ho_ImageReduced2;
        HObject ho_Regions2, ho_RegionFillUp2, ho_ConnectedRegions2;
        HObject ho_SelectedRegions2, ho_BinImage2, ho_Characters2;

        // Local control variables

        HTuple hv_AcqHandle = null, hv_Width = null;
        HTuple hv_Height = null, hv_TextModel = null, hv_TextResultID = null;
        HTuple hv_ResultValue = null, hv_Width2 = null, hv_Height2 = null;
        HTuple hv_TextModel2 = null, hv_TextResultID2 = null, hv_ResultValue2 = null;
        HTuple hv_license = null, hv_container = null, hv_licLength = null;
        HTuple hv_i = null, hv_conLength = null;

        // Initialize local and output iconic variables
        HOperatorSet.GenEmptyObj(out ho_Image);
        HOperatorSet.GenEmptyObj(out ho_ImageRotate);
        HOperatorSet.GenEmptyObj(out ho_ROI_0);
        HOperatorSet.GenEmptyObj(out ho_ImageReduced);
        HOperatorSet.GenEmptyObj(out ho_Regions);
        HOperatorSet.GenEmptyObj(out ho_RegionFillUp);
        HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
        HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
        HOperatorSet.GenEmptyObj(out ho_BinImage);
        HOperatorSet.GenEmptyObj(out ho_Characters);
        HOperatorSet.GenEmptyObj(out ho_Image2);
        HOperatorSet.GenEmptyObj(out ho_ImageRotate2);
        HOperatorSet.GenEmptyObj(out ho_ROI_02);
        HOperatorSet.GenEmptyObj(out ho_ImageReduced2);
        HOperatorSet.GenEmptyObj(out ho_Regions2);
        HOperatorSet.GenEmptyObj(out ho_RegionFillUp2);
        HOperatorSet.GenEmptyObj(out ho_ConnectedRegions2);
        HOperatorSet.GenEmptyObj(out ho_SelectedRegions2);
        HOperatorSet.GenEmptyObj(out ho_BinImage2);
        HOperatorSet.GenEmptyObj(out ho_Characters2);
        //open_framegrabber ('GenICamTL', 0, 0, 0, 0, 0, 0, 'progressive', -1, 'default', -1, 'false', 'default', '0x305316d5c4', 0, -1, AcqHandle)
        //Image Acquisition 01: Code generated by Image Acquisition 01
        //Image Acquisition 01: Code generated by Image Acquisition 01
        HOperatorSet.OpenFramegrabber("GigEVision2", 0, 0, 0, 0, 0, 0, "progressive",
                                      -1, "default", -1, "false", "default", "licenseCam", 0, -1, out hv_AcqHandle);
        //open_framegrabber ('GigEVision', 0, 0, 0, 0, 0, 0, 'default', -1, 'default', -1, 'false', 'default', 'licenseCam', 0, -1, AcqHandle)
        HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "AcquisitionMode", "SingleFrame");

        //set_framegrabber_param (AcqHandle, 'ExposureTimeAbs', 55555.0)
        HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
        //

        ho_Image.Dispose();
        HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
        HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
        ho_ImageRotate.Dispose();
        HOperatorSet.RotateImage(ho_Image, out ho_ImageRotate, 0, "constant");
        ho_Image.Dispose();
        HOperatorSet.AccessChannel(ho_ImageRotate, out ho_Image, 1);
        //OLD
        //gen_rectangle1 (ROI_0, 431.688, 468.25, 607.938, 1265.75)
        //gen_rectangle1 (ROI_0, 442.63, 403.582, 632.248, 1275.42)
        ho_ROI_0.Dispose();
        HOperatorSet.GenRectangle1(out ho_ROI_0, 490.034, 428.071, 681.714, 1339.09);


        //Remove large blobs
        //gray_closing_rect (Image, ImageClosing, 10, 10)
        //sub_image (Image, ImageClosing, ImageSub, 3, 0)
        //
        //Preprocessing to enhance contrast
        ho_ImageReduced.Dispose();
        HOperatorSet.ReduceDomain(ho_Image, ho_ROI_0, out ho_ImageReduced);
        ho_Regions.Dispose();
        HOperatorSet.Threshold(ho_ImageReduced, out ho_Regions, 0, 80);
        ho_RegionFillUp.Dispose();
        HOperatorSet.FillUpShape(ho_Regions, out ho_RegionFillUp, "area", 0, 40);
        ho_ConnectedRegions.Dispose();
        HOperatorSet.Connection(ho_RegionFillUp, out ho_ConnectedRegions);
        ho_SelectedRegions.Dispose();
        HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area",
                                 "and", 150, 99999);
        ho_BinImage.Dispose();
        HOperatorSet.RegionToBin(ho_SelectedRegions, out ho_BinImage, 0, 255, hv_Width,
                                 hv_Height);
        //
        //Perform OCR using the automatic text model reader
        HOperatorSet.CreateTextModelReader("auto", "Industrial_0-9+_Rej.omc", out hv_TextModel);
        //set_text_model_param (TextModel, 'max_char_height', 200)
        //set_text_model_param (TextModel, 'min_char_height', 20)
        //set_text_model_param (TextModel, 'min_char_width', 5)
        //set_text_model_param (TextModel, 'max_char_width', 175)
        //set_text_model_param (TextModel, 'return_separators', 'false')
        //set_text_model_param (TextModel, 'min_contrast', 30)
        HOperatorSet.FindText(ho_BinImage, hv_TextModel, out hv_TextResultID);
        //
        //Get the results from the OCR
        ho_Characters.Dispose();
        HOperatorSet.GetTextObject(out ho_Characters, hv_TextResultID, "all_lines");
        HOperatorSet.GetTextResult(hv_TextResultID, "class", out hv_ResultValue);
        //
        if (HDevWindowStack.IsOpen())
        {
            HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
        }
        if (HDevWindowStack.IsOpen())
        {
            HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green");
        }
        if (HDevWindowStack.IsOpen())
        {
            HOperatorSet.DispObj(ho_Characters, HDevWindowStack.GetActive());
        }
        //do memory cleanup here
        if (HDevWindowStack.IsOpen())
        {
            HOperatorSet.ClearWindow(HDevWindowStack.GetActive());
        }
        dev_update_on();

        HOperatorSet.CloseFramegrabber(hv_AcqHandle);



        //********************************************second camera

        //Image Acquisition 02: Code generated by Image Acquisition 02
        //open_framegrabber ('GigEVision', 0, 0, 0, 0, 0, 0, 'default', -1, 'default', -1, 'false', 'default', 'containerCam', 0, -1, AcqHandle)
        //Image Acquisition 02: Code generated by Image Acquisition 02
        HOperatorSet.OpenFramegrabber("GigEVision2", 0, 0, 0, 0, 0, 0, "progressive",
                                      -1, "default", -1, "false", "default", "containerCam", 0, -1, out hv_AcqHandle);

        //open_framegrabber ('GenICamTL', 0, 0, 0, 0, 0, 0, 'progressive', -1, 'default', -1, 'false', 'default', '0x3053228741', 0, -1, AcqHandle)
        //set_framegrabber_param (AcqHandle, 'ExposureTimeAbs', 55555.0)
        HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
        //
        ho_Image2.Dispose();
        HOperatorSet.GrabImageAsync(out ho_Image2, hv_AcqHandle, -1);
        HOperatorSet.GetImageSize(ho_Image2, out hv_Width2, out hv_Height2);
        ho_ImageRotate2.Dispose();
        HOperatorSet.RotateImage(ho_Image2, out ho_ImageRotate2, 180, "constant");
        ho_Image2.Dispose();
        HOperatorSet.AccessChannel(ho_ImageRotate2, out ho_Image2, 1);
        //OLD
        //gen_rectangle1 (ROI_02, 330.438, 690.75, 459.813, 1548.25)
        //gen_rectangle1 (ROI_02, 30.416, 658.276, 199.424, 1564.4)
        ho_ROI_02.Dispose();
        HOperatorSet.GenRectangle1(out ho_ROI_02, -4.62214, 670.52, 114.92, 1527.66);

        //Remove large blobs
        //gray_closing_rect (Image2, ImageClosing2, 10, 10)
        //sub_image (Image2, ImageClosing2, ImageSub2, 3, 180)
        //
        //Preprocessing to enhance contrast
        ho_ImageReduced2.Dispose();
        HOperatorSet.ReduceDomain(ho_Image2, ho_ROI_02, out ho_ImageReduced2);
        ho_Regions2.Dispose();
        HOperatorSet.Threshold(ho_ImageReduced2, out ho_Regions2, 0, 120);
        ho_RegionFillUp2.Dispose();
        HOperatorSet.FillUpShape(ho_Regions2, out ho_RegionFillUp2, "area", 0, 40);
        ho_ConnectedRegions2.Dispose();
        HOperatorSet.Connection(ho_RegionFillUp2, out ho_ConnectedRegions2);
        ho_SelectedRegions2.Dispose();
        HOperatorSet.SelectShape(ho_ConnectedRegions2, out ho_SelectedRegions2, "area",
                                 "and", 150, 99999);
        ho_BinImage2.Dispose();
        HOperatorSet.RegionToBin(ho_SelectedRegions2, out ho_BinImage2, 255, 0, hv_Width2,
                                 hv_Height2);
        //
        //Perform OCR using the automatic text model reader
        HOperatorSet.CreateTextModelReader("auto", "Industrial_0-9A-Z_Rej.omc", out hv_TextModel2);
        //set_text_model_param (TextModel2, 'max_char_height', 200)
        //set_text_model_param (TextModel2, 'min_char_height', 1)
        //set_text_model_param (TextModel2, 'min_char_width', 1)
        //set_text_model_param (TextModel2, 'max_char_width', 175)
        //set_text_model_param (TextModel2, 'return_separators', 'false')
        //set_text_model_param (TextModel2, 'min_contrast', 30)
        HOperatorSet.FindText(ho_BinImage2, hv_TextModel2, out hv_TextResultID2);
        //
        //Get the results from the OCR
        ho_Characters2.Dispose();
        HOperatorSet.GetTextObject(out ho_Characters2, hv_TextResultID2, "all_lines");
        HOperatorSet.GetTextResult(hv_TextResultID2, "class", out hv_ResultValue2);
        //
        if (HDevWindowStack.IsOpen())
        {
            HOperatorSet.DispObj(ho_Image2, HDevWindowStack.GetActive());
        }
        if (HDevWindowStack.IsOpen())
        {
            HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green");
        }
        if (HDevWindowStack.IsOpen())
        {
            HOperatorSet.DispObj(ho_Characters2, HDevWindowStack.GetActive());
        }
        //do memory cleanup here
        if (HDevWindowStack.IsOpen())
        {
            HOperatorSet.ClearWindow(HDevWindowStack.GetActive());
        }
        dev_update_on();

        HOperatorSet.CloseFramegrabber(hv_AcqHandle);
        hv_license   = "";
        hv_container = "";
        HOperatorSet.TupleStrlen(hv_ResultValue, out hv_licLength);
        HOperatorSet.TupleSum(hv_licLength, out hv_licLength);
        HTuple end_val117  = hv_licLength - 1;
        HTuple step_val117 = 1;

        for (hv_i = 0; hv_i.Continue(end_val117, step_val117); hv_i = hv_i.TupleAdd(step_val117))
        {
            hv_license = hv_license + (hv_ResultValue.TupleSelect(hv_i));
        }
        HOperatorSet.TupleStrlen(hv_ResultValue2, out hv_conLength);
        HOperatorSet.TupleSum(hv_conLength, out hv_conLength);
        HTuple end_val122  = hv_conLength - 1;
        HTuple step_val122 = 1;

        for (hv_i = 0; hv_i.Continue(end_val122, step_val122); hv_i = hv_i.TupleAdd(step_val122))
        {
            hv_container = hv_container + (hv_ResultValue2.TupleSelect(hv_i));
        }
        hv_license = hv_license.TupleSelect(0);


        ho_Image.Dispose();
        ho_ImageRotate.Dispose();
        ho_ROI_0.Dispose();
        ho_ImageReduced.Dispose();
        ho_Regions.Dispose();
        ho_RegionFillUp.Dispose();
        ho_ConnectedRegions.Dispose();
        ho_SelectedRegions.Dispose();
        ho_BinImage.Dispose();
        ho_Characters.Dispose();
        ho_Image2.Dispose();
        ho_ImageRotate2.Dispose();
        ho_ROI_02.Dispose();
        ho_ImageReduced2.Dispose();
        ho_Regions2.Dispose();
        ho_RegionFillUp2.Dispose();
        ho_ConnectedRegions2.Dispose();
        ho_SelectedRegions2.Dispose();
        ho_BinImage2.Dispose();
        ho_Characters2.Dispose();
    }
예제 #12
0
        public int myOcrSplit(HObject ho_inImage, HObject ho_inRegion, HTuple hv_ocrClassifier,
                              HTuple hv_standardText, HTuple hv_charCount, HTuple hv_returnPunctuation, HTuple hv_lightOnDark,
                              out HTuple hv_Word)
        {
            // Local iconic variables

            HObject ho_Timg1 = null, ho_ImageReduced, ho_Region = null;
            HObject ho_Characters = null;

            // Local control variables

            HTuple hv_step = null, hv_stepMax = null, hv_useEnhanced = null;
            HTuple hv_TextModel = null, hv_OCRHandle = null, hv_RegularExpression = null;
            HTuple hv_UsedThreshold = new HTuple(), hv_Area = new HTuple();
            HTuple hv_Row = new HTuple(), hv_Column = new HTuple();
            HTuple hv_Area1 = new HTuple(), hv_Row1 = new HTuple();
            HTuple hv_Column1 = new HTuple(), hv_findText = new HTuple();
            HTuple hv_bufferCount = new HTuple(), hv_bufferMincontrast = new HTuple();
            HTuple hv_Index = new HTuple(), hv_add = new HTuple();
            HTuple hv_TextResultID = new HTuple(), hv_Class = new HTuple();
            HTuple hv_Confidence = new HTuple(), hv_Score = new HTuple();
            HTuple hv_Number = new HTuple(), hv_ScoreSum = new HTuple();
            HTuple hv_Index1 = new HTuple(), hv_Length = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Timg1);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_Characters);
            hv_Word = new HTuple();

            ho_Timg1.Dispose();
            ho_Timg1 = ho_inImage.CopyObj(1, -1);

            ho_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_Timg1, ho_inRegion, out ho_ImageReduced);
            hv_step        = 3;
            hv_stepMax     = 25;
            hv_useEnhanced = 1;
            HOperatorSet.CreateTextModelReader("auto", hv_ocrClassifier, out hv_TextModel);
            HOperatorSet.ReadOcrClassCnn(hv_ocrClassifier, out hv_OCRHandle);
            HOperatorSet.SetTextModelParam(hv_TextModel, "separate_touching_chars", "enhanced");

            HOperatorSet.SetTextModelParam(hv_TextModel, "return_separators", "true");
            HOperatorSet.SetTextModelParam(hv_TextModel, "add_fragments", "true");
            HOperatorSet.SetTextModelParam(hv_TextModel, "return_punctuation", hv_returnPunctuation == 0 ? "false" : "true");
            //RegularExpression := ''

            //字符串转正则表达式
            myStringToRegular(hv_standardText, out hv_RegularExpression);
            hv_RegularExpression = ("(" + hv_RegularExpression) + ")";


            //判断是否需要反转颜色
            if ((int)(new HTuple(hv_lightOnDark.TupleEqual(1))) != 0)
            {
                HObject ExpTmpOutVar_0;
                HOperatorSet.InvertImage(ho_ImageReduced, out ExpTmpOutVar_0);
                ho_ImageReduced.Dispose();
                ho_ImageReduced = ExpTmpOutVar_0;
            }
            else if ((int)(new HTuple(hv_lightOnDark.TupleEqual(2))) != 0)
            {
                //自动判断是否反转颜色,金工之前的项目中并未使用该功能
                ho_Region.Dispose();
                HOperatorSet.BinaryThreshold(ho_ImageReduced, out ho_Region, "max_separability",
                                             "dark", out hv_UsedThreshold);
                HOperatorSet.AreaCenter(ho_ImageReduced, out hv_Area, out hv_Row, out hv_Column);
                HOperatorSet.AreaCenter(ho_Region, out hv_Area1, out hv_Row1, out hv_Column1);
                if ((int)(new HTuple(hv_Area1.TupleGreater(hv_Area / 2))) != 0)
                {
                    {
                        HObject ExpTmpOutVar_0;
                        HOperatorSet.InvertImage(ho_ImageReduced, out ExpTmpOutVar_0);
                        ho_ImageReduced.Dispose();
                        ho_ImageReduced = ExpTmpOutVar_0;
                    }
                }
            }
            //以置信度最高为分割目标
            if ((int)(new HTuple(hv_charCount.TupleEqual(0))) != 0)
            {
                hv_findText          = 0;
                hv_bufferCount       = 0;
                hv_bufferMincontrast = 5;
                HTuple end_val52  = hv_stepMax;
                HTuple step_val52 = 1;
                for (hv_Index = 1; hv_Index.Continue(end_val52, step_val52); hv_Index = hv_Index.TupleAdd(step_val52))
                {
                    hv_add = hv_step * hv_Index;
                    HOperatorSet.SetTextModelParam(hv_TextModel, "min_contrast", hv_add);
                    HOperatorSet.FindText(ho_ImageReduced, hv_TextModel, out hv_TextResultID);
                    ho_Characters.Dispose();
                    HOperatorSet.GetTextObject(out ho_Characters, hv_TextResultID, "all_lines");
                    HOperatorSet.DoOcrWordCnn(ho_Characters, ho_ImageReduced, hv_OCRHandle, hv_RegularExpression,
                                              3, 10, out hv_Class, out hv_Confidence, out hv_Word, out hv_Score);
                    HOperatorSet.CountObj(ho_Characters, out hv_Number);
                    HOperatorSet.TupleSum(hv_Confidence, out hv_ScoreSum);
                    HOperatorSet.TupleFindFirst(hv_Class, "\x1A", out hv_Index1);
                    if ((int)((new HTuple(hv_bufferCount.TupleLess(hv_ScoreSum))).TupleAnd(new HTuple(hv_Index1.TupleEqual(
                                                                                                          -1)))) != 0)
                    {
                        hv_findText          = 1;
                        hv_bufferCount       = hv_ScoreSum.Clone();
                        hv_bufferMincontrast = hv_add.Clone();
                    }
                }
                HOperatorSet.SetTextModelParam(hv_TextModel, "min_contrast", hv_bufferMincontrast);
                HOperatorSet.FindText(ho_ImageReduced, hv_TextModel, out hv_TextResultID);
                ho_Characters.Dispose();
                HOperatorSet.GetTextObject(out ho_Characters, hv_TextResultID, "all_lines");
                HOperatorSet.DoOcrWordCnn(ho_Characters, ho_ImageReduced, hv_OCRHandle, hv_RegularExpression,
                                          3, 10, out hv_Class, out hv_Confidence, out hv_Word, out hv_Score);
            }


            //以识别字符为固定长度为分割目标
            if ((int)(new HTuple(hv_charCount.TupleGreater(0))) != 0)
            {
                hv_findText          = 0;
                hv_bufferCount       = 0;
                hv_bufferMincontrast = 5;
                HTuple end_val86  = hv_stepMax;
                HTuple step_val86 = 1;
                for (hv_Index = 1; hv_Index.Continue(end_val86, step_val86); hv_Index = hv_Index.TupleAdd(step_val86))
                {
                    hv_add = hv_step * hv_Index;
                    HOperatorSet.SetTextModelParam(hv_TextModel, "min_contrast", hv_add);
                    HOperatorSet.FindText(ho_ImageReduced, hv_TextModel, out hv_TextResultID);
                    ho_Characters.Dispose();
                    HOperatorSet.GetTextObject(out ho_Characters, hv_TextResultID, "all_lines");

                    HOperatorSet.CountObj(ho_Characters, out hv_Number);

                    if ((int)(new HTuple(hv_charCount.TupleEqual(hv_Number))) != 0)
                    {
                        hv_findText = 1;
                        //
                        HOperatorSet.DoOcrWordCnn(ho_Characters, ho_ImageReduced, hv_OCRHandle,
                                                  hv_RegularExpression, 3, 10, out hv_Class, out hv_Confidence, out hv_Word,
                                                  out hv_Score);
                        HOperatorSet.TupleSum(hv_Confidence, out hv_ScoreSum);
                        HOperatorSet.TupleFindFirst(hv_Class, "\x1A", out hv_Index1);
                        if ((int)((new HTuple(hv_bufferCount.TupleLess(hv_ScoreSum))).TupleAnd(
                                      new HTuple(hv_Index1.TupleEqual(-1)))) != 0)
                        {
                            hv_findText          = 1;
                            hv_bufferCount       = hv_ScoreSum.Clone();
                            hv_bufferMincontrast = hv_add.Clone();
                        }
                    }
                }
                //以置信度最高为分割目标 再识别一次
                //if (findText == 0)
                HOperatorSet.SetTextModelParam(hv_TextModel, "min_contrast", hv_bufferMincontrast);
                HOperatorSet.FindText(ho_ImageReduced, hv_TextModel, out hv_TextResultID);
                ho_Characters.Dispose();
                HOperatorSet.GetTextObject(out ho_Characters, hv_TextResultID, "all_lines");
                HOperatorSet.DoOcrWordCnn(ho_Characters, ho_ImageReduced, hv_OCRHandle, hv_RegularExpression,
                                          3, 10, out hv_Class, out hv_Confidence, out hv_Word, out hv_Score);
                //endif
            }

            //使用标准文本对比
            if ((int)(new HTuple(hv_charCount.TupleLess(0))) != 0)
            {
                //判断是否和标准文本一致

                hv_findText          = 0;
                hv_bufferCount       = 0;
                hv_bufferMincontrast = 5;
                HTuple end_val130  = hv_stepMax;
                HTuple step_val130 = 1;
                for (hv_Index = 1; hv_Index.Continue(end_val130, step_val130); hv_Index = hv_Index.TupleAdd(step_val130))
                {
                    hv_add = hv_step * hv_Index;
                    HOperatorSet.SetTextModelParam(hv_TextModel, "min_contrast", hv_add);
                    HOperatorSet.FindText(ho_ImageReduced, hv_TextModel, out hv_TextResultID);
                    ho_Characters.Dispose();
                    HOperatorSet.GetTextObject(out ho_Characters, hv_TextResultID, "all_lines");
                    HOperatorSet.CountObj(ho_Characters, out hv_Number);
                    HOperatorSet.TupleStrlen(hv_standardText, out hv_Length);
                    if ((int)(new HTuple(hv_Length.TupleNotEqual(hv_Number))) != 0)
                    {
                        continue;
                    }
                    HOperatorSet.DoOcrWordCnn(ho_Characters, ho_ImageReduced, hv_OCRHandle, hv_RegularExpression,
                                              5, 10, out hv_Class, out hv_Confidence, out hv_Word, out hv_Score);

                    if ((int)(new HTuple(hv_Word.TupleEqual(hv_standardText))) != 0)
                    {
                        hv_findText = 1;
                        break;
                    }

                    HOperatorSet.TupleSum(hv_Confidence, out hv_ScoreSum);
                    HOperatorSet.TupleFindFirst(hv_Class, "\x1A", out hv_Index1);
                    if ((int)((new HTuple(hv_bufferCount.TupleLess(hv_ScoreSum))).TupleAnd(new HTuple(hv_Index1.TupleEqual(
                                                                                                          -1)))) != 0)
                    {
                        hv_bufferCount       = hv_ScoreSum.Clone();
                        hv_bufferMincontrast = hv_add.Clone();
                    }
                }
                //以置信度最高为分割目标 再识别一次
                if ((int)(new HTuple(hv_findText.TupleEqual(0))) != 0)
                {
                    HOperatorSet.SetTextModelParam(hv_TextModel, "min_contrast", hv_bufferMincontrast);
                    HOperatorSet.FindText(ho_ImageReduced, hv_TextModel, out hv_TextResultID);
                    ho_Characters.Dispose();
                    HOperatorSet.GetTextObject(out ho_Characters, hv_TextResultID, "all_lines");
                    HOperatorSet.DoOcrWordCnn(ho_Characters, ho_ImageReduced, hv_OCRHandle, hv_RegularExpression,
                                              5, 10, out hv_Class, out hv_Confidence, out hv_Word, out hv_Score);
                }
            }
            HOperatorSet.ClearTextModel(hv_TextModel);
            HOperatorSet.ClearOcrClassCnn(hv_OCRHandle);
            ho_Timg1.Dispose();
            ho_ImageReduced.Dispose();
            ho_Region.Dispose();
            ho_Characters.Dispose();

            int nRet = 1;

            if (hv_findText == 1)
            {
                nRet = 0;
            }
            return(nRet);
        }
예제 #13
0
    // Chapter: Filters / Arithmetic
    // Short Description: Scale the gray values of an image from the interval [Min,Max] to [0,255]
    public void scale_image_range(HObject ho_Image, out HObject ho_ImageScaled, HTuple hv_Min,
                                  HTuple hv_Max)
    {
        // Stack for temporary objects
        HObject[] OTemp = new HObject[20];

        // Local iconic variables

        HObject ho_ImageSelected = null, ho_SelectedChannel = null;
        HObject ho_LowerRegion = null, ho_UpperRegion = null, ho_ImageSelectedScaled = null;

        // Local copy input parameter variables
        HObject ho_Image_COPY_INP_TMP;

        ho_Image_COPY_INP_TMP = ho_Image.CopyObj(1, -1);



        // Local control variables

        HTuple hv_LowerLimit = new HTuple(), hv_UpperLimit = new HTuple();
        HTuple hv_Mult = null, hv_Add = null, hv_NumImages = null;
        HTuple hv_ImageIndex = null, hv_Channels = new HTuple();
        HTuple hv_ChannelIndex = new HTuple(), hv_MinGray = new HTuple();
        HTuple hv_MaxGray = new HTuple(), hv_Range = new HTuple();
        HTuple hv_Max_COPY_INP_TMP = hv_Max.Clone();
        HTuple hv_Min_COPY_INP_TMP = hv_Min.Clone();

        // Initialize local and output iconic variables
        HOperatorSet.GenEmptyObj(out ho_ImageScaled);
        HOperatorSet.GenEmptyObj(out ho_ImageSelected);
        HOperatorSet.GenEmptyObj(out ho_SelectedChannel);
        HOperatorSet.GenEmptyObj(out ho_LowerRegion);
        HOperatorSet.GenEmptyObj(out ho_UpperRegion);
        HOperatorSet.GenEmptyObj(out ho_ImageSelectedScaled);
        //Convenience procedure to scale the gray values of the
        //input image Image from the interval [Min,Max]
        //to the interval [0,255] (default).
        //Gray values < 0 or > 255 (after scaling) are clipped.
        //
        //If the image shall be scaled to an interval different from [0,255],
        //this can be achieved by passing tuples with 2 values [From, To]
        //as Min and Max.
        //Example:
        //scale_image_range(Image:ImageScaled:[100,50],[200,250])
        //maps the gray values of Image from the interval [100,200] to [50,250].
        //All other gray values will be clipped.
        //
        //input parameters:
        //Image: the input image
        //Min: the minimum gray value which will be mapped to 0
        //     If a tuple with two values is given, the first value will
        //     be mapped to the second value.
        //Max: The maximum gray value which will be mapped to 255
        //     If a tuple with two values is given, the first value will
        //     be mapped to the second value.
        //
        //Output parameter:
        //ImageScale: the resulting scaled image.
        //
        if ((int)(new HTuple((new HTuple(hv_Min_COPY_INP_TMP.TupleLength())).TupleEqual(
                                 2))) != 0)
        {
            hv_LowerLimit       = hv_Min_COPY_INP_TMP.TupleSelect(1);
            hv_Min_COPY_INP_TMP = hv_Min_COPY_INP_TMP.TupleSelect(0);
        }
        else
        {
            hv_LowerLimit = 0.0;
        }
        if ((int)(new HTuple((new HTuple(hv_Max_COPY_INP_TMP.TupleLength())).TupleEqual(
                                 2))) != 0)
        {
            hv_UpperLimit       = hv_Max_COPY_INP_TMP.TupleSelect(1);
            hv_Max_COPY_INP_TMP = hv_Max_COPY_INP_TMP.TupleSelect(0);
        }
        else
        {
            hv_UpperLimit = 255.0;
        }
        //
        //Calculate scaling parameters.
        hv_Mult = (((hv_UpperLimit - hv_LowerLimit)).TupleReal()) / (hv_Max_COPY_INP_TMP - hv_Min_COPY_INP_TMP);
        hv_Add  = ((-hv_Mult) * hv_Min_COPY_INP_TMP) + hv_LowerLimit;
        //
        //Scale image.
        {
            HObject ExpTmpOutVar_0;
            HOperatorSet.ScaleImage(ho_Image_COPY_INP_TMP, out ExpTmpOutVar_0, hv_Mult, hv_Add);
            ho_Image_COPY_INP_TMP.Dispose();
            ho_Image_COPY_INP_TMP = ExpTmpOutVar_0;
        }
        //
        //Clip gray values if necessary.
        //This must be done for each image and channel separately.
        ho_ImageScaled.Dispose();
        HOperatorSet.GenEmptyObj(out ho_ImageScaled);
        HOperatorSet.CountObj(ho_Image_COPY_INP_TMP, out hv_NumImages);
        HTuple end_val49  = hv_NumImages;
        HTuple step_val49 = 1;

        for (hv_ImageIndex = 1; hv_ImageIndex.Continue(end_val49, step_val49); hv_ImageIndex = hv_ImageIndex.TupleAdd(step_val49))
        {
            ho_ImageSelected.Dispose();
            HOperatorSet.SelectObj(ho_Image_COPY_INP_TMP, out ho_ImageSelected, hv_ImageIndex);
            HOperatorSet.CountChannels(ho_ImageSelected, out hv_Channels);
            HTuple end_val52  = hv_Channels;
            HTuple step_val52 = 1;
            for (hv_ChannelIndex = 1; hv_ChannelIndex.Continue(end_val52, step_val52); hv_ChannelIndex = hv_ChannelIndex.TupleAdd(step_val52))
            {
                ho_SelectedChannel.Dispose();
                HOperatorSet.AccessChannel(ho_ImageSelected, out ho_SelectedChannel, hv_ChannelIndex);
                HOperatorSet.MinMaxGray(ho_SelectedChannel, ho_SelectedChannel, 0, out hv_MinGray,
                                        out hv_MaxGray, out hv_Range);
                ho_LowerRegion.Dispose();
                HOperatorSet.Threshold(ho_SelectedChannel, out ho_LowerRegion, ((hv_MinGray.TupleConcat(
                                                                                     hv_LowerLimit))).TupleMin(), hv_LowerLimit);
                ho_UpperRegion.Dispose();
                HOperatorSet.Threshold(ho_SelectedChannel, out ho_UpperRegion, hv_UpperLimit,
                                       ((hv_UpperLimit.TupleConcat(hv_MaxGray))).TupleMax());
                {
                    HObject ExpTmpOutVar_0;
                    HOperatorSet.PaintRegion(ho_LowerRegion, ho_SelectedChannel, out ExpTmpOutVar_0,
                                             hv_LowerLimit, "fill");
                    ho_SelectedChannel.Dispose();
                    ho_SelectedChannel = ExpTmpOutVar_0;
                }
                {
                    HObject ExpTmpOutVar_0;
                    HOperatorSet.PaintRegion(ho_UpperRegion, ho_SelectedChannel, out ExpTmpOutVar_0,
                                             hv_UpperLimit, "fill");
                    ho_SelectedChannel.Dispose();
                    ho_SelectedChannel = ExpTmpOutVar_0;
                }
                if ((int)(new HTuple(hv_ChannelIndex.TupleEqual(1))) != 0)
                {
                    ho_ImageSelectedScaled.Dispose();
                    HOperatorSet.CopyObj(ho_SelectedChannel, out ho_ImageSelectedScaled, 1,
                                         1);
                }
                else
                {
                    {
                        HObject ExpTmpOutVar_0;
                        HOperatorSet.AppendChannel(ho_ImageSelectedScaled, ho_SelectedChannel,
                                                   out ExpTmpOutVar_0);
                        ho_ImageSelectedScaled.Dispose();
                        ho_ImageSelectedScaled = ExpTmpOutVar_0;
                    }
                }
            }
            {
                HObject ExpTmpOutVar_0;
                HOperatorSet.ConcatObj(ho_ImageScaled, ho_ImageSelectedScaled, out ExpTmpOutVar_0
                                       );
                ho_ImageScaled.Dispose();
                ho_ImageScaled = ExpTmpOutVar_0;
            }
        }
        ho_Image_COPY_INP_TMP.Dispose();
        ho_ImageSelected.Dispose();
        ho_SelectedChannel.Dispose();
        ho_LowerRegion.Dispose();
        ho_UpperRegion.Dispose();
        ho_ImageSelectedScaled.Dispose();

        return;
    }
예제 #14
0
    // Main procedure
    private void action()
    {
        // Local iconic variables

        HObject ho_Bond, ho_Bright = null, ho_Die = null;
        HObject ho_DieGrey = null, ho_Wires = null, ho_WiresFilled = null;
        HObject ho_Balls = null, ho_SingleBalls = null, ho_Rect = null;
        HObject ho_IntermediateBalls = null, ho_Forbidden = null, ho_RegionExpand = null;
        HObject ho_RoundBalls = null, ho_FinalBalls = null;

        // Local control variables

        HTuple hv_ImageNames = null, hv_Width = null;
        HTuple hv_Height = null, hv_WindowHandle = new HTuple();
        HTuple hv_NumImages = null, hv_I = null, hv_Min = new HTuple();
        HTuple hv_Max = new HTuple(), hv_Range = new HTuple();
        HTuple hv_Row = new HTuple(), hv_Column = new HTuple();
        HTuple hv_Radius = new HTuple(), hv_NumBalls = new HTuple();
        HTuple hv_Diameter = new HTuple(), hv_meanDiameter = new HTuple();
        HTuple hv_mimDiameter = new HTuple();

        // Initialize local and output iconic variables
        HOperatorSet.GenEmptyObj(out ho_Bond);
        HOperatorSet.GenEmptyObj(out ho_Bright);
        HOperatorSet.GenEmptyObj(out ho_Die);
        HOperatorSet.GenEmptyObj(out ho_DieGrey);
        HOperatorSet.GenEmptyObj(out ho_Wires);
        HOperatorSet.GenEmptyObj(out ho_WiresFilled);
        HOperatorSet.GenEmptyObj(out ho_Balls);
        HOperatorSet.GenEmptyObj(out ho_SingleBalls);
        HOperatorSet.GenEmptyObj(out ho_Rect);
        HOperatorSet.GenEmptyObj(out ho_IntermediateBalls);
        HOperatorSet.GenEmptyObj(out ho_Forbidden);
        HOperatorSet.GenEmptyObj(out ho_RegionExpand);
        HOperatorSet.GenEmptyObj(out ho_RoundBalls);
        HOperatorSet.GenEmptyObj(out ho_FinalBalls);
        try
        {
            //ball_seq.hdev: Inspection of Ball Bonding
            //
            dev_update_off();
            hv_ImageNames = new HTuple("die/") + (((new HTuple("die_02")).TupleConcat("die_03")).TupleConcat(
                                                      "die_04")).TupleConcat("die_07");
            HOperatorSet.SetColored(hv_ExpDefaultWinHandle, 12);
            ho_Bond.Dispose();
            HOperatorSet.ReadImage(out ho_Bond, hv_ImageNames.TupleSelect(0));
            HOperatorSet.GetImageSize(ho_Bond, out hv_Width, out hv_Height);
            //dev_close_window(...);
            //dev_open_window(...);
            set_display_font(hv_ExpDefaultWinHandle, 16, "mono", "true", "false");
            HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "margin");
            HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 3);
            hv_NumImages = new HTuple(hv_ImageNames.TupleLength());
            HTuple end_val13  = hv_NumImages - 1;
            HTuple step_val13 = 1;
            for (hv_I = 0; hv_I.Continue(end_val13, step_val13); hv_I = hv_I.TupleAdd(step_val13))
            {
                ho_Bond.Dispose();
                HOperatorSet.ReadImage(out ho_Bond, hv_ImageNames.TupleSelect(hv_I));
                HOperatorSet.DispObj(ho_Bond, hv_ExpDefaultWinHandle);
                HOperatorSet.MinMaxGray(ho_Bond, ho_Bond, 0, out hv_Min, out hv_Max, out hv_Range);
                ho_Bright.Dispose();
                HOperatorSet.Threshold(ho_Bond, out ho_Bright, hv_Max - 80, 255);
                ho_Die.Dispose();
                HOperatorSet.ShapeTrans(ho_Bright, out ho_Die, "rectangle2");
                HOperatorSet.DispObj(ho_Die, hv_ExpDefaultWinHandle);
                ho_DieGrey.Dispose();
                HOperatorSet.ReduceDomain(ho_Bond, ho_Die, out ho_DieGrey);
                HOperatorSet.MinMaxGray(ho_Die, ho_Bond, 0, out hv_Min, out hv_Max, out hv_Range);
                ho_Wires.Dispose();
                HOperatorSet.Threshold(ho_DieGrey, out ho_Wires, 0, hv_Min + 30);
                ho_WiresFilled.Dispose();
                HOperatorSet.FillUpShape(ho_Wires, out ho_WiresFilled, "area", 1, 100);
                ho_Balls.Dispose();
                HOperatorSet.OpeningCircle(ho_WiresFilled, out ho_Balls, 9.5);
                ho_SingleBalls.Dispose();
                HOperatorSet.Connection(ho_Balls, out ho_SingleBalls);
                ho_Rect.Dispose();
                HOperatorSet.SelectShapeStd(ho_SingleBalls, out ho_Rect, "rectangle1", 90);
                ho_IntermediateBalls.Dispose();
                HOperatorSet.Difference(ho_SingleBalls, ho_Rect, out ho_IntermediateBalls
                                        );
                ho_Forbidden.Dispose();
                HOperatorSet.GenEmptyRegion(out ho_Forbidden);
                ho_RegionExpand.Dispose();
                HOperatorSet.ExpandGray(ho_IntermediateBalls, ho_Bond, ho_Forbidden, out ho_RegionExpand,
                                        4, "image", 6);
                ho_RoundBalls.Dispose();
                HOperatorSet.OpeningCircle(ho_RegionExpand, out ho_RoundBalls, 15.5);
                ho_FinalBalls.Dispose();
                HOperatorSet.SortRegion(ho_RoundBalls, out ho_FinalBalls, "first_point",
                                        "true", "column");
                HOperatorSet.SmallestCircle(ho_FinalBalls, out hv_Row, out hv_Column, out hv_Radius);
                hv_NumBalls     = new HTuple(hv_Radius.TupleLength());
                hv_Diameter     = 2 * hv_Radius;
                hv_meanDiameter = (hv_Diameter.TupleSum()) / hv_NumBalls;
                hv_mimDiameter  = hv_Diameter.TupleMin();
                HOperatorSet.DispObj(ho_RoundBalls, hv_ExpDefaultWinHandle);
                if ((int)(new HTuple(hv_I.TupleNotEqual(hv_NumImages))) != 0)
                {
                    disp_continue_message(hv_ExpDefaultWinHandle, "black", "true");
                }
                HDevelopStop();
            }
        }
        catch (HalconException HDevExpDefaultException)
        {
            ho_Bond.Dispose();
            ho_Bright.Dispose();
            ho_Die.Dispose();
            ho_DieGrey.Dispose();
            ho_Wires.Dispose();
            ho_WiresFilled.Dispose();
            ho_Balls.Dispose();
            ho_SingleBalls.Dispose();
            ho_Rect.Dispose();
            ho_IntermediateBalls.Dispose();
            ho_Forbidden.Dispose();
            ho_RegionExpand.Dispose();
            ho_RoundBalls.Dispose();
            ho_FinalBalls.Dispose();

            throw HDevExpDefaultException;
        }
        ho_Bond.Dispose();
        ho_Bright.Dispose();
        ho_Die.Dispose();
        ho_DieGrey.Dispose();
        ho_Wires.Dispose();
        ho_WiresFilled.Dispose();
        ho_Balls.Dispose();
        ho_SingleBalls.Dispose();
        ho_Rect.Dispose();
        ho_IntermediateBalls.Dispose();
        ho_Forbidden.Dispose();
        ho_RegionExpand.Dispose();
        ho_RoundBalls.Dispose();
        ho_FinalBalls.Dispose();
    }
예제 #15
0
        /// <summary>
        /// 初始化後才能叫用 Action
        /// </summary>
        public MeasureResult Action()
        {
            #region 輸出結果, CircleResult or Distance Result
            CircleResult mResult = null;
            #endregion

            // Local iconic variables

            HObject ho_R1_Circle = null;
            HObject ho_R1_ROI_Image = null, ho_R1_Region = null, ho_R1_ImageReduced = null;
            HObject ho_R1_Edges = null, ho_R1_ContoursSplit = null, ho_R1_SingleSegment = null;
            //HObject ho_R1_ContEllipse = null;


            // Local control variables

            HTuple hv_msgOffsetY, hv_msgOffsetX;
            //HTuple hv_AllModelId;
            HTuple hv_STD_Row;
            HTuple hv_STD_Col, hv_Img_Row, hv_Img_Col, hv_Img_Rotate_Angle;
            HTuple hv_OffsetRow, hv_OffsetCol, hv_STD_R1_Row, hv_STD_R1_Col;
            HTuple hv_STD_R1_V_Row, hv_STD_R1_V_Col, hv_R1_X, hv_R1_Y;
            HTuple hv_R1_Pos_Row, hv_R1_Pos_Col, hv_R1_R;
            HTuple hv_alpha = new HTuple(), hv_R1_low = new HTuple(), hv_R1_high = new HTuple();
            HTuple hv_R1_NumSegments = new HTuple(), hv_NumCircles = new HTuple();
            HTuple hv_Num_Circle_Point = new HTuple(), hv_R1 = new HTuple();
            HTuple hv_R1_limit = new HTuple(), hv_i = new HTuple(), hv_Attrib = new HTuple();
            HTuple hv_R1_Row = new HTuple(), hv_R1_Column = new HTuple();
            HTuple hv_R1_Radius = new HTuple(), hv_R1_StartPhi = new HTuple();
            HTuple hv_R1_EndPhi = new HTuple(), hv_R1_PointOrder = new HTuple();
            HTuple hv_R1_MinDist = new HTuple(), hv_R1_MaxDist = new HTuple();
            HTuple hv_R1_AvgDist = new HTuple(), hv_R1_SigmaDist = new HTuple();
            HTuple hv_ResultText = new HTuple(), hv_MeasureReasult;

            // Initialize local and output iconic variables
            //HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_R1_Circle);
            HOperatorSet.GenEmptyObj(out ho_R1_ROI_Image);
            HOperatorSet.GenEmptyObj(out ho_R1_Region);
            HOperatorSet.GenEmptyObj(out ho_R1_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_R1_Edges);
            HOperatorSet.GenEmptyObj(out ho_R1_ContoursSplit);
            HOperatorSet.GenEmptyObj(out ho_R1_SingleSegment);
            //HOperatorSet.GenEmptyObj(out ho_R1_ContEllipse);

            //Measure: SDMS_R1
            //Author: John Hsieh
            //Date: 2012
            //ho_Image.Dispose();
            //HOperatorSet.ReadImage(out ho_Image, "D:/Projects/Halcon/SDMS/SDMS_Measure/Images/E-1/E1-1.bmp");
            //dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle);
            //dev_update_off ()
            // dev_update_window(...); only in hdevelop
            HOperatorSet.SetSystem("border_shape_models", "false");

            //****Message Args
            hv_msgOffsetY = 100;
            hv_msgOffsetX = 100;

            //****Model All
            //HOperatorSet.ReadShapeModel("D:/Projects/Halcon/SDMS/SDMS_Measure/Model/MatchingAll.shm",
            //	out hv_AllModelId);
            //ho_AllModelContours.Dispose();
            //HOperatorSet.GetShapeModelContours(out ho_AllModelContours, hv_AllModelId, 1);
            //HOperatorSet.FindShapeModel(ho_Image, hv_AllModelId, (new HTuple(0)).TupleRad()
            //	, (new HTuple(360)).TupleRad(), 0.5, 1, 0.5, "least_squares", 6, 0.75, out hv_AllModelRow,
            //	out hv_AllModelColumn, out hv_AllModelAngle, out modelScore);

            //****Model Args

            //STD 中心點
            hv_STD_Row = 772;
            hv_STD_Col = 1003;

            //目前圖形 中心點
            hv_Img_Row = hv_AllModelRow.Clone();
            hv_Img_Col = hv_AllModelColumn.Clone();

            //目前圖形 Rotate Angle
            hv_Img_Rotate_Angle = hv_AllModelAngle.Clone();

            //目前圖形偏移量
            hv_OffsetRow = hv_Img_Row - hv_STD_Row;
            hv_OffsetCol = hv_Img_Col - hv_STD_Col;


            //****R1
            //STD R1_ 位置
            hv_STD_R1_Row = 589;
            hv_STD_R1_Col = 705;

            //STD 向量 STD_R1_
            hv_STD_R1_V_Row = hv_STD_R1_Row - hv_STD_Row;
            hv_STD_R1_V_Col = hv_STD_R1_Col - hv_STD_Col;


            //R1_X, R1_Y 分量
            hv_R1_X = (hv_STD_R1_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_R1_V_Row * (hv_Img_Rotate_Angle.TupleSin()
                                                                                                 ));
            hv_R1_Y = (hv_STD_R1_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_R1_V_Col * (hv_Img_Rotate_Angle.TupleSin()
                                                                                                 ));


            //目前圖形 R1_ 位置
            hv_R1_Pos_Row = (hv_STD_Row + hv_R1_Y) + hv_OffsetRow;
            hv_R1_Pos_Col = (hv_STD_Col + hv_R1_X) + hv_OffsetCol;

            hv_R1_R = 20;
            //*ROI
            ho_R1_Circle.Dispose();
            HOperatorSet.GenCircle(out ho_R1_Circle, hv_R1_Pos_Row, hv_R1_Pos_Col, hv_R1_R);
            ho_R1_ROI_Image.Dispose();
            HOperatorSet.ReduceDomain(ho_Image, ho_R1_Circle, out ho_R1_ROI_Image);
            ho_R1_Region.Dispose();
            HOperatorSet.FastThreshold(ho_R1_ROI_Image, out ho_R1_Region, 100, 255, 20);
            ho_R1_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_R1_ROI_Image, ho_R1_Region, out ho_R1_ImageReduced
                                      );

            //sobel_fast 具有較寬的選擇範圍,搭配 alpha 參數 (alpha 越大, 容錯範圍大)
            hv_alpha   = 0.9;
            hv_R1_low  = 20;
            hv_R1_high = 40;
            ho_R1_Edges.Dispose();
            HOperatorSet.EdgesSubPix(ho_R1_ImageReduced, out ho_R1_Edges, "sobel_fast",
                                     hv_alpha, hv_R1_low, hv_R1_high);
            //stop ()
            //*所有的數值越小,表示容錯範圍大,反之亦然
            ho_R1_ContoursSplit.Dispose();
            HOperatorSet.SegmentContoursXld(ho_R1_Edges, out ho_R1_ContoursSplit, "lines_circles",
                                            17, 1, 1);
            //Display the results
            //===========================================================
            HOperatorSet.CountObj(ho_R1_ContoursSplit, out hv_R1_NumSegments);
            hv_NumCircles       = 0;
            hv_Num_Circle_Point = 5;
            hv_R1       = 999;
            hv_R1_limit = 10;
            for (hv_i = 1; hv_i.Continue(hv_R1_NumSegments, 1); hv_i = hv_i.TupleAdd(1))
            {
                ho_R1_SingleSegment.Dispose();
                HOperatorSet.SelectObj(ho_R1_ContoursSplit, out ho_R1_SingleSegment, hv_i);
                HOperatorSet.GetContourGlobalAttribXld(ho_R1_SingleSegment, "cont_approx", out hv_Attrib);


                if ((int)(new HTuple(hv_Attrib.TupleEqual(1))) != 0)
                {
                    HOperatorSet.FitCircleContourXld(ho_R1_SingleSegment, "atukey", -1, 2,
                                                     hv_Num_Circle_Point, 5, 2, out hv_R1_Row, out hv_R1_Column, out hv_R1_Radius,
                                                     out hv_R1_StartPhi, out hv_R1_EndPhi, out hv_R1_PointOrder);

                    //ho_R1_ContEllipse.Dispose();
                    //HOperatorSet.GenEllipseContourXld(out ho_R1_ContEllipse, hv_R1_Row, hv_R1_Column,
                    //	0, hv_R1_Radius, hv_R1_Radius, 0, (new HTuple(360)).TupleRad(), "positive",
                    //	1.0);

                    //HOperatorSet.DistEllipseContourXld(ho_R1_SingleSegment, "algebraic", -1,
                    //	0, hv_R1_Row, hv_R1_Column, 0, hv_R1_Radius, hv_R1_Radius, out hv_R1_MinDist,
                    //	out hv_R1_MaxDist, out hv_R1_AvgDist, out hv_R1_SigmaDist);

                    hv_NumCircles = hv_NumCircles + 1;
                    if ((int)(new HTuple(hv_R1.TupleGreater(hv_R1_Radius))) != 0)
                    {
                        hv_R1 = hv_R1_Radius.Clone();
                        //hv_ResultText = (((("C" + hv_NumCircles) + ": Radius = ") + (hv_R1_Radius.TupleString(
                        //	".3"))) + " / MaxDeviation: ") + (hv_R1_MaxDist.TupleString(".3"));
                        //HOperatorSet.SetTposition(hv_WindowHandle, hv_R1_Pos_Row - hv_msgOffsetY,
                        //	hv_R1_Pos_Col - hv_msgOffsetX);
                        //HOperatorSet.WriteString(hv_WindowHandle, "R1");

                        #region 組合結果
                        mResult = new CircleResult(new HTuple(hv_R1_Row)
                                                   , new HTuple(hv_R1_Column)
                                                   , new HTuple(hv_R1_Radius)
                                                   , new HTuple(hv_R1_StartPhi)
                                                   , new HTuple(hv_R1_EndPhi)
                                                   , new HTuple(hv_R1_PointOrder))
                        {
                        };
                        #endregion
                    }
                }
            }
            hv_MeasureReasult = hv_R1.Clone();
            //****R1 End
            //ho_Image.Dispose();
            ho_R1_Circle.Dispose();
            ho_R1_ROI_Image.Dispose();
            ho_R1_Region.Dispose();
            ho_R1_ImageReduced.Dispose();
            ho_R1_Edges.Dispose();
            ho_R1_ContoursSplit.Dispose();
            ho_R1_SingleSegment.Dispose();
            //ho_R1_ContEllipse.Dispose();

            return(mResult);
        }
예제 #16
0
    // Procedures
    public void image_judge_sign_judge(HObject ho_RoiSign, HObject ho_OriImage, out HObject ho_SignFeat,
                                       out HTuple hv_sign)
    {
        // Stack for temporary objects
        HObject[] OTemp = new HObject[20];

        // Local iconic variables

        HObject ho_GrayImage, ho_ObjectSelected = null;
        HObject ho_ImageReduced = null, ho_Edges = null;

        // Local control variables

        HTuple hv_SignNum = new HTuple(), hv_SignThreshold = new HTuple();
        HTuple hv_Index = new HTuple(), hv_Length = new HTuple();
        HTuple hv_Length1 = new HTuple();

        // Initialize local and output iconic variables
        HOperatorSet.GenEmptyObj(out ho_SignFeat);
        HOperatorSet.GenEmptyObj(out ho_GrayImage);
        HOperatorSet.GenEmptyObj(out ho_ObjectSelected);
        HOperatorSet.GenEmptyObj(out ho_ImageReduced);
        HOperatorSet.GenEmptyObj(out ho_Edges);
        hv_sign = new HTuple();

        //** Porg
        //* Pre
        ho_GrayImage.Dispose();
        HOperatorSet.Rgb1ToGray(ho_OriImage, out ho_GrayImage);
        //get_region_convex (ROI_0, Rows, Columns)


        //* Judgement
        ho_SignFeat.Dispose();
        HOperatorSet.GenEmptyObj(out ho_SignFeat);
        //SignNum := 4
        hv_SignNum.Dispose();
        HOperatorSet.CountObj(ho_RoiSign, out hv_SignNum);
        hv_SignThreshold.Dispose();
        hv_SignThreshold = 5.0;
        HTuple end_val12  = hv_SignNum;
        HTuple step_val12 = 1;

        for (hv_Index = 1; hv_Index.Continue(end_val12, step_val12); hv_Index = hv_Index.TupleAdd(step_val12))
        {
            ho_ObjectSelected.Dispose();
            HOperatorSet.SelectObj(ho_RoiSign, out ho_ObjectSelected, hv_Index);
            //截取对应区域图片
            ho_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_GrayImage, ho_ObjectSelected, out ho_ImageReduced
                                      );
            //区域图片映射为轮廓
            ho_Edges.Dispose();
            HOperatorSet.EdgesSubPix(ho_ImageReduced, out ho_Edges, "canny", 1, 20, 40);
            {
                HObject ExpTmpOutVar_0;
                HOperatorSet.ConcatObj(ho_Edges, ho_SignFeat, out ExpTmpOutVar_0);
                ho_SignFeat.Dispose();
                ho_SignFeat = ExpTmpOutVar_0;
            }
            //得到轮廓长度数组
            hv_Length.Dispose();
            HOperatorSet.LengthXld(ho_Edges, out hv_Length);
            //云团数目大于五则有签字
            hv_Length1.Dispose();
            HOperatorSet.TupleLength(hv_Length, out hv_Length1);
            if ((int)(new HTuple(hv_Length1.TupleGreater(hv_SignThreshold))) != 0)
            {
                if (hv_sign == null)
                {
                    hv_sign = new HTuple();
                }
                hv_sign[hv_Index - 1] = 1;
            }
            else
            {
                if (hv_sign == null)
                {
                    hv_sign = new HTuple();
                }
                hv_sign[hv_Index - 1] = 0;
            }
        }



        ho_GrayImage.Dispose();
        ho_ObjectSelected.Dispose();
        ho_ImageReduced.Dispose();
        ho_Edges.Dispose();

        hv_SignNum.Dispose();
        hv_SignThreshold.Dispose();
        hv_Index.Dispose();
        hv_Length.Dispose();
        hv_Length1.Dispose();

        return;
    }
예제 #17
0
        private void train_classifier(HTuple hv_DLClassifierHandle, HTuple hv_FileName,
                                      HTuple hv_NumEpochs, HTuple hv_TrainingImages, HTuple hv_TrainingLabels, HTuple hv_ValidationImages,
                                      HTuple hv_ValidationLabels, HTuple hv_LearningRateStepEveryNthEpoch, HTuple hv_LearningRateStepRatio,
                                      HTuple hv_PlotEveryNthEpoch, HTuple hv_WindowHandle)
        {
            // Stack for temporary objects
            HObject[] OTemp = new HObject[20];

            // Local iconic variables

            HObject ho_BatchImages = null;

            // Local control variables

            HTuple hv_TrainingErrors = new HTuple(), hv_ValidationErrors = new HTuple();
            HTuple hv_LearningRates = new HTuple(), hv_Epochs = new HTuple();
            HTuple hv_LossByIteration = new HTuple(), hv_BatchSize = new HTuple();
            HTuple hv_MinValidationError = new HTuple(), hv_NumBatchesInEpoch = new HTuple();
            HTuple hv_NumTotalIterations = new HTuple(), hv_PlottedIterations = new HTuple();
            HTuple hv_TrainSequence = new HTuple(), hv_SelectPercentageTrainingImages = new HTuple();
            HTuple hv_TrainingImagesSelected = new HTuple(), hv_TrainingLabelsSelected = new HTuple();
            HTuple hv_Epoch = new HTuple(), hv_Iteration = new HTuple();
            HTuple hv_BatchStart = new HTuple(), hv_BatchEnd = new HTuple();
            HTuple hv_BatchIndices = new HTuple(), hv_BatchImageFiles = new HTuple();
            HTuple hv_BatchLabels = new HTuple(), hv_GenParamName = new HTuple();
            HTuple hv_GenParamValue = new HTuple(), hv_DLClassifierTrainResultHandle = new HTuple();
            HTuple hv_Loss = new HTuple(), hv_CurrentIteration = new HTuple();
            HTuple hv_TrainingDLClassifierResultIDs = new HTuple();
            HTuple hv_ValidationDLClassifierResultIDs = new HTuple();
            HTuple hv_TrainingTop1Error = new HTuple(), hv_ValidationTop1Error = new HTuple();
            HTuple hv_LearningRate = new HTuple();

            HTupleVector hvec_TrainingPredictedLabels   = new HTupleVector(1);
            HTupleVector hvec_TrainingConfidences       = new HTupleVector(1);
            HTupleVector hvec_ValidationPredictedLabels = new HTupleVector(1);
            HTupleVector hvec_ValidationConfidences     = new HTupleVector(1);

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_BatchImages);
            try
            {
                //For the plot during training,
                //we need to concatenate some intermediate results.
                hv_TrainingErrors.Dispose();
                hv_TrainingErrors = new HTuple();
                hv_ValidationErrors.Dispose();
                hv_ValidationErrors = new HTuple();
                hv_LearningRates.Dispose();
                hv_LearningRates = new HTuple();
                hv_Epochs.Dispose();
                hv_Epochs = new HTuple();
                hv_LossByIteration.Dispose();
                hv_LossByIteration = new HTuple();
                hv_BatchSize.Dispose();
                HOperatorSet.GetDlClassifierParam(hv_DLClassifierHandle, "batch_size", out hv_BatchSize);
                hv_MinValidationError.Dispose();
                hv_MinValidationError = 1;
                //
                //Create a tuple that includes all the iterations
                //where the plot should be computed (including the last ieration).
                hv_NumBatchesInEpoch.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_NumBatchesInEpoch = (((((new HTuple(hv_TrainingImages.TupleLength()
                                                           )) / (hv_BatchSize.TupleReal()))).TupleFloor())).TupleInt();
                }
                hv_NumTotalIterations.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_NumTotalIterations = (hv_NumBatchesInEpoch * hv_NumEpochs) - 1;
                }
                hv_PlottedIterations.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_PlottedIterations = ((((hv_NumBatchesInEpoch * HTuple.TupleGenSequence(
                                                   0, hv_NumEpochs - 1, hv_PlotEveryNthEpoch))).TupleConcat(hv_NumTotalIterations))).TupleRound()
                    ;
                }
                //
                //TrainSequence is used for easier indexing of the training data.
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_TrainSequence.Dispose();
                    HOperatorSet.TupleGenSequence(0, (new HTuple(hv_TrainingImages.TupleLength()
                                                                 )) - 1, 1, out hv_TrainSequence);
                }
                //
                //Select a subset of the training data set
                //in order to obtain a fast approximation
                //of the training error during training (plotting).
                hv_SelectPercentageTrainingImages.Dispose();
                hv_SelectPercentageTrainingImages = 100;
                hv_TrainingImagesSelected.Dispose(); hv_TrainingLabelsSelected.Dispose();
                select_percentage_dl_classifier_data(hv_TrainingImages, hv_TrainingLabels,
                                                     hv_SelectPercentageTrainingImages, out hv_TrainingImagesSelected, out hv_TrainingLabelsSelected);
                //
                HTuple end_val25  = hv_NumEpochs - 1;
                HTuple step_val25 = 1;
                #region
                for (hv_Epoch = 0; hv_Epoch.Continue(end_val25, step_val25); hv_Epoch = hv_Epoch.TupleAdd(step_val25))
                {
                    //In order to get randomness in each epoch,
                    //the training set is shuffled every epoch.
                    {
                        HTuple ExpTmpOutVar_0;
                        tuple_shuffle(hv_TrainSequence, out ExpTmpOutVar_0);
                        hv_TrainSequence.Dispose();
                        hv_TrainSequence = ExpTmpOutVar_0;
                    }
                    HTuple end_val29  = hv_NumBatchesInEpoch - 1;
                    HTuple step_val29 = 1;
                    for (hv_Iteration = 0; hv_Iteration.Continue(end_val29, step_val29); hv_Iteration = hv_Iteration.TupleAdd(step_val29))
                    {
                        //Select a batch from the training data set.
                        hv_BatchStart.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_BatchStart = hv_Iteration * hv_BatchSize;
                        }
                        hv_BatchEnd.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_BatchEnd = hv_BatchStart + (hv_BatchSize - 1);
                        }
                        hv_BatchIndices.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_BatchIndices = hv_TrainSequence.TupleSelectRange(
                                hv_BatchStart, hv_BatchEnd);
                        }
                        hv_BatchImageFiles.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_BatchImageFiles = hv_TrainingImages.TupleSelect(
                                hv_BatchIndices);
                        }
                        hv_BatchLabels.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_BatchLabels = hv_TrainingLabels.TupleSelect(
                                hv_BatchIndices);
                        }
                        //
                        //Read the image of the current batch.
                        ho_BatchImages.Dispose();
                        HOperatorSet.ReadImage(out ho_BatchImages, hv_BatchImageFiles);
                        //Augment the images to get a better variety of training images.
                        hv_GenParamName.Dispose();
                        hv_GenParamName = "mirror";
                        hv_GenParamValue.Dispose();
                        hv_GenParamValue = "rc";
                        {
                            HObject ExpTmpOutVar_0;
                            augment_images(ho_BatchImages, out ExpTmpOutVar_0, hv_GenParamName, hv_GenParamValue);
                            ho_BatchImages.Dispose();
                            ho_BatchImages = ExpTmpOutVar_0;
                        }
                        //
                        //Train the network with these images and ground truth labels.
                        hv_DLClassifierTrainResultHandle.Dispose();
                        HOperatorSet.TrainDlClassifierBatch(ho_BatchImages, hv_DLClassifierHandle,
                                                            hv_BatchLabels, out hv_DLClassifierTrainResultHandle);
                        //You can access the current value of the loss function,
                        //which should decrease during the training.
                        hv_Loss.Dispose();
                        HOperatorSet.GetDlClassifierTrainResult(hv_DLClassifierTrainResultHandle,
                                                                "loss", out hv_Loss);
                        //Store the loss in a tuple .
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            {
                                HTuple
                                    ExpTmpLocalVar_LossByIteration = hv_LossByIteration.TupleConcat(
                                    hv_Loss);
                                hv_LossByIteration.Dispose();
                                hv_LossByIteration = ExpTmpLocalVar_LossByIteration;
                            }
                        }
                        //
                        //In regular intervals, we want to evaluate
                        //how well our classifier performs.
                        hv_CurrentIteration.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_CurrentIteration = ((hv_Iteration + (hv_NumBatchesInEpoch * hv_Epoch))).TupleInt()
                            ;
                        }
                        if ((int)(((hv_CurrentIteration.TupleEqualElem(hv_PlottedIterations))).TupleSum()
                                  ) != 0)
                        {
                            //Plot the progress regularly.
                            //Evaluate the current classifier on the training and validation set.
                            hv_TrainingDLClassifierResultIDs.Dispose(); hvec_TrainingPredictedLabels.Dispose(); hvec_TrainingConfidences.Dispose();
                            apply_dl_classifier_batchwise(hv_TrainingImagesSelected, hv_DLClassifierHandle,
                                                          out hv_TrainingDLClassifierResultIDs, out hvec_TrainingPredictedLabels,
                                                          out hvec_TrainingConfidences);
                            hv_ValidationDLClassifierResultIDs.Dispose(); hvec_ValidationPredictedLabels.Dispose(); hvec_ValidationConfidences.Dispose();
                            apply_dl_classifier_batchwise(hv_ValidationImages, hv_DLClassifierHandle,
                                                          out hv_ValidationDLClassifierResultIDs, out hvec_ValidationPredictedLabels,
                                                          out hvec_ValidationConfidences);
                            //Evaluate the top-1 error on each dataset.
                            hv_TrainingTop1Error.Dispose();
                            evaluate_dl_classifier(hv_TrainingLabelsSelected, hv_DLClassifierHandle,
                                                   hv_TrainingDLClassifierResultIDs, "top1_error", "global", out hv_TrainingTop1Error);
                            hv_ValidationTop1Error.Dispose();
                            evaluate_dl_classifier(hv_ValidationLabels, hv_DLClassifierHandle, hv_ValidationDLClassifierResultIDs,
                                                   "top1_error", "global", out hv_ValidationTop1Error);
                            //Concatenate the values for the plot.
                            hv_LearningRate.Dispose();
                            HOperatorSet.GetDlClassifierParam(hv_DLClassifierHandle, "learning_rate",
                                                              out hv_LearningRate);
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                {
                                    HTuple
                                        ExpTmpLocalVar_TrainingErrors = hv_TrainingErrors.TupleConcat(
                                        hv_TrainingTop1Error);
                                    hv_TrainingErrors.Dispose();
                                    hv_TrainingErrors = ExpTmpLocalVar_TrainingErrors;
                                }
                            }
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                {
                                    HTuple
                                        ExpTmpLocalVar_ValidationErrors = hv_ValidationErrors.TupleConcat(
                                        hv_ValidationTop1Error);
                                    hv_ValidationErrors.Dispose();
                                    hv_ValidationErrors = ExpTmpLocalVar_ValidationErrors;
                                }
                            }
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                {
                                    HTuple
                                        ExpTmpLocalVar_LearningRates = hv_LearningRates.TupleConcat(
                                        hv_LearningRate);
                                    hv_LearningRates.Dispose();
                                    hv_LearningRates = ExpTmpLocalVar_LearningRates;
                                }
                            }
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                {
                                    HTuple
                                        ExpTmpLocalVar_Epochs = hv_Epochs.TupleConcat(
                                        (hv_PlottedIterations.TupleSelect(new HTuple(hv_Epochs.TupleLength()
                                                                                     ))) / (hv_NumBatchesInEpoch.TupleReal()));
                                    hv_Epochs.Dispose();
                                    hv_Epochs = ExpTmpLocalVar_Epochs;
                                }
                            }

                            //Plot validation and error against the epochs in order to
                            //observe the progress of the training.
                            plot_dl_classifier_training_progress(hv_TrainingErrors, hv_ValidationErrors, hv_LearningRates, hv_Epochs, hv_NumEpochs, hv_WindowHandle);
                            //如果上面绘制坐标系到窗体过程报错,搞不定的话就用下面这句代替,直接显示训练数据到窗体
                            //disp_training_progress_datas(hv_TrainingErrors, hv_ValidationErrors, hv_LearningRates, hv_Epochs, hv_NumEpochs, hv_WindowHandle);


                            if ((int)(new HTuple(hv_ValidationTop1Error.TupleLessEqual(hv_MinValidationError))) != 0)
                            {
                                HOperatorSet.WriteDlClassifier(hv_DLClassifierHandle, hv_FileName);
                                hv_MinValidationError.Dispose();
                                hv_MinValidationError = new HTuple(hv_ValidationTop1Error);
                            }
                        }
                    }
                    //Reduce the learning rate every nth epoch.
                    if ((int)(new HTuple((((hv_Epoch + 1) % hv_LearningRateStepEveryNthEpoch)).TupleEqual(
                                             0))) != 0)
                    {
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            HOperatorSet.SetDlClassifierParam(hv_DLClassifierHandle, "learning_rate",
                                                              hv_LearningRate * hv_LearningRateStepRatio);
                        }
                        hv_LearningRate.Dispose();
                        HOperatorSet.GetDlClassifierParam(hv_DLClassifierHandle, "learning_rate",
                                                          out hv_LearningRate);
                    }
                }
                #endregion
                // stop(...); only in hdevelop
                ho_BatchImages.Dispose();

                hv_TrainingErrors.Dispose();
                hv_ValidationErrors.Dispose();
                hv_LearningRates.Dispose();
                hv_Epochs.Dispose();
                hv_LossByIteration.Dispose();
                hv_BatchSize.Dispose();
                hv_MinValidationError.Dispose();
                hv_NumBatchesInEpoch.Dispose();
                hv_NumTotalIterations.Dispose();
                hv_PlottedIterations.Dispose();
                hv_TrainSequence.Dispose();
                hv_SelectPercentageTrainingImages.Dispose();
                hv_TrainingImagesSelected.Dispose();
                hv_TrainingLabelsSelected.Dispose();
                hv_Epoch.Dispose();
                hv_Iteration.Dispose();
                hv_BatchStart.Dispose();
                hv_BatchEnd.Dispose();
                hv_BatchIndices.Dispose();
                hv_BatchImageFiles.Dispose();
                hv_BatchLabels.Dispose();
                hv_GenParamName.Dispose();
                hv_GenParamValue.Dispose();
                hv_DLClassifierTrainResultHandle.Dispose();
                hv_Loss.Dispose();
                hv_CurrentIteration.Dispose();
                hv_TrainingDLClassifierResultIDs.Dispose();
                hv_ValidationDLClassifierResultIDs.Dispose();
                hv_TrainingTop1Error.Dispose();
                hv_ValidationTop1Error.Dispose();
                hv_LearningRate.Dispose();
                hvec_TrainingPredictedLabels.Dispose();
                hvec_TrainingConfidences.Dispose();
                hvec_ValidationPredictedLabels.Dispose();
                hvec_ValidationConfidences.Dispose();

                return;
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_BatchImages.Dispose();

                hv_TrainingErrors.Dispose();
                hv_ValidationErrors.Dispose();
                hv_LearningRates.Dispose();
                hv_Epochs.Dispose();
                hv_LossByIteration.Dispose();
                hv_BatchSize.Dispose();
                hv_MinValidationError.Dispose();
                hv_NumBatchesInEpoch.Dispose();
                hv_NumTotalIterations.Dispose();
                hv_PlottedIterations.Dispose();
                hv_TrainSequence.Dispose();
                hv_SelectPercentageTrainingImages.Dispose();
                hv_TrainingImagesSelected.Dispose();
                hv_TrainingLabelsSelected.Dispose();
                hv_Epoch.Dispose();
                hv_Iteration.Dispose();
                hv_BatchStart.Dispose();
                hv_BatchEnd.Dispose();
                hv_BatchIndices.Dispose();
                hv_BatchImageFiles.Dispose();
                hv_BatchLabels.Dispose();
                hv_GenParamName.Dispose();
                hv_GenParamValue.Dispose();
                hv_DLClassifierTrainResultHandle.Dispose();
                hv_Loss.Dispose();
                hv_CurrentIteration.Dispose();
                hv_TrainingDLClassifierResultIDs.Dispose();
                hv_ValidationDLClassifierResultIDs.Dispose();
                hv_TrainingTop1Error.Dispose();
                hv_ValidationTop1Error.Dispose();
                hv_LearningRate.Dispose();
                hvec_TrainingPredictedLabels.Dispose();
                hvec_TrainingConfidences.Dispose();
                hvec_ValidationPredictedLabels.Dispose();
                hvec_ValidationConfidences.Dispose();

                throw HDevExpDefaultException;
            }
        }
    // Main procedure
    private void action()
    {
        // Local iconic variables

        HObject ho_Image, ho_GrayImage3, ho_ImageGauss;
        HObject ho_ModelContours = null, ho_TransContours = null, ho_Rectangle = null;
        HObject ho_ImageReduced = null;

        // Local control variables

        HTuple hv_T1 = null, hv_CheckNO = null, hv_GODS = null;
        HTuple hv_Minspec = null, hv_Setnumber = null, hv_ocvgod = null;
        HTuple hv_RowPlus = null, hv_ColPlus = null, hv_path = null;
        HTuple hv_FileHandle = null, hv_OutLine = null, hv_IsEOF = null;
        HTuple hv_CheckNumber = null, hv_GreedinessNumber = null;
        HTuple hv_Index = null, hv_checkNO = new HTuple(), hv_gods = new HTuple();
        HTuple hv_Mins = new HTuple(), hv_Setn = new HTuple();
        HTuple hv_ocvgodfile = new HTuple(), hv_Setocvchar = new HTuple();
        HTuple hv_ocvchar = new HTuple(), hv_Setrowplus = new HTuple();
        HTuple hv_Setcolplus = new HTuple(), hv_ModelID1 = null;
        HTuple hv_ModelID2 = null, hv_ModelID3 = null, hv_ModelID4 = null;
        HTuple hv_ModelID5 = null, hv_ModelID6 = null, hv_ModelIDs = null;
        HTuple hv_Width = null, hv_Height = null, hv_WindowHandle = new HTuple();
        HTuple hv_Index2 = null, hv_Row1 = new HTuple(), hv_Column1 = new HTuple();
        HTuple hv_Row2 = new HTuple(), hv_Column2 = new HTuple();
        HTuple hv_RefRow = new HTuple(), hv_RefColumn = new HTuple();
        HTuple hv_HomMat2D = new HTuple(), hv_Row = new HTuple();
        HTuple hv_Column = new HTuple(), hv_Angle = new HTuple();
        HTuple hv_Score = new HTuple(), hv_I = new HTuple(), hv_OCVHandle = new HTuple();
        HTuple hv_Quality = new HTuple(), hv_Index1 = new HTuple();
        HTuple hv_T2 = null;

        // Initialize local and output iconic variables
        HOperatorSet.GenEmptyObj(out ho_Image);
        HOperatorSet.GenEmptyObj(out ho_GrayImage3);
        HOperatorSet.GenEmptyObj(out ho_ImageGauss);
        HOperatorSet.GenEmptyObj(out ho_ModelContours);
        HOperatorSet.GenEmptyObj(out ho_TransContours);
        HOperatorSet.GenEmptyObj(out ho_Rectangle);
        HOperatorSet.GenEmptyObj(out ho_ImageReduced);
        HOperatorSet.CountSeconds(out hv_T1);

        //**外部控制参数读入***
        hv_CheckNO   = new HTuple();
        hv_GODS      = new HTuple();
        hv_Minspec   = new HTuple();
        hv_Setnumber = new HTuple();
        hv_ocvgod    = new HTuple();
        hv_RowPlus   = new HTuple();
        hv_ColPlus   = new HTuple();
        hv_path      = "matchingpara_fengwu_hougai.txt";
        HOperatorSet.OpenFile(hv_path, "input", out hv_FileHandle);
        HOperatorSet.FreadLine(hv_FileHandle, out hv_OutLine, out hv_IsEOF);
        HOperatorSet.TupleNumber(((hv_OutLine.TupleSplit(new HTuple(",,,")))).TupleSelect(
                                     0), out hv_CheckNumber);
        HOperatorSet.TupleNumber(((hv_OutLine.TupleSplit(new HTuple(",,,")))).TupleSelect(
                                     1), out hv_GreedinessNumber);
        HTuple end_val15  = hv_CheckNumber - 1;
        HTuple step_val15 = 1;

        for (hv_Index = 0; hv_Index.Continue(end_val15, step_val15); hv_Index = hv_Index.TupleAdd(step_val15))
        {
            HOperatorSet.FreadLine(hv_FileHandle, out hv_OutLine, out hv_IsEOF);
            HOperatorSet.TupleNumber(((hv_OutLine.TupleSplit(new HTuple(",,,")))).TupleSelect(
                                         0), out hv_checkNO);
            if (hv_CheckNO == null)
            {
                hv_CheckNO = new HTuple();
            }
            hv_CheckNO[hv_Index] = hv_checkNO;
            HOperatorSet.TupleNumber(((hv_OutLine.TupleSplit(new HTuple(",,,")))).TupleSelect(
                                         1), out hv_gods);
            if (hv_GODS == null)
            {
                hv_GODS = new HTuple();
            }
            hv_GODS[hv_Index] = hv_gods;
            HOperatorSet.TupleNumber(((hv_OutLine.TupleSplit(new HTuple(",,,")))).TupleSelect(
                                         2), out hv_Mins);
            if (hv_Minspec == null)
            {
                hv_Minspec = new HTuple();
            }
            hv_Minspec[hv_Index] = hv_Mins;
            HOperatorSet.TupleNumber(((hv_OutLine.TupleSplit(new HTuple(",,,")))).TupleSelect(
                                         3), out hv_Setn);
            if (hv_Setnumber == null)
            {
                hv_Setnumber = new HTuple();
            }
            hv_Setnumber[hv_Index] = hv_Setn;
            HOperatorSet.TupleNumber(((hv_OutLine.TupleSplit(new HTuple(",,,")))).TupleSelect(
                                         4), out hv_ocvgodfile);
            if (hv_ocvgod == null)
            {
                hv_ocvgod = new HTuple();
            }
            hv_ocvgod[hv_Index] = hv_ocvgodfile;
            HOperatorSet.TupleNumber(((hv_OutLine.TupleSplit(new HTuple(",,,")))).TupleSelect(
                                         5), out hv_Setocvchar);
            if (hv_ocvchar == null)
            {
                hv_ocvchar = new HTuple();
            }
            hv_ocvchar[hv_Index] = hv_Setocvchar;
            HOperatorSet.TupleNumber(((hv_OutLine.TupleSplit(new HTuple(",,,")))).TupleSelect(
                                         6), out hv_Setrowplus);
            if (hv_RowPlus == null)
            {
                hv_RowPlus = new HTuple();
            }
            hv_RowPlus[hv_Index] = hv_Setrowplus;
            HOperatorSet.TupleNumber(((hv_OutLine.TupleSplit(new HTuple(",,,")))).TupleSelect(
                                         7), out hv_Setcolplus);
            if (hv_ColPlus == null)
            {
                hv_ColPlus = new HTuple();
            }
            hv_ColPlus[hv_Index] = hv_Setcolplus;
        }

        HOperatorSet.CloseFile(hv_FileHandle);

        //**Log 文件创建***

        HOperatorSet.OpenFile("matching_fengwu_result.csv", "output", out hv_FileHandle);


        //**模板读入***

        HOperatorSet.ReadShapeModel("god_fengwu/01_MOVIE_O.shm", out hv_ModelID1);
        HOperatorSet.ReadShapeModel("god_fengwu/02_MENU_M.shm", out hv_ModelID2);
        HOperatorSet.ReadShapeModel("god_fengwu/03_MOVIE_E.shm", out hv_ModelID3);
        HOperatorSet.ReadShapeModel("god_fengwu/04_jiajian.shm", out hv_ModelID4);
        HOperatorSet.ReadShapeModel("god_fengwu/05_C.shm", out hv_ModelID5);
        HOperatorSet.ReadShapeModel("god_fengwu/06_MOVIE_V.shm", out hv_ModelID6);

        hv_ModelIDs = new HTuple();
        hv_ModelIDs = hv_ModelIDs.TupleConcat(hv_ModelID1);
        hv_ModelIDs = hv_ModelIDs.TupleConcat(hv_ModelID2);
        hv_ModelIDs = hv_ModelIDs.TupleConcat(hv_ModelID3);
        hv_ModelIDs = hv_ModelIDs.TupleConcat(hv_ModelID4);
        hv_ModelIDs = hv_ModelIDs.TupleConcat(hv_ModelID5);
        hv_ModelIDs = hv_ModelIDs.TupleConcat(hv_ModelID6);


        ho_Image.Dispose();
        HOperatorSet.ReadImage(out ho_Image, "fw10.bmp");
        HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
        dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle);
        HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);
        ho_GrayImage3.Dispose();
        HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage3);
        ho_ImageGauss.Dispose();
        HOperatorSet.GaussImage(ho_Image, out ho_ImageGauss, 5);

        HTuple end_val61  = hv_CheckNumber - 1;
        HTuple step_val61 = 1;

        int idx = 0;

        for (hv_Index2 = 0; hv_Index2.Continue(end_val61, step_val61); hv_Index2 = hv_Index2.TupleAdd(step_val61))
        {
            //Matching 01: ************************************************
            //Matching 01: BEGIN of generated code for model initialization
            //Matching 01: ************************************************
            HOperatorSet.SetSystem("border_shape_models", "false");
            //
            //Matching 01: Read the shape model from file
            //
            //Matching 01: Get the model contour for transforming it later into the image
            ho_ModelContours.Dispose();
            HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelIDs.TupleSelect(
                                                   hv_Index2), 1);
            //
            //Matching 01: Set a reference position to show the model
            HOperatorSet.SmallestRectangle1Xld(ho_ModelContours, out hv_Row1, out hv_Column1,
                                               out hv_Row2, out hv_Column2);
            hv_RefRow    = ((hv_Row2.TupleMax()) - (hv_Row1.TupleMin())) / 2;
            hv_RefColumn = ((hv_Column2.TupleMax()) - (hv_Column1.TupleMin())) / 2;
            HOperatorSet.VectorAngleToRigid(0, 0, 0, hv_RefRow, hv_RefColumn, 0, out hv_HomMat2D);
            ho_TransContours.Dispose();
            HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_TransContours,
                                               hv_HomMat2D);
            //
            //Matching 01: Display the model contours
            HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green");
            HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "margin");
            HOperatorSet.DispObj(ho_TransContours, hv_ExpDefaultWinHandle);
            //
            //Matching 01: END of generated code for model initialization
            //Matching 01:  * * * * * * * * * * * * * * * * * * * * * * *
            //Matching 01: BEGIN of generated code for model application
            //
            //Matching 01: Loop over all specified test images

            //Matching 01: Find the model
            HOperatorSet.FindShapeModel(ho_ImageGauss, hv_ModelIDs.TupleSelect(hv_Index2),
                                        (new HTuple(0)).TupleRad(), (new HTuple(360)).TupleRad(), hv_Minspec.TupleSelect(
                                            hv_Index2), hv_Setnumber.TupleSelect(hv_Index2), 0.5, "least_squares",
                                        (new HTuple(3)).TupleConcat(1), hv_GreedinessNumber, out hv_Row, out hv_Column,
                                        out hv_Angle, out hv_Score);
            //
            //Matching 01: Transform the model contours into the detected positions
            HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);
            for (hv_I = 0; (int)hv_I <= (int)((new HTuple(hv_Score.TupleLength())) - 1); hv_I = (int)hv_I + 1)
            {
                HOperatorSet.HomMat2dIdentity(out hv_HomMat2D);
                HOperatorSet.HomMat2dRotate(hv_HomMat2D, hv_Angle.TupleSelect(hv_I), 0, 0,
                                            out hv_HomMat2D);
                HOperatorSet.HomMat2dTranslate(hv_HomMat2D, hv_Row.TupleSelect(hv_I), hv_Column.TupleSelect(
                                                   hv_I), out hv_HomMat2D);
                ho_TransContours.Dispose();
                HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_TransContours,
                                                   hv_HomMat2D);
                //HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green");
                // HOperatorSet.DispObj(ho_TransContours, hv_ExpDefaultWinHandle);
            }

            //
            //Matching 01: Clear model when done

            HOperatorSet.ClearShapeModel(hv_ModelIDs.TupleSelect(hv_Index2));
            //Matching 01: *******************************************
            //Matching 01: END of generated code for model application
            //Matching 01: *******************************************
            //
            //

            //**Matching  Log数据写入外部文件***


            if ((int)(new HTuple(hv_I.TupleEqual(hv_Setnumber.TupleSelect(hv_Index2)))) != 0)
            {
                HOperatorSet.ReadOcv(hv_ocvgod.TupleSelect(hv_Index2), out hv_OCVHandle);
                ho_Rectangle.Dispose();
                HOperatorSet.GenRectangle1(out ho_Rectangle, hv_Row - (hv_RowPlus.TupleSelect(
                                                                           hv_Index2)), hv_Column - (hv_ColPlus.TupleSelect(hv_Index2)), hv_Row + (hv_RowPlus.TupleSelect(
                                                                                                                                                       hv_Index2)), hv_Column + (hv_ColPlus.TupleSelect(hv_Index2)));
                ho_ImageReduced.Dispose();
                HOperatorSet.ReduceDomain(ho_ImageGauss, ho_Rectangle, out ho_ImageReduced
                                          );
                HOperatorSet.DoOcvSimple(ho_ImageReduced, hv_OCVHandle, hv_ocvchar.TupleSelect(
                                             hv_Index2), "true", "true", "true", "true", 5, out hv_Quality);
                cs_Quality[idx] = hv_Quality;
                cs_x[idx]       = hv_Column;
                cs_y[idx]       = hv_Row;
                idx++;

                HTuple end_val126  = hv_I;
                HTuple step_val126 = 1;
                for (hv_Index1 = 1; hv_Index1.Continue(end_val126, step_val126); hv_Index1 = hv_Index1.TupleAdd(step_val126))
                {
                    HOperatorSet.FwriteString(hv_FileHandle, ((((((((((((hv_CheckNO.TupleSelect(
                                                                             hv_Index2)) + new HTuple(",")) + (hv_GODS.TupleSelect(hv_Index2))) + new HTuple(",")) + "OK") + new HTuple(",")) + hv_I) + new HTuple(",")) + (hv_Score.TupleSelect(
                                                                                                                                                                                                                                hv_Index1 - 1))) + new HTuple(",")) + "Quality") + new HTuple(",")) + hv_Quality);
                    HOperatorSet.FnewLine(hv_FileHandle);
                }
            }
            else
            {
                HOperatorSet.FwriteString(hv_FileHandle, ((((((((hv_CheckNO.TupleSelect(hv_Index2)) + new HTuple(",")) + (hv_GODS.TupleSelect(
                                                                                                                              hv_Index2))) + new HTuple(",")) + "NG") + new HTuple(",")) + hv_I) + new HTuple(",")) + (hv_Setnumber.TupleSelect(
                                                                                                                                                                                                                           hv_Index2)));
                HOperatorSet.FnewLine(hv_FileHandle);
            }
        }


        HOperatorSet.CountSeconds(out hv_T2);
        HOperatorSet.FwriteString(hv_FileHandle, ("执行时间:" + (hv_T2 - hv_T1)) + "S");
        HOperatorSet.CloseFile(hv_FileHandle);

        ho_Image.Dispose();
        ho_GrayImage3.Dispose();
        ho_ImageGauss.Dispose();
        ho_ModelContours.Dispose();
        ho_TransContours.Dispose();
        ho_Rectangle.Dispose();
        ho_ImageReduced.Dispose();
    }
        //DateTime t1, t2, t3, t4,t5,t6,t7;
        private void action()
        {
            HObject ho_Rectangle, ho_ImageReduced, imagexz, retxz;
            HObject ho_Border, ho_Circle, ho_ImageReduced2, ho_Region, ho_RegionLines;
            HObject ho_Rectangle1 = null, ho_ImageReduced1 = null, ho_Border1 = null;
            HObject ho_SelectedContours = null;

            // Local control variables

            HTuple hv_Row1 = null, hv_Column1 = null, hv_Angle = null, hv_l1 = null, hv_l2 = null;
            HTuple hv_Score = null, hv_zj = null, hv_Area = null, hv_Row3 = null;
            HTuple hv_Column2 = null, hv_Phi1 = null, hv_zpj = null;
            HTuple hv_zcj = null, hv_zpr = null, hv_zpc = null, hv_zcr = null;
            HTuple hv_zcc = null, hv_Tp = null, hv_Tc = null, hv_Length = null;
            HTuple hv_dx = null, hv_dn = null, hv_dxr = null, hv_dxc = null;
            HTuple hv_dnr = null, hv_dnc = null, hv_i = null;
            //HTuple hv_Col = new HTuple(), hv_dis = new HTuple(), hv_dxi = new HTuple(), hv_Row2 = new HTuple();
            //HTuple hv_dni = new HTuple(), hv_Ix = new HTuple(), hv_In = new HTuple();
            //HTuple hv_Exception = new HTuple(), hv_j = new HTuple(), hv_jh = new HTuple();
            HTuple hv_disdxpx = null;
            HTuple hv_disdnpx = null;
            HTuple hv_djd = null, hv_djdr = null, hv_djdc = null, hv_djx = null;
            HTuple hv_djxr = null, hv_djxc = null, hv_xjd = null, hv_xjdr = null;
            HTuple hv_xjdc = null, hv_xjx = null, hv_xjxr = null, hv_xjxc = null;
            HTuple hv_ljd = null, hv_ljx = null, hv_dj = null, hv_xj = null;
            HTuple hv_djn = null, hv_djm = null, hv_xjn = null, hv_xjm = null;
            HTuple hv_djzd = null, hv_djzx = null, hv_xjzd = null;
            HTuple hv_xjzx = null, hv_ljtd = null, hv_ljtx = null;
            HTuple hv_Length3 = null, hv_Length4 = null, hv_ljdx = null;
            HTuple hv_ljdn = null, hv_ljxx = null, hv_ljxn = null;
            HTuple hv_ljdm = null, hv_ljxm = null, hv_djiaodu = null;
            HTuple hv_xjiaodu = null, hv_Length5 = null, hv_Length6 = null;
            HTuple hv_djiaodu1 = null, hv_xjiaodu1 = null, hv_jjdd = null;
            HTuple hv_jjdx = null, hv_jjxd = null, hv_jjxx = null;
            HTuple hv_jjdm = null, hv_jjxm = null;

            // Initialize local and output iconic variables

            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Border);
            HOperatorSet.GenEmptyObj(out ho_Circle);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced2);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_Rectangle1);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced1);
            HOperatorSet.GenEmptyObj(out ho_Border1);
            HOperatorSet.GenEmptyObj(out ho_SelectedContours);
            HOperatorSet.GenEmptyObj(out ho_RegionLines);
            HOperatorSet.GenEmptyObj(out imagexz);
            HOperatorSet.GenEmptyObj(out retxz);
            HOperatorSet.Union1(algorithm.Region, out RegionToDisp);
            //t3 = DateTime.Now;
            try
            {
                if (hv_ModelID == null)
                {
                    HOperatorSet.ReadShapeModel(PathHelper.currentProductPath + @"\lwspmd.shm", out hv_ModelID);
                }
                retxz.Dispose();
                HOperatorSet.GenRectangle2(out retxz, hv1_Row, hv1_Column, hv1_Phi, hv1_Length1, hv1_Length2);
                imagexz.Dispose();
                HOperatorSet.ReduceDomain(Image, retxz, out imagexz);
                HOperatorSet.FindShapeModel(imagexz, hv_ModelID, -3.14, 6.29, 0.65, 0, 0.5,
                                            "least_squares", 0, 0.8, out hv_Row1, out hv_Column1, out hv_Angle, out hv_Score);
                hv_zj = (((((hv_Row1.TupleMax()) - (hv_Row1.TupleMin())) * ((hv_Row1.TupleMax()
                                                                             ) - (hv_Row1.TupleMin()))) + (((hv_Column1.TupleMax()) - (hv_Column1.TupleMin()
                                                                                                                                       )) * ((hv_Column1.TupleMax()) - (hv_Column1.TupleMin()))))).TupleSqrt();
                ho_Circle.Dispose();
                HOperatorSet.GenCircle(out ho_Circle, hv_Row1.TupleMean(), hv_Column1.TupleMean()
                                       , hv_zj / 2);
                ho_ImageReduced2.Dispose();
                HOperatorSet.ReduceDomain(Image, ho_Circle, out ho_ImageReduced2);
                ho_Region.Dispose();
                HOperatorSet.Threshold(ho_ImageReduced2, out ho_Region, 0, thv);
                HOperatorSet.AreaCenter(ho_Region, out hv_Area, out hv_Row3, out hv_Column2);
                HOperatorSet.OrientationRegion(ho_Region, out hv_Phi1);
                hv_zpj = hv_Phi1.Clone();
                hv_zcj = hv_Phi1 + ((new HTuple(90)).TupleRad());
                hv_zpr = hv_Row3 + ((hv_zcj.TupleSin()) * hv_zj);
                hv_zpc = hv_Column2 - ((hv_zcj.TupleCos()) * hv_zj);
                hv_zcr = hv_Row3 + ((hv_zpj.TupleSin()) * hv_zj);
                hv_zcc = hv_Row3 - ((hv_zpj.TupleCos()) * hv_zj);
                HOperatorSet.TupleTan(hv_zpj, out hv_Tp);
                HOperatorSet.TupleTan(hv_zcj, out hv_Tc);
                HOperatorSet.TupleLength(hv_Row1, out hv_Length);
                hv_dx  = new HTuple();
                hv_dn  = new HTuple();
                hv_dxr = new HTuple();
                hv_dxc = new HTuple();
                hv_dnr = new HTuple();
                hv_dnc = new HTuple();
                HTuple end_val29  = hv_Length - 1;
                HTuple step_val29 = 1;
                for (hv_i = 0; hv_i.Continue(end_val29, step_val29); hv_i = hv_i.TupleAdd(step_val29))
                {
                    //try


                    ho_Rectangle1.Dispose();
                    HOperatorSet.GenRectangle2(out ho_Rectangle1, hv_Row1.TupleSelect(hv_i),
                                               hv_Column1.TupleSelect(hv_i), hv_Phi + (hv_Angle.TupleSelect(hv_i)),
                                               hv_Length1, hv_Length2);
                    ho_ImageReduced1.Dispose();
                    HOperatorSet.ReduceDomain(Image, ho_Rectangle1, out ho_ImageReduced1
                                              );
                    ho_Border1.Dispose();
                    HOperatorSet.ThresholdSubPix(ho_ImageReduced1, out ho_Border1, thv);
                    ho_SelectedContours.Dispose();
                    HOperatorSet.SelectContoursXld(ho_Border1, out ho_SelectedContours, "contour_length",
                                                   30, 2000000, -0.5, 0.5);
                    HOperatorSet.UnionAdjacentContoursXld(ho_SelectedContours, out ho_SelectedContours, 10, 1, "attr_keep");
                    HOperatorSet.GetContourXld(ho_SelectedContours, out hv_Row2, out hv_Col);
                    hv_dis = ((((((hv_Tp * hv_Col) + hv_Row2) - (hv_Tp * hv_zpc)) - hv_zpr) / (((1 + (hv_Tp * hv_Tp))).TupleSqrt()
                                                                                               ))).TupleAbs();
                    hv_dxi = hv_dis.TupleMax();
                    hv_dni = hv_dis.TupleMin();
                    HOperatorSet.TupleFind(hv_dis, hv_dxi, out hv_Ix);
                    HOperatorSet.TupleFind(hv_dis, hv_dni, out hv_In);
                    hv_dx  = hv_dx.TupleConcat(hv_dxi);
                    hv_dn  = hv_dn.TupleConcat(hv_dni);
                    hv_dxr = hv_dxr.TupleConcat(hv_Row2.TupleSelect(hv_Ix));
                    hv_dxc = hv_dxc.TupleConcat(hv_Col.TupleSelect(hv_Ix));
                    hv_dnr = hv_dnr.TupleConcat(hv_Row2.TupleSelect(hv_In));
                    hv_dnc = hv_dnc.TupleConcat(hv_Col.TupleSelect(hv_In));
                    //catch (Exception)
                    //endtry
                    //stop ()
                }
                hv_disdxpx = ((((((hv_Tc * hv_dxc) + hv_dxr) - (hv_Tc * hv_zcc)) - hv_zcr) / (((1 + (hv_Tc * hv_Tc))).TupleSqrt()
                                                                                              ))).TupleAbs();
                hv_disdnpx = ((((((hv_Tc * hv_dnc) + hv_dnr) - (hv_Tc * hv_zcc)) - hv_zcr) / (((1 + (hv_Tc * hv_Tc))).TupleSqrt()
                                                                                              ))).TupleAbs();
                HTuple end_val55  = hv_Length - 2;
                HTuple step_val55 = 1;
                //t1 = DateTime.Now;
                for (hv_i = 0; hv_i.Continue(end_val55, step_val55); hv_i = hv_i.TupleAdd(step_val55))
                {
                    //try


                    HTuple end_val59  = hv_Length - 1;
                    HTuple step_val59 = 1;
                    for (hv_j = hv_i + 1; hv_j.Continue(end_val59, step_val59); hv_j = hv_j.TupleAdd(step_val59))
                    {
                        if ((int)(new HTuple(((hv_disdxpx.TupleSelect(hv_i))).TupleGreater(hv_disdxpx.TupleSelect(
                                                                                               hv_j)))) != 0)
                        {
                            hv_jh = hv_disdxpx.TupleSelect(hv_i);
                            if (hv_disdxpx == null)
                            {
                                hv_disdxpx = new HTuple();
                            }
                            hv_disdxpx[hv_i] = hv_disdxpx.TupleSelect(hv_j);
                            if (hv_disdxpx == null)
                            {
                                hv_disdxpx = new HTuple();
                            }
                            hv_disdxpx[hv_j] = hv_jh;
                            hv_jh            = hv_dx.TupleSelect(hv_i);
                            if (hv_dx == null)
                            {
                                hv_dx = new HTuple();
                            }
                            hv_dx[hv_i] = hv_dx.TupleSelect(hv_j);
                            if (hv_dx == null)
                            {
                                hv_dx = new HTuple();
                            }
                            hv_dx[hv_j] = hv_jh;
                            hv_jh       = hv_dxr.TupleSelect(hv_i);
                            if (hv_dxr == null)
                            {
                                hv_dxr = new HTuple();
                            }
                            hv_dxr[hv_i] = hv_dxr.TupleSelect(hv_j);
                            if (hv_dxr == null)
                            {
                                hv_dxr = new HTuple();
                            }
                            hv_dxr[hv_j] = hv_jh;
                            hv_jh        = hv_dxc.TupleSelect(hv_i);
                            if (hv_dxc == null)
                            {
                                hv_dxc = new HTuple();
                            }
                            hv_dxc[hv_i] = hv_dxc.TupleSelect(hv_j);
                            if (hv_dxc == null)
                            {
                                hv_dxc = new HTuple();
                            }
                            hv_dxc[hv_j] = hv_jh;
                        }
                        if ((int)(new HTuple(((hv_disdnpx.TupleSelect(hv_i))).TupleGreater(hv_disdnpx.TupleSelect(
                                                                                               hv_j)))) != 0)
                        {
                            hv_jh = hv_disdnpx.TupleSelect(hv_i);
                            if (hv_disdnpx == null)
                            {
                                hv_disdnpx = new HTuple();
                            }
                            hv_disdnpx[hv_i] = hv_disdnpx.TupleSelect(hv_j);
                            if (hv_disdnpx == null)
                            {
                                hv_disdnpx = new HTuple();
                            }
                            hv_disdnpx[hv_j] = hv_jh;
                            hv_jh            = hv_dn.TupleSelect(hv_i);
                            if (hv_dn == null)
                            {
                                hv_dn = new HTuple();
                            }
                            hv_dn[hv_i] = hv_dn.TupleSelect(hv_j);
                            if (hv_dn == null)
                            {
                                hv_dn = new HTuple();
                            }
                            hv_dn[hv_j] = hv_jh;
                            hv_jh       = hv_dnr.TupleSelect(hv_i);
                            if (hv_dnr == null)
                            {
                                hv_dnr = new HTuple();
                            }
                            hv_dnr[hv_i] = hv_dnr.TupleSelect(hv_j);
                            if (hv_dnr == null)
                            {
                                hv_dnr = new HTuple();
                            }
                            hv_dnr[hv_j] = hv_jh;
                            hv_jh        = hv_dnc.TupleSelect(hv_i);
                            if (hv_dnc == null)
                            {
                                hv_dnc = new HTuple();
                            }
                            hv_dnc[hv_i] = hv_dnc.TupleSelect(hv_j);
                            if (hv_dnc == null)
                            {
                                hv_dnc = new HTuple();
                            }
                            hv_dnc[hv_j] = hv_jh;
                        }
                    }
                    //catch (Exception)
                    //endtry
                }
                //t2 = DateTime.Now;
                hv_djd  = new HTuple();
                hv_djdr = new HTuple();
                hv_djdc = new HTuple();
                hv_djx  = new HTuple();
                hv_djxr = new HTuple();
                hv_djxc = new HTuple();
                hv_xjd  = new HTuple();
                hv_xjdr = new HTuple();
                hv_xjdc = new HTuple();
                hv_xjx  = new HTuple();
                hv_xjxr = new HTuple();
                hv_xjxc = new HTuple();
                hv_ljd  = new HTuple();
                hv_ljx  = new HTuple();
                HTuple end_val107  = hv_Length - 1;
                HTuple step_val107 = 1;
                for (hv_i = 0; hv_i.Continue(end_val107, step_val107); hv_i = hv_i.TupleAdd(step_val107))
                {
                    if ((int)(new HTuple(((hv_dx.TupleSelect(hv_i))).TupleGreater(hv_zj))) != 0)
                    {
                        for (hv_j = -3; (int)hv_j <= 3; hv_j = (int)hv_j + 1)
                        {
                            if (((hv_i + hv_j) >= 0) && ((hv_i + hv_j) <= end_val107))
                            {
                                if ((int)((new HTuple(((hv_disdnpx.TupleSelect(hv_i + hv_j))).TupleGreater(
                                                          hv_disdxpx.TupleSelect(hv_i)))).TupleAnd(new HTuple(((hv_dn.TupleSelect(
                                                                                                                    hv_i + hv_j))).TupleLess(hv_zj)))) != 0)
                                {
                                    hv_ljd  = hv_ljd.TupleConcat(hv_disdxpx.TupleSelect(hv_i));
                                    hv_djd  = hv_djd.TupleConcat(hv_dx.TupleSelect(hv_i));
                                    hv_djdr = hv_djdr.TupleConcat(hv_dxr.TupleSelect(hv_i));
                                    hv_djdc = hv_djdc.TupleConcat(hv_dxc.TupleSelect(hv_i));
                                    hv_djx  = hv_djx.TupleConcat(hv_dn.TupleSelect(hv_i + hv_j));
                                    hv_djxr = hv_djxr.TupleConcat(hv_dnr.TupleSelect(hv_i + hv_j));
                                    hv_djxc = hv_djxc.TupleConcat(hv_dnc.TupleSelect(hv_i + hv_j));
                                    break;
                                }
                            }
                            // catch (Exception)
                            //catch (HalconException)
                            //{
                            //    //HDevExpDefaultException1.ToHTuple(out hv_Exception);
                            //}
                        }
                    }

                    if ((int)(new HTuple(((hv_dn.TupleSelect(hv_i))).TupleGreater(hv_zj))) != 0)
                    {
                        for (hv_j = -3; (int)hv_j <= 3; hv_j = (int)hv_j + 1)
                        {
                            //try
                            if (((hv_i + hv_j) >= 0) && ((hv_i + hv_j) <= end_val107))
                            {
                                if ((int)((new HTuple(((hv_disdxpx.TupleSelect(hv_i + hv_j))).TupleGreater(
                                                          hv_disdnpx.TupleSelect(hv_i)))).TupleAnd(new HTuple(((hv_dx.TupleSelect(
                                                                                                                    hv_i + hv_j))).TupleLess(hv_zj)))) != 0)
                                {
                                    hv_ljx  = hv_ljx.TupleConcat(hv_disdnpx.TupleSelect(hv_i));
                                    hv_xjd  = hv_xjd.TupleConcat(hv_dn.TupleSelect(hv_i));
                                    hv_xjdr = hv_xjdr.TupleConcat(hv_dnr.TupleSelect(hv_i));
                                    hv_xjdc = hv_xjdc.TupleConcat(hv_dnc.TupleSelect(hv_i));
                                    hv_xjx  = hv_xjx.TupleConcat(hv_dx.TupleSelect(hv_i + hv_j));
                                    hv_xjxr = hv_xjxr.TupleConcat(hv_dxr.TupleSelect(hv_i + hv_j));
                                    hv_xjxc = hv_xjxc.TupleConcat(hv_dxc.TupleSelect(hv_i + hv_j));
                                    break;
                                }
                            }
                            // catch (Exception)
                            //catch (HalconException)
                            //{
                            //    //HDevExpDefaultException1.ToHTuple(out hv_Exception);
                            //}
                        }
                    }
                }
                HOperatorSet.TupleLength(hv_ljd, out hv_l1);
                HTuple end_val153  = 1;
                HTuple step_val153 = -1;
                for (hv_i = hv_l1 - 1; hv_i.Continue(end_val153, step_val153); hv_i = hv_i.TupleAdd(step_val153))
                {
                    if ((int)(new HTuple(((hv_ljd.TupleSelect(hv_i))).TupleEqual(hv_ljd.TupleSelect(
                                                                                     hv_i - 1)))) != 0)
                    {
                        HOperatorSet.TupleRemove(hv_ljd, hv_i, out hv_ljd);
                        HOperatorSet.TupleRemove(hv_djd, hv_i, out hv_djd);
                        HOperatorSet.TupleRemove(hv_djdr, hv_i, out hv_djdr);
                        HOperatorSet.TupleRemove(hv_djdc, hv_i, out hv_djdc);
                        HOperatorSet.TupleRemove(hv_djx, hv_i, out hv_djx);
                        HOperatorSet.TupleRemove(hv_djxr, hv_i, out hv_djxr);
                        HOperatorSet.TupleRemove(hv_djxc, hv_i, out hv_djxc);
                    }
                }
                HOperatorSet.TupleLength(hv_ljx, out hv_l2);
                HTuple end_val165  = 1;
                HTuple step_val165 = -1;
                for (hv_i = hv_l2 - 1; hv_i.Continue(end_val165, step_val165); hv_i = hv_i.TupleAdd(step_val165))
                {
                    if ((int)(new HTuple(((hv_ljd.TupleSelect(hv_i))).TupleEqual(hv_ljd.TupleSelect(
                                                                                     hv_i - 1)))) != 0)
                    {
                        HOperatorSet.TupleRemove(hv_ljx, hv_i, out hv_ljx);
                        HOperatorSet.TupleRemove(hv_xjd, hv_i, out hv_xjd);
                        HOperatorSet.TupleRemove(hv_xjdr, hv_i, out hv_xjdr);
                        HOperatorSet.TupleRemove(hv_xjdc, hv_i, out hv_xjdc);
                        HOperatorSet.TupleRemove(hv_xjx, hv_i, out hv_xjx);
                        HOperatorSet.TupleRemove(hv_xjxr, hv_i, out hv_xjxr);
                        HOperatorSet.TupleRemove(hv_xjxc, hv_i, out hv_xjxc);
                    }
                }
                HOperatorSet.TupleLength(hv_ljx, out hv_l2);
                HOperatorSet.TupleLength(hv_ljd, out hv_l1);
                //t5 = DateTime.Now;
                hv_dj   = hv_djd - hv_djx;
                hv_xj   = hv_xjd - hv_xjx;
                hv_djx  = hv_dj.TupleMax();
                hv_djn  = hv_dj.TupleMin();
                hv_djm  = hv_dj.TupleMean();
                hv_xjx  = hv_xj.TupleMax();
                hv_xjn  = hv_xj.TupleMin();
                hv_xjm  = hv_xj.TupleMean();
                hv_djzd = (hv_djd.TupleMax()) - (hv_djx.TupleMin());
                hv_djzx = (hv_djd.TupleMin()) - (hv_djx.TupleMax());
                hv_xjzd = (hv_xjd.TupleMax()) - (hv_xjx.TupleMin());
                hv_xjzx = (hv_xjd.TupleMin()) - (hv_xjx.TupleMax());
                hv_ljtd = (hv_ljd.TupleConcat(0)) - ((new HTuple(0)).TupleConcat(hv_ljd));
                hv_ljtx = (hv_ljx.TupleConcat(0)) - ((new HTuple(0)).TupleConcat(hv_ljx));
                HOperatorSet.TupleLength(hv_ljtd, out hv_Length3);
                HOperatorSet.TupleLength(hv_ljtx, out hv_Length4);
                hv_ljtd = hv_ljtd.TupleSelectRange(1, hv_Length3 - 2);
                hv_ljtx = hv_ljtx.TupleSelectRange(1, hv_Length4 - 2);
                hv_ljdx = hv_ljtd.TupleMax();
                hv_ljdn = hv_ljtd.TupleMin();
                hv_ljxx = hv_ljtx.TupleMax();
                hv_ljxn = hv_ljtx.TupleMin();
                hv_ljdm = hv_ljtd.TupleMean();
                hv_ljxm = hv_ljtx.TupleMean();
                HOperatorSet.TupleAtan((hv_djxr - hv_djdr) / (hv_djdc - hv_djxc), out hv_djiaodu);
                HOperatorSet.TupleAtan((hv_xjxr - hv_xjdr) / (hv_xjdc - hv_xjxc), out hv_xjiaodu);
                HOperatorSet.TupleLength(hv_djiaodu, out hv_Length5);
                HOperatorSet.TupleLength(hv_xjiaodu, out hv_Length6);
                hv_djiaodu1 = ((hv_djiaodu - hv_zcj)).TupleAbs();
                hv_xjiaodu1 = ((hv_xjiaodu - hv_zcj)).TupleAbs();
                HTuple end_val177  = hv_Length5 - 1;
                HTuple step_val177 = 1;
                //t6 = DateTime.Now;
                for (hv_i = 0; hv_i.Continue(end_val177, step_val177); hv_i = hv_i.TupleAdd(step_val177))
                {
                    while ((int)(new HTuple(hv_djiaodu1.TupleGreater((new HTuple(90)).TupleRad()
                                                                     ))) != 0)
                    {
                        hv_djiaodu1 = hv_djiaodu1 - ((new HTuple(90)).TupleRad());
                    }
                    if ((int)(new HTuple(hv_djiaodu1.TupleGreater((new HTuple(45)).TupleRad()
                                                                  ))) != 0)
                    {
                        hv_djiaodu1 = (-hv_djiaodu1) + ((new HTuple(90)).TupleRad());
                    }
                }
                HTuple end_val185  = hv_Length6 - 1;
                HTuple step_val185 = 1;
                for (hv_i = 0; hv_i.Continue(end_val185, step_val185); hv_i = hv_i.TupleAdd(step_val185))
                {
                    while ((int)(new HTuple(hv_xjiaodu1.TupleGreater((new HTuple(90)).TupleRad()
                                                                     ))) != 0)
                    {
                        hv_xjiaodu1 = hv_xjiaodu1 - ((new HTuple(90)).TupleRad());
                    }
                    if ((int)(new HTuple(hv_xjiaodu1.TupleGreater((new HTuple(45)).TupleRad()
                                                                  ))) != 0)
                    {
                        hv_xjiaodu1 = (-hv_xjiaodu1) + ((new HTuple(90)).TupleRad());
                    }
                }
                hv_jjdd = hv_djiaodu1.TupleMax().TupleDeg();
                hv_jjdx = hv_djiaodu1.TupleMin().TupleDeg();
                hv_jjxd = hv_xjiaodu1.TupleMax().TupleDeg();
                hv_jjxx = hv_xjiaodu1.TupleMin().TupleDeg();
                hv_jjdm = hv_djiaodu1.TupleMean().TupleDeg();
                hv_jjxm = hv_xjiaodu1.TupleMean().TupleDeg();
                ho_RegionLines.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLines, hv_djdr, hv_djdc, hv_djxr, hv_djxc);
                HOperatorSet.Union2(ho_RegionLines, ho_RegionLines, out RegionToDisp);
                HTuple hv_result = GetHv_result();
                hv_result = hv_result.TupleConcat("螺纹条数1");
                hv_result = hv_result.TupleConcat(hv_l1);
                hv_result = hv_result.TupleConcat("螺纹条数2");
                hv_result = hv_result.TupleConcat(hv_l2);
                hv_result = hv_result.TupleConcat("大径最大值");
                hv_result = hv_result.TupleConcat(hv_djx.D * pixeldist);
                hv_result = hv_result.TupleConcat("大径最小值");
                hv_result = hv_result.TupleConcat(hv_djn.D * pixeldist);
                hv_result = hv_result.TupleConcat("大径平均值");
                hv_result = hv_result.TupleConcat(hv_djm.D * pixeldist);
                hv_result = hv_result.TupleConcat("小径最大值");
                hv_result = hv_result.TupleConcat(hv_xjx.D * pixeldist);
                hv_result = hv_result.TupleConcat("小径最小值");
                hv_result = hv_result.TupleConcat(hv_xjn.D * pixeldist);
                hv_result = hv_result.TupleConcat("小径平均值");
                hv_result = hv_result.TupleConcat(hv_xjm.D * pixeldist);
                hv_result = hv_result.TupleConcat("整体大径最大值");
                hv_result = hv_result.TupleConcat(hv_djzd.D * pixeldist);
                hv_result = hv_result.TupleConcat("整体大径最小值");
                hv_result = hv_result.TupleConcat(hv_djzx.D * pixeldist);
                hv_result = hv_result.TupleConcat("整体小径最大值");
                hv_result = hv_result.TupleConcat(hv_xjzd.D * pixeldist);
                hv_result = hv_result.TupleConcat("整体小径最小值");
                hv_result = hv_result.TupleConcat(hv_xjzx.D * pixeldist);
                hv_result = hv_result.TupleConcat("螺距最大值");
                hv_result = hv_result.TupleConcat(hv_ljdx.D * pixeldist);
                hv_result = hv_result.TupleConcat("螺距最小值");
                hv_result = hv_result.TupleConcat(hv_ljdn.D * pixeldist);
                hv_result = hv_result.TupleConcat("螺距平均值");
                hv_result = hv_result.TupleConcat(hv_ljdm.D * pixeldist);
                hv_result = hv_result.TupleConcat("角度最大值");
                hv_result = hv_result.TupleConcat(hv_jjdd.D);
                hv_result = hv_result.TupleConcat("角度最大值");
                hv_result = hv_result.TupleConcat(hv_jjdx.D);
                hv_result = hv_result.TupleConcat("角度平均值");
                hv_result = hv_result.TupleConcat(hv_jjdm.D);
                result    = hv_result.Clone();

                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Border.Dispose();
                ho_Circle.Dispose();
                ho_ImageReduced2.Dispose();
                ho_Region.Dispose();
                ho_Rectangle1.Dispose();
                ho_ImageReduced1.Dispose();
                ho_Border1.Dispose();
                ho_SelectedContours.Dispose();
                imagexz.Dispose();
                retxz.Dispose();
                ho_RegionLines.Dispose();
                algorithm.Region.Dispose();
                //t4 = DateTime.Now;
            }
            catch
            {
                HTuple hv_result = GetHv_result();
                hv_result = hv_result.TupleConcat("螺纹条数1");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("螺纹条数2");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("大径最大值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("大径最小值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("大径平均值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("小径最大值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("小径最小值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("小径平均值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("整体大径最大值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("整体大径最小值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("整体小径最大值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("整体小径最小值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("螺距最大值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("螺距最小值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("螺距平均值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("角度最大值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("角度最大值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("角度平均值");
                hv_result = hv_result.TupleConcat(0);
                result    = hv_result.Clone();
                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Border.Dispose();
                ho_Circle.Dispose();
                ho_ImageReduced2.Dispose();
                ho_Region.Dispose();
                ho_Rectangle1.Dispose();
                ho_ImageReduced1.Dispose();
                ho_Border1.Dispose();
                ho_SelectedContours.Dispose();
                imagexz.Dispose();
                retxz.Dispose();
                ho_RegionLines.Dispose();
                algorithm.Region.Dispose();
            }
        }
예제 #20
0
        private void action()
        {
            HObject ho_Rectangle, ho_ImageReduced;
            HObject ho_Border, ho_Region, ho_RegionUnion;
            HTuple  hv_RowBegin = null;
            HTuple  hv_ColBegin = null, hv_RowEnd = null, hv_ColEnd = null;
            HTuple  hv_Nr = null, hv_Nc = null, hv_Dist = null, hv_dis = null;
            HTuple  hv_Length = null, hv_i = null, hv_j = new HTuple();
            HTuple  hv_temp = new HTuple(), hv_dianr = null, hv_dianc = null;
            HTuple  hv_t = null, hv_jl = null;

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Border);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_RegionUnion);
            try
            {
                ho_Rectangle.Dispose();
                HOperatorSet.GenRectangle2(out ho_Rectangle, hv_centerRowm, hv_centerColumnm, hv_Phim, hv_Length1m,
                                           hv_Length2m);
                ho_ImageReduced.Dispose();
                HOperatorSet.ReduceDomain(Image, ho_Rectangle, out ho_ImageReduced);
                ho_Border.Dispose();
                HOperatorSet.ThresholdSubPix(ho_ImageReduced, out ho_Border, thv);
                HOperatorSet.FitLineContourXld(ho_Border, "tukey", -1, 0, 5, 2, out hv_RowBegin,
                                               out hv_ColBegin, out hv_RowEnd, out hv_ColEnd, out hv_Nr, out hv_Nc, out hv_Dist);
                hv_dis = ((((hv_RowBegin - hv_RowEnd) * (hv_RowBegin - hv_RowEnd)) + ((hv_ColBegin - hv_ColEnd) * (hv_ColBegin - hv_ColEnd)))).TupleSqrt()
                ;
                HOperatorSet.TupleLength(hv_dis, out hv_Length);
                if (hv_Length.I >= 2)
                {
                    HTuple end_val8  = hv_Length - 2;
                    HTuple step_val8 = 1;
                    for (hv_i = 0; hv_i.Continue(end_val8, step_val8); hv_i = hv_i.TupleAdd(step_val8))
                    {
                        HTuple end_val9  = hv_Length - 1;
                        HTuple step_val9 = 1;
                        for (hv_j = hv_i + 1; hv_j.Continue(end_val9, step_val9); hv_j = hv_j.TupleAdd(step_val9))
                        {
                            if ((int)(new HTuple(((hv_dis.TupleSelect(hv_i))).TupleLess(hv_dis.TupleSelect(
                                                                                            hv_j)))) != 0)
                            {
                                hv_temp = hv_dis.TupleSelect(hv_i);
                                if (hv_dis == null)
                                {
                                    hv_dis = new HTuple();
                                }
                                hv_dis[hv_i] = hv_dis.TupleSelect(hv_j);
                                if (hv_dis == null)
                                {
                                    hv_dis = new HTuple();
                                }
                                hv_dis[hv_j] = hv_temp;
                                hv_temp      = hv_RowBegin.TupleSelect(hv_i);
                                if (hv_RowBegin == null)
                                {
                                    hv_RowBegin = new HTuple();
                                }
                                hv_RowBegin[hv_i] = hv_RowBegin.TupleSelect(hv_j);
                                if (hv_RowBegin == null)
                                {
                                    hv_RowBegin = new HTuple();
                                }
                                hv_RowBegin[hv_j] = hv_temp;
                                hv_temp           = hv_ColBegin.TupleSelect(hv_i);
                                if (hv_ColBegin == null)
                                {
                                    hv_ColBegin = new HTuple();
                                }
                                hv_ColBegin[hv_i] = hv_ColBegin.TupleSelect(hv_j);
                                if (hv_ColBegin == null)
                                {
                                    hv_ColBegin = new HTuple();
                                }
                                hv_ColBegin[hv_j] = hv_temp;
                                hv_temp           = hv_RowEnd.TupleSelect(hv_i);
                                if (hv_RowEnd == null)
                                {
                                    hv_RowEnd = new HTuple();
                                }
                                hv_RowEnd[hv_i] = hv_RowEnd.TupleSelect(hv_j);
                                if (hv_RowEnd == null)
                                {
                                    hv_RowEnd = new HTuple();
                                }
                                hv_RowEnd[hv_j] = hv_temp;
                                hv_temp         = hv_ColEnd.TupleSelect(hv_i);
                                if (hv_ColEnd == null)
                                {
                                    hv_ColEnd = new HTuple();
                                }
                                hv_ColEnd[hv_i] = hv_ColEnd.TupleSelect(hv_j);
                                if (hv_ColEnd == null)
                                {
                                    hv_ColEnd = new HTuple();
                                }
                                hv_ColEnd[hv_j] = hv_temp;
                            }
                        }
                    }
                    hv_dianr = ((hv_RowBegin.TupleSelect(0)) + (hv_RowEnd.TupleSelect(0))) / 2;
                    hv_dianc = ((hv_ColBegin.TupleSelect(0)) + (hv_ColEnd.TupleSelect(0))) / 2;
                    hv_t     = ((-(hv_RowBegin.TupleSelect(1))) + (hv_RowEnd.TupleSelect(1))) / ((hv_ColBegin.TupleSelect(
                                                                                                      1)) - (hv_ColEnd.TupleSelect(1)));
                    hv_jl = ((((((hv_t * hv_dianc) + hv_dianr) - (hv_RowBegin.TupleSelect(1))) - ((hv_ColBegin.TupleSelect(
                                                                                                       1)) * hv_t)) / ((((hv_t * hv_t) + 1)).TupleSqrt()))).TupleAbs();
                    ho_Region.Dispose();
                    HOperatorSet.GenRegionContourXld(ho_Border, out ho_Region, "filled");
                    ho_RegionUnion.Dispose();
                    HOperatorSet.Union2(ho_Region, ho_Region, out RegionToDisp);
                }


                HTuple hv_result = GetHv_result();
                hv_result = hv_result.TupleConcat("ParallelDistance");
                hv_result = hv_result.TupleConcat(hv_jl.D * pixeldist);
                result    = hv_result.Clone();

                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Border.Dispose();
                ho_Region.Dispose();
                ho_RegionUnion.Dispose();
            }
            catch
            {
                HTuple hv_result = GetHv_result();
                hv_result = hv_result.TupleConcat("ParallelDistance");
                hv_result = hv_result.TupleConcat(0);
                result    = hv_result.Clone();

                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Border.Dispose();
                ho_Region.Dispose();
                ho_RegionUnion.Dispose();
            }
        }
예제 #21
0
        private void porosityVertical()
        {
            // Local iconic variables
            HObject ho_Image = null, ho_Rectangle = null, ho_Circle = null;
            HObject ho_RegionUnion = null, ho_ImageReduced = null, ho_ImageMean = null;
            HObject ho_Region = null, ho_ConnectedRegions = null, ho_RegionClosing = null;
            HObject ho_SmallConnection = null, ho_ContCircle = null;
            // Local control variables
            HTuple hv_AcqHandle = null, hv_found = null;
            HTuple hv_cnt = null, hv_bol = null, hv_porosity_area_px = null;
            HTuple hv_porosity_area_mm = null;
            HTuple hv_UsedThreshold = new HTuple(), hv_Circularity = new HTuple();
            HTuple hv_Area = new HTuple(), hv_Row = new HTuple(), hv_Column = new HTuple();
            HTuple hv_Length = new HTuple(), hv_circ_min = new HTuple();
            HTuple hv_area_min = new HTuple(), hv_index = new HTuple();
            HTuple hv_i = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_Circle);
            HOperatorSet.GenEmptyObj(out ho_RegionUnion);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_ImageMean);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_RegionClosing);
            HOperatorSet.GenEmptyObj(out ho_SmallConnection);
            HOperatorSet.GenEmptyObj(out ho_ContCircle);
            // Wait for CAM4 thread to be closed
            _waitHandleCam2.WaitOne();
            // Close te thread DOOR
            _waitHandleCam2.Reset();
            // Open camera frame
            HOperatorSet.OpenFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1, "default", -1, "false", "default", "GC3851MP_CAM_2", 0, -1, out hv_AcqHandle);
            HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "ExposureTime", 20000.0); // 20000
            // Information for PLC that frame is opened
            DetectionStart();
            hv_found = 0;
            hv_cnt   = 0;
            hv_bol   = 0;

            // Detection flag
            Porositydetectedver = false;

            while (Porositydetectedver == false)
            {
                ho_Image.Dispose();
                HOperatorSet.GrabImage(out ho_Image, hv_AcqHandle);
                ho_Rectangle.Dispose();
                HOperatorSet.GenRectangle1(out ho_Rectangle, 716, 720, 2200, 1850);
                //ho_Circle.Dispose();
                //HOperatorSet.GenCircle(out ho_Circle, 900, 2000, 600);
                //ho_RegionUnion.Dispose();
                //HOperatorSet.Union2(ho_Rectangle, ho_Circle, out ho_RegionUnion);
                ho_ImageReduced.Dispose();
                HOperatorSet.ReduceDomain(ho_Image, ho_Rectangle, out ho_ImageReduced);
                ho_ImageMean.Dispose();
                HOperatorSet.MeanImage(ho_ImageReduced, out ho_ImageMean, 21, 21);
                ho_Region.Dispose();
                HOperatorSet.BinaryThreshold(ho_ImageMean, out ho_Region, "max_separability", "dark", out hv_UsedThreshold);
                ho_ConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions);
                ho_RegionClosing.Dispose();
                HOperatorSet.ClosingCircle(ho_ConnectedRegions, out ho_RegionClosing, 17);
                ho_SmallConnection.Dispose();
                HOperatorSet.Connection(ho_RegionClosing, out ho_SmallConnection);
                HOperatorSet.Circularity(ho_SmallConnection, out hv_Circularity);
                HOperatorSet.AreaCenter(ho_SmallConnection, out hv_Area, out hv_Row, out hv_Column);
                HOperatorSet.TupleLength(hv_Row, out hv_Length);
                // Criteria for porosity
                hv_circ_min = 0.5;
                hv_area_min = 1500; //1500; //1000;
                hv_index    = 0;
                HTuple end_val44  = hv_Length;
                HTuple step_val44 = 1;

                for (hv_i = 1; hv_i.Continue(end_val44, step_val44); hv_i = hv_i.TupleAdd(step_val44))
                {
                    if ((int)((new HTuple(((hv_Circularity.TupleSelect(hv_i - 1))).TupleGreater(
                                              hv_circ_min))).TupleAnd(new HTuple(((hv_Area.TupleSelect(hv_i - 1))).TupleGreater(
                                                                                     hv_area_min)))) != 0)
                    {
                        hv_index = hv_i - 1;
                        hv_found = hv_found + 1;
                        hv_bol   = 1;
                        break;
                    }
                    else
                    {
                        hv_bol = 0;
                    }
                }

                //HOperatorSet.ClearWindow(hv_porosityWinHandle);
                //HOperatorSet.DispObj(ho_Image, hv_porosityWinHandle);

                if ((int)((new HTuple(hv_found.TupleGreater(0))).TupleAnd(new HTuple(hv_bol.TupleEqual(
                                                                                         1)))) != 0)
                {
                    PorosityIsDetected();
                    ho_ContCircle.Dispose();
                    //HOperatorSet.GenCircleContourXld(out ho_ContCircle, hv_Row.TupleSelect(hv_index), hv_Column.TupleSelect(hv_index), 50, 0, 6.28318, "positive", 1);
                    //HOperatorSet.DispObj(ho_ContCircle, hv_porosityWinHandle);
                    hv_found            = 0;
                    hv_cnt              = 0;
                    Porositydetectedver = true;
                }
                hv_cnt = hv_cnt + 1;
            }

            //HOperatorSet.ClearWindow(hv_porosityWinHandle);
            HOperatorSet.CloseFramegrabber(hv_AcqHandle);
            // Open the thread DOOR
            _waitHandleCam2.Set();
            // Dispose all iconic variables
            ho_Image.Dispose();
            ho_Rectangle.Dispose();
            ho_Circle.Dispose();
            ho_RegionUnion.Dispose();
            ho_ImageReduced.Dispose();
            ho_ImageMean.Dispose();
            ho_Region.Dispose();
            ho_ConnectedRegions.Dispose();
            ho_RegionClosing.Dispose();
            ho_SmallConnection.Dispose();
            ho_ContCircle.Dispose();
        }
        private void action()
        {
            HObject ho_Rectangle, ho_ImageReduced, ho_ConnectedRegions2, ho_ObjectSelected3, ho_cl;
            HObject ho_Regions, ho_ObjectSelected, ho_RegionFillUp;
            HObject ho_Contours, ho_Rectangle1, ho_Rectangle3, ho_RegionDifference;
            HObject ho_ImageReduced1, ho_Border, ho_SelectedContours = null;
            HObject ho_ObjectSelected1 = null, ho_ObjectSelected2 = null;
            HObject ho_ContoursSplit1 = null, ho_RegionLines = null, ho_Region = null;
            HObject ho_RegionIntersection = null, ho_ConnectedRegions = null;
            HObject ho_Circle = null, ho_RegionLines1 = null, ho_Region1 = null;
            HObject ho_RegionIntersection1 = null, ho_ConnectedRegions1 = null;
            HObject ho_Circle1 = null;

            // Local control variables


            HTuple hv_Row4 = null;
            HTuple hv_Column4 = null, hv_Phi2 = null, hv_Length12 = null;
            HTuple hv_Length22 = null, hv_Area2 = null, hv_Row5 = null;
            HTuple hv_Column5 = null, hv_p = null, hv_fx = new HTuple();
            HTuple hv_cz = new HTuple(), hv_dz = new HTuple(), hv_lll = null;
            HTuple hv_Mean = null, hv_Deviation1 = null, hv_Length8 = null;
            HTuple hv_Number = new HTuple(), hv_Rowo1 = new HTuple();
            HTuple hv_Colo1 = new HTuple(), hv_Rowo2 = new HTuple();
            HTuple hv_Colo2 = new HTuple(), hv_as = new HTuple(), hv_ac = new HTuple();
            HTuple hv_b = new HTuple(), hv_cs = new HTuple(), hv_cc = new HTuple();
            HTuple hv_ds1 = new HTuple(), hv_ds2 = new HTuple(), hv_dc1 = new HTuple();
            HTuple hv_dc2 = new HTuple(), hv_Length3 = new HTuple();
            HTuple hv_dd = new HTuple(), hv_Floor = new HTuple(), hv_Length4 = new HTuple();
            HTuple hv_Sequence1 = new HTuple(), hv_Length5 = new HTuple();
            HTuple hv_Sequence = new HTuple(), hv_Sequence2 = new HTuple();
            HTuple hv_Reduced1 = new HTuple(), hv_Reduced2 = new HTuple();
            HTuple hv_Reduced3 = new HTuple(), hv_Reduced4 = new HTuple();
            HTuple hv_d1 = new HTuple(), hv_Length6 = new HTuple();
            HTuple hv_Sequence3 = new HTuple(), hv_Area = new HTuple();
            HTuple hv_Row2 = new HTuple(), hv_Column2 = new HTuple();
            HTuple hv_Selectedc = new HTuple(), hv_Selectedr = new HTuple();
            HTuple hv_Newtuple = new HTuple(), hv_d2 = new HTuple();
            HTuple hv_Length7 = new HTuple(), hv_Sequence4 = new HTuple();
            HTuple hv_Area1 = new HTuple(), hv_Row3 = new HTuple();
            HTuple hv_Column3 = new HTuple(), hv_Selectedc1 = new HTuple();
            HTuple hv_Selectedr1 = new HTuple(), hv_Newtuple5 = new HTuple();
            HTuple hv_Selectedds1 = new HTuple(), hv_Selecteddc1 = new HTuple();
            HTuple hv_Selectedds2 = new HTuple(), hv_Selecteddc2 = new HTuple();
            HTuple hv_hs1 = new HTuple(), hv_hs1r = new HTuple(), hv_hs1c = new HTuple();
            HTuple hv_hs2 = new HTuple(), hv_hs2r = new HTuple(), hv_hs2c = new HTuple();
            HTuple hv_hc1 = new HTuple(), hv_hc2 = new HTuple(), hv_jdr = new HTuple();
            HTuple hv_jdc = new HTuple(), hv_jds = new HTuple(), hv_jdC = new HTuple();
            HTuple hv_jxr = new HTuple(), hv_jxc = new HTuple(), hv_jxs = new HTuple();
            HTuple hv_jxC = new HTuple(), hv_ydr = new HTuple(), hv_ydc = new HTuple();
            HTuple hv_yds = new HTuple(), hv_ydC = new HTuple(), hv_yxr = new HTuple();
            HTuple hv_yxc = new HTuple(), hv_yxs = new HTuple(), hv_yxC = new HTuple();
            HTuple hv_jin = new HTuple(), hv_yu = new HTuple(), hv_jp = new HTuple(), hv_Indices = new HTuple();
            HTuple hv_yp = new HTuple(), hv_j = new HTuple(), hv_luoju1 = new HTuple(), hv_Area3 = new HTuple(), hv_Row1 = new HTuple(), hv_Column1 = new HTuple();
            HTuple hv_luoju2 = new HTuple(), hv_dajing = new HTuple();
            HTuple hv_xiaojing = new HTuple(), hv_changdu = new HTuple();
            HTuple hv_tiaoshu = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Regions);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUp);
            HOperatorSet.GenEmptyObj(out ho_Contours);
            HOperatorSet.GenEmptyObj(out ho_Rectangle1);
            HOperatorSet.GenEmptyObj(out ho_Rectangle3);
            HOperatorSet.GenEmptyObj(out ho_RegionDifference);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced1);
            HOperatorSet.GenEmptyObj(out ho_Border);
            HOperatorSet.GenEmptyObj(out ho_SelectedContours);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected1);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected2);
            HOperatorSet.GenEmptyObj(out ho_ContoursSplit1);
            HOperatorSet.GenEmptyObj(out ho_RegionLines);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_RegionIntersection);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_Circle);
            HOperatorSet.GenEmptyObj(out ho_RegionLines1);
            HOperatorSet.GenEmptyObj(out ho_Region1);
            HOperatorSet.GenEmptyObj(out ho_RegionIntersection1);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_Circle1);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions2);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected3);
            HOperatorSet.GenEmptyObj(out ho_cl);
            HOperatorSet.Union1(algorithm.Region, out RegionToDisp);
            try
            {
                ho_Rectangle.Dispose();
                HOperatorSet.GenRectangle2(out ho_Rectangle, hv_centerRowm, hv_centerColumnm, hv_Phim, hv_Length1m,
                                           hv_Length2m);
                ho_ImageReduced.Dispose();
                HOperatorSet.ReduceDomain(Image, ho_Rectangle, out ho_ImageReduced);
                ho_Regions.Dispose();
                HOperatorSet.AutoThreshold(ho_ImageReduced, out ho_Regions, 3);

                ho_ObjectSelected.Dispose();
                HOperatorSet.SelectObj(ho_Regions, out ho_ObjectSelected, 1);
                ho_ObjectSelected.Dispose();
                HOperatorSet.SelectObj(ho_Regions, out ho_ObjectSelected, 1);
                ho_ConnectedRegions2.Dispose();
                HOperatorSet.Connection(ho_ObjectSelected, out ho_ConnectedRegions2);
                HOperatorSet.AreaCenter(ho_ConnectedRegions2, out hv_Area3, out hv_Row1, out hv_Column1);
                HOperatorSet.TupleFind(hv_Area3, hv_Area3.TupleMax(), out hv_Indices);
                ho_ObjectSelected3.Dispose();
                HOperatorSet.SelectObj(ho_ConnectedRegions2, out ho_ObjectSelected3, hv_Indices + 1);
                ho_cl.Dispose();
                HOperatorSet.ClosingCircle(ho_ObjectSelected3, out ho_cl, 3.5);
                ho_RegionFillUp.Dispose();
                HOperatorSet.FillUp(ho_ObjectSelected3, out ho_RegionFillUp);
                ho_Contours.Dispose();
                HOperatorSet.GenContourRegionXld(ho_RegionFillUp, out ho_Contours, "border");
                HOperatorSet.SmallestRectangle2Xld(ho_Contours, out hv_Row4, out hv_Column4,
                                                   out hv_Phi2, out hv_Length12, out hv_Length22);
                HOperatorSet.AreaCenter(ho_RegionFillUp, out hv_Area2, out hv_Row5, out hv_Column5);
                hv_p = ((hv_Phi2 - hv_Phim)).TupleAbs();
                while ((int)(new HTuple(hv_p.TupleGreater((new HTuple(90)).TupleRad()))) != 0)
                {
                    hv_p = ((hv_p - ((new HTuple(180)).TupleRad()))).TupleAbs();
                }
                if ((int)(new HTuple(hv_p.TupleGreater((new HTuple(45)).TupleRad()))) != 0)
                {
                    hv_fx = hv_Phi2.Clone();
                    hv_cz = hv_Length12.Clone();
                    hv_dz = hv_Length22.Clone();
                }
                else
                {
                    hv_fx = hv_Phi2 + ((new HTuple(90)).TupleRad());
                    hv_cz = hv_Length22.Clone();
                    hv_dz = hv_Length12.Clone();
                }
                hv_lll = ((((hv_cz * hv_dz) * 4) - hv_Area2) / 2) / hv_cz;
                ho_Rectangle1.Dispose();
                HOperatorSet.GenRectangle2(out ho_Rectangle1, hv_Row4, hv_Column4, hv_fx, hv_cz,
                                           hv_dz + 5);
                ho_Rectangle3.Dispose();
                HOperatorSet.GenRectangle2(out ho_Rectangle3, hv_Row4, hv_Column4, hv_Phi2, hv_Length12 + 200,
                                           hv_lll);
                ho_RegionDifference.Dispose();
                HOperatorSet.Difference(ho_Rectangle1, ho_Rectangle3, out ho_RegionDifference
                                        );
                ho_ImageReduced1.Dispose();
                HOperatorSet.ReduceDomain(ho_ImageReduced, ho_RegionDifference, out ho_ImageReduced1
                                          );
                HOperatorSet.Intensity(ho_RegionDifference, ho_ImageReduced1, out hv_Mean, out hv_Deviation1);
                ho_Border.Dispose();
                HOperatorSet.ThresholdSubPix(ho_ImageReduced1, out ho_Border, hv_Mean);
                HOperatorSet.LengthXld(ho_Border, out hv_Length8);
                if ((int)(new HTuple((new HTuple(hv_Length8.TupleLength())).TupleGreater(1))) != 0)
                {
                    ho_SelectedContours.Dispose();
                    HOperatorSet.SelectContoursXld(ho_Border, out ho_SelectedContours, "contour_length",
                                                   0.5 * (hv_Length8.TupleMax()), 1.5 * (hv_Length8.TupleMax()), -0.5, 0.5);
                    HOperatorSet.CountObj(ho_SelectedContours, out hv_Number);
                    if ((int)(new HTuple(hv_Number.TupleEqual(2))) != 0)
                    {
                        ho_ObjectSelected1.Dispose();
                        HOperatorSet.SelectObj(ho_SelectedContours, out ho_ObjectSelected1, 1);
                        ho_ObjectSelected2.Dispose();
                        HOperatorSet.SelectObj(ho_SelectedContours, out ho_ObjectSelected2, 2);
                        HOperatorSet.GetContourXld(ho_ObjectSelected1, out hv_Rowo1, out hv_Colo1);
                        HOperatorSet.GetContourXld(ho_ObjectSelected2, out hv_Rowo2, out hv_Colo2);
                        HOperatorSet.TupleTan(hv_fx + ((new HTuple(0.000001)).TupleRad()), out hv_as);
                        HOperatorSet.TupleTan(hv_fx + ((new HTuple(90.000001)).TupleRad()), out hv_ac);
                        hv_b  = -1;
                        hv_cs = (-(hv_Row4 + (5000 * (((hv_fx + ((new HTuple(90)).TupleRad()))).TupleSin()
                                                      )))) - (hv_as * (hv_Column4 - (5000 * (((hv_fx + ((new HTuple(90)).TupleRad()))).TupleCos()
                                                                                             ))));
                        hv_cc = (-(hv_Row4 + (5000 * (hv_fx.TupleSin())))) - (hv_ac * (hv_Column4 - (5000 * (hv_fx.TupleCos()
                                                                                                             ))));
                        hv_ds1 = (((((hv_as * hv_Colo1) + (hv_b * (-hv_Rowo1))) + hv_cs)).TupleAbs()) / (((1 + (hv_as * hv_as))).TupleSqrt()
                                                                                                         );
                        hv_ds2 = (((((hv_as * hv_Colo2) + (hv_b * (-hv_Rowo2))) + hv_cs)).TupleAbs()) / (((1 + (hv_as * hv_as))).TupleSqrt()
                                                                                                         );
                        hv_dc1 = (((((hv_ac * hv_Colo1) + (hv_b * (-hv_Rowo1))) + hv_cc)).TupleAbs()) / (((1 + (hv_ac * hv_ac))).TupleSqrt()
                                                                                                         );
                        hv_dc2 = (((((hv_ac * hv_Colo2) + (hv_b * (-hv_Rowo2))) + hv_cc)).TupleAbs()) / (((1 + (hv_ac * hv_ac))).TupleSqrt()
                                                                                                         );
                        ho_ContoursSplit1.Dispose();
                        HOperatorSet.SegmentContoursXld(ho_ObjectSelected2, out ho_ContoursSplit1,
                                                        "lines_circles", 5, 4, 2);
                        HOperatorSet.LengthXld(ho_ContoursSplit1, out hv_Length3);
                        hv_dd = hv_Length3.TupleMean();
                        HOperatorSet.TupleFloor(hv_dd, out hv_Floor);
                        HOperatorSet.TupleLength(hv_Colo1, out hv_Length4);
                        HOperatorSet.TupleGenSequence(hv_Length4 - hv_Floor, hv_Length4 - 1, 1, out hv_Sequence1);
                        HOperatorSet.TupleLength(hv_Colo2, out hv_Length5);
                        HOperatorSet.TupleGenSequence(0, hv_Floor - 1, 1, out hv_Sequence);
                        HOperatorSet.TupleGenSequence(hv_Length5 - hv_Floor, hv_Length5 - 1, 1, out hv_Sequence2);
                        HOperatorSet.TupleRemove(hv_ds1, hv_Sequence, out hv_Reduced1);
                        HOperatorSet.TupleRemove(hv_ds1, hv_Sequence1, out hv_Reduced2);
                        HOperatorSet.TupleRemove(hv_ds2, hv_Sequence, out hv_Reduced3);
                        HOperatorSet.TupleRemove(hv_ds2, hv_Sequence2, out hv_Reduced4);
                        hv_d1 = hv_Reduced1 - hv_Reduced2;
                        HOperatorSet.TupleLength(hv_d1, out hv_Length6);
                        HOperatorSet.TupleGenSequence(1, hv_Length6, 1, out hv_Sequence3);
                        ho_RegionLines.Dispose();
                        HOperatorSet.GenRegionLine(out ho_RegionLines, 0, 0, 0, hv_Length6);
                        ho_Region.Dispose();
                        HOperatorSet.GenRegionPoints(out ho_Region, hv_d1, hv_Sequence3);
                        ho_RegionIntersection.Dispose();
                        HOperatorSet.Intersection(ho_RegionLines, ho_Region, out ho_RegionIntersection
                                                  );
                        ho_ConnectedRegions.Dispose();
                        HOperatorSet.Connection(ho_RegionIntersection, out ho_ConnectedRegions);
                        HOperatorSet.AreaCenter(ho_ConnectedRegions, out hv_Area, out hv_Row2, out hv_Column2);
                        HOperatorSet.TupleSelect(hv_Colo1, ((hv_Column2 + (hv_dd / 2))).TupleFloor(),
                                                 out hv_Selectedc);
                        HOperatorSet.TupleSelect(hv_Rowo1, ((hv_Column2 + (hv_dd / 2))).TupleFloor(),
                                                 out hv_Selectedr);
                        HOperatorSet.TupleGenConst(new HTuple(hv_Selectedr.TupleLength()), 1, out hv_Newtuple);
                        ho_Circle.Dispose();
                        HOperatorSet.GenCircle(out ho_Circle, hv_Selectedr, hv_Selectedc, hv_Newtuple);
                        hv_d2 = hv_Reduced3 - hv_Reduced4;
                        HOperatorSet.TupleLength(hv_d2, out hv_Length7);
                        HOperatorSet.TupleGenSequence(1, hv_Length7, 1, out hv_Sequence4);
                        ho_RegionLines1.Dispose();
                        HOperatorSet.GenRegionLine(out ho_RegionLines1, 0, 0, 0, hv_Length7);
                        ho_Region1.Dispose();
                        HOperatorSet.GenRegionPoints(out ho_Region1, hv_d2, hv_Sequence4);
                        ho_RegionIntersection1.Dispose();
                        HOperatorSet.Intersection(ho_Region1, ho_RegionLines1, out ho_RegionIntersection1
                                                  );
                        ho_ConnectedRegions1.Dispose();
                        HOperatorSet.Connection(ho_RegionIntersection1, out ho_ConnectedRegions1);
                        HOperatorSet.AreaCenter(ho_ConnectedRegions1, out hv_Area1, out hv_Row3,
                                                out hv_Column3);
                        HOperatorSet.TupleSelect(hv_Colo2, ((hv_Column3 + (hv_dd / 2))).TupleFloor(),
                                                 out hv_Selectedc1);
                        HOperatorSet.TupleSelect(hv_Rowo2, ((hv_Column3 + (hv_dd / 2))).TupleFloor(),
                                                 out hv_Selectedr1);
                        HOperatorSet.TupleGenConst(new HTuple(hv_Selectedr1.TupleLength()), 1, out hv_Newtuple5);
                        ho_Circle1.Dispose();
                        HOperatorSet.GenCircle(out ho_Circle1, hv_Selectedr1, hv_Selectedc1, hv_Newtuple5);
                        HOperatorSet.TupleSelect(hv_ds1, ((hv_Column2 + (hv_dd / 2))).TupleFloor(), out hv_Selectedds1);
                        HOperatorSet.TupleSelect(hv_dc1, ((hv_Column2 + (hv_dd / 2))).TupleFloor(), out hv_Selecteddc1);
                        HOperatorSet.TupleSelect(hv_ds2, ((hv_Column3 + (hv_dd / 2))).TupleFloor(), out hv_Selectedds2);
                        HOperatorSet.TupleSelect(hv_dc2, ((hv_Column3 + (hv_dd / 2))).TupleFloor(), out hv_Selecteddc2);
                        if ((int)(new HTuple((new HTuple(hv_Selectedds1.TupleMean())).TupleLess(hv_Selectedds2.TupleMean()
                                                                                                ))) != 0)
                        {
                            hv_hs1  = hv_Selectedds1.Clone();
                            hv_hs1r = hv_Selectedr.Clone();
                            hv_hs1c = hv_Selectedc.Clone();
                            hv_hs2  = hv_Selectedds2.Clone();
                            hv_hs2r = hv_Selectedr1.Clone();
                            hv_hs2c = hv_Selectedc1.Clone();
                            hv_hc1  = hv_Selecteddc1.Clone();
                            hv_hc2  = hv_Selecteddc2.Clone();
                        }
                        else
                        {
                            hv_hs1  = hv_Selectedds2.Clone();
                            hv_hs2r = hv_Selectedr.Clone();
                            hv_hs2c = hv_Selectedc.Clone();
                            hv_hs2  = hv_Selectedds1.Clone();
                            hv_hs1r = hv_Selectedr1.Clone();
                            hv_hs1c = hv_Selectedc1.Clone();
                            hv_hc2  = hv_Selecteddc1.Clone();
                            hv_hc1  = hv_Selecteddc2.Clone();
                        }
                        hv_jdr = new HTuple();
                        hv_jdc = new HTuple();
                        hv_jds = new HTuple();
                        hv_jdC = new HTuple();
                        hv_jxr = new HTuple();
                        hv_jxc = new HTuple();
                        hv_jxs = new HTuple();
                        hv_jxC = new HTuple();
                        hv_ydr = new HTuple();
                        hv_ydc = new HTuple();
                        hv_yds = new HTuple();
                        hv_ydC = new HTuple();
                        hv_yxr = new HTuple();
                        hv_yxc = new HTuple();
                        hv_yxs = new HTuple();
                        hv_yxC = new HTuple();
                        hv_jin = new HTuple(hv_hs1.TupleLength());
                        hv_yu  = new HTuple(hv_hs2.TupleLength());
                        hv_jp  = hv_hs1.TupleMean();
                        hv_yp  = hv_hs2.TupleMean();
                        HTuple end_val128  = hv_jin - 1;
                        HTuple step_val128 = 1;
                        for (hv_j = 0; hv_j.Continue(end_val128, step_val128); hv_j = hv_j.TupleAdd(step_val128))
                        {
                            if ((int)(new HTuple(((hv_hs1.TupleSelect(hv_j))).TupleLess(hv_jp))) != 0)
                            {
                                hv_jdr = hv_jdr.TupleConcat(hv_hs1r.TupleSelect(hv_j));
                                hv_jdc = hv_jdc.TupleConcat(hv_hs1c.TupleSelect(hv_j));
                                hv_jds = hv_jds.TupleConcat(hv_hs1.TupleSelect(hv_j));
                                hv_jdC = hv_jdC.TupleConcat(hv_hc1.TupleSelect(hv_j));
                            }
                            else
                            {
                                hv_jxr = hv_jxr.TupleConcat(hv_hs1r.TupleSelect(hv_j));
                                hv_jxc = hv_jxc.TupleConcat(hv_hs1c.TupleSelect(hv_j));
                                hv_jxs = hv_jxs.TupleConcat(hv_hs1.TupleSelect(hv_j));
                                hv_jxC = hv_jxC.TupleConcat(hv_hc1.TupleSelect(hv_j));
                            }
                        }
                        HTuple end_val141  = hv_yu - 1;
                        HTuple step_val141 = 1;
                        for (hv_j = 0; hv_j.Continue(end_val141, step_val141); hv_j = hv_j.TupleAdd(step_val141))
                        {
                            if ((int)(new HTuple(((hv_hs2.TupleSelect(hv_j))).TupleGreater(hv_yp))) != 0)
                            {
                                hv_ydr = hv_ydr.TupleConcat(hv_hs2r.TupleSelect(hv_j));
                                hv_ydc = hv_ydc.TupleConcat(hv_hs2c.TupleSelect(hv_j));
                                hv_yds = hv_yds.TupleConcat(hv_hs2.TupleSelect(hv_j));
                                hv_ydC = hv_ydC.TupleConcat(hv_hc2.TupleSelect(hv_j));
                            }
                            else
                            {
                                hv_yxr = hv_yxr.TupleConcat(hv_hs2r.TupleSelect(hv_j));
                                hv_yxc = hv_yxc.TupleConcat(hv_hs2c.TupleSelect(hv_j));
                                hv_yxs = hv_yxs.TupleConcat(hv_hs2.TupleSelect(hv_j));
                                hv_yxC = hv_yxC.TupleConcat(hv_hc2.TupleSelect(hv_j));
                            }
                        }
                        hv_luoju1 = (((hv_hc1.TupleMax()) - (hv_hc1.TupleMin())) / ((new HTuple(hv_hc1.TupleLength()
                                                                                                )) - 1)) * 2;
                        hv_luoju2 = (((hv_hc2.TupleMax()) - (hv_hc2.TupleMin())) / ((new HTuple(hv_hc2.TupleLength()
                                                                                                )) - 1)) * 2;
                        hv_dajing   = (hv_yds.TupleMean()) - (hv_jds.TupleMean());
                        hv_xiaojing = (hv_yxs.TupleMean()) - (hv_jxs.TupleMean());
                        hv_changdu  = (((hv_hc1.TupleConcat(hv_hc2))).TupleMax()) - (((hv_hc1.TupleConcat(
                                                                                           hv_hc2))).TupleMin());
                        hv_tiaoshu = (((((((new HTuple(hv_jds.TupleLength())).TupleConcat(new HTuple(hv_jxs.TupleLength()
                                                                                                     )))).TupleConcat(new HTuple(hv_yds.TupleLength())))).TupleConcat(new HTuple(hv_yxs.TupleLength()
                                                                                                                                                                                 )))).TupleMax();
                    }
                }
                HOperatorSet.Union2(ho_Circle, ho_Circle1, out RegionToDisp);



                HTuple hv_result = GetHv_result();
                hv_result = hv_result.TupleConcat("大径平均值");
                hv_result = hv_result.TupleConcat(hv_dajing.D * pixeldist);
                hv_result = hv_result.TupleConcat("螺纹长度");
                hv_result = hv_result.TupleConcat(hv_changdu.D * pixeldist);
                hv_result = hv_result.TupleConcat("螺纹条数");
                hv_result = hv_result.TupleConcat(hv_tiaoshu.D);
                hv_result = hv_result.TupleConcat("螺距1");
                hv_result = hv_result.TupleConcat(hv_luoju1.D * pixeldist);
                hv_result = hv_result.TupleConcat("螺距2");
                hv_result = hv_result.TupleConcat(hv_luoju2.D * pixeldist);
                result    = hv_result.Clone();

                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Regions.Dispose();
                ho_ObjectSelected.Dispose();
                ho_RegionFillUp.Dispose();
                ho_Contours.Dispose();
                ho_Rectangle1.Dispose();
                ho_Rectangle3.Dispose();
                ho_RegionDifference.Dispose();
                ho_ImageReduced1.Dispose();
                ho_Border.Dispose();
                ho_SelectedContours.Dispose();
                ho_ObjectSelected1.Dispose();
                ho_ObjectSelected2.Dispose();
                ho_ContoursSplit1.Dispose();
                ho_RegionLines.Dispose();
                ho_Region.Dispose();
                ho_RegionIntersection.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_Circle.Dispose();
                ho_RegionLines1.Dispose();
                ho_Region1.Dispose();
                ho_RegionIntersection1.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_Circle1.Dispose();
                ho_ConnectedRegions2.Dispose();
                ho_ObjectSelected3.Dispose();
                ho_cl.Dispose();
                algorithm.Region.Dispose();
            }
            catch
            {
                HTuple hv_result = GetHv_result();
                hv_result = hv_result.TupleConcat("大径平均值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("螺纹长度");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("螺纹条数");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("螺距1");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("螺距2");
                hv_result = hv_result.TupleConcat(0);
                result    = hv_result.Clone();

                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Regions.Dispose();
                ho_ObjectSelected.Dispose();
                ho_RegionFillUp.Dispose();
                ho_Contours.Dispose();
                ho_Rectangle1.Dispose();
                ho_Rectangle3.Dispose();
                ho_RegionDifference.Dispose();
                ho_ImageReduced1.Dispose();
                ho_Border.Dispose();
                ho_SelectedContours.Dispose();
                ho_ObjectSelected1.Dispose(); ho_cl.Dispose();
                ho_ObjectSelected2.Dispose();
                ho_ContoursSplit1.Dispose();
                ho_RegionLines.Dispose();
                ho_Region.Dispose();
                ho_RegionIntersection.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_Circle.Dispose();
                ho_RegionLines1.Dispose();
                ho_Region1.Dispose();
                ho_RegionIntersection1.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_Circle1.Dispose();
                ho_ConnectedRegions2.Dispose();
                ho_ObjectSelected3.Dispose();
                algorithm.Region.Dispose();
            }
        }
예제 #23
0
        private void action()
        {
            // Local iconic variables

            HObject ho_Rectangle, ho_ImageReduced;
            HObject ho_Region, ho_Rectangle1, ho_Region1, ho_ConnectedRegions;
            HObject ho_RegionFillUp, ho_SelectedRegions, ho_SortedRegions;
            HObject ho_ObjectSelected = null, ho_ObjectSelected1;

            // Local control variables

            HTuple hv_Row1 = null, hv_Column1 = null, hv_Row2 = null;
            HTuple hv_Column2 = null, hv_Row11 = null, hv_Column11 = null;
            HTuple hv_Row21 = null, hv_Column21 = null, hv_Mean = null;
            HTuple hv_Deviation = null, hv_Number = null, hv_a = null;
            HTuple hv_l1 = null, hv_l2 = null, hv_i = null, hv_Area = new HTuple();
            HTuple hv_Row = new HTuple(), hv_Column = new HTuple();
            HTuple hv_Row3 = new HTuple(), hv_Column3 = new HTuple();
            HTuple hv_Phi = new HTuple(), hv_Length1 = new HTuple();
            HTuple hv_Length2 = new HTuple(), hv_Indices = null, hv_cl0 = null;
            HTuple hv_cl1 = null, hv_cl2 = null, hv_cl3 = null, hv_cl4 = null;
            HTuple hv_cl5 = null, hv_cl6 = null, hv_cl7 = null, hv_cl8 = null;
            HTuple hv_cl9 = null;

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_Rectangle1);
            HOperatorSet.GenEmptyObj(out ho_Region1);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUp);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SortedRegions);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected1);
            HOperatorSet.Union1(algorithm.Region, out RegionToDisp);
            try
            {
                HOperatorSet.GenRectangle1(out ho_Rectangle, DRow1m, DCol1m, DRow2m, DCol2m);
                ho_ImageReduced.Dispose();
                HOperatorSet.ReduceDomain(Image, ho_Rectangle, out ho_ImageReduced);
                ho_Region.Dispose();
                HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, 128, 255);
                HOperatorSet.SmallestRectangle1(ho_Region, out hv_Row11, out hv_Column11, out hv_Row21,
                                                out hv_Column21);
                ho_Rectangle1.Dispose();
                HOperatorSet.GenRectangle1(out ho_Rectangle1, hv_Row11, hv_Column11, hv_Row21,
                                           hv_Column21);
                HOperatorSet.Intensity(ho_Rectangle1, ho_ImageReduced, out hv_Mean, out hv_Deviation);
                ho_Region1.Dispose();
                HOperatorSet.Threshold(ho_ImageReduced, out ho_Region1, hv_Mean, 255);
                ho_ConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_Region1, out ho_ConnectedRegions);
                ho_RegionFillUp.Dispose();
                HOperatorSet.FillUp(ho_ConnectedRegions, out ho_RegionFillUp);
                ho_SelectedRegions.Dispose();
                HOperatorSet.SelectShape(ho_RegionFillUp, out ho_SelectedRegions, "area", "and",
                                         200, 99999);
                ho_SortedRegions.Dispose();
                HOperatorSet.SortRegion(ho_SelectedRegions, out ho_SortedRegions, "upper_left",
                                        "true", "column");
                HOperatorSet.CountObj(ho_SortedRegions, out hv_Number);
                hv_a  = new HTuple();
                hv_l1 = new HTuple();
                hv_l2 = new HTuple();
                HTuple end_val20  = hv_Number - 2;
                HTuple step_val20 = 1;
                for (hv_i = 1; hv_i.Continue(end_val20, step_val20); hv_i = hv_i.TupleAdd(step_val20))
                {
                    ho_ObjectSelected.Dispose();
                    HOperatorSet.SelectObj(ho_SortedRegions, out ho_ObjectSelected, hv_i + 1);
                    HOperatorSet.AreaCenter(ho_ObjectSelected, out hv_Area, out hv_Row, out hv_Column);
                    HOperatorSet.SmallestRectangle2(ho_ObjectSelected, out hv_Row3, out hv_Column3,
                                                    out hv_Phi, out hv_Length1, out hv_Length2);
                    hv_a  = hv_a.TupleConcat(hv_Area);
                    hv_l1 = hv_l1.TupleConcat(hv_Length1);
                    hv_l2 = hv_l2.TupleConcat(hv_Length2);
                }
                HOperatorSet.TupleFind(hv_a, hv_a.TupleMin(), out hv_Indices);
                ho_ObjectSelected1.Dispose();
                HOperatorSet.SelectObj(ho_SortedRegions, out ho_ObjectSelected1, hv_Indices + 2);
                hv_cl0 = hv_Number - 2;
                hv_cl1 = hv_a.TupleMean();
                hv_cl2 = (hv_a.TupleMin()) / (hv_a.TupleMean());
                hv_cl3 = (hv_a.TupleMax()) / (hv_a.TupleMean());
                hv_cl4 = hv_l1.TupleMean();
                hv_cl5 = (hv_l1.TupleMin()) / (hv_l1.TupleMean());
                hv_cl6 = (hv_l1.TupleMax()) / (hv_l1.TupleMean());
                hv_cl7 = hv_l2.TupleMean();
                hv_cl8 = (hv_l2.TupleMin()) / (hv_l2.TupleMean());
                hv_cl9 = (hv_l2.TupleMax()) / (hv_l2.TupleMean());
                HOperatorSet.Union1(ho_ObjectSelected1, out RegionToDisp);

                HTuple hv_result = GetHv_result();
                hv_result = hv_result.TupleConcat("数量");
                hv_result = hv_result.TupleConcat(hv_cl0.D);
                hv_result = hv_result.TupleConcat("面积平均");
                hv_result = hv_result.TupleConcat(hv_cl1.D * pixeldist);
                hv_result = hv_result.TupleConcat("面积最小比值");
                hv_result = hv_result.TupleConcat(hv_cl2.D);
                hv_result = hv_result.TupleConcat("面积最大比值");
                hv_result = hv_result.TupleConcat(hv_cl3.D);
                hv_result = hv_result.TupleConcat("长平均");
                hv_result = hv_result.TupleConcat(hv_cl4.D * pixeldist);
                hv_result = hv_result.TupleConcat("长最小比值");
                hv_result = hv_result.TupleConcat(hv_cl5.D);
                hv_result = hv_result.TupleConcat("长最大比值");
                hv_result = hv_result.TupleConcat(hv_cl6.D);
                hv_result = hv_result.TupleConcat("宽平均");
                hv_result = hv_result.TupleConcat(hv_cl7.D * pixeldist);
                hv_result = hv_result.TupleConcat("宽最小比值");
                hv_result = hv_result.TupleConcat(hv_cl8.D);
                hv_result = hv_result.TupleConcat("宽最大比值");
                hv_result = hv_result.TupleConcat(hv_cl9.D);
                result    = hv_result.Clone();
                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region.Dispose();
                ho_Rectangle1.Dispose();
                ho_Region1.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_RegionFillUp.Dispose();
                ho_SelectedRegions.Dispose();
                ho_SortedRegions.Dispose();
                ho_ObjectSelected.Dispose();
                ho_ObjectSelected1.Dispose();
                algorithm.Region.Dispose();
            }
            catch
            {
                HTuple hv_result = GetHv_result();
                hv_result = hv_result.TupleConcat("数量");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("面积平均");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("面积最小比值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("面积最大比值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("长平均");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("长最小比值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("长最大比值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("宽平均");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("宽最小比值");
                hv_result = hv_result.TupleConcat(0);
                hv_result = hv_result.TupleConcat("宽最大比值");
                hv_result = hv_result.TupleConcat(0);
                result    = hv_result.Clone();
                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region.Dispose();
                ho_Rectangle1.Dispose();
                ho_Region1.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_RegionFillUp.Dispose();
                ho_SelectedRegions.Dispose();
                ho_SortedRegions.Dispose();
                ho_ObjectSelected.Dispose();
                ho_ObjectSelected1.Dispose();
                algorithm.Region.Dispose();
            }
            finally
            {
                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region.Dispose();
                ho_Rectangle1.Dispose();
                ho_Region1.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_RegionFillUp.Dispose();
                ho_SelectedRegions.Dispose();
                ho_SortedRegions.Dispose();
                ho_ObjectSelected.Dispose();
                ho_ObjectSelected1.Dispose();
                algorithm.Region.Dispose();
            }
        }
예제 #24
0
        static private void dilate_regions_pro(HObject ho_image, out HObject ho_ConnectedDilations,
                                               HTuple hv_Row1OfRectangles, HTuple hv_Row2OfRectangles, HTuple hv_Column1OfRectangles,
                                               HTuple hv_Column2OfRectangles, HTuple hv_RadiusOfDilation, out HTuple hv_IndexOfRectangles,
                                               out HTuple hv_NumberListOfRectangles)
        {
            // Stack for temporary objects
            HObject[] OTemp = new HObject[20];

            // Local iconic variables

            HObject ho_OrgRectangles = null, ho_RegionDilation = null;
            HObject ho_RegionUnion = null, ho_ConnectedRegions = null, ho_RectangleDilationSelected = null;
            HObject ho_RectangleSelected = null;

            // Local control variables

            HTuple hv_numOfRectangles = new HTuple();
            HTuple hv_Number = new HTuple(), hv_IndexOfConnectedRegions = new HTuple();
            HTuple hv_NumberOfRectangles = new HTuple(), hv_SubDefectIndexList = new HTuple();
            HTuple hv_IndexOfOrgRectangles = new HTuple(), hv_IsSubset = new HTuple();
            HTuple hv_Exception                        = null;
            HTuple hv_Column2OfRectangles_COPY_INP_TMP = hv_Column2OfRectangles.Clone();
            HTuple hv_RadiusOfDilation_COPY_INP_TMP    = hv_RadiusOfDilation.Clone();
            HTuple hv_Row2OfRectangles_COPY_INP_TMP    = hv_Row2OfRectangles.Clone();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_ConnectedDilations);
            HOperatorSet.GenEmptyObj(out ho_OrgRectangles);
            HOperatorSet.GenEmptyObj(out ho_RegionDilation);
            HOperatorSet.GenEmptyObj(out ho_RegionUnion);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_RectangleDilationSelected);
            HOperatorSet.GenEmptyObj(out ho_RectangleSelected);
            try
            {
                dev_update_off();
                HOperatorSet.SetSystem("clip_region", "true");
                //
                ho_ConnectedDilations.Dispose();
                HOperatorSet.GenEmptyObj(out ho_ConnectedDilations);
                //
                hv_IndexOfRectangles      = new HTuple();
                hv_NumberListOfRectangles = new HTuple();
                //
                try
                {
                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.DispObj(ho_image, HDevWindowStack.GetActive());
                    }
                    hv_numOfRectangles = 0;
                    HOperatorSet.TupleLength(hv_Row1OfRectangles, out hv_numOfRectangles);
                    if ((int)(new HTuple(hv_numOfRectangles.TupleLessEqual(0))) != 0)
                    {
                        ho_OrgRectangles.Dispose();
                        ho_RegionDilation.Dispose();
                        ho_RegionUnion.Dispose();
                        ho_ConnectedRegions.Dispose();
                        ho_RectangleDilationSelected.Dispose();
                        ho_RectangleSelected.Dispose();

                        return;
                    }
                    if ((int)((new HTuple((new HTuple(hv_numOfRectangles.TupleNotEqual(new HTuple(hv_Row2OfRectangles_COPY_INP_TMP.TupleLength()
                                                                                                  )))).TupleOr(new HTuple(hv_numOfRectangles.TupleNotEqual(new HTuple(hv_Column1OfRectangles.TupleLength()
                                                                                                                                                                      )))))).TupleOr(new HTuple(hv_numOfRectangles.TupleNotEqual(new HTuple(hv_Column2OfRectangles_COPY_INP_TMP.TupleLength()
                                                                                                                                                                                                                                            ))))) != 0)
                    {
                        ho_OrgRectangles.Dispose();
                        ho_RegionDilation.Dispose();
                        ho_RegionUnion.Dispose();
                        ho_ConnectedRegions.Dispose();
                        ho_RectangleDilationSelected.Dispose();
                        ho_RectangleSelected.Dispose();

                        return;
                    }
                    HOperatorSet.TupleMax2(hv_Row1OfRectangles, hv_Row2OfRectangles_COPY_INP_TMP,
                                           out hv_Row2OfRectangles_COPY_INP_TMP);
                    HOperatorSet.TupleMax2(hv_Column1OfRectangles, hv_Column2OfRectangles_COPY_INP_TMP,
                                           out hv_Column2OfRectangles_COPY_INP_TMP);
                    ho_OrgRectangles.Dispose();
                    HOperatorSet.GenRectangle1(out ho_OrgRectangles, hv_Row1OfRectangles, hv_Column1OfRectangles,
                                               hv_Row2OfRectangles_COPY_INP_TMP, hv_Column2OfRectangles_COPY_INP_TMP);
                    HOperatorSet.TupleMax2(1.5, hv_RadiusOfDilation_COPY_INP_TMP, out hv_RadiusOfDilation_COPY_INP_TMP);
                    ho_RegionDilation.Dispose();
                    HOperatorSet.DilationCircle(ho_OrgRectangles, out ho_RegionDilation, hv_RadiusOfDilation_COPY_INP_TMP);
                    ho_RegionUnion.Dispose();
                    HOperatorSet.Union1(ho_RegionDilation, out ho_RegionUnion);
                    ho_ConnectedRegions.Dispose();
                    HOperatorSet.Connection(ho_RegionUnion, out ho_ConnectedRegions);
                    {
                        HObject ExpTmpOutVar_0;
                        HOperatorSet.SortRegion(ho_ConnectedRegions, out ExpTmpOutVar_0, "first_point",
                                                "true", "row");
                        ho_ConnectedRegions.Dispose();
                        ho_ConnectedRegions = ExpTmpOutVar_0;
                    }
                    HOperatorSet.CountObj(ho_ConnectedRegions, out hv_Number);
                    HTuple end_val27  = hv_Number;
                    HTuple step_val27 = 1;
                    for (hv_IndexOfConnectedRegions = 1; hv_IndexOfConnectedRegions.Continue(end_val27, step_val27); hv_IndexOfConnectedRegions = hv_IndexOfConnectedRegions.TupleAdd(step_val27))
                    {
                        hv_NumberOfRectangles = 0;
                        ho_RectangleDilationSelected.Dispose();
                        HOperatorSet.SelectObj(ho_ConnectedRegions, out ho_RectangleDilationSelected,
                                               hv_IndexOfConnectedRegions);
                        hv_SubDefectIndexList = new HTuple();
                        HTuple end_val31  = hv_numOfRectangles;
                        HTuple step_val31 = 1;
                        for (hv_IndexOfOrgRectangles = 1; hv_IndexOfOrgRectangles.Continue(end_val31, step_val31); hv_IndexOfOrgRectangles = hv_IndexOfOrgRectangles.TupleAdd(step_val31))
                        {
                            ho_RectangleSelected.Dispose();
                            HOperatorSet.SelectObj(ho_OrgRectangles, out ho_RectangleSelected, hv_IndexOfOrgRectangles);
                            HOperatorSet.TestSubsetRegion(ho_RectangleSelected, ho_RectangleDilationSelected,
                                                          out hv_IsSubset);
                            if ((int)(hv_IsSubset) != 0)
                            {
                                hv_IndexOfRectangles  = hv_IndexOfRectangles.TupleConcat(hv_IndexOfOrgRectangles - 1);
                                hv_NumberOfRectangles = hv_NumberOfRectangles + 1;
                            }
                        }
                        hv_NumberListOfRectangles = hv_NumberListOfRectangles.TupleConcat(hv_NumberOfRectangles);
                    }
                    ho_ConnectedDilations.Dispose();
                    ho_ConnectedDilations = ho_ConnectedRegions.CopyObj(1, -1);
                }
                // catch (Exception)
                catch (HalconException HDevExpDefaultException1)
                {
                    HDevExpDefaultException1.ToHTuple(out hv_Exception);
                    //do nothing.
                }
                //
                ho_OrgRectangles.Dispose();
                ho_RegionDilation.Dispose();
                ho_RegionUnion.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_RectangleDilationSelected.Dispose();
                ho_RectangleSelected.Dispose();

                return;
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_OrgRectangles.Dispose();
                ho_RegionDilation.Dispose();
                ho_RegionUnion.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_RectangleDilationSelected.Dispose();
                ho_RectangleSelected.Dispose();

                throw HDevExpDefaultException;
            }
        }
예제 #25
0
        public MeasureResult Action()
        {
            #region 輸出結果
            CircleResult mResult = null;
            #endregion

            // Local iconic variables

            HObject ho_R3_Circle = null;
            HObject ho_R3_ROI_Image = null, ho_R3_Region = null, ho_R3_ImageReduced = null;
            HObject ho_R3_Edges = null, ho_R3_ContoursSplit = null, ho_R3_SingleSegment = null;
            HObject ho_R3_ContEllipse = null;


            // Local control variables

            HTuple hv_msgOffsetY, hv_msgOffsetX;
            HTuple hv_STD_Row;
            HTuple hv_STD_Col, hv_Img_Row, hv_Img_Col, hv_Img_Rotate_Angle;
            HTuple hv_OffsetRow, hv_OffsetCol, hv_R3_R;
            HTuple hv_STD_R3_Row = new HTuple(), hv_STD_R3_Col = new HTuple();
            HTuple hv_STD_R3_V_Row = new HTuple(), hv_STD_R3_V_Col = new HTuple();
            HTuple hv_R3_X = new HTuple(), hv_R3_Y = new HTuple(), hv_R3_Pos_Row = new HTuple();
            HTuple hv_R3_Pos_Col = new HTuple(), hv_alpha = new HTuple();
            HTuple hv_R3_low = new HTuple(), hv_R3_high = new HTuple();
            HTuple hv_R3_NumSegments = new HTuple(), hv_NumCircles = new HTuple();
            HTuple hv_Num_Circle_Point = new HTuple(), hv_R3 = new HTuple();
            HTuple hv_i = new HTuple(), hv_Attrib = new HTuple(), hv_R3_Row = new HTuple();
            HTuple hv_R3_Column = new HTuple(), hv_R3_Radius = new HTuple();
            HTuple hv_R3_StartPhi = new HTuple(), hv_R3_EndPhi = new HTuple();
            HTuple hv_R3_PointOrder = new HTuple(), hv_R3_MinDist = new HTuple();
            HTuple hv_R3_MaxDist = new HTuple(), hv_R3_AvgDist = new HTuple();
            HTuple hv_R3_SigmaDist = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_R3_Circle);
            HOperatorSet.GenEmptyObj(out ho_R3_ROI_Image);
            HOperatorSet.GenEmptyObj(out ho_R3_Region);
            HOperatorSet.GenEmptyObj(out ho_R3_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_R3_Edges);
            HOperatorSet.GenEmptyObj(out ho_R3_ContoursSplit);
            HOperatorSet.GenEmptyObj(out ho_R3_SingleSegment);
            HOperatorSet.GenEmptyObj(out ho_R3_ContEllipse);

            //Measure: SDMS_R3
            //Author: John Hsieh
            //Date: 2012
            //ho_Image.Dispose();
            //HOperatorSet.ReadImage(out ho_Image, "Images/STD.bmp");
            //dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle);
            //// dev_update_off(...); only in hdevelop
            HOperatorSet.SetSystem("border_shape_models", "false");

            //****Message Args
            hv_msgOffsetY = 100;
            hv_msgOffsetX = 100;

            //****Model All
            //HOperatorSet.ReadShapeModel("D:/Projects/Halcon/SDMS/SDMS_Measure/Model/MatchingAll.shm",
            //	out hv_AllModelId);
            //ho_AllModelContours.Dispose();
            //HOperatorSet.GetShapeModelContours(out ho_AllModelContours, hv_AllModelId, 1);
            //HOperatorSet.FindShapeModel(ho_Image, hv_AllModelId, (new HTuple(0)).TupleRad()
            //	, (new HTuple(360)).TupleRad(), 0.5, 1, 0.5, "least_squares", 6, 0.75, out hv_AllModelRow,
            //	out hv_AllModelColumn, out hv_AllModelAngle, out hv_AllModelScore);

            //****Model Args

            //STD 中心點
            hv_STD_Row = 772;
            hv_STD_Col = 1003;

            //目前圖形 中心點
            hv_Img_Row = hv_AllModelRow.Clone();
            hv_Img_Col = hv_AllModelColumn.Clone();

            //目前圖形 Rotate Angle
            hv_Img_Rotate_Angle = hv_AllModelAngle.Clone();

            //目前圖形偏移量
            hv_OffsetRow = hv_Img_Row - hv_STD_Row;
            hv_OffsetCol = hv_Img_Col - hv_STD_Col;

            //****Display
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.ClearWindow(HDevWindowStack.GetActive());
            }
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
            }
            //*****R3
            hv_R3_R = 29;

            //STD R3_ 位置
            hv_STD_R3_Row = 252;
            hv_STD_R3_Col = 1381;

            //STD 向量 STD_R3_
            hv_STD_R3_V_Row = hv_STD_R3_Row - hv_STD_Row;
            hv_STD_R3_V_Col = hv_STD_R3_Col - hv_STD_Col;

            //R3_X, R3_Y 分量
            hv_R3_X = (hv_STD_R3_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_R3_V_Row * (hv_Img_Rotate_Angle.TupleSin()
                                                                                                 ));
            hv_R3_Y = (hv_STD_R3_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_R3_V_Col * (hv_Img_Rotate_Angle.TupleSin()
                                                                                                 ));


            //目前圖形 R3_ 位置
            hv_R3_Pos_Row = (hv_STD_Row + hv_R3_Y) + hv_OffsetRow;
            hv_R3_Pos_Col = (hv_STD_Col + hv_R3_X) + hv_OffsetCol;

            //** 開始計算
            ho_R3_Circle.Dispose();
            HOperatorSet.GenCircle(out ho_R3_Circle, hv_R3_Pos_Row, hv_R3_Pos_Col, hv_R3_R);
            ho_R3_ROI_Image.Dispose();
            HOperatorSet.ReduceDomain(ho_Image, ho_R3_Circle, out ho_R3_ROI_Image);
            ho_R3_Region.Dispose();
            HOperatorSet.FastThreshold(ho_R3_ROI_Image, out ho_R3_Region, 100, 255, 15);
            ho_R3_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_R3_ROI_Image, ho_R3_Region, out ho_R3_ImageReduced
                                      );
            //stop ()
            //sobel_fast 具有較寬的選擇範圍,搭配 alpha 參數 (alpha 越大, 容錯範圍大)
            hv_alpha   = 0.9;
            hv_R3_low  = 20;
            hv_R3_high = 60;
            ho_R3_Edges.Dispose();
            HOperatorSet.EdgesSubPix(ho_R3_ImageReduced, out ho_R3_Edges, "sobel_fast",
                                     hv_alpha, hv_R3_low, hv_R3_high);
            //stop ()
            //*所有的數值越小,表示容錯範圍大,反之亦然
            ho_R3_ContoursSplit.Dispose();
            HOperatorSet.SegmentContoursXld(ho_R3_Edges, out ho_R3_ContoursSplit, "lines_circles",
                                            17, 4, 2);
            //Display the results
            //===========================================================
            HOperatorSet.CountObj(ho_R3_ContoursSplit, out hv_R3_NumSegments);
            hv_NumCircles       = 0;
            hv_Num_Circle_Point = 0;
            hv_R3 = 999;
            for (hv_i = 1; hv_i.Continue(hv_R3_NumSegments, 1); hv_i = hv_i.TupleAdd(1))
            {
                ho_R3_SingleSegment.Dispose();
                HOperatorSet.SelectObj(ho_R3_ContoursSplit, out ho_R3_SingleSegment, hv_i);
                HOperatorSet.GetContourGlobalAttribXld(ho_R3_SingleSegment, "cont_approx",
                                                       out hv_Attrib);
                if ((int)(new HTuple(hv_Attrib.TupleEqual(1))) != 0)
                {
                    HOperatorSet.FitCircleContourXld(ho_R3_SingleSegment, "atukey", -1, 2,
                                                     hv_Num_Circle_Point, 5, 2, out hv_R3_Row, out hv_R3_Column, out hv_R3_Radius,
                                                     out hv_R3_StartPhi, out hv_R3_EndPhi, out hv_R3_PointOrder);

                    hv_NumCircles = hv_NumCircles + 1;
                    if ((int)(new HTuple(hv_R3.TupleGreater(hv_R3_Radius))) != 0)
                    {
                        hv_R3   = hv_R3_Radius.Clone();
                        mResult = new CircleResult()
                        {
                            Row        = new HTuple(hv_R3_Row),
                            Col        = new HTuple(hv_R3_Column),
                            Radius     = new HTuple(hv_R3_Radius),
                            StartPhi   = new HTuple(hv_R3_StartPhi),
                            EndPhi     = new HTuple(hv_R3_EndPhi),
                            PointOrder = new HTuple(hv_R3_PointOrder),
                        };
                        //HOperatorSet.SetTposition(hv_WindowHandle, (hv_R3_Pos_Row - hv_msgOffsetY) + 60,
                        //	(hv_R3_Pos_Col + hv_msgOffsetX) - 60);
                        //HOperatorSet.WriteString(hv_WindowHandle, "R3");
                        //stop ()
                    }
                }
            }

            //*****R3 End
            ho_R3_Circle.Dispose();
            ho_R3_ROI_Image.Dispose();
            ho_R3_Region.Dispose();
            ho_R3_ImageReduced.Dispose();
            ho_R3_Edges.Dispose();
            ho_R3_ContoursSplit.Dispose();
            ho_R3_SingleSegment.Dispose();
            ho_R3_ContEllipse.Dispose();


            return(mResult);
        }
예제 #26
0
        public MeasureResult Action()
        {
            #region 輸出結果
            CircleResult mResult = null;
            #endregion

            // Local iconic variables

            HObject ho_R3_Circle = null;
            HObject ho_R3_ROI_Image = null, ho_R3_Region = null, ho_R3_ImageReduced = null;
            HObject ho_R3_Edges = null, ho_R3_ContoursSplit = null, ho_R3_SingleSegment = null;
            HObject ho_R3_ContEllipse = null;

            // Local control variables

            HTuple hv_msgOffsetY, hv_msgOffsetX;
            HTuple hv_STD_Row;
            HTuple hv_STD_Col, hv_Img_Row, hv_Img_Col, hv_Img_Rotate_Angle;
            HTuple hv_OffsetRow, hv_OffsetCol, hv_R3_R;
            HTuple hv_STD_R3_Row = new HTuple(), hv_STD_R3_Col = new HTuple();
            HTuple hv_STD_R3_V_Row = new HTuple(), hv_STD_R3_V_Col = new HTuple();
            HTuple hv_R3_X = new HTuple(), hv_R3_Y = new HTuple(), hv_R3_Pos_Row = new HTuple();
            HTuple hv_R3_Pos_Col = new HTuple(), hv_alpha = new HTuple();
            HTuple hv_R3_low = new HTuple(), hv_R3_high = new HTuple();
            HTuple hv_R3_NumSegments = new HTuple(), hv_NumCircles = new HTuple();
            HTuple hv_Num_Circle_Point = new HTuple(), hv_R3 = new HTuple();
            HTuple hv_i = new HTuple(), hv_Attrib = new HTuple(), hv_R3_Row = new HTuple();
            HTuple hv_R3_Column = new HTuple(), hv_R3_Radius = new HTuple();
            HTuple hv_R3_StartPhi = new HTuple(), hv_R3_EndPhi = new HTuple();
            HTuple hv_R3_PointOrder = new HTuple(), hv_R3_MinDist = new HTuple();
            HTuple hv_R3_MaxDist = new HTuple(), hv_R3_AvgDist = new HTuple();
            HTuple hv_R3_SigmaDist = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_R3_Circle);
            HOperatorSet.GenEmptyObj(out ho_R3_ROI_Image);
            HOperatorSet.GenEmptyObj(out ho_R3_Region);
            HOperatorSet.GenEmptyObj(out ho_R3_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_R3_Edges);
            HOperatorSet.GenEmptyObj(out ho_R3_ContoursSplit);
            HOperatorSet.GenEmptyObj(out ho_R3_SingleSegment);
            HOperatorSet.GenEmptyObj(out ho_R3_ContEllipse);

            //Measure: SDMS_R3
            //Author: John Hsieh
            //Date: 2012
            //ho_Image.Dispose();
            //HOperatorSet.ReadImage(out ho_Image, "Images/STD.bmp");
            //dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle);
            //// dev_update_off(...); only in hdevelop
            HOperatorSet.SetSystem("border_shape_models", "false");

            //****Message Args
            hv_msgOffsetY = 100;
            hv_msgOffsetX = 100;

            //****Model All
            //HOperatorSet.ReadShapeModel("D:/Projects/Halcon/SDMS/SDMS_Measure/Model/MatchingAll.shm",
            //	out hv_AllModelId);
            //ho_AllModelContours.Dispose();
            //HOperatorSet.GetShapeModelContours(out ho_AllModelContours, hv_AllModelId, 1);
            //HOperatorSet.FindShapeModel(ho_Image, hv_AllModelId, (new HTuple(0)).TupleRad()
            //	, (new HTuple(360)).TupleRad(), 0.5, 1, 0.5, "least_squares", 6, 0.75, out hv_AllModelRow,
            //	out hv_AllModelColumn, out hv_AllModelAngle, out hv_AllModelScore);

            //****Model Args

            //STD 中心點
            hv_STD_Row = 772;
            hv_STD_Col = 1003;

            //目前圖形 中心點
            hv_Img_Row = hv_AllModelRow.Clone();
            hv_Img_Col = hv_AllModelColumn.Clone();

            //目前圖形 Rotate Angle
            hv_Img_Rotate_Angle = hv_AllModelAngle.Clone();

            //目前圖形偏移量
            hv_OffsetRow = hv_Img_Row - hv_STD_Row;
            hv_OffsetCol = hv_Img_Col - hv_STD_Col;

            //****Display
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.ClearWindow(HDevWindowStack.GetActive());
            }
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
            }
            //*****R3
            hv_R3_R = 29;

            //STD R3_ 位置
            hv_STD_R3_Row = 252;
            hv_STD_R3_Col = 1381;

            //STD 向量 STD_R3_
            hv_STD_R3_V_Row = hv_STD_R3_Row - hv_STD_Row;
            hv_STD_R3_V_Col = hv_STD_R3_Col - hv_STD_Col;

            //R3_X, R3_Y 分量
            hv_R3_X = (hv_STD_R3_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_R3_V_Row * (hv_Img_Rotate_Angle.TupleSin()
                ));
            hv_R3_Y = (hv_STD_R3_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_R3_V_Col * (hv_Img_Rotate_Angle.TupleSin()
                ));

            //目前圖形 R3_ 位置
            hv_R3_Pos_Row = (hv_STD_Row + hv_R3_Y) + hv_OffsetRow;
            hv_R3_Pos_Col = (hv_STD_Col + hv_R3_X) + hv_OffsetCol;

            //** 開始計算
            ho_R3_Circle.Dispose();
            HOperatorSet.GenCircle(out ho_R3_Circle, hv_R3_Pos_Row, hv_R3_Pos_Col, hv_R3_R);
            ho_R3_ROI_Image.Dispose();
            HOperatorSet.ReduceDomain(ho_Image, ho_R3_Circle, out ho_R3_ROI_Image);
            ho_R3_Region.Dispose();
            HOperatorSet.FastThreshold(ho_R3_ROI_Image, out ho_R3_Region, 100, 255, 15);
            ho_R3_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_R3_ROI_Image, ho_R3_Region, out ho_R3_ImageReduced
                );
            //stop ()
            //sobel_fast 具有較寬的選擇範圍,搭配 alpha 參數 (alpha 越大, 容錯範圍大)
            hv_alpha = 0.9;
            hv_R3_low = 20;
            hv_R3_high = 60;
            ho_R3_Edges.Dispose();
            HOperatorSet.EdgesSubPix(ho_R3_ImageReduced, out ho_R3_Edges, "sobel_fast",
                hv_alpha, hv_R3_low, hv_R3_high);
            //stop ()
            //*所有的數值越小,表示容錯範圍大,反之亦然
            ho_R3_ContoursSplit.Dispose();
            HOperatorSet.SegmentContoursXld(ho_R3_Edges, out ho_R3_ContoursSplit, "lines_circles",
                17, 4, 2);
            //Display the results
            //===========================================================
            HOperatorSet.CountObj(ho_R3_ContoursSplit, out hv_R3_NumSegments);
            hv_NumCircles = 0;
            hv_Num_Circle_Point = 0;
            hv_R3 = 999;
            for (hv_i = 1; hv_i.Continue(hv_R3_NumSegments, 1); hv_i = hv_i.TupleAdd(1))
            {
                ho_R3_SingleSegment.Dispose();
                HOperatorSet.SelectObj(ho_R3_ContoursSplit, out ho_R3_SingleSegment, hv_i);
                HOperatorSet.GetContourGlobalAttribXld(ho_R3_SingleSegment, "cont_approx",
                    out hv_Attrib);
                if ((int)(new HTuple(hv_Attrib.TupleEqual(1))) != 0)
                {
                    HOperatorSet.FitCircleContourXld(ho_R3_SingleSegment, "atukey", -1, 2,
                        hv_Num_Circle_Point, 5, 2, out hv_R3_Row, out hv_R3_Column, out hv_R3_Radius,
                        out hv_R3_StartPhi, out hv_R3_EndPhi, out hv_R3_PointOrder);

                    hv_NumCircles = hv_NumCircles + 1;
                    if ((int)(new HTuple(hv_R3.TupleGreater(hv_R3_Radius))) != 0)
                    {
                        hv_R3 = hv_R3_Radius.Clone();
                        mResult = new CircleResult()
                        {
                            Row = new HTuple(hv_R3_Row),
                            Col = new HTuple(hv_R3_Column),
                            Radius = new HTuple(hv_R3_Radius),
                            StartPhi = new HTuple(hv_R3_StartPhi),
                            EndPhi = new HTuple(hv_R3_EndPhi),
                            PointOrder = new HTuple(hv_R3_PointOrder),
                        };
                        //HOperatorSet.SetTposition(hv_WindowHandle, (hv_R3_Pos_Row - hv_msgOffsetY) + 60,
                        //	(hv_R3_Pos_Col + hv_msgOffsetX) - 60);
                        //HOperatorSet.WriteString(hv_WindowHandle, "R3");
                        //stop ()
                    }
                }
            }

            //*****R3 End
            ho_R3_Circle.Dispose();
            ho_R3_ROI_Image.Dispose();
            ho_R3_Region.Dispose();
            ho_R3_ImageReduced.Dispose();
            ho_R3_Edges.Dispose();
            ho_R3_ContoursSplit.Dispose();
            ho_R3_SingleSegment.Dispose();
            ho_R3_ContEllipse.Dispose();

            return mResult;
        }
예제 #27
0
        /// <summary>
        /// 测试小球OKNG的方法
        /// </summary>
        /// <param name="hv_liudao">小球所在位置</param>
        /// <param name="hv_channels">流道信息</param>
        /// <param name="hv_liudaoR">小球直径(像素值)</param>
        /// <param name="hv_myLength">小球标准直径(像素值)</param>
        /// <param name="hv_myThreashod">小球阈值范围</param>
        /// <param name="hv_myResult">是否都是好的</param>
        /// <param name="hv_NGLengths">NG小球直径</param>
        /// <param name="hv_NGChls">NG小球所在流道</param>
        public void testOKNG2(HTuple hv_liudao, HTuple hv_channels, HTuple hv_liudaoR,
                              HTuple hv_myLength, HTuple hv_myThreashod, out HTuple hv_myResult, out HTuple hv_NGLengths,
                              out HTuple hv_NGChls)
        {
            // Local iconic variables

            // Local control variables

            HTuple hv_num = null, hv_num2 = null, hv_Index1 = null;
            HTuple hv_Index2 = new HTuple(), hv_lll = new HTuple();
            HTuple hv_ff1 = new HTuple(), hv_ff2 = new HTuple(), hv_ff = new HTuple();

            // Initialize local and output iconic variables
            hv_myResult = 1;
            hv_num      = new HTuple(hv_liudao.TupleLength());
            hv_num2     = new HTuple(hv_channels.TupleLength());

            hv_NGChls    = new HTuple();
            hv_NGLengths = new HTuple();


            HTuple end_val8  = hv_num - 1;
            HTuple step_val8 = 1;

            for (hv_Index1 = 0; hv_Index1.Continue(end_val8, step_val8); hv_Index1 = hv_Index1.TupleAdd(step_val8))
            {
                HTuple end_val9  = hv_num2;
                HTuple step_val9 = 1;
                for (hv_Index2 = 1; hv_Index2.Continue(end_val9, step_val9); hv_Index2 = hv_Index2.TupleAdd(step_val9))
                {
                    if ((int)(new HTuple(((hv_liudao.TupleSelect(hv_Index1))).TupleLess(hv_channels.TupleSelect(
                                                                                            hv_Index2)))) != 0)
                    {
                        break;
                    }
                }
                hv_lll = hv_Index2 - 1;
                hv_ff1 = new HTuple(((hv_liudaoR.TupleSelect(hv_Index1))).TupleGreater((hv_myLength.TupleSelect(
                                                                                            hv_lll)) - (hv_myThreashod.TupleSelect(hv_lll))));
                hv_ff2 = new HTuple(((hv_liudaoR.TupleSelect(hv_Index1))).TupleLess((hv_myLength.TupleSelect(
                                                                                         hv_lll)) + (hv_myThreashod.TupleSelect(hv_lll))));
                hv_ff = hv_ff1.TupleAnd(hv_ff2);
                if ((int)(hv_ff) != 0)
                {
                }
                else
                {
                    hv_NGChls    = hv_NGChls.TupleConcat(hv_Index2);
                    hv_NGLengths = hv_NGLengths.TupleConcat(hv_liudaoR.TupleSelect(hv_Index1));
                }
                hv_myResult = hv_myResult.TupleAnd(hv_ff);
            }

            return;
        }
예제 #28
0
        public MeasureResult Action()
        {
            #region 輸出結果
            CircleResult mResult = null;
            #endregion

            // Local iconic variables

            HObject ho_R17_Circle = null;
            HObject ho_R17_ROI_Image = null, ho_R17_Region = null, ho_R17_ImageReduced = null;
            HObject ho_R17_Edges = null, ho_R17_ContoursSplit = null, ho_R17_SingleSegment = null;
            HObject ho_R17_ContEllipse = null;

            // Local control variables

            HTuple hv_msgOffsetY, hv_msgOffsetX;
            HTuple hv_STD_Row;
            HTuple hv_STD_Col, hv_Img_Row, hv_Img_Col, hv_Img_Rotate_Angle;
            HTuple hv_OffsetRow, hv_OffsetCol, hv_STD_R17_Row, hv_STD_R17_Col;
            HTuple hv_STD_R17_V_Row, hv_STD_R17_V_Col, hv_R17_X, hv_R17_Y;
            HTuple hv_R17_Pos_Row, hv_R17_Pos_Col, hv_R17_R;
            HTuple hv_alpha = new HTuple(), hv_R17_low = new HTuple();
            HTuple hv_R17_high = new HTuple(), hv_R17_NumSegments = new HTuple();
            HTuple hv_NumCircles = new HTuple(), hv_Num_Circle_Point = new HTuple();
            HTuple hv_R17 = new HTuple(), hv_i = new HTuple(), hv_Attrib = new HTuple();
            HTuple hv_R17_Row = new HTuple(), hv_R17_Column = new HTuple();
            HTuple hv_R17_Radius = new HTuple(), hv_R17_StartPhi = new HTuple();
            HTuple hv_R17_EndPhi = new HTuple(), hv_R17_PointOrder = new HTuple();
            HTuple hv_R17_MinDist = new HTuple(), hv_R17_MaxDist = new HTuple();
            HTuple hv_R17_AvgDist = new HTuple(), hv_R17_SigmaDist = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_R17_Circle);
            HOperatorSet.GenEmptyObj(out ho_R17_ROI_Image);
            HOperatorSet.GenEmptyObj(out ho_R17_Region);
            HOperatorSet.GenEmptyObj(out ho_R17_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_R17_Edges);
            HOperatorSet.GenEmptyObj(out ho_R17_ContoursSplit);
            HOperatorSet.GenEmptyObj(out ho_R17_SingleSegment);
            HOperatorSet.GenEmptyObj(out ho_R17_ContEllipse);

            //Measure: SDMS_R17
            //Author: John Hsieh
            //Date: 2012
            // dev_update_off(...); only in hdevelop
            HOperatorSet.SetSystem("border_shape_models", "false");

            //****Message Args
            hv_msgOffsetY = 100;
            hv_msgOffsetX = 100;

            //****Model Args

            //STD 中心點
            hv_STD_Row = 772;
            hv_STD_Col = 1003;

            //目前圖形 中心點
            hv_Img_Row = hv_AllModelRow.Clone();
            hv_Img_Col = hv_AllModelColumn.Clone();

            //目前圖形 Rotate Angle
            hv_Img_Rotate_Angle = hv_AllModelAngle.Clone();

            //目前圖形偏移量
            hv_OffsetRow = hv_Img_Row - hv_STD_Row;
            hv_OffsetCol = hv_Img_Col - hv_STD_Col;

            //****Display
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.ClearWindow(HDevWindowStack.GetActive());
            }
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
            }
            //*****R17
            //STD R17_ 位置
            hv_STD_R17_Row = 802;
            hv_STD_R17_Col = 660;

            //STD 向量 STD_R17_
            hv_STD_R17_V_Row = hv_STD_R17_Row - hv_STD_Row;
            hv_STD_R17_V_Col = hv_STD_R17_Col - hv_STD_Col;

            //R17_X, R17_Y 分量
            hv_R17_X = (hv_STD_R17_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_R17_V_Row * (hv_Img_Rotate_Angle.TupleSin()
                ));
            hv_R17_Y = (hv_STD_R17_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_R17_V_Col * (hv_Img_Rotate_Angle.TupleSin()
                ));

            //目前圖形 R17_ 位置
            hv_R17_Pos_Row = (hv_STD_Row + hv_R17_Y) + hv_OffsetRow;
            hv_R17_Pos_Col = (hv_STD_Col + hv_R17_X) + hv_OffsetCol;

            hv_R17_R = 17;

            ho_R17_Circle.Dispose();
            HOperatorSet.GenCircle(out ho_R17_Circle, hv_R17_Pos_Row, hv_R17_Pos_Col, hv_R17_R);
            if (HDevWindowStack.IsOpen())
            {
                //dev_display (R17_Circle)
            }
            //stop ()

            ho_R17_ROI_Image.Dispose();
            HOperatorSet.ReduceDomain(ho_Image, ho_R17_Circle, out ho_R17_ROI_Image);
            ho_R17_Region.Dispose();
            HOperatorSet.FastThreshold(ho_R17_ROI_Image, out ho_R17_Region, 100, 255, 15);
            ho_R17_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_R17_ROI_Image, ho_R17_Region, out ho_R17_ImageReduced
                );
            //stop ()
            //sobel_fast 具有較寬的選擇範圍,搭配 alpha 參數 (alpha 越大, 容錯範圍大)
            hv_alpha = 0.9;
            hv_R17_low = 10;
            hv_R17_high = 60;
            ho_R17_Edges.Dispose();
            HOperatorSet.EdgesSubPix(ho_R17_ImageReduced, out ho_R17_Edges, "sobel_fast",
                hv_alpha, hv_R17_low, hv_R17_high);
            //stop ()
            //*所有的數值越小,表示容錯範圍大,反之亦然
            ho_R17_ContoursSplit.Dispose();
            HOperatorSet.SegmentContoursXld(ho_R17_Edges, out ho_R17_ContoursSplit, "lines_circles",
                17, 1, 1);
            //Display the results
            //===========================================================
            HOperatorSet.CountObj(ho_R17_ContoursSplit, out hv_R17_NumSegments);
            hv_NumCircles = 0;
            hv_Num_Circle_Point = 0;
            hv_R17 = 999;
            for (hv_i = 1; hv_i.Continue(hv_R17_NumSegments, 1); hv_i = hv_i.TupleAdd(1))
            {
                ho_R17_SingleSegment.Dispose();
                HOperatorSet.SelectObj(ho_R17_ContoursSplit, out ho_R17_SingleSegment, hv_i);
                HOperatorSet.GetContourGlobalAttribXld(ho_R17_SingleSegment, "cont_approx",
                    out hv_Attrib);
                if ((int)(new HTuple(hv_Attrib.TupleEqual(1))) != 0)
                {
                    HOperatorSet.FitCircleContourXld(ho_R17_SingleSegment, "atukey", -1, 2,
                        hv_Num_Circle_Point, 5, 2, out hv_R17_Row, out hv_R17_Column, out hv_R17_Radius,
                        out hv_R17_StartPhi, out hv_R17_EndPhi, out hv_R17_PointOrder);
                    ho_R17_ContEllipse.Dispose();
                    HOperatorSet.GenEllipseContourXld(out ho_R17_ContEllipse, hv_R17_Row, hv_R17_Column,
                        0, hv_R17_Radius, hv_R17_Radius, 0, (new HTuple(360)).TupleRad(), "positive",
                        1.0);
                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.DispObj(ho_R17_ContEllipse, HDevWindowStack.GetActive());
                    }
                    HOperatorSet.DistEllipseContourXld(ho_R17_SingleSegment, "algebraic", -1,
                        0, hv_R17_Row, hv_R17_Column, 0, hv_R17_Radius, hv_R17_Radius, out hv_R17_MinDist,
                        out hv_R17_MaxDist, out hv_R17_AvgDist, out hv_R17_SigmaDist);
                    hv_NumCircles = hv_NumCircles + 1;
                    if ((int)(new HTuple(hv_R17.TupleGreater(hv_R17_Radius))) != 0)
                    {
                        hv_R17 = hv_R17_Radius.Clone();
                        mResult = new CircleResult()
                        {
                            Row = new HTuple(hv_R17_Row),
                            Col = new HTuple(hv_R17_Column),
                            Radius = new HTuple(hv_R17_Radius),
                            StartPhi = new HTuple(hv_R17_StartPhi),
                            EndPhi = new HTuple(hv_R17_EndPhi),
                            PointOrder = new HTuple(hv_R17_PointOrder),
                        };
                    }
                    //stop ()
                }
            }

            ho_R17_Circle.Dispose();
            ho_R17_ROI_Image.Dispose();
            ho_R17_Region.Dispose();
            ho_R17_ImageReduced.Dispose();
            ho_R17_Edges.Dispose();
            ho_R17_ContoursSplit.Dispose();
            ho_R17_SingleSegment.Dispose();
            ho_R17_ContEllipse.Dispose();

            return mResult;
        }
예제 #29
0
        public MeasureResult Action()
        {
            #region 輸出結果
            CircleResult mResult = null;
            #endregion
            // Local iconic variables

            HObject ho_R6_Circle = null;
            HObject ho_R6_ROI_Image = null, ho_R6_Region = null, ho_R6_RegionBorder = null;
            HObject ho_R6_RegionDilation = null, ho_R6_ImageReduced = null;
            HObject ho_R6_Edges = null, ho_R6_ContoursSplit = null, ho_R6_SingleSegment = null;
            HObject ho_R6_ContEllipse = null;

            // Local control variables

            HTuple hv_msgOffsetY, hv_msgOffsetX;
            HTuple hv_STD_Row;
            HTuple hv_STD_Col, hv_Img_Row, hv_Img_Col, hv_Img_Rotate_Angle;
            HTuple hv_OffsetRow, hv_OffsetCol, hv_R6;
            HTuple hv_STD_R6_Row = new HTuple(), hv_STD_R6_Col = new HTuple();
            HTuple hv_STD_R6_V_Row = new HTuple(), hv_STD_R6_V_Col = new HTuple();
            HTuple hv_R6_X = new HTuple(), hv_R6_Y = new HTuple(), hv_R6_Pos_Row = new HTuple();
            HTuple hv_R6_Pos_Col = new HTuple(), hv_R6_R = new HTuple();
            HTuple hv_R6_R_Max = new HTuple(), hv_R6_R_Inc = new HTuple();
            HTuple hv_R6_NumSegments = new HTuple(), hv_NumCircles = new HTuple();
            HTuple hv_Num_Circle_Point = new HTuple(), hv_i = new HTuple();
            HTuple hv_Attrib = new HTuple(), hv_R6_Row = new HTuple();
            HTuple hv_R6_Column = new HTuple(), hv_R6_Radius = new HTuple();
            HTuple hv_R6_StartPhi = new HTuple(), hv_R6_EndPhi = new HTuple();
            HTuple hv_R6_PointOrder = new HTuple(), hv_R6_MinDist = new HTuple();
            HTuple hv_R6_MaxDist = new HTuple(), hv_R6_AvgDist = new HTuple();
            HTuple hv_R6_SigmaDist = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_R6_Circle);
            HOperatorSet.GenEmptyObj(out ho_R6_ROI_Image);
            HOperatorSet.GenEmptyObj(out ho_R6_Region);
            HOperatorSet.GenEmptyObj(out ho_R6_RegionBorder);
            HOperatorSet.GenEmptyObj(out ho_R6_RegionDilation);
            HOperatorSet.GenEmptyObj(out ho_R6_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_R6_Edges);
            HOperatorSet.GenEmptyObj(out ho_R6_ContoursSplit);
            HOperatorSet.GenEmptyObj(out ho_R6_SingleSegment);
            HOperatorSet.GenEmptyObj(out ho_R6_ContEllipse);

            //Measure: SDMS_R6
            //Author: John Hsieh
            //Date: 2012
            // dev_update_off(...); only in hdevelop
            HOperatorSet.SetSystem("border_shape_models", "false");

            //****Message Args
            hv_msgOffsetY = 100;
            hv_msgOffsetX = 100;

            //****Model Args

            //STD 中心點
            hv_STD_Row = 772;
            hv_STD_Col = 1003;

            //目前圖形 中心點
            hv_Img_Row = hv_AllModelRow.Clone();
            hv_Img_Col = hv_AllModelColumn.Clone();

            //目前圖形 Rotate Angle
            hv_Img_Rotate_Angle = hv_AllModelAngle.Clone();

            //目前圖形偏移量
            hv_OffsetRow = hv_Img_Row - hv_STD_Row;
            hv_OffsetCol = hv_Img_Col - hv_STD_Col;

            //****Display
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.ClearWindow(HDevWindowStack.GetActive());
            }
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
            }
            //*****R6
            hv_R6 = 999;
            //STD R6_ 位置
            hv_STD_R6_Row = 1292;
            hv_STD_R6_Col = 639;

            //STD 向量 STD_R6_
            hv_STD_R6_V_Row = hv_STD_R6_Row - hv_STD_Row;
            hv_STD_R6_V_Col = hv_STD_R6_Col - hv_STD_Col;

            //R6_X, R6_Y 分量
            hv_R6_X = (hv_STD_R6_V_Col * (hv_Img_Rotate_Angle.TupleCos())) + (hv_STD_R6_V_Row * (hv_Img_Rotate_Angle.TupleSin()
                ));
            hv_R6_Y = (hv_STD_R6_V_Row * (hv_Img_Rotate_Angle.TupleCos())) - (hv_STD_R6_V_Col * (hv_Img_Rotate_Angle.TupleSin()
                ));

            //目前圖形 R4 位置
            hv_R6_Pos_Row = (hv_STD_Row + hv_R6_Y) + hv_OffsetRow;
            hv_R6_Pos_Col = (hv_STD_Col + hv_R6_X) + hv_OffsetCol;

            //R6_Region 由半徑 29 開始搜尋, 最大搜尋至 41, Inc =2
            hv_R6_R = 29;
            hv_R6_R_Max = 41;
            hv_R6_R_Inc = 2;
            while ((int)((new HTuple(hv_R6.TupleEqual(999))).TupleAnd(new HTuple(hv_R6_R.TupleLess(
                hv_R6_R_Max)))) != 0)
            {
                //*******************************
                ho_R6_Circle.Dispose();
                HOperatorSet.GenCircle(out ho_R6_Circle, hv_R6_Pos_Row, hv_R6_Pos_Col, hv_R6_R);
                ho_R6_ROI_Image.Dispose();
                HOperatorSet.ReduceDomain(ho_Image, ho_R6_Circle, out ho_R6_ROI_Image);
                ho_R6_Region.Dispose();
                HOperatorSet.Threshold(ho_R6_ROI_Image, out ho_R6_Region, 100, 255);
                ho_R6_RegionBorder.Dispose();
                HOperatorSet.Boundary(ho_R6_Region, out ho_R6_RegionBorder, "inner");
                ho_R6_RegionDilation.Dispose();
                HOperatorSet.DilationCircle(ho_R6_RegionBorder, out ho_R6_RegionDilation,
                    1.5);
                ho_R6_ImageReduced.Dispose();
                HOperatorSet.ReduceDomain(ho_R6_ROI_Image, ho_R6_RegionDilation, out ho_R6_ImageReduced
                    );
                ho_R6_Edges.Dispose();
                HOperatorSet.EdgesSubPix(ho_R6_ImageReduced, out ho_R6_Edges, "lanser2",
                    0.3, 40, 90);

                //fast_threshold (R6_ROI_Image, R6_Region, 80, 255, 15)
                //reduce_domain (R6_ROI_Image, R6_Region, R6_ImageReduced)
                //sobel_fast 具有較寬的選擇範圍,搭配 alpha 參數 (alpha 越大, 容錯範圍大)
                //alpha := 10
                //R6_low := 10
                //R6_high := 60
                //edges_sub_pix (R6_ImageReduced, R6_Edges, 'sobel_fast', alpha, R6_low, R6_high)
                //stop ()
                //*所有的數值越小,表示容錯範圍大,反之亦然
                ho_R6_ContoursSplit.Dispose();
                HOperatorSet.SegmentContoursXld(ho_R6_Edges, out ho_R6_ContoursSplit, "lines_circles",
                    6, 4, 4);

                //Display the results
                //===========================================================
                HOperatorSet.CountObj(ho_R6_ContoursSplit, out hv_R6_NumSegments);
                hv_NumCircles = 0;
                hv_Num_Circle_Point = 0;
                for (hv_i = 1; hv_i.Continue(hv_R6_NumSegments, 1); hv_i = hv_i.TupleAdd(1))
                {
                    ho_R6_SingleSegment.Dispose();
                    HOperatorSet.SelectObj(ho_R6_ContoursSplit, out ho_R6_SingleSegment, hv_i);
                    HOperatorSet.GetContourGlobalAttribXld(ho_R6_SingleSegment, "cont_approx",
                        out hv_Attrib);
                    if ((int)(new HTuple(hv_Attrib.TupleEqual(1))) != 0)
                    {
                        HOperatorSet.FitCircleContourXld(ho_R6_SingleSegment, "atukey", -1, 2,
                            hv_Num_Circle_Point, 5, 2, out hv_R6_Row, out hv_R6_Column, out hv_R6_Radius,
                            out hv_R6_StartPhi, out hv_R6_EndPhi, out hv_R6_PointOrder);
                        ho_R6_ContEllipse.Dispose();
                        HOperatorSet.GenEllipseContourXld(out ho_R6_ContEllipse, hv_R6_Row, hv_R6_Column,
                            0, hv_R6_Radius, hv_R6_Radius, 0, (new HTuple(360)).TupleRad(), "positive",
                            1.0);
                        if (HDevWindowStack.IsOpen())
                        {
                            HOperatorSet.DispObj(ho_R6_ContEllipse, HDevWindowStack.GetActive()
                                );
                        }
                        HOperatorSet.DistEllipseContourXld(ho_R6_SingleSegment, "algebraic",
                            -1, 0, hv_R6_Row, hv_R6_Column, 0, hv_R6_Radius, hv_R6_Radius, out hv_R6_MinDist,
                            out hv_R6_MaxDist, out hv_R6_AvgDist, out hv_R6_SigmaDist);
                        hv_NumCircles = hv_NumCircles + 1;
                        if ((int)(new HTuple(hv_R6.TupleGreater(hv_R6_Radius))) != 0)
                        {
                            hv_R6 = hv_R6_Radius.Clone();
                            mResult = new CircleResult()
                            {
                                Row = new HTuple(hv_R6_Row),
                                Col = new HTuple(hv_R6_Column),
                                Radius = new HTuple(hv_R6_Radius),
                                StartPhi = new HTuple(hv_R6_StartPhi),
                                EndPhi = new HTuple(hv_R6_EndPhi),
                                PointOrder = new HTuple(hv_R6_PointOrder),
                            };
                        }
                    }
                }
                hv_R6_R = hv_R6_R + hv_R6_R_Inc;
            }

            ho_R6_Circle.Dispose();
            ho_R6_ROI_Image.Dispose();
            ho_R6_Region.Dispose();
            ho_R6_RegionBorder.Dispose();
            ho_R6_RegionDilation.Dispose();
            ho_R6_ImageReduced.Dispose();
            ho_R6_Edges.Dispose();
            ho_R6_ContoursSplit.Dispose();
            ho_R6_SingleSegment.Dispose();
            ho_R6_ContEllipse.Dispose();

            return mResult;
        }