private void action()
        {
            // Stack for temporary objects
            HObject[] OTemp = new HObject[20];

            // Local iconic variables

            HObject ho_Rectangle, ho_Rectangle1, ho_UnionContours1;
            HObject ho_Region, ho_ContoursL = null, ho_Contour1 = null;
            HObject ho_ContoursSplit2 = null, ho_RegionLines2 = null, ho_Contour = null;
            HObject ho_ImageReduced1 = null, ho_Edges1 = null, ho_ContoursSplit1 = null;
            HObject ho_UnionContours = null, ho_RegionLines1 = null, ho_Contour2 = null;

            // Local control variables

            HTuple hv_Width = new HTuple();
            HTuple hv_Height = 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_RowBegin2 = new HTuple(), hv_ColBegin2 = new HTuple();
            HTuple hv_RowEnd2 = new HTuple(), hv_ColEnd2 = new HTuple();
            HTuple hv_Nr2 = new HTuple(), hv_Nc2 = new HTuple(), hv_Dist2 = new HTuple();
            HTuple hv_dis2 = new HTuple(), hv_Indices2 = new HTuple();
            HTuple hv_R = new HTuple(), hv_C = new HTuple(), hv_RowBegin1 = new HTuple();
            HTuple hv_ColBegin1 = new HTuple(), hv_RowEnd1 = new HTuple();
            HTuple hv_ColEnd1 = new HTuple(), hv_Nr1 = new HTuple();
            HTuple hv_Nc1 = new HTuple(), hv_Dist1 = new HTuple();
            HTuple hv_dis1 = new HTuple(), hv_Indices1 = new HTuple();
            HTuple hv_R1 = new HTuple(), hv_C1 = new HTuple(), hv_DistanceMin1 = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_Rectangle1);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_ContoursL);
            HOperatorSet.GenEmptyObj(out ho_UnionContours1);
            HOperatorSet.GenEmptyObj(out ho_Contour1);
            HOperatorSet.GenEmptyObj(out ho_ContoursSplit2);
            HOperatorSet.GenEmptyObj(out ho_RegionLines2);
            HOperatorSet.GenEmptyObj(out ho_Contour);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced1);
            HOperatorSet.GenEmptyObj(out ho_Edges1);
            HOperatorSet.GenEmptyObj(out ho_ContoursSplit1);
            HOperatorSet.GenEmptyObj(out ho_UnionContours);
            HOperatorSet.GenEmptyObj(out ho_Contour2);
            HOperatorSet.GenEmptyObj(out ho_RegionLines1);
            HOperatorSet.Union1(algorithm.Region, out RegionToDisp);
            try
            {
                HOperatorSet.GetImageSize(Image, out hv_Width, out hv_Height);
                ho_Rectangle.Dispose();
                HOperatorSet.GenRectangle1(out ho_Rectangle, DRow1m, DCol1m, DRow2m, DCol2m);
                ho_Rectangle1.Dispose();
                HOperatorSet.GenRectangle1(out ho_Rectangle1, DRow1n, DCol1n, DRow2n, DCol2n);

                //*
                //找左侧点
                hv_shapeParam = new HTuple();
                hv_shapeParam = hv_shapeParam.TupleConcat(DRow1m);
                hv_shapeParam = hv_shapeParam.TupleConcat((DCol1m + DCol2m) / 2);
                hv_shapeParam = hv_shapeParam.TupleConcat(DRow2m);
                hv_shapeParam = hv_shapeParam.TupleConcat((DCol1m + DCol2m) / 2);

                HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandleL);
                HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandleL, hv_Width, hv_Height);
                HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandleL, "line", hv_shapeParam,
                                                       (DCol2m - DCol1m) / 2, 1, 1, 50, new HTuple(), new HTuple(), out hv_Index);
                //设置参数
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "measure_transition",
                                                     "positive");
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "num_measures",
                                                     100);
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "num_instances",
                                                     1);
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "measure_select",
                                                     "last");

                //开始找边缘
                HOperatorSet.ApplyMetrologyModel(Image, hv_MetrologyHandleL);
                ho_ContoursL.Dispose();
                HOperatorSet.GetMetrologyObjectMeasures(out ho_ContoursL, hv_MetrologyHandleL,
                                                        "all", "all", out hv_RowL, out hv_ColumnL);
                //gen_cross_contour_xld (CrossL, RowL, ColumnL, 6, 0)
                ///释放测量句柄
                HOperatorSet.ClearMetrologyModel(hv_MetrologyHandleL);
                ho_Contour1.Dispose();
                HOperatorSet.GenContourPolygonXld(out ho_Contour1, hv_RowL, hv_ColumnL);
                ho_ContoursSplit2.Dispose();
                HOperatorSet.SegmentContoursXld(ho_Contour1, out ho_ContoursSplit2, "lines_circles",
                                                5, 4, 2);
                ho_UnionContours1.Dispose();
                HOperatorSet.UnionCollinearContoursXld(ho_ContoursSplit2, out ho_UnionContours1,
                                                       10000, 10000, 2, 0.1, "attr_keep");

                HOperatorSet.FitLineContourXld(ho_UnionContours1, "tukey", -1, 0, 5, 2, out hv_RowBegin2,
                                               out hv_ColBegin2, out hv_RowEnd2, out hv_ColEnd2, out hv_Nr2, out hv_Nc2,
                                               out hv_Dist2);
                hv_dis2 = ((((hv_RowBegin2 - hv_RowEnd2) * (hv_RowBegin2 - hv_RowEnd2)) + ((hv_ColBegin2 - hv_ColEnd2) * (hv_ColBegin2 - hv_ColEnd2)))).TupleSqrt()
                ;
                HOperatorSet.TupleFind(hv_dis2, hv_dis2.TupleMax(), out hv_Indices2);
                ho_RegionLines2.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLines2, hv_RowBegin2.TupleSelect(hv_Indices2),
                                           hv_ColBegin2.TupleSelect(hv_Indices2), hv_RowEnd2.TupleSelect(hv_Indices2),
                                           hv_ColEnd2.TupleSelect(hv_Indices2));

                hv_R = new HTuple();
                hv_R = hv_R.TupleConcat(hv_RowBegin2.TupleSelect(
                                            hv_Indices2));
                hv_R = hv_R.TupleConcat(hv_RowEnd2.TupleSelect(hv_Indices2));
                hv_C = new HTuple();
                hv_C = hv_C.TupleConcat(hv_ColBegin2.TupleSelect(
                                            hv_Indices2));
                hv_C = hv_C.TupleConcat(hv_ColEnd2.TupleSelect(hv_Indices2));
                ho_Contour.Dispose();
                HOperatorSet.GenContourPolygonXld(out ho_Contour, hv_R, hv_C);
                //2
                ho_ImageReduced1.Dispose();
                HOperatorSet.ReduceDomain(Image, ho_Rectangle1, out ho_ImageReduced1);
                ho_Edges1.Dispose();
                HOperatorSet.EdgesSubPix(ho_ImageReduced1, out ho_Edges1, "canny", 1, 20, 40);
                ho_ContoursSplit1.Dispose();
                HOperatorSet.SegmentContoursXld(ho_Edges1, out ho_ContoursSplit1, "lines_circles",
                                                5, 4, 2);
                ho_UnionContours.Dispose();
                HOperatorSet.UnionCollinearContoursXld(ho_ContoursSplit1, out ho_UnionContours,
                                                       10000, 10000, 2, 0.1, "attr_keep");
                HOperatorSet.FitLineContourXld(ho_UnionContours, "tukey", -1, 0, 5, 2, out hv_RowBegin1,
                                               out hv_ColBegin1, out hv_RowEnd1, out hv_ColEnd1, out hv_Nr1, out hv_Nc1,
                                               out hv_Dist1);
                hv_dis1 = ((((hv_RowBegin1 - hv_RowEnd1) * (hv_RowBegin1 - hv_RowEnd1)) + ((hv_ColBegin1 - hv_ColEnd1) * (hv_ColBegin1 - hv_ColEnd1)))).TupleSqrt()
                ;
                HOperatorSet.TupleFind(hv_dis1, hv_dis1.TupleMax(), out hv_Indices1);
                ho_RegionLines1.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLines1, hv_RowBegin1.TupleSelect(hv_Indices1),
                                           hv_ColBegin1.TupleSelect(hv_Indices1), hv_RowEnd1.TupleSelect(hv_Indices1),
                                           hv_ColEnd1.TupleSelect(hv_Indices1));

                //distance_cc_min (UnionContours, UnionContours, 'fast_point_to_segment', DistanceMin)

                //distance_pl ((RowBegin2[Indices2]+RowEnd2[Indices2])/2, (ColBegin2[Indices2]+ColEnd2[Indices2]), RowBegin1[Indices1], ColBegin1[Indices1], RowEnd1[Indices1], ColEnd1[Indices1], Distance)
                hv_R1 = new HTuple();
                hv_R1 = hv_R1.TupleConcat(hv_RowBegin1.TupleSelect(
                                              hv_Indices1));
                hv_R1 = hv_R1.TupleConcat(hv_RowEnd1.TupleSelect(hv_Indices1));
                hv_C1 = new HTuple();
                hv_C1 = hv_C1.TupleConcat(hv_ColBegin1.TupleSelect(
                                              hv_Indices1));
                hv_C1 = hv_C1.TupleConcat(hv_ColEnd1.TupleSelect(hv_Indices1));
                ho_Contour2.Dispose();
                HOperatorSet.GenContourPolygonXld(out ho_Contour2, hv_R1, hv_C1);

                HOperatorSet.DistanceCcMin(ho_Contour, ho_Contour2, "fast_point_to_segment",
                                           out hv_DistanceMin1);
                {
                    HObject ExpTmpOutVar_0;
                    HOperatorSet.Union2(ho_Region, ho_RegionLines1, out ExpTmpOutVar_0);
                    ho_Region.Dispose();
                    ho_Region = ExpTmpOutVar_0;
                }
                {
                    HObject ExpTmpOutVar_0;
                    HOperatorSet.Union2(ho_Region, ho_RegionLines2, out ExpTmpOutVar_0);
                    ho_Region.Dispose();
                    ho_Region = ExpTmpOutVar_0;
                }
                HOperatorSet.Union1(ho_Region, out RegionToDisp);

                HTuple hv_result = GetHv_result();

                hv_result = hv_result.TupleConcat("高度");
                hv_result = hv_result.TupleConcat(hv_DistanceMin1.D * pixeldist);
                result    = hv_result.Clone();
                ho_Rectangle.Dispose();
                ho_Rectangle1.Dispose();
                ho_Region.Dispose();
                ho_ContoursL.Dispose();
                ho_Contour1.Dispose();
                ho_ContoursSplit2.Dispose();
                ho_UnionContours1.Dispose();
                ho_RegionLines2.Dispose();
                ho_Contour.Dispose();
                ho_ImageReduced1.Dispose();
                ho_Edges1.Dispose();
                ho_ContoursSplit1.Dispose();
                ho_UnionContours.Dispose();
                ho_RegionLines1.Dispose();
                ho_Contour2.Dispose();
                algorithm.Region.Dispose();
            }
            catch
            {
                HTuple hv_result = GetHv_result();
                hv_result = hv_result.TupleConcat("高度");
                hv_result = hv_result.TupleConcat(0);
                result    = hv_result.Clone();
                ho_Rectangle.Dispose();
                ho_Rectangle1.Dispose();
                ho_Region.Dispose();
                ho_ContoursL.Dispose();
                ho_Contour1.Dispose();
                ho_ContoursSplit2.Dispose();
                ho_UnionContours1.Dispose();
                ho_RegionLines2.Dispose();
                ho_Contour.Dispose();
                ho_ImageReduced1.Dispose();
                ho_Edges1.Dispose();
                ho_ContoursSplit1.Dispose();
                ho_UnionContours.Dispose();
                ho_RegionLines1.Dispose();
                ho_Contour2.Dispose();
                algorithm.Region.Dispose();
            }
            finally
            {
                ho_Rectangle.Dispose();
                ho_Rectangle1.Dispose();
                ho_Region.Dispose();
                ho_ContoursL.Dispose();
                ho_Contour1.Dispose();
                ho_ContoursSplit2.Dispose();
                ho_UnionContours1.Dispose();
                ho_RegionLines2.Dispose();
                ho_Contour.Dispose();
                ho_ImageReduced1.Dispose();
                ho_Edges1.Dispose();
                ho_ContoursSplit1.Dispose();
                ho_UnionContours.Dispose();
                ho_RegionLines1.Dispose();
                ho_Contour2.Dispose();
                algorithm.Region.Dispose();
            }
        }
Exemple #2
0
    // Main procedure
    private void action()
    {
        // Local iconic variables

        HObject ho_Image, ho_Rectangle, ho_ImageReduced;
        HObject ho_ModelContours, ho_ShowContours, ho_ModelContour;
        HObject ho_MeasureContour, ho_ResultContours = null, ho_Contour = null;
        HObject ho_UsedEdges = null, ho_Cross = null;

        // Local control variables

        HTuple hv_ImageFiles = null, hv_Width = null;
        HTuple hv_Height = null, hv_WindowHandle = new HTuple();
        HTuple hv_Area = null, hv_RowRefer = null, hv_ColRefer = null;
        HTuple hv_ModelID = null, hv_HomMat2D = null, hv_MetrologyHandle = null;
        HTuple hv_Line = null, hv_LineIndices = null, hv_Row = null;
        HTuple hv_Column = null, hv_i = null, hv_RowFound = new HTuple();
        HTuple hv_ColFound = new HTuple(), hv_AngleFound = new HTuple();
        HTuple hv_ScoreFound = new HTuple(), hv_UsedRow = new HTuple();
        HTuple hv_UsedColumn = new HTuple(), hv_Angle = new HTuple();
        HTuple hv_Degree = new HTuple();

        // Initialize local and output iconic variables
        HOperatorSet.GenEmptyObj(out ho_Image);
        HOperatorSet.GenEmptyObj(out ho_Rectangle);
        HOperatorSet.GenEmptyObj(out ho_ImageReduced);
        HOperatorSet.GenEmptyObj(out ho_ModelContours);
        HOperatorSet.GenEmptyObj(out ho_ShowContours);
        HOperatorSet.GenEmptyObj(out ho_ModelContour);
        HOperatorSet.GenEmptyObj(out ho_MeasureContour);
        HOperatorSet.GenEmptyObj(out ho_ResultContours);
        HOperatorSet.GenEmptyObj(out ho_Contour);
        HOperatorSet.GenEmptyObj(out ho_UsedEdges);
        HOperatorSet.GenEmptyObj(out ho_Cross);
        //dev_close_window(...);
        //**读取图片所在路径**
        list_image_files("Image", "default", new HTuple(), out hv_ImageFiles);
        ho_Image.Dispose();
        HOperatorSet.ReadImage(out ho_Image, hv_ImageFiles.TupleSelect(0));
        HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
        //dev_open_window(...);
        HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "margin");
        HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 2);
        HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);

        //创建模板
        ho_Rectangle.Dispose();
        HOperatorSet.GenRectangle1(out ho_Rectangle, 400, 570, 550, 700);
        ho_ImageReduced.Dispose();
        HOperatorSet.ReduceDomain(ho_Image, ho_Rectangle, out ho_ImageReduced);
        HOperatorSet.AreaCenter(ho_ImageReduced, out hv_Area, out hv_RowRefer, out hv_ColRefer);
        HOperatorSet.CreateShapeModel(ho_ImageReduced, "auto", (new HTuple(-10)).TupleRad()
                                      , (new HTuple(20)).TupleRad(), "auto", "auto", "use_polarity", "auto", "auto",
                                      out hv_ModelID);
        ho_ModelContours.Dispose();
        HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelID, 1);
        HOperatorSet.VectorAngleToRigid(0, 0, 0, hv_RowRefer, hv_ColRefer, 0, out hv_HomMat2D);
        ho_ShowContours.Dispose();
        HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_ShowContours, hv_HomMat2D);
        HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);
        HOperatorSet.DispObj(ho_ShowContours, hv_ExpDefaultWinHandle);

        //创建测量模板
        HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle);
        HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandle, hv_Width, hv_Height);

        //从左向右在矩形上边缘画一条直线*
        hv_Line    = new HTuple();
        hv_Line[0] = 445;
        hv_Line[1] = 600;
        hv_Line[2] = 445;
        hv_Line[3] = 670;
        HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle, "line", hv_Line, 25,
                                               5, 1, 30, new HTuple(), new HTuple(), out hv_LineIndices);
        ho_ModelContour.Dispose();
        HOperatorSet.GetMetrologyObjectModelContour(out ho_ModelContour, hv_MetrologyHandle,
                                                    "all", 1.5);
        ho_MeasureContour.Dispose();
        HOperatorSet.GetMetrologyObjectMeasures(out ho_MeasureContour, hv_MetrologyHandle,
                                                "all", "all", out hv_Row, out hv_Column);

        //把测量的位置和模板的位置关联起来
        HOperatorSet.SetMetrologyModelParam(hv_MetrologyHandle, "reference_system", ((hv_RowRefer.TupleConcat(
                                                                                          hv_ColRefer))).TupleConcat(0));
        HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_LineIndices, "measure_transition",
                                             "positive");
        HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_LineIndices, "measure_select",
                                             "first");
        HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_LineIndices, "measure_length1",
                                             25);
        HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_LineIndices, "measure_length2",
                                             5);
        HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_LineIndices, "measure_threshold",
                                             15);
        HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_LineIndices, "min_score",
                                             0.3);

        for (hv_i = 0; (int)hv_i <= (int)((new HTuple(hv_ImageFiles.TupleLength())) - 1); hv_i = (int)hv_i + 1)
        {
            ho_Image.Dispose();
            HOperatorSet.ReadImage(out ho_Image, hv_ImageFiles.TupleSelect(hv_i));
            HOperatorSet.FindShapeModel(ho_Image, hv_ModelID, (new HTuple(-10)).TupleRad()
                                        , (new HTuple(20)).TupleRad(), 0.3, 1, 0.5, "least_squares", 0, 0.75, out hv_RowFound,
                                        out hv_ColFound, out hv_AngleFound, out hv_ScoreFound);
            if ((int)(new HTuple((new HTuple(1)).TupleEqual(new HTuple(hv_RowFound.TupleLength()
                                                                       )))) != 0)
            {
                HOperatorSet.HomMat2dIdentity(out hv_HomMat2D);
                HOperatorSet.HomMat2dRotate(hv_HomMat2D, hv_AngleFound, 0, 0, out hv_HomMat2D);
                HOperatorSet.HomMat2dTranslate(hv_HomMat2D, hv_RowFound - 0, hv_ColFound - 0,
                                               out hv_HomMat2D);
                ho_ResultContours.Dispose();
                HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_ResultContours,
                                                   hv_HomMat2D);
                HOperatorSet.AlignMetrologyModel(hv_MetrologyHandle, hv_RowFound, hv_ColFound,
                                                 hv_AngleFound);
                //应用测量
                HOperatorSet.ApplyMetrologyModel(ho_Image, hv_MetrologyHandle);
                //获取结果
                ho_Contour.Dispose();
                HOperatorSet.GetMetrologyObjectMeasures(out ho_Contour, hv_MetrologyHandle,
                                                        "all", "all", out hv_Row, out hv_Column);
                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "used_edges",
                                                      "row", out hv_UsedRow);
                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "used_edges",
                                                      "column", out hv_UsedColumn);
                ho_UsedEdges.Dispose();
                HOperatorSet.GenCrossContourXld(out ho_UsedEdges, hv_UsedRow, hv_UsedColumn,
                                                10, (new HTuple(45)).TupleRad());
                ho_ResultContours.Dispose();
                HOperatorSet.GetMetrologyObjectResultContour(out ho_ResultContours, hv_MetrologyHandle,
                                                             "all", "all", 1.5);
                ho_Cross.Dispose();
                HOperatorSet.GenCrossContourXld(out ho_Cross, hv_RowFound, hv_ColFound, 40,
                                                hv_AngleFound);
                HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);
                HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green");
                HOperatorSet.DispObj(ho_Cross, hv_ExpDefaultWinHandle);
                HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green");
                HOperatorSet.DispObj(ho_ResultContours, hv_ExpDefaultWinHandle);

                HOperatorSet.AngleLx(hv_UsedRow.TupleSelect(1), hv_UsedColumn.TupleSelect(
                                         1), hv_UsedRow.TupleSelect(6), hv_UsedColumn.TupleSelect(6), out hv_Angle);
                HOperatorSet.TupleDeg(hv_Angle, out hv_Degree);
                disp_message(hv_ExpDefaultWinHandle, "定位成功!", "window", 12, 12, "black",
                             "true");

                disp_message(hv_ExpDefaultWinHandle, ((((new HTuple("定位区域中心坐标") + "(") + hv_RowFound) + new HTuple(",")) + hv_ColFound) + ")",
                             "window", 32, 12, "black", "true");
                HOperatorSet.TupleDeg(hv_AngleFound, out hv_Degree);
                disp_message(hv_ExpDefaultWinHandle, ("相对模板角度为" + hv_Degree) + "°", "window",
                             52, 12, "black", "true");
            }
            else
            {
                disp_message(hv_ExpDefaultWinHandle, "没有找到模板", "window", hv_Row, hv_Column,
                             "black", "true");
            }
            HDevelopStop();
        }
        HOperatorSet.ClearMetrologyModel(hv_MetrologyHandle);
        HOperatorSet.ClearShapeModel(hv_ModelID);


        ho_Image.Dispose();
        ho_Rectangle.Dispose();
        ho_ImageReduced.Dispose();
        ho_ModelContours.Dispose();
        ho_ShowContours.Dispose();
        ho_ModelContour.Dispose();
        ho_MeasureContour.Dispose();
        ho_ResultContours.Dispose();
        ho_Contour.Dispose();
        ho_UsedEdges.Dispose();
        ho_Cross.Dispose();
    }
Exemple #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;

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

            // 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.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.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
                hv_h = new HTuple();
                hv_w = new HTuple();
                HTuple end_val36  = hv_Number - 1;
                HTuple step_val36 = 1;
                for (hv_i = 2; hv_i.Continue(end_val36, step_val36); hv_i = hv_i.TupleAdd(step_val36))
                {
                    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);
                    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.9) * hv_Cos) + (hv_Length2 * hv_Sin);
                    hv_LB_Y = ((hv_Length1 * 0.9) * 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);
                    HOperatorSet.CountObj(ho_RegionUnionc3, out hv_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);
                    hv_h = hv_h.TupleConcat((hv_Distance1 * 1) / 0.9);
                    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",
                                                         100);
                    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())
                    {
                        HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red");
                    }
                    HOperatorSet.DispCross(3600, hv_RowL, hv_ColumnL, 6, hv_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;
                        }
                    }
                    if (HDevWindowStack.IsOpen())
                    {
                        //dev_set_color ('blue')
                    }
                    //gen_contour_polygon_xld (Contour, RowL, ColumnL)
                    //stop ()
                    //disp_obj (Image, 3600)

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

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

                hv_hm = (hv_h.TupleMin()) / (hv_h.TupleMean());
                hv_wm = (hv_w.TupleMax()) / (hv_w.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();
                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();
                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();
                algorithm.Region.Dispose();
            }
        }
Exemple #4
0
        //创建测量工具
        private void buttonCreateMetrology_Click(object sender, EventArgs e)
        {
            try
            {
                if (L1StartRowDraw == null || L1StartColumnDraw == null || L1EndRowDraw == null || L1EndColumnDraw == null)
                {
                    MessageBox.Show("请先画直线1");
                    return;
                }
                else if (L2StartRowDraw == null || L2StartColumnDraw == null || L2EndRowDraw == null || L2EndColumnDraw == null)
                {
                    MessageBox.Show("请先画直线2");
                    return;
                }
                else if (Circle1RowDraw == null || Circle1ColumnDraw == null || Circle1RadiusDraw == null)
                {
                    MessageBox.Show("请先画圆1");
                    return;
                }
                else if (Circle2RowDraw == null || Circle2ColumnDraw == null || Circle2RadiusDraw == null)
                {
                    MessageBox.Show("请先画圆2");
                    return;
                }

                //创建测量模型
                HOperatorSet.CreateMetrologyModel(out MetrologyHandle);

                //设置测量对象的图像大小
                HOperatorSet.SetMetrologyModelImageSize(MetrologyHandle, ImageWidth, ImageHeight);

                //添加测量直线对象到测量模型中
                HTuple Circle1Index = null, Circle2Index = null, Line1Index = null, Line2Index = null;
                HTuple Circle1Param = null, Circle2Param = null, Line1Param = null, Line2Param = null;

                Line1Param   = new HTuple();
                Line1Param   = Line1Param.TupleConcat(L1StartRowDraw);
                Line1Param   = Line1Param.TupleConcat(L1StartColumnDraw);
                Line1Param   = Line1Param.TupleConcat(L1EndRowDraw);
                Line1Param   = Line1Param.TupleConcat(L1EndColumnDraw);
                Line2Param   = new HTuple();
                Line2Param   = Line2Param.TupleConcat(L2StartRowDraw);
                Line2Param   = Line2Param.TupleConcat(L2StartColumnDraw);
                Line2Param   = Line2Param.TupleConcat(L2EndRowDraw);
                Line2Param   = Line2Param.TupleConcat(L2EndColumnDraw);
                Circle1Param = new HTuple();
                Circle1Param = Circle1Param.TupleConcat(Circle1RowDraw);
                Circle1Param = Circle1Param.TupleConcat(Circle1ColumnDraw);
                Circle1Param = Circle1Param.TupleConcat(Circle1RadiusDraw);
                Circle2Param = new HTuple();
                Circle2Param = Circle2Param.TupleConcat(Circle2RowDraw);
                Circle2Param = Circle2Param.TupleConcat(Circle2ColumnDraw);
                Circle2Param = Circle2Param.TupleConcat(Circle2RadiusDraw);

                HOperatorSet.AddMetrologyObjectGeneric(MetrologyHandle, "circle", Circle1Param, 20, 5, 1, 30, new HTuple(), new HTuple(), out Circle1Index);
                HOperatorSet.AddMetrologyObjectGeneric(MetrologyHandle, "circle", Circle2Param, 20, 5, 1, 30, new HTuple(), new HTuple(), out Circle2Index);
                HOperatorSet.AddMetrologyObjectGeneric(MetrologyHandle, "line", Line1Param, 20, 5, 1, 30, new HTuple(), new HTuple(), out Line1Index);
                HOperatorSet.AddMetrologyObjectGeneric(MetrologyHandle, "line", Line2Param, 20, 5, 1, 30, new HTuple(), new HTuple(), out Line2Index);



                //获取测量模型里的模型轮廓
                HObject ModelContour;
                HOperatorSet.GenEmptyObj(out ModelContour);
                ModelContour.Dispose();
                HOperatorSet.GetMetrologyObjectModelContour(out ModelContour, MetrologyHandle, "all", 1.5);

                //获取测量模型里的测量区域
                HObject MeasureContour;
                HOperatorSet.GenEmptyObj(out MeasureContour);
                MeasureContour.Dispose();

                HTuple Row = null, Column = null;
                HOperatorSet.GetMetrologyObjectMeasures(out MeasureContour, MetrologyHandle,
                                                        ((((Circle1Index.TupleConcat(Circle2Index))).TupleConcat(Line1Index))).TupleConcat(Line2Index),
                                                        "all", out Row, out Column);


                //显示图像及轮廓
                this.tabControl1.SelectedIndex = 0;
                hWindowControl1.HalconWindow.ClearWindow();
                HOperatorSet.DispObj(getImage, hwindow);
                HOperatorSet.DispObj(ModelContour, hwindow);
                HOperatorSet.DispObj(MeasureContour, hwindow);

                //* 设置测量对象的参考坐标系原点在模板坐标位置

                /*
                 * HTuple TempleteRow = null, TempleteColumn = null, TempleteAngle = null;
                 * TempleteRow = double.Parse(this.ModelRow_textBox.Text);
                 * TempleteColumn = double.Parse(this.ModelColumn_textBox.Text);
                 * TempleteAngle = double.Parse(this.ModelAngle_textBox.Text);
                 *
                 * HOperatorSet.SetMetrologyModelParam(MetrologyHandle, "reference_system",
                 *                                 ((TempleteRow.TupleConcat(TempleteColumn))).TupleConcat(TempleteAngle));
                 */
                //HOperatorSet.WriteMetrologyModel(MetrologyHandle, Directory.GetCurrentDirectory() + @"/MetrologyHandle.mtr");
            }
            catch (HalconException hex)
            {
                MessageBox.Show(hex.GetErrorMessage(), "HALCON Exception:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Windows Exception:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
        private void action()
        {
            // Stack for temporary objects
            HObject[] OTemp = new HObject[20];

            // Local iconic variables



            HObject ho_Rectangle, ho_ImageReduced;
            HObject ho_Region, ho_ConnectedRegions1, ho_SelectedRegions1;
            HObject ho_ContoursL, ho_RegionLines, ho_RegionUnion1, ho_RegionLines1 = null;
            HObject ho_RegionLines2, ho_RegionUnion2, ho_ConnectedRegions;
            HObject ho_SelectedRegions, ho_RegionFillUp, ho_Contours;
            HObject ho_ContoursSplit, ho_SelectedContours, ho_UnionContours;
            HObject ho_RegionDifference;

            // 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_Mean = null, hv_Deviation = null, hv_Row12 = null;
            HTuple hv_Column12 = null, hv_Row22 = null, hv_Column22 = null;
            HTuple hv_shapeParam = null, hv_MetrologyHandleL = null;
            HTuple hv_Index = null, hv_RowL = null, hv_ColumnL = null;
            HTuple hv_cm = null, hv_Reducedc = null, hv_Reducedr = null;
            HTuple hv_j = null, hv_i = null, hv_a = null, hv_Area = null;
            HTuple hv_Row = null, hv_Column = 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_ConnectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_ContoursL);
            HOperatorSet.GenEmptyObj(out ho_RegionLines);
            HOperatorSet.GenEmptyObj(out ho_RegionUnion1);
            HOperatorSet.GenEmptyObj(out ho_RegionLines1);
            HOperatorSet.GenEmptyObj(out ho_RegionLines2);
            HOperatorSet.GenEmptyObj(out ho_RegionUnion2);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUp);
            HOperatorSet.GenEmptyObj(out ho_Contours);
            HOperatorSet.GenEmptyObj(out ho_ContoursSplit);
            HOperatorSet.GenEmptyObj(out ho_SelectedContours);
            HOperatorSet.GenEmptyObj(out ho_UnionContours);
            HOperatorSet.GenEmptyObj(out ho_RegionDifference);
            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.Intensity(ho_Rectangle, ho_ImageReduced, out hv_Mean, out hv_Deviation);

                ho_Region.Dispose();
                HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, hv_Mean + (hv_Deviation / 2),
                                       255);
                ho_ConnectedRegions1.Dispose();
                HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions1);
                ho_SelectedRegions1.Dispose();
                HOperatorSet.SelectShapeStd(ho_ConnectedRegions1, out ho_SelectedRegions1, "max_area",
                                            70);


                HOperatorSet.SmallestRectangle1(ho_SelectedRegions1, out hv_Row12, out hv_Column12,
                                                out hv_Row22, out hv_Column22);

                //找左侧点
                hv_shapeParam = new HTuple();
                hv_shapeParam = hv_shapeParam.TupleConcat(hv_Row12);
                hv_shapeParam = hv_shapeParam.TupleConcat((hv_Column22 + hv_Column12) / 2);
                hv_shapeParam = hv_shapeParam.TupleConcat(hv_Row22);
                hv_shapeParam = hv_shapeParam.TupleConcat((hv_Column22 + hv_Column12) / 2);

                HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandleL);
                HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandleL, hv_Width, hv_Height);
                HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandleL, "line", hv_shapeParam,
                                                       ((hv_Column22 - hv_Column12) / 2) + 1, 1, 1, 30, new HTuple(), new HTuple(), out hv_Index);
                //设置参数
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "measure_transition",
                                                     "negative");
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "num_measures",
                                                     100);
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "num_instances",
                                                     1);
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandleL, "all", "measure_select",
                                                     "first");

                //开始找边缘
                HOperatorSet.ApplyMetrologyModel(ho_ImageReduced, hv_MetrologyHandleL);
                ho_ContoursL.Dispose();
                HOperatorSet.GetMetrologyObjectMeasures(out ho_ContoursL, hv_MetrologyHandleL,
                                                        "all", "all", out hv_RowL, out hv_ColumnL);
                //gen_cross_contour_xld (CrossL, RowL, ColumnL, 6, 0)
                // stop(); only in hdevelop
                HOperatorSet.ClearMetrologyModel(hv_MetrologyHandleL);
                ho_RegionLines.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLines, hv_Row12, hv_Column12, hv_Row12 + 1,
                                           hv_Column12);
                ho_RegionUnion1.Dispose();
                HOperatorSet.Union1(ho_RegionLines, out ho_RegionUnion1);
                hv_cm = (((hv_ColumnL.TupleSelect(0)) + (hv_ColumnL.TupleSelect((new HTuple(hv_RowL.TupleLength()
                                                                                            )) - 1))) / 2) + 1;
                hv_Reducedc = new HTuple();
                hv_Reducedr = new HTuple();
                for (hv_j = 0; (int)hv_j <= (int)((new HTuple(hv_RowL.TupleLength())) - 1); hv_j = (int)hv_j + 1)
                {
                    if ((int)(new HTuple(((hv_ColumnL.TupleSelect(hv_j))).TupleLessEqual(hv_cm))) != 0)
                    {
                        hv_Reducedc = hv_Reducedc.TupleConcat(hv_ColumnL.TupleSelect(hv_j));
                        hv_Reducedr = hv_Reducedr.TupleConcat(hv_RowL.TupleSelect(hv_j));
                    }
                }
                //gen_cross_contour_xld (Cross, Reducedr, Reducedc, 6, 0.785398)


                // stop(); only in hdevelop
                for (hv_i = 0; (int)hv_i <= (int)((new HTuple(hv_Reducedr.TupleLength())) - 2); hv_i = (int)hv_i + 1)
                {
                    ho_RegionLines1.Dispose();
                    HOperatorSet.GenRegionLine(out ho_RegionLines1, hv_Reducedr.TupleSelect(hv_i),
                                               hv_Reducedc.TupleSelect(hv_i), hv_Reducedr.TupleSelect(hv_i + 1), hv_Reducedc.TupleSelect(
                                                   hv_i + 1));
                    {
                        HObject ExpTmpOutVar_0;
                        HOperatorSet.Union2(ho_RegionUnion1, ho_RegionLines1, out ExpTmpOutVar_0);
                        ho_RegionUnion1.Dispose();
                        ho_RegionUnion1 = ExpTmpOutVar_0;
                    }
                }
                // stop(); only in hdevelop
                ho_RegionLines2.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLines2, 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_RegionUnion2.Dispose();
                HOperatorSet.Union2(ho_RegionUnion1, ho_RegionLines2, out ho_RegionUnion2);
                ho_ConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_RegionUnion2, out ho_ConnectedRegions);
                ho_SelectedRegions.Dispose();
                HOperatorSet.SelectShapeStd(ho_ConnectedRegions, out ho_SelectedRegions, "max_area",
                                            70);
                ho_RegionFillUp.Dispose();
                HOperatorSet.FillUp(ho_SelectedRegions, out ho_RegionFillUp);
                ho_Contours.Dispose();
                HOperatorSet.GenContourRegionXld(ho_RegionFillUp, out ho_Contours, "border");
                ho_ContoursSplit.Dispose();
                HOperatorSet.SegmentContoursXld(ho_Contours, out ho_ContoursSplit, "lines_circles",
                                                5, 8, 3.5);
                ho_SelectedContours.Dispose();
                HOperatorSet.SelectContoursXld(ho_ContoursSplit, out ho_SelectedContours, "contour_length",
                                               70, 2000, -0.5, 0.5);
                ho_UnionContours.Dispose();
                HOperatorSet.UnionAdjacentContoursXld(ho_SelectedContours, out ho_UnionContours,
                                                      10, 1, "attr_keep");
                ho_Region.Dispose();
                HOperatorSet.GenRegionContourXld(ho_UnionContours, out ho_Region, "filled");

                ho_RegionDifference.Dispose();
                HOperatorSet.Difference(ho_RegionFillUp, ho_Region, out ho_RegionDifference);
                hv_a = 0;
                HOperatorSet.AreaCenter(ho_RegionDifference, out hv_Area, out hv_Row, out hv_Column);
                if ((int)(new HTuple((new HTuple(hv_Area.TupleLength())).TupleGreater(0))) != 0)
                {
                    hv_a = hv_Area.TupleSum();
                    HOperatorSet.Union1(ho_RegionDifference, out RegionToDisp);
                }
                else
                {
                    hv_a = 0;
                }
                HTuple hv_result = GetHv_result();
                hv_result = hv_result.TupleConcat("缺陷面积");
                hv_result = hv_result.TupleConcat(hv_a.D);
                result    = hv_result.Clone();

                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_SelectedRegions1.Dispose();
                ho_ContoursL.Dispose();
                ho_RegionLines.Dispose();
                ho_RegionUnion1.Dispose();
                ho_RegionLines1.Dispose();
                ho_RegionLines2.Dispose();
                ho_RegionUnion2.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_SelectedRegions.Dispose();
                ho_RegionFillUp.Dispose();
                ho_Contours.Dispose();
                ho_ContoursSplit.Dispose();
                ho_SelectedContours.Dispose();
                ho_UnionContours.Dispose();
                ho_RegionDifference.Dispose();
                algorithm.Region.Dispose();
            }
            catch
            {
                HTuple hv_result = GetHv_result();
                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_ConnectedRegions1.Dispose();
                ho_SelectedRegions1.Dispose();
                ho_ContoursL.Dispose();
                ho_RegionLines.Dispose();
                ho_RegionUnion1.Dispose();
                ho_RegionLines1.Dispose();
                ho_RegionLines2.Dispose();
                ho_RegionUnion2.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_SelectedRegions.Dispose();
                ho_RegionFillUp.Dispose();
                ho_Contours.Dispose();
                ho_ContoursSplit.Dispose();
                ho_SelectedContours.Dispose();
                ho_UnionContours.Dispose();
                ho_RegionDifference.Dispose();
                algorithm.Region.Dispose();
            }
            finally
            {
                ho_Rectangle.Dispose();
                ho_ImageReduced.Dispose();
                ho_Region.Dispose();
                ho_ConnectedRegions1.Dispose();
                ho_SelectedRegions1.Dispose();
                ho_ContoursL.Dispose();
                ho_RegionLines.Dispose();
                ho_RegionUnion1.Dispose();
                ho_RegionLines1.Dispose();
                ho_RegionLines2.Dispose();
                ho_RegionUnion2.Dispose();
                ho_ConnectedRegions.Dispose();
                ho_SelectedRegions.Dispose();
                ho_RegionFillUp.Dispose();
                ho_Contours.Dispose();
                ho_ContoursSplit.Dispose();
                ho_SelectedContours.Dispose();
                ho_UnionContours.Dispose();
                ho_RegionDifference.Dispose();
                algorithm.Region.Dispose();
            }
        }
Exemple #6
0
        private void DrawMeasureModel(object sender, EventArgs e)
        {
            try
            {
                if (hv_FirstLineBeginRowModal == null || hv_FirstLineBeginColumnModal == null ||
                    hv_FirstLineEndRowModal == null || hv_FirstLineEndColumnModal == null ||
                    hv_SecondLineBeginRowModal == null || hv_SecondLineBeginColumnModal == null ||
                    hv_SecondLineEndRowModal == null || hv_SecondLineEndColumnModal == null)
                {
                    MessageBox.Show("请先画直线");
                    return;
                }

                //创建测量模型
                HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle);

                //设置测量对象的图像大小
                HTuple hv_Width = null, hv_Height = null;
                HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
                HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandle, hv_Width, hv_Height);

                //添加测量直线对象到测量模型中
                HTuple hv_Line1 = null, hv_Line2 = null, hv_MetrologyLine = null;
                hv_Line1 = new HTuple();
                hv_Line1 = hv_Line1.TupleConcat(hv_FirstLineBeginRowModal);
                hv_Line1 = hv_Line1.TupleConcat(hv_FirstLineBeginColumnModal);
                hv_Line1 = hv_Line1.TupleConcat(hv_FirstLineEndRowModal);
                hv_Line1 = hv_Line1.TupleConcat(hv_FirstLineEndColumnModal);
                hv_Line2 = new HTuple();
                hv_Line2 = hv_Line2.TupleConcat(hv_SecondLineBeginRowModal);
                hv_Line2 = hv_Line2.TupleConcat(hv_SecondLineBeginColumnModal);
                hv_Line2 = hv_Line2.TupleConcat(hv_SecondLineEndRowModal);
                hv_Line2 = hv_Line2.TupleConcat(hv_SecondLineEndColumnModal);
                HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle, "line", hv_Line1.TupleConcat(hv_Line2), 20, 10, 1, 1, new HTuple(), new HTuple(), out hv_MetrologyLine);

                //获取测量模型里的模型轮廓
                HObject ho_ModelContour;
                HOperatorSet.GenEmptyObj(out ho_ModelContour);
                ho_ModelContour.Dispose();
                HOperatorSet.GetMetrologyObjectModelContour(out ho_ModelContour, hv_MetrologyHandle, "all", 1.5);

                //获取测量模型里的测量区域
                HObject ho_MeasureContour;
                HOperatorSet.GenEmptyObj(out ho_MeasureContour);
                ho_MeasureContour.Dispose();

                HTuple hv_Row = null, hv_Column = null;
                HOperatorSet.GetMetrologyObjectMeasures(out ho_MeasureContour, hv_MetrologyHandle, "all", "all", out hv_Row, out hv_Column);


                //显示图像及轮廓
                HOperatorSet.DispObj(ho_Image, GraphInteractiveObect.halconWindow.HalconWindow);
                HOperatorSet.DispObj(ho_ModelContour, GraphInteractiveObect.halconWindow.HalconWindow);
                HOperatorSet.DispObj(ho_MeasureContour, GraphInteractiveObect.halconWindow.HalconWindow);

                //* 设置测量对象的参考坐标系原点在模板坐标位置
                HTuple hv_TempleteRow = null, hv_TempleteColumn = null, hv_TempleteAngle = null;
                //hv_TempleteRow = double.Parse(this.ModelRow_textBox.Text);
                //hv_TempleteColumn = double.Parse(this.ModelColumn_textBox.Text);
                //hv_TempleteAngle = double.Parse(this.ModelAngle_textBox.Text);
                hv_TempleteRow    = double.Parse(this.txtRow.Text);
                hv_TempleteColumn = double.Parse(this.txtCol.Text);
                hv_TempleteAngle  = double.Parse(this.txtAngle.Text);

                HOperatorSet.SetMetrologyModelParam(hv_MetrologyHandle, "reference_system", ((hv_TempleteRow.TupleConcat(hv_TempleteColumn))).TupleConcat(hv_TempleteAngle));

                HOperatorSet.WriteMetrologyModel(hv_MetrologyHandle, Directory.GetCurrentDirectory() + @"/MetrologyHandle.mtr");
            }
            catch (HalconException hex)
            {
                MessageBox.Show(hex.GetErrorMessage(), "HALCON error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Windows error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Exemple #7
0
        //generate mark model
        public void  GenMarkModel(out HTuple hv_ModelID, out HObject ho_ModelContours, out HTuple hv_MetrologyHandle)
        {
            // Local iconic variables
            HObject ho_Image, ho_Rectangle, ho_ImageReduced;
            HObject ho_ShowContours, ho_ModelContour;
            HObject ho_MeasureContour = null;

            // Local control variables
            HTuple hv_Height = null, hv_Width = null;
            HTuple hv_Area = null, hv_RowRefer = null, hv_ColRefer = null;
            HTuple hv_HomMat2D = null;
            HTuple hv_Line = null, hv_LineIndices = null, hv_Row = null, hv_Column = null;

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_ModelContours);
            HOperatorSet.GenEmptyObj(out ho_ShowContours);
            HOperatorSet.GenEmptyObj(out ho_ModelContour);

            //**读取图片所在路径**
            ho_Image.Dispose();
            HOperatorSet.ReadImage(out ho_Image, "Model.bmp");
            HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
            //dev_open_window(...);
            //HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "margin");
            //HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 2);
            //HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);

            //创建模板
            ho_Rectangle.Dispose();
            HOperatorSet.GenRectangle1(out ho_Rectangle, 400, 570, 550, 700);
            ho_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_Image, ho_Rectangle, out ho_ImageReduced);
            HOperatorSet.AreaCenter(ho_ImageReduced, out hv_Area, out hv_RowRefer, out hv_ColRefer);
            HOperatorSet.CreateShapeModel(ho_ImageReduced, "auto", (new HTuple(-10)).TupleRad()
                                          , (new HTuple(20)).TupleRad(), "auto", "auto", "use_polarity", "auto", "auto",
                                          out hv_ModelID);
            ho_ModelContours.Dispose();
            HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelID, 1);
            HOperatorSet.VectorAngleToRigid(0, 0, 0, hv_RowRefer, hv_ColRefer, 0, out hv_HomMat2D);
            ho_ShowContours.Dispose();
            HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_ShowContours, hv_HomMat2D);
            HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);
            HOperatorSet.DispObj(ho_ShowContours, hv_ExpDefaultWinHandle);

            //创建测量模板
            HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle);
            HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandle, hv_Width, hv_Height);

            //从左向右在矩形上边缘画一条直线*
            hv_Line    = new HTuple();
            hv_Line[0] = 445;
            hv_Line[1] = 600;
            hv_Line[2] = 445;
            hv_Line[3] = 670;
            HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle, "line", hv_Line, 25,
                                                   5, 1, 30, new HTuple(), new HTuple(), out hv_LineIndices);
            ho_ModelContour.Dispose();
            HOperatorSet.GetMetrologyObjectModelContour(out ho_ModelContour, hv_MetrologyHandle,
                                                        "all", 1.5);
            // ho_MeasureContour.Dispose();
            HOperatorSet.GetMetrologyObjectMeasures(out ho_MeasureContour, hv_MetrologyHandle,
                                                    "all", "all", out hv_Row, out hv_Column);

            //把测量的位置和模板的位置关联起来
            HOperatorSet.SetMetrologyModelParam(hv_MetrologyHandle, "reference_system", ((hv_RowRefer.TupleConcat(
                                                                                              hv_ColRefer))).TupleConcat(0));
            HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_LineIndices, "measure_transition",
                                                 "positive");
            HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_LineIndices, "measure_select",
                                                 "first");
            HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_LineIndices, "measure_length1",
                                                 25);
            HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_LineIndices, "measure_length2",
                                                 5);
            HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_LineIndices, "measure_threshold",
                                                 15);
            HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_LineIndices, "min_score",
                                                 0.3);
        }
Exemple #8
0
        private void Camera2()
        {
            ho_Image.Dispose();
            HOperatorSet.ReadImage(out ho_Image, "G:/防火砖缺陷检测/Image_20201109161631787.bmp");
            HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
            HOperatorSet.SetPart(hWindow_Final1.hWindowControl.HalconWindow, 0, 0, hv_Height, hv_Width);
            HOperatorSet.DispObj(ho_Image, hWindow_Final1.hWindowControl.HalconWindow);
            //gen_rectangle2 (ROI_0, 1368.71, 2059.78, rad(-167.642), 1471.49, 851.652)
            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
                ho_ROI_0.Dispose();
                HOperatorSet.GenRectangle2(out ho_ROI_0, 1049.75, 1241.71, 0.108671, 1082.21, 632.588);
            }
            ho_Contours.Dispose();
            HOperatorSet.GenContourRegionXld(ho_ROI_0, out ho_Contours, "border");
            ho_Image1.Dispose();
            HOperatorSet.GenImageConst(out ho_Image1, "byte", hv_Width, hv_Height);
            ho_ImageModel.Dispose();
            HOperatorSet.PaintXld(ho_Contours, ho_Image1, out ho_ImageModel, 128);
            ho_RegionDilation.Dispose();
            HOperatorSet.DilationCircle(ho_ROI_0, out ho_RegionDilation, 3.5);
            ho_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_ImageModel, ho_RegionDilation, out ho_ImageReduced
                                      );
            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
                HOperatorSet.CreateScaledShapeModel(ho_ImageReduced, "auto", (new HTuple(0)).TupleRad()
                                                    , (new HTuple(360)).TupleRad(), "auto", 0.9, 1.1, "auto", "auto", "use_polarity",
                                                    "auto", "auto", out hv_ModelID);
                c2s.Modle = hv_ModelID;
            }
            ho_ModelContours.Dispose();
            HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelID, 1);
            HOperatorSet.DispObj(ho_ModelContours, hWindow_Final1.hWindowControl.HalconWindow);
            HOperatorSet.AreaCenter(ho_ROI_0, out hv_Area, out hv_Row1, out hv_Column1);
            HOperatorSet.VectorAngleToRigid(0, 0, 0, hv_Row1, hv_Column1, 0, out hv_HomMat2D);
            ho_ContoursAffineTrans.Dispose();
            HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_ContoursAffineTrans,
                                               hv_HomMat2D);
            HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle);
            HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandle, hv_Width, hv_Height);

            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
                hv_RectangleParam    = new HTuple();
                hv_RectangleParam[0] = 1049.75;
                hv_RectangleParam[1] = 1241.71;
                hv_RectangleParam    = hv_RectangleParam.TupleConcat(0.108671);
                hv_RectangleParam    = hv_RectangleParam.TupleConcat(new HTuple(1082.21, 632.588));
            }
            HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle, "rectangle2", hv_RectangleParam,
                                                   80, 20, 6, 15, new HTuple(), new HTuple(), out hv_RectIndices);
            //Line1 := [879,784,558,3426]
            //add_metrology_object_generic (MetrologyHandle, 'line', [Line1], 20, 5, 1, 30, [], [], LineIndices)
            HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "measure_length1",
                                                 80);

            HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "measure_transition",
                                                 "all");
            //检查已添加到计量模型中的形状
            //获取测量轮廓
            ho_ModelContour.Dispose();
            HOperatorSet.GetMetrologyObjectModelContour(out ho_ModelContour, hv_MetrologyHandle,
                                                        "all", 1.5);
            //获取测量卡尺
            ho_MeasureContour.Dispose();
            HOperatorSet.GetMetrologyObjectMeasures(out ho_MeasureContour, hv_MetrologyHandle,
                                                    "all", "all", out hv_Row, out hv_Column);
            //find_shape_model对齐计量模型

            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
                HOperatorSet.SetMetrologyModelParam(hv_MetrologyHandle, "reference_system", ((hv_Row1.TupleConcat(
                                                                                                  hv_Column1))).TupleConcat(0));
            }
            c2s.MetrologyHandle = hv_MetrologyHandle;
            Module.SerializableTool.Save2File <Module.Camera2Serializable>(c2s, "cam2.sol");
        }