예제 #1
0
        public static bool findCircle(HObject image, HTuple handle, out HTuple row, out HTuple col, out HTuple radius, out HObject obj, out HObject contours, out HObject ho_Cross)
        {
            row    = null;
            col    = null;
            radius = null;
            HTuple hv_Row1, hv_Column1;

            HOperatorSet.GenEmptyObj(out obj);
            HOperatorSet.GenEmptyObj(out contours);
            HOperatorSet.GenEmptyObj(out ho_Cross);
            HTuple parameter;

            try
            {
                HOperatorSet.ApplyMetrologyModel(image, handle);
                HOperatorSet.GetMetrologyObjectResult(handle, 0, "all", "result_type", "all_param", out parameter);
                HOperatorSet.GetMetrologyObjectMeasures(out contours, handle, "all", "all", out hv_Row1, out hv_Column1);
                HOperatorSet.GenCrossContourXld(out ho_Cross, hv_Row1, hv_Column1, 6, 0.785398);
                if (3 == parameter.Length)
                {
                    row    = parameter[0];
                    col    = parameter[1];
                    radius = parameter[2];
                    HOperatorSet.GenCircle(out obj, row, col, radius);
                    return(true);
                }
            }
            catch (HalconException ex)
            {
                string msg = ex.GetErrorMessage();
            }
            return(false);
        }
예제 #2
0
        ///<summary>
        ///直线拟合(仿射变换)
        ///</summary>
        public void FindLine(HTuple hv_HomMat2DPix, HObject ho_Image, HTuple hv_MetrologyHandle, HTuple hv_Index, HTuple hv_MeasureNumInstances, HTuple hv_MeasureDistThreshold, HTuple hv_MeasureTransition,
                             HTuple hv_MeasureSelect, HTuple hv_MeasurePointsNum, HTuple hv_MeasureMinScore, HTuple hv_MaxNumIterations, HTuple hv_MeasureInterpolation, HTuple hv_MeasureIORegions,
                             out HTuple hv_CrossRow, out HTuple hv_CrossColumn, out HTuple hv_LineRowBegin_Real, out HTuple hv_LineColumnBegin_Real, out HTuple hv_LineRowEnd_Real, out HTuple hv_LineColumnEnd_Real)
        {
            HObject ho_Contours        = new HObject();
            HTuple  hv_LineRowBegin    = new HTuple();
            HTuple  hv_LineColumnBegin = new HTuple();
            HTuple  hv_LineRowEnd      = new HTuple();
            HTuple  hv_LineColumnEnd   = new HTuple();

            hv_CrossRow             = new HTuple();
            hv_CrossColumn          = new HTuple();
            hv_LineRowBegin_Real    = new HTuple();
            hv_LineColumnBegin_Real = new HTuple();
            hv_LineRowEnd_Real      = new HTuple();
            hv_LineColumnEnd_Real   = new HTuple();

            try
            {
                //1.直线拟合数量:默认值: 1 值的列表: 1,2,3,4
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "num_instances", hv_MeasureNumInstances);
                //2.距离剔除:默认值: 3.5  值的列表: 0,1.0,2.0, 3.5,5.0
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "distance_threshold", hv_MeasureDistThreshold);
                //3.明暗方向:值列表: 'all','negative', 'positive','uniform'
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "measure_transition", hv_MeasureTransition);
                //4.边缘选取:'all','first','last'
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "measure_select", hv_MeasureSelect);
                //5.Roi数量:'num_measures'
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "num_measures", hv_MeasurePointsNum);
                //6.最小得分:'min_score'
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "min_score", hv_MeasureMinScore);
                //7.迭代次数: 默认值:-1  值的列表: 10,100,1000
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "max_num_iterations", hv_MaxNumIterations);
                //8.插值类型:nearest_neighbor'灰度值是从最近像素的灰度值获得;'bilinear',使用双线性插值 ;'bicubic',则使用双三次插值。
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "measure_interpolation", hv_MeasureInterpolation);
                //9.指定对测量结果的验证:默认值: “ false”  值列表: 'true','false'
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "instances_outside_measure_regions", hv_MeasureIORegions);

                HOperatorSet.ApplyMetrologyModel(ho_Image, hv_MetrologyHandle);
                HOperatorSet.GetMetrologyObjectMeasures(out ho_Contours, hv_MetrologyHandle, "all", "all", out hv_CrossRow, out hv_CrossColumn);
                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type", "row_begin", out hv_LineRowBegin);
                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type", "column_begin", out hv_LineColumnBegin);
                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type", "row_end", out hv_LineRowEnd);
                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type", "column_end", out hv_LineColumnEnd);

                HOperatorSet.AffineTransPixel(hv_HomMat2DPix, hv_LineRowBegin, hv_LineColumnBegin, out hv_LineRowBegin_Real, out hv_LineColumnBegin_Real);
                HOperatorSet.AffineTransPixel(hv_HomMat2DPix, hv_LineRowEnd, hv_LineColumnEnd, out hv_LineRowEnd_Real, out hv_LineColumnEnd_Real);

                //清除测量模型
                ho_Contours.Dispose();
                HOperatorSet.ClearMetrologyModel(hv_MetrologyHandle);
                return;
            }
            catch (Exception)
            {
                return;
            }
        }
예제 #3
0
        private bool FindCorner()
        {
            if (cornerParams.ho_Image == null)
            {
                MessageBox.Show("图像为空");
                return(false);
            }
            try
            {
                Show2HWindow(cornerParams.ho_Image);
                HOperatorSet.ApplyMetrologyModel(cornerParams.ho_Image, cornerParams.hv_MetrologyHandle);

                HOperatorSet.GetMetrologyObjectResult(cornerParams.hv_MetrologyHandle, "all", "all", "result_type", "row_begin", out cornerParams.hv_LineRowBegin);
                HOperatorSet.GetMetrologyObjectResult(cornerParams.hv_MetrologyHandle, "all", "all", "result_type", "column_begin", out cornerParams.hv_LineColumnBegin);
                HOperatorSet.GetMetrologyObjectResult(cornerParams.hv_MetrologyHandle, "all", "all", "result_type", "row_end", out cornerParams.hv_LineRowEnd);
                HOperatorSet.GetMetrologyObjectResult(cornerParams.hv_MetrologyHandle, "all", "all", "result_type", "column_end", out cornerParams.hv_LineColumnEnd);

                if (cornerParams.hv_LineRowBegin.Length != cornerParams.hv_Number)
                {
                    MessageBox.Show("直线数量错误!");
                    return(false);
                }

                HTuple hv_IsOverlapping = null;
                HOperatorSet.IntersectionLines(cornerParams.hv_LineRowBegin[0], cornerParams.hv_LineColumnBegin[0], cornerParams.hv_LineRowEnd[0], cornerParams.hv_LineColumnEnd[0],
                                               cornerParams.hv_LineRowBegin[1], cornerParams.hv_LineColumnBegin[1], cornerParams.hv_LineRowEnd[1], cornerParams.hv_LineColumnEnd[1], out cornerParams.hv_PointRow, out cornerParams.hv_PointColumn,
                                               out hv_IsOverlapping);

                if (cornerParams.hv_PointRow.Length != 1)
                {
                    MessageBox.Show("直线交点错误!");
                    return(false);
                }
                HObject ho_MeasuredCross;
                HOperatorSet.GenCrossContourXld(out ho_MeasuredCross, cornerParams.hv_PointRow, cornerParams.hv_PointColumn, 40, (new HTuple(45)).TupleRad());//在模板中心产生一个x,表示模板中心

                HObject ho_MeasuredLines;
                HOperatorSet.GenEmptyObj(out ho_MeasuredLines);
                HOperatorSet.GetMetrologyObjectResultContour(out ho_MeasuredLines, cornerParams.hv_MetrologyHandle, "all", "all", 1.5);

                HOperatorSet.AffineTransContourXld(ho_MeasuredLines, out ho_MeasuredLines, hv_XldHomMat2D);
                HOperatorSet.AffineTransContourXld(ho_MeasuredCross, out ho_MeasuredCross, hv_XldHomMat2D);

                HOperatorSet.SetColor(hv_ImageWindow, "red");
                HOperatorSet.DispObj(ho_MeasuredLines, hv_ImageWindow);
                HOperatorSet.DispObj(ho_MeasuredCross, hv_ImageWindow);

                MessageBox.Show("查找直线模型成功!");
                return(true);
            }
            catch (Exception exc)
            {
                MessageBox.Show("查找直线模型失败!" + exc.ToString());
                return(false);
            }
        }
예제 #4
0
        public bool FindLine()
        {
            if (ho_Image == null)
            {
                errorFlag = true;
                return(false);
            }

            if (hv_Model != 0)
            {
                HObject ho_Region;

                HOperatorSet.Threshold(ho_Image, out ho_Region, hv_RegionMin, hv_RegionMax);

                HOperatorSet.ReduceDomain(ho_Image, ho_Region, out ho_Image);
            }

            if (!CreateLineModel())
            {
                errorFlag = true;
                return(false);
            }

            try
            {
                HOperatorSet.ApplyMetrologyModel(ho_Image, hv_MetrologyHandle);

                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type", "row_begin", out hv_LineRowBegin);
                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type", "column_begin", out hv_LineColumnBegin);
                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type", "row_end", out hv_LineRowEnd);
                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type", "column_end", out hv_LineColumnEnd);

                if (hv_LineRowBegin.Length != 1)
                {
                    errorFlag = true;
                    return(false);
                }
                hv_LineRowBegin_Real    = hv_LineRowBegin;
                hv_LineColumnBegin_Real = hv_LineColumnBegin;
                hv_LineRowEnd_Real      = hv_LineRowEnd;
                hv_LineColumnEnd_Real   = hv_LineColumnEnd;

                errorFlag = false;
                return(true);
            }
            catch (Exception exc)
            {
                errorFlag = true;
                return(false);
            }
        }
예제 #5
0
        public static bool findLine(HObject image, HTuple handle, out HTuple row1, out HTuple col1, out HTuple row2, out HTuple col2, out HObject obj, out HObject contours, out HObject ho_Cross, out HTuple phi)
        {
            row1 = null;
            col1 = null;
            row2 = null;
            col2 = null;
            phi  = null;
            HTuple hv_Row1, hv_Column1;

            HOperatorSet.GenEmptyObj(out obj);
            HOperatorSet.GenEmptyObj(out contours);
            HOperatorSet.GenEmptyObj(out ho_Cross);

            HTuple parameter;

            try
            {
                HOperatorSet.ApplyMetrologyModel(image, handle);
                HOperatorSet.GetMetrologyObjectResult(handle, 0, "all", "result_type", "all_param", out parameter);
                HOperatorSet.GetMetrologyObjectMeasures(out contours, handle, "all", "all", out hv_Row1, out hv_Column1);
                HOperatorSet.GenCrossContourXld(out ho_Cross, hv_Row1, hv_Column1, 6, 0.785398);
                if (4 == parameter.Length)
                {
                    row1 = parameter[0];
                    col1 = parameter[1];
                    row2 = parameter[2];
                    col2 = parameter[3];
                    HOperatorSet.GenContourPolygonXld(out obj, row1.TupleConcat(row2), col1.TupleConcat(col2));
                    HOperatorSet.LineOrientation(row1, col1, row2, col2, out phi);
                    HOperatorSet.ClearMetrologyObject(handle, "all");
                    HOperatorSet.ClearMetrologyModel(handle);

                    return(true);
                }
            }
            catch (HalconException ex)
            {
                string msg = ex.GetErrorMessage();
            }

            return(false);
        }
예제 #6
0
        public bool FindCorner()
        {
            if (ho_Image == null)
            {
                errorFlag = true;
                return(false);
            }

            try
            {
                HOperatorSet.ApplyMetrologyModel(ho_Image, hv_MetrologyHandle);

                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type", "row_begin", out hv_LineRowBegin);
                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type", "column_begin", out hv_LineColumnBegin);
                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type", "row_end", out hv_LineRowEnd);
                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type", "column_end", out hv_LineColumnEnd);

                if (hv_LineRowBegin.Length != hv_Number)
                {
                    errorFlag = true;
                    return(false);
                }
                HTuple hv_IsOverlapping = null;
                HOperatorSet.IntersectionLines(hv_LineRowBegin[0], hv_LineColumnBegin[0], hv_LineRowEnd[0], hv_LineColumnEnd[0],
                                               hv_LineRowBegin[1], hv_LineColumnBegin[1], hv_LineRowEnd[1], hv_LineColumnEnd[1], out hv_PointRow, out hv_PointColumn,
                                               out hv_IsOverlapping);

                if (hv_PointRow.Length != 1)
                {
                    errorFlag = true;
                    return(false);
                }

                errorFlag = false;
                return(true);
            }
            catch (Exception exc)
            {
                errorFlag = true;
                return(false);
            }
        }
예제 #7
0
        private bool FindCorner()
        {
            if (lineParams.ho_Image == null)
            {
                MessageBox.Show("图像为空");
                return(false);
            }
            try
            {
                Show2HWindow(lineParams.ho_Image);
                HOperatorSet.ApplyMetrologyModel(lineParams.ho_Image, lineParams.hv_MetrologyHandle);

                HOperatorSet.GetMetrologyObjectResult(lineParams.hv_MetrologyHandle, "all", "all", "result_type", "row_begin", out lineParams.hv_LineRowBegin);
                HOperatorSet.GetMetrologyObjectResult(lineParams.hv_MetrologyHandle, "all", "all", "result_type", "column_begin", out lineParams.hv_LineColumnBegin);
                HOperatorSet.GetMetrologyObjectResult(lineParams.hv_MetrologyHandle, "all", "all", "result_type", "row_end", out lineParams.hv_LineRowEnd);
                HOperatorSet.GetMetrologyObjectResult(lineParams.hv_MetrologyHandle, "all", "all", "result_type", "column_end", out lineParams.hv_LineColumnEnd);

                if (lineParams.hv_LineRowBegin.Length != 1)
                {
                    MessageBox.Show("直线数量错误!");
                    return(false);
                }

                HObject ho_MeasuredLine;
                HOperatorSet.GenEmptyObj(out ho_MeasuredLine);
                HOperatorSet.GetMetrologyObjectResultContour(out ho_MeasuredLine, lineParams.hv_MetrologyHandle, "all", "all", 1.5);

                HOperatorSet.AffineTransContourXld(ho_MeasuredLine, out ho_MeasuredLine, hv_XldHomMat2D);

                HOperatorSet.SetColor(hv_ImageWindow, "red");
                HOperatorSet.DispObj(ho_MeasuredLine, hv_ImageWindow);

                MessageBox.Show("查找直线模型成功!");
                return(true);
            }
            catch (Exception exc)
            {
                MessageBox.Show("查找直线模型失败!" + exc.ToString());
                return(false);
            }
        }
    // Main procedure
    private void action()
    {
        // Local iconic variables

        HObject ho_Image, ho_Rectangle, ho_ImageReduced;
        HObject ho_Region, ho_ResultContour = null, ho_ContCircle = null;
        HObject ho_Contour = null, ho_Cross = null;

        // Local control variables

        HTuple hv_WindowHandle = null, hv_Width = null;
        HTuple hv_Height = null, hv_MetrologyHandle = null, hv_LineRow1 = null;
        HTuple hv_LineColumn1 = null, hv_LineRow2 = null, hv_LineColumn2 = null;
        HTuple hv_Tolerance = null, hv_Index1 = null, hv_Rows = null;
        HTuple hv_Columns = null, hv_I = null, hv_LineParameter = new HTuple();
        HTuple hv_Angle = new HTuple(), hv_Row = new HTuple();
        HTuple hv_Column = new HTuple(), hv_IsOverlapping1 = new HTuple();
        HTuple hv_Orientation1 = new HTuple(), hv_Orientation2 = new HTuple();
        HTuple hv_MRow = new HTuple(), hv_MColumn = 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_Region);
        HOperatorSet.GenEmptyObj(out ho_ResultContour);
        HOperatorSet.GenEmptyObj(out ho_ContCircle);
        HOperatorSet.GenEmptyObj(out ho_Contour);
        HOperatorSet.GenEmptyObj(out ho_Cross);
        try
        {
            //This program shows how to detect the edges of a diamond
            //with subpixel accuracy and calculate the angle between them.
            //
            //In contrast to the example measure_diamond.hdev,
            //this example uses a metrology model to measure the edges.
            //
            //First, the top of the diamond is roughly segmented
            //to align the metrology objects.
            //Then, the metrology model is applied and returns the
            //parameters of the fitted lines.
            //Finally, the angle between the two lines is computed.
            //
            //Display initializations
            dev_update_off();
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.CloseWindow(HDevWindowStack.Pop());
            }
            ho_Image.Dispose();
            HOperatorSet.ReadImage(out ho_Image, "diamond/diamond_01");
            dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle);
            set_display_font(hv_WindowHandle, 16, "mono", "true", "false");
            HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
            //
            //Create the metrology model data structure
            HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle);
            //The image size is set in advance to speed up the
            //first call of apply_metrology_model.
            HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandle, hv_Width, hv_Height);
            //Define the parameters of the metrology line objects
            hv_LineRow1       = new HTuple();
            hv_LineRow1[0]    = 155;
            hv_LineRow1[1]    = 155;
            hv_LineColumn1    = new HTuple();
            hv_LineColumn1[0] = 400;
            hv_LineColumn1[1] = 400;
            hv_LineRow2       = new HTuple();
            hv_LineRow2[0]    = 290;
            hv_LineRow2[1]    = 290;
            hv_LineColumn2    = new HTuple();
            hv_LineColumn2[0] = 230;
            hv_LineColumn2[1] = 570;
            hv_Tolerance      = 20;
            //
            //Create two metrology line objects and set parameters
            HOperatorSet.AddMetrologyObjectLineMeasure(hv_MetrologyHandle, hv_LineRow1,
                                                       hv_LineColumn1, hv_LineRow2, hv_LineColumn2, hv_Tolerance, 10, 1, 20, new HTuple(),
                                                       new HTuple(), out hv_Index1);
            //Create region of interest for the alignment
            ho_Rectangle.Dispose();
            HOperatorSet.GenRectangle1(out ho_Rectangle, (hv_LineRow1.TupleSelect(0)) - 40,
                                       (hv_LineColumn1.TupleSelect(0)) - 50, (hv_LineRow1.TupleSelect(0)) + 20, (hv_LineColumn1.TupleSelect(
                                                                                                                     0)) + 50);
            //Change the reference coordinate system in which the
            //metrology model is given to be situated at the top of the diamond
            ho_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_Image, ho_Rectangle, out ho_ImageReduced);
            ho_Region.Dispose();
            HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, 128, 255);
            HOperatorSet.GetRegionPoints(ho_Region, out hv_Rows, out hv_Columns);
            HOperatorSet.SetMetrologyModelParam(hv_MetrologyHandle, "reference_system",
                                                ((((hv_Rows.TupleSelect(0))).TupleConcat(hv_Columns.TupleSelect(0)))).TupleConcat(
                                                    0));
            //
            //Main loop
            //
            for (hv_I = 1; (int)hv_I <= 5; hv_I = (int)hv_I + 1)
            {
                ho_Image.Dispose();
                HOperatorSet.ReadImage(out ho_Image, "diamond/diamond_" + (hv_I.TupleString(
                                                                               "02")));
                //Roughly segment the diamond's position
                ho_ImageReduced.Dispose();
                HOperatorSet.ReduceDomain(ho_Image, ho_Rectangle, out ho_ImageReduced);
                ho_Region.Dispose();
                HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, 128, 255);
                //Extract the top of the diamond
                HOperatorSet.GetRegionPoints(ho_Region, out hv_Rows, out hv_Columns);
                //
                //Use the top of the diamond to align the metrology model in
                //the current image
                //
                HOperatorSet.AlignMetrologyModel(hv_MetrologyHandle, hv_Rows.TupleSelect(
                                                     0), hv_Columns.TupleSelect(0), 0);
                //
                //
                //Perform the measurement for both lines in one call
                //
                HOperatorSet.ApplyMetrologyModel(ho_Image, hv_MetrologyHandle);
                //
                //Access results
                //
                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type",
                                                      "all_param", out hv_LineParameter);
                HOperatorSet.AngleLl(hv_LineParameter.TupleSelect(0), hv_LineParameter.TupleSelect(
                                         1), hv_LineParameter.TupleSelect(2), hv_LineParameter.TupleSelect(3),
                                     hv_LineParameter.TupleSelect(4), hv_LineParameter.TupleSelect(5), hv_LineParameter.TupleSelect(
                                         6), hv_LineParameter.TupleSelect(7), out hv_Angle);
                hv_Angle = hv_Angle.TupleDeg();
                //
                //Display results
                //
                //Create line contours
                ho_ResultContour.Dispose();
                HOperatorSet.GetMetrologyObjectResultContour(out ho_ResultContour, hv_MetrologyHandle,
                                                             "all", "all", 1.5);
                HOperatorSet.IntersectionLines(hv_LineParameter.TupleSelect(0), hv_LineParameter.TupleSelect(
                                                   1), hv_LineParameter.TupleSelect(2), hv_LineParameter.TupleSelect(3),
                                               hv_LineParameter.TupleSelect(4), hv_LineParameter.TupleSelect(5), hv_LineParameter.TupleSelect(
                                                   6), hv_LineParameter.TupleSelect(7), out hv_Row, out hv_Column, out hv_IsOverlapping1);
                //Calculate the orientation of the two lines
                HOperatorSet.LineOrientation(hv_LineParameter.TupleSelect(0), hv_LineParameter.TupleSelect(
                                                 1), hv_LineParameter.TupleSelect(2), hv_LineParameter.TupleSelect(3),
                                             out hv_Orientation1);
                if ((int)(new HTuple(hv_Orientation1.TupleGreater(0))) != 0)
                {
                    hv_Orientation1 = hv_Orientation1 - ((new HTuple(180)).TupleRad());
                }
                HOperatorSet.LineOrientation(hv_LineParameter.TupleSelect(4), hv_LineParameter.TupleSelect(
                                                 5), hv_LineParameter.TupleSelect(6), hv_LineParameter.TupleSelect(7),
                                             out hv_Orientation2);
                //
                //Visualize the angle between the lines
                ho_ContCircle.Dispose();
                HOperatorSet.GenCircleContourXld(out ho_ContCircle, hv_Row, hv_Column, 100,
                                                 hv_Orientation1, hv_Orientation2, "positive", 1);
                //Get the used measure regions and the measured points
                //for visualization
                ho_Contour.Dispose();
                HOperatorSet.GetMetrologyObjectMeasures(out ho_Contour, hv_MetrologyHandle,
                                                        "all", "all", out hv_MRow, out hv_MColumn);
                ho_Cross.Dispose();
                HOperatorSet.GenCrossContourXld(out ho_Cross, hv_MRow, hv_MColumn, 6, (new HTuple(45)).TupleRad()
                                                );
                //Display everything
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
                }
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 1);
                }
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetColor(HDevWindowStack.GetActive(), "yellow");
                }
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.DispObj(ho_Contour, HDevWindowStack.GetActive());
                }
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.DispObj(ho_Cross, HDevWindowStack.GetActive());
                }
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 2);
                }
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green");
                }
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.DispObj(ho_ResultContour, HDevWindowStack.GetActive());
                }
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetColor(HDevWindowStack.GetActive(), "blue");
                }
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.DispObj(ho_ContCircle, HDevWindowStack.GetActive());
                }
                disp_message(hv_WindowHandle, ("Angle = " + (hv_Angle.TupleString(".5"))) + "бу",
                             "window", 12, 12, "black", "true");
                if ((int)(new HTuple(hv_I.TupleLess(5))) != 0)
                {
                    disp_continue_message(hv_WindowHandle, "black", "true");
                }
                // stop(); only in hdevelop
            }
            //Clean up memory
            HOperatorSet.ClearMetrologyModel(hv_MetrologyHandle);
        }
        catch (HalconException HDevExpDefaultException)
        {
            ho_Image.Dispose();
            ho_Rectangle.Dispose();
            ho_ImageReduced.Dispose();
            ho_Region.Dispose();
            ho_ResultContour.Dispose();
            ho_ContCircle.Dispose();
            ho_Contour.Dispose();
            ho_Cross.Dispose();

            throw HDevExpDefaultException;
        }
        ho_Image.Dispose();
        ho_Rectangle.Dispose();
        ho_ImageReduced.Dispose();
        ho_Region.Dispose();
        ho_ResultContour.Dispose();
        ho_ContCircle.Dispose();
        ho_Contour.Dispose();
        ho_Cross.Dispose();
    }
예제 #9
0
        //测量工具模拟测量
        private void buttonTestMetrology_Click(object sender, EventArgs e)
        {
            int         TopPt = 0, LeftPt = 0, BottomPt = 0, RightPt = 0;
            HObject     ROI = null, SearchImage = null, ResultContour = null;
            HTuple      Parameter = new HTuple();
            XmlDocument xmlDoc    = new XmlDocument();

            try
            {
                xmlDoc.Load("InputLocateParam.txt");
                XmlNode     rootNode           = xmlDoc.FirstChild;
                XmlNodeList InputParamNodeList = rootNode.ChildNodes;
                foreach (XmlNode InputParamNode in InputParamNodeList)
                {
                    if (InputParamNode.Name == "TopPt")
                    {
                        TopPt = Int32.Parse(InputParamNode.InnerText);
                    }
                    else if (InputParamNode.Name == "LeftPt")
                    {
                        LeftPt = Int32.Parse(InputParamNode.InnerText);
                    }
                    else if (InputParamNode.Name == "BottomPt")
                    {
                        BottomPt = Int32.Parse(InputParamNode.InnerText);
                    }
                    else if (InputParamNode.Name == "RightPt")
                    {
                        RightPt = Int32.Parse(InputParamNode.InnerText);
                    }
                }
                if (MetrologyHandle != null)
                {
                    this.tabControl1.SelectedIndex = 0;
                    hWindowControl1.HalconWindow.ClearWindow();
                    HOperatorSet.DispObj(getImage, hwindow);
                    HOperatorSet.GenRectangle1(out ROI, (HTuple)TopPt, (HTuple)LeftPt, (HTuple)BottomPt, (HTuple)RightPt);
                    HOperatorSet.ReduceDomain(getImage, ROI, out SearchImage);
                    HOperatorSet.ApplyMetrologyModel(SearchImage, MetrologyHandle);

                    HOperatorSet.GetMetrologyObjectResult(MetrologyHandle, "all", "all", "result_type", "all_param", out Parameter);

                    HOperatorSet.GenEmptyObj(out ResultContour);
                    ResultContour.Dispose();
                    HOperatorSet.GetMetrologyObjectResultContour(out ResultContour, MetrologyHandle, "all", "all", 1);

                    HOperatorSet.SetColor(hwindow, "blue");
                    HOperatorSet.DispObj(ResultContour, hwindow);

                    GC.Collect();
                }
                else
                {
                    MessageBox.Show("请先创建测量工具!");
                    return;
                }
            }
            catch (HalconException ex)
            {
                string str, expMsg;
                HTuple expTpl;
                ex.ToHTuple(out expTpl);
                expMsg = expTpl[1].S;
                str    = "Halcon Exception:";
                str   += expMsg;
                MessageBox.Show(str);
                return;
            }
        }
예제 #10
0
        private void Run()
        {
            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
                // ho_Image.Dispose();
                //HOperatorSet.ReadImage(out ho_Image, hv_ImageFiles.TupleSelect(hv_Index));
                HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
                HOperatorSet.SetPart(hWindow_Final1.hWindowControl.HalconWindow, 0, 0, hv_Height, hv_Width);
            }
            ho_ImageEmphasize.Dispose();

            HOperatorSet.Emphasize(ho_Image, out ho_ImageEmphasize, hv_Width, hv_Height,
                                   1);
            //HOperatorSet.DispObj(ho_Image, hWindow_Final1.hWindowControl.HalconWindow);
            // hWindow_Final1.hWindowControl.HalconWindow.DispImage((HImage)ho_Image);
            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
                HOperatorSet.FindScaledShapeModel(ho_Image, c2s.Modle, (new HTuple(0)).TupleRad()
                                                  , (new HTuple(360)).TupleRad(), 0.5, 5, 0.6, 0, 0.5, "none", 0, 0.3,
                                                  out hv_Row3, out hv_Column3, out hv_Angle3, out hv_Scale, out hv_Score);
            }
            HOperatorSet.TupleLength(hv_Score, out hv_Length);
            if ((int)(new HTuple(hv_Length.TupleEqual(0))) != 0)
            {
                //disp_message(hWindow_Final1.hWindowControl.HalconWindow, "failed!!!!!!!!!!!!!!!", "window", 20,
                //    20, "black", "true");
                //disp_continue_message(hWindow_Final1.hWindowControl.HalconWindow, "black", "true");
                ////wait_seconds (2)
                //HDevelopStop();
            }
            else
            {
                //hom_mat2d_identity (HomMat2DIdentity)
                //hom_mat2d_translate (HomMat2DIdentity, Row, Column, HomMat2DTranslate)
                //hom_mat2d_rotate (HomMat2DTranslate, Angle, Row, Column, HomMat2DRotate)
                //hom_mat2d_scale (HomMat2DRotate, Scale, Scale, Row, Column, HomMat2DScale)
                //affine_trans_contour_xld (ModelContours, ModelTrans, HomMat2DScale)
                //dev_display (ModelTrans)
                //下面开始测量尺寸

                HOperatorSet.AlignMetrologyModel(c2s.MetrologyHandle, hv_Row3, hv_Column3,
                                                 hv_Angle3);

                ho_ModelContour.Dispose();
                HOperatorSet.GetMetrologyObjectModelContour(out ho_ModelContour, c2s.MetrologyHandle,
                                                            "all", 1.5);
                //向图像应用测量模型
                HOperatorSet.ApplyMetrologyModel(ho_Image, c2s.MetrologyHandle);
                //获取测量卡尺
                ho_Contour1.Dispose();
                HOperatorSet.GetMetrologyObjectMeasures(out ho_Contour1, c2s.MetrologyHandle,
                                                        "all", "all", out hv_Row, out hv_Column);
                HOperatorSet.TupleLength(hv_Score, out hv_Length);
                if ((int)(new HTuple(hv_Length.TupleEqual(0))) != 0)
                {
                }
                HOperatorSet.GetMetrologyObjectResult(c2s.MetrologyHandle, "all", "all", "used_edges",
                                                      "row", out hv_UsedRow);
                HOperatorSet.GetMetrologyObjectResult(c2s.MetrologyHandle, "all", "all", "used_edges",
                                                      "column", out hv_UsedColumn);
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    ho_UsedEdges.Dispose();
                    HOperatorSet.GenCrossContourXld(out ho_UsedEdges, hv_UsedRow, hv_UsedColumn,
                                                    10, (new HTuple(45)).TupleRad());
                }
                //提取矩形边的长度
                hv_RectIndices = 0;
                HOperatorSet.GetMetrologyObjectResult(c2s.MetrologyHandle, hv_RectIndices,
                                                      "all", "result_type", "length1", out hv_Length1R);
                seqGray.Length1 = hv_Length1R.Length > 0? hv_Length1R.D : 0D;
                HOperatorSet.GetMetrologyObjectResult(c2s.MetrologyHandle, hv_RectIndices,
                                                      "all", "result_type", "length2", out hv_Length2R);
                seqGray.Length2 = hv_Length2R.Length > 0 ? hv_Length2R.D : 0D;
                //中心坐标及角度
                HOperatorSet.GetMetrologyObjectResult(c2s.MetrologyHandle, hv_RectIndices,
                                                      "all", "result_type", "row", out hv_Parameter);
                seqGray.CenterX = hv_Parameter.Length > 0 ? hv_Parameter.D : 0D;;
                HOperatorSet.GetMetrologyObjectResult(c2s.MetrologyHandle, hv_RectIndices,
                                                      "all", "result_type", "column", out hv_Parameter1);
                seqGray.CenterY = hv_Parameter1 > 0 ? hv_Parameter1.D : 0D;
                HOperatorSet.GetMetrologyObjectResult(c2s.MetrologyHandle, hv_RectIndices,
                                                      "all", "result_type", "phi", out hv_Parameter2);
                seqGray.Rotate = hv_Parameter2 > 0 ? hv_Parameter2.D : 0D;
                HOperatorSet.SetColor(hWindow_Final1.hWindowControl.HalconWindow, "red");
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    ho_Cross2.Dispose();
                    HOperatorSet.GenCrossContourXld(out ho_Cross2, hv_Parameter, hv_Parameter1,
                                                    20, (new HTuple(45)).TupleRad());
                }
                //提取直线
                //get_metrology_object_result (MetrologyHandle, 0, 'all', 'result_type', 'all_param', ParamLine1)
                //get_metrology_object_result_contour (Contour, MetrologyHandle, 0, 'all', 1.5)
                ho_ResultContours.Dispose();
                HOperatorSet.GetMetrologyObjectResultContour(out ho_ResultContours, c2s.MetrologyHandle,
                                                             "all", "all", 1.5);
                HOperatorSet.DispObj(ho_ImageEmphasize, hWindow_Final1.hWindowControl.HalconWindow);
                HOperatorSet.SetLineWidth(hWindow_Final1.hWindowControl.HalconWindow, 1);
                HOperatorSet.SetColor(hWindow_Final1.hWindowControl.HalconWindow, "light gray");
                HOperatorSet.DispObj(ho_Contour1, hWindow_Final1.hWindowControl.HalconWindow);
                HOperatorSet.SetColor(hWindow_Final1.hWindowControl.HalconWindow, "green");
                HOperatorSet.SetLineWidth(hWindow_Final1.hWindowControl.HalconWindow, 2);
                HOperatorSet.DispObj(ho_ResultContours, hWindow_Final1.hWindowControl.HalconWindow);
                HOperatorSet.SetLineWidth(hWindow_Final1.hWindowControl.HalconWindow, 1);
                HOperatorSet.SetColor(hWindow_Final1.hWindowControl.HalconWindow, "white");
                HOperatorSet.DispObj(ho_UsedEdges, hWindow_Final1.hWindowControl.HalconWindow);
            }
        }
예제 #11
0
        public bool Find_halcon_line(ExecuteBuffer _executeBuffer, HWndCtrl hWndCtrl, 模板GVName_halcon Model_result, Dictionary <int, PointName> Point_temp_result, out string result_info, out HTuple Row1, out HTuple Column1, out HTuple Row2, out HTuple Column2, bool show_info)
        {
            HTuple MetrologyHandle;
            HTuple pic_wid, pic_height;

            Row1        = new HTuple();
            Column1     = new HTuple();
            Row2        = new HTuple();
            Column2     = new HTuple();
            result_info = "";
            HOperatorSet.CreateMetrologyModel(out MetrologyHandle);
            if (!_executeBuffer.imageBuffer.ContainsKey(this.Threshold_image.SelectedItem.ToString() + ".img"))
            {
                //     MessageBox.Show("查找直线: 输入图像已经不存在,请重置设置输入图像");
                result_info = " 查找直线: 输入图像已经不存在,请重置设置输入图像";
                return(false);
            }
            if (_executeBuffer.imageBuffer[this.Threshold_image.SelectedItem.ToString() + ".img"] == null)
            {
                //     MessageBox.Show("查找直线: image参数为空或者未赋值");
                result_info = " 查找直线: 输入图像已经不存在,请重置设置输入图像";
                return(false);
            }
            HObject imagein = _executeBuffer.imageBuffer[this.Threshold_image.SelectedItem.ToString() + ".img"];

            HOperatorSet.GetImageSize(imagein, out pic_wid, out pic_height);
            HOperatorSet.SetMetrologyModelImageSize(MetrologyHandle, pic_wid, pic_height);
            HTuple Index;
            HTuple trow1, trow2, tcol1, tcol2;

            if (checkBox_line.Checked)
            {
                if (xrow1 == null || xrow2 == null || xcol1 == null || xcol2 == null)
                {
                    //    MessageBox.Show("查找直线:未确认线位置");
                    result_info = " 查找直线: 未确认线位置";
                    return(false);
                }
                else
                {
                    trow1 = xrow1;
                    tcol1 = xcol1;
                    trow2 = xrow2;
                    tcol2 = xcol2;
                }
            }
            else
            {
                if (this.cbb_source1.SelectedIndex == 0)
                {
                    if (Model_result == null)
                    {
                        //      MessageBox.Show("查找直线:匹配列表为空,请设置");
                        result_info = " 查找直线: 匹配列表为空,请设置";
                        return(false);
                    }
                    if (Model_result.点X.TupleLength() < 1)
                    {
                        //    MessageBox.Show("查找直线:匹配列表为空,请设置");
                        result_info = " 查找直线: 匹配列表为空,请设置";
                        return(false);
                    }
                    trow1 = Model_result.点Y[0];
                    tcol1 = Model_result.点X[0];
                }
                else
                {
                    int number1 = Convert.ToInt32(this.tb_value1.Text);
                    if (!Point_temp_result.ContainsKey(number1))
                    {
                        //           MessageBox.Show("查找直线:全局列表点无此点,请设置");
                        result_info = " 查找直线: 全局列表无此点,请设置";
                        return(false);
                    }
                    else
                    {
                        trow1 = Point_temp_result[number1].点Y;
                        tcol1 = Point_temp_result[number1].点X;
                    }
                }
                int number2 = Convert.ToInt32(this.tb_value2.Text);
                if (!Point_temp_result.ContainsKey(number2))
                {
                    //      MessageBox.Show("查找直线:全局列表点无此点,请设置");
                    result_info = " 查找直线: 全局列表无此点,请设置";
                    return(false);
                }
                else
                {
                    trow2 = Point_temp_result[number2].点Y;
                    tcol2 = Point_temp_result[number2].点X;
                }
            }

            HOperatorSet.AddMetrologyObjectLineMeasure(MetrologyHandle, trow1, tcol1, trow2, tcol2, Convert.ToInt32(this.measure_length1.Text.ToString()), Convert.ToInt32(this.measure_length2.Text.ToString()),
                                                       1, Convert.ToInt32(this.measure_threshold.Text.ToString()), new HTuple(), new HTuple(), out Index);
            HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "measure_transition", this.cbb_transition.SelectedItem.ToString());
            HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "num_measures", Convert.ToInt32(this.num_measure.Text.ToString()));
            HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "num_instances", 40);
            HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "measure_sigma", 1);

            //HOperatorSet.SetMetrologyObjectParam(MetrologyHandle,"all","measure_threshold",50);
            HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "measure_interpolation", "bicubic");
            HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "measure_select", this.cbb_measure_select.SelectedItem.ToString());
            HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "min_score", Convert.ToDouble(this.min_score.Text));
            HOperatorSet.ApplyMetrologyModel(imagein, MetrologyHandle);
            HObject Contours, Cross;
            HTuple  Rowx, Columnx;

            HOperatorSet.GetMetrologyObjectMeasures(out Contours, MetrologyHandle, "all", "all", out Rowx, out Columnx);
            HOperatorSet.GenCrossContourXld(out Cross, Rowx, Columnx, 6, 0.785398);
            HTuple hv_Parameter = null;

            //得到线的起点和终点坐标并显示出来
            HOperatorSet.GetMetrologyObjectResult(MetrologyHandle, "all", "all", "result_type",
                                                  "all_param", out hv_Parameter);
            if (hv_Parameter.TupleLength() == 0)
            {
                result_info = " 查找直线: 在指定位置,找直线失败";
                //      MessageBox.Show("未找到直线");
                return(false);
            }
            int line_count = hv_Parameter.TupleLength() / 4;

            double[] Row_1    = new double[line_count];
            double[] Column_1 = new double[line_count];
            double[] Row_2    = new double[line_count];
            double[] Column_2 = new double[line_count];

            for (int imx = 0; imx < line_count; imx++)
            {
                Row_1[imx]    = hv_Parameter[imx * 4 + 1].D;
                Column_1[imx] = hv_Parameter[imx * 4].D;
                Row_2[imx]    = hv_Parameter[imx * 4 + 3].D;
                Column_2[imx] = hv_Parameter[imx * 4 + 2].D;
            }
            Row1    = (HTuple)Row_1;
            Column1 = (HTuple)Column_1;
            Row2    = (HTuple)Row_2;
            Column2 = (HTuple)Column_2;

            HObject ho_Contour;

            HOperatorSet.GenEmptyObj(out ho_Contour);
            ho_Contour.Dispose();
            HOperatorSet.GetMetrologyObjectResultContour(out ho_Contour, MetrologyHandle,
                                                         "all", "all", 1.5);


            HOperatorSet.ClearMetrologyModel(MetrologyHandle);
            if (show_info)
            {
                hWndCtrl.changeGraphicSettings(GraphicsContext.GC_COLOR, this.comboBox1.SelectedItem.ToString());
                hWndCtrl.addIconicVar(Cross);
                hWndCtrl.addIconicVar(Contours);
            }
            hWndCtrl.changeGraphicSettings(GraphicsContext.GC_COLOR, this.comboBox1.SelectedItem.ToString());
            hWndCtrl.addIconicVar(ho_Contour);
            hWndCtrl.repaint();
            return(true);
        }
        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();
            }
        }
예제 #13
0
        private void Camera3()
        {
            c2s = Module.SerializableTool.FromByFile <Module.Camera2Serializable>("cam2.sol");
            //Image Acquisition 01: Code generated by Image Acquisition 01
            hv_ImageFiles = new HTuple();
            if (hv_ImageFiles == null)
            {
                hv_ImageFiles = new HTuple();
            }
            hv_ImageFiles[0] = "G:/防火砖缺陷检测/Image_20201109161631787.bmp";
            if (hv_ImageFiles == null)
            {
                hv_ImageFiles = new HTuple();
            }
            hv_ImageFiles[1] = "G:/防火砖缺陷检测/Image_20201109161638427.bmp";
            if (hv_ImageFiles == null)
            {
                hv_ImageFiles = new HTuple();
            }
            hv_ImageFiles[2] = "G:/防火砖缺陷检测/Image_20201109161641675.bmp";
            if (hv_ImageFiles == null)
            {
                hv_ImageFiles = new HTuple();
            }
            hv_ImageFiles[3] = "G:/防火砖缺陷检测/Image_20201109161651523.bmp";
            if (hv_ImageFiles == null)
            {
                hv_ImageFiles = new HTuple();
            }
            hv_ImageFiles[4] = "G:/防火砖缺陷检测/Image_20201109161654667.bmp";
            if (hv_ImageFiles == null)
            {
                hv_ImageFiles = new HTuple();
            }
            hv_ImageFiles[5] = "G:/防火砖缺陷检测/Image_20201109161701411.bmp";
            if (hv_ImageFiles == null)
            {
                hv_ImageFiles = new HTuple();
            }
            hv_ImageFiles[6] = "G:/防火砖缺陷检测/Image_20201109161704812.bmp";
            if (hv_ImageFiles == null)
            {
                hv_ImageFiles = new HTuple();
            }
            hv_ImageFiles[7] = "G:/防火砖缺陷检测/Image_20201109161707571.bmp";
            if (hv_ImageFiles == null)
            {
                hv_ImageFiles = new HTuple();
            }
            hv_ImageFiles[8] = "G:/防火砖缺陷检测/Image_20201109161715763.bmp";
            if (hv_ImageFiles == null)
            {
                hv_ImageFiles = new HTuple();
            }
            hv_ImageFiles[9] = "G:/防火砖缺陷检测/Image_20201109161720699.bmp";

            for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_ImageFiles.TupleLength()
                                                                  )) - 1); hv_Index = (int)hv_Index + 1)
            {
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    ho_Image.Dispose();
                    HOperatorSet.ReadImage(out ho_Image, hv_ImageFiles.TupleSelect(hv_Index));
                    HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
                    HOperatorSet.SetPart(hWindow_Final1.hWindowControl.HalconWindow, 0, 0, hv_Height, hv_Width);
                }
                ho_ImageEmphasize.Dispose();

                HOperatorSet.Emphasize(ho_Image, out ho_ImageEmphasize, hv_Width, hv_Height,
                                       1);
                HOperatorSet.DispObj(ho_Image, hWindow_Final1.hWindowControl.HalconWindow);
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    HOperatorSet.FindScaledShapeModel(ho_Image, c2s.Modle, (new HTuple(0)).TupleRad()
                                                      , (new HTuple(360)).TupleRad(), 0.7, 1.3, 0.6, 0, 0.5, "none", 0, 0.3,
                                                      out hv_Row3, out hv_Column3, out hv_Angle3, out hv_Scale, out hv_Score);
                }
                HOperatorSet.TupleLength(hv_Score, out hv_Length);
                if ((int)(new HTuple(hv_Length.TupleEqual(0))) != 0)
                {
                    //disp_message(hWindow_Final1.hWindowControl.HalconWindow, "failed!!!!!!!!!!!!!!!", "window", 20,
                    //    20, "black", "true");
                    //disp_continue_message(hWindow_Final1.hWindowControl.HalconWindow, "black", "true");
                    ////wait_seconds (2)
                    //HDevelopStop();
                }
                else
                {
                    //hom_mat2d_identity (HomMat2DIdentity)
                    //hom_mat2d_translate (HomMat2DIdentity, Row, Column, HomMat2DTranslate)
                    //hom_mat2d_rotate (HomMat2DTranslate, Angle, Row, Column, HomMat2DRotate)
                    //hom_mat2d_scale (HomMat2DRotate, Scale, Scale, Row, Column, HomMat2DScale)
                    //affine_trans_contour_xld (ModelContours, ModelTrans, HomMat2DScale)
                    //dev_display (ModelTrans)
                    //下面开始测量尺寸

                    HOperatorSet.AlignMetrologyModel(c2s.MetrologyHandle, hv_Row3, hv_Column3,
                                                     hv_Angle3);

                    ho_ModelContour.Dispose();
                    HOperatorSet.GetMetrologyObjectModelContour(out ho_ModelContour, c2s.MetrologyHandle,
                                                                "all", 1.5);
                    //向图像应用测量模型
                    HOperatorSet.ApplyMetrologyModel(ho_Image, c2s.MetrologyHandle);
                    //获取测量卡尺
                    ho_Contour1.Dispose();
                    HOperatorSet.GetMetrologyObjectMeasures(out ho_Contour1, c2s.MetrologyHandle,
                                                            "all", "all", out hv_Row, out hv_Column);
                    HOperatorSet.TupleLength(hv_Score, out hv_Length);
                    if ((int)(new HTuple(hv_Length.TupleEqual(0))) != 0)
                    {
                    }
                    HOperatorSet.GetMetrologyObjectResult(c2s.MetrologyHandle, "all", "all", "used_edges",
                                                          "row", out hv_UsedRow);
                    HOperatorSet.GetMetrologyObjectResult(c2s.MetrologyHandle, "all", "all", "used_edges",
                                                          "column", out hv_UsedColumn);
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        ho_UsedEdges.Dispose();
                        HOperatorSet.GenCrossContourXld(out ho_UsedEdges, hv_UsedRow, hv_UsedColumn,
                                                        10, (new HTuple(45)).TupleRad());
                    }
                    //提取矩形边的长度
                    hv_RectIndices = 0;
                    HOperatorSet.GetMetrologyObjectResult(c2s.MetrologyHandle, hv_RectIndices,
                                                          "all", "result_type", "length1", out hv_Length1R);
                    HOperatorSet.GetMetrologyObjectResult(c2s.MetrologyHandle, hv_RectIndices,
                                                          "all", "result_type", "length2", out hv_Length2R);
                    //中心坐标及角度
                    HOperatorSet.GetMetrologyObjectResult(c2s.MetrologyHandle, hv_RectIndices,
                                                          "all", "result_type", "row", out hv_Parameter);
                    HOperatorSet.GetMetrologyObjectResult(c2s.MetrologyHandle, hv_RectIndices,
                                                          "all", "result_type", "column", out hv_Parameter1);
                    HOperatorSet.GetMetrologyObjectResult(c2s.MetrologyHandle, hv_RectIndices,
                                                          "all", "result_type", "phi", out hv_Parameter2);
                    HOperatorSet.SetColor(hWindow_Final1.hWindowControl.HalconWindow, "red");
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        ho_Cross2.Dispose();
                        HOperatorSet.GenCrossContourXld(out ho_Cross2, hv_Parameter, hv_Parameter1,
                                                        20, (new HTuple(45)).TupleRad());
                    }
                    //提取直线
                    //get_metrology_object_result (MetrologyHandle, 0, 'all', 'result_type', 'all_param', ParamLine1)
                    //get_metrology_object_result_contour (Contour, MetrologyHandle, 0, 'all', 1.5)
                    ho_ResultContours.Dispose();
                    HOperatorSet.GetMetrologyObjectResultContour(out ho_ResultContours, c2s.MetrologyHandle,
                                                                 "all", "all", 1.5);
                    HOperatorSet.DispObj(ho_ImageEmphasize, hWindow_Final1.hWindowControl.HalconWindow);
                    HOperatorSet.SetLineWidth(hWindow_Final1.hWindowControl.HalconWindow, 1);
                    HOperatorSet.SetColor(hWindow_Final1.hWindowControl.HalconWindow, "light gray");
                    HOperatorSet.DispObj(ho_Contour1, hWindow_Final1.hWindowControl.HalconWindow);
                    HOperatorSet.SetColor(hWindow_Final1.hWindowControl.HalconWindow, "green");
                    HOperatorSet.SetLineWidth(hWindow_Final1.hWindowControl.HalconWindow, 2);
                    HOperatorSet.DispObj(ho_ResultContours, hWindow_Final1.hWindowControl.HalconWindow);
                    HOperatorSet.SetLineWidth(hWindow_Final1.hWindowControl.HalconWindow, 1);
                    HOperatorSet.SetColor(hWindow_Final1.hWindowControl.HalconWindow, "white");
                    HOperatorSet.DispObj(ho_UsedEdges, hWindow_Final1.hWindowControl.HalconWindow);
                    ho_Image.Dispose();
                }
            }
            hv_ImageFiles.Dispose();
            GC.Collect();
        }
예제 #14
0
        public bool FindLine(HTuple hv_HomMat2D, HTuple hv_HomMat2DPix)
        {
            if (ho_Image == null)
            {
                errorFlag = true;
                return(false);
            }

            if (hv_Model != 0)
            {
                //HTuple hv_Width;
                //HTuple hv_Height;
                //HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
                //for (int i = 0; i < hv_Height; i++)
                //{

                //    for (int j = 0; j < hv_Width; j++)
                //    {
                //        HTuple hv_Grayval;
                //        HOperatorSet.GetGrayval(ho_Image, i, j, out hv_Grayval);
                //        if (hv_Grayval <= hv_RegionMax && hv_Grayval >= hv_RegionMin)
                //        {
                //            HOperatorSet.SetGrayval(ho_Image, i, j, 255);
                //        }
                //    }
                //}

                HObject ho_Region;

                HOperatorSet.Threshold(ho_Image, out ho_Region, hv_RegionMin, hv_RegionMax);

                HOperatorSet.ReduceDomain(ho_Image, ho_Region, out ho_Image);
                //string Path = AppDomain.CurrentDomain.BaseDirectory + @".//Parameters//test"+ DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_ffff");
                //HOperatorSet.WriteImage(ho_Image,"bmp",0, Path);
            }

            if (!CreateLineModel(hv_HomMat2D))
            {
                errorFlag = true;
                return(false);
            }

            try
            {
                HOperatorSet.ApplyMetrologyModel(ho_Image, hv_MetrologyHandle);

                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type", "row_begin", out hv_LineRowBegin);
                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type", "column_begin", out hv_LineColumnBegin);
                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type", "row_end", out hv_LineRowEnd);
                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type", "column_end", out hv_LineColumnEnd);

                if (hv_LineRowBegin.Length != 1)
                {
                    errorFlag = true;
                    return(false);
                }

                HOperatorSet.AffineTransPixel(hv_HomMat2DPix, hv_LineRowBegin, hv_LineColumnBegin, out hv_LineRowBegin_Real, out hv_LineColumnBegin_Real);
                HOperatorSet.AffineTransPixel(hv_HomMat2DPix, hv_LineRowEnd, hv_LineColumnEnd, out hv_LineRowEnd_Real, out hv_LineColumnEnd_Real);

                errorFlag = false;
                return(true);
            }
            catch (Exception exc)
            {
                errorFlag = true;
                return(false);
            }
        }
예제 #15
0
        public bool FindMark(string FileName, out double centerRow, out double centerCol, out double angle)
        {
            bool result = false;

            if (hv_ModelID == null)
            {
                GenMarkModel(out hv_ModelID, out ho_ModelContours, out hv_MetrologyHandle);
            }

            HObject ho_Image;
            HObject ho_ResultContours = null, ho_Contour = null;
            HObject ho_UsedEdges = null, ho_Cross = null;

            HTuple hv_RowFound = new HTuple(), hv_ColFound = new HTuple(), hv_AngleFound = new HTuple();
            HTuple hv_ScoreFound = new HTuple();
            HTuple hv_HomMat2D = null;
            HTuple hv_Column = null, hv_Row = null;

            HTuple hv_UsedColumn = new HTuple(), hv_UsedRow = new HTuple();
            HTuple hv_Angle = new HTuple(), hv_Degree = new HTuple();

            HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_ResultContours);
            HOperatorSet.GenEmptyObj(out ho_Contour);
            HOperatorSet.GenEmptyObj(out ho_UsedEdges);
            HOperatorSet.GenEmptyObj(out ho_Cross);

            ho_Image.Dispose();
            HOperatorSet.ReadImage(out ho_Image, FileName);
            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.DispObj(ho_Image, hv_ExpDefaultWinHandle);
                HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "red");
                HOperatorSet.DispObj(ho_ResultContours, hv_ExpDefaultWinHandle);

                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("mark位置:") + "[ ") + hv_RowFound) + new HTuple(",")) + hv_ColFound) + new HTuple(",")) + hv_Degree) + "° ]",
                             "window", 32, 12, "black", "true");
                //相对模板角度
                //HOperatorSet.TupleDeg(hv_AngleFound, out hv_Degree);
                //disp_message(hv_ExpDefaultWinHandle, ("相对模板角度为" + hv_Degree) + "°", "window",
                //    52, 12, "black", "true");

                centerRow = hv_RowFound;
                centerCol = hv_ColFound;
                angle     = hv_Degree;

                result = true;
            }
            else
            {
                disp_message(hv_ExpDefaultWinHandle, "未识别到靶标", "window", hv_Row, hv_Column,
                             "black", "true");
                centerRow = centerCol = angle = 0;
            }

            ho_Image.Dispose();
            ho_ResultContours.Dispose();
            ho_Contour.Dispose();
            ho_UsedEdges.Dispose();
            ho_Cross.Dispose();

            return(result);
        }
예제 #16
0
        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();
            }
        }
예제 #17
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();
    }
예제 #18
0
        public void Run()
        {
            if (inputImage == null)
            {
                FormFindLine.Instance.TextBoxMessageDisp("图像为空", System.Drawing.Color.Red);
                toolRunStatu = ToolRunStatu.Not_Input_Image;
                return;
            }
            try
            {
                UpdateImage();
                if (inputPose != null)
                {
                    HTuple Row   = inputPose.X - templatePose.X;
                    HTuple Col   = inputPose.Y - templatePose.Y;
                    HTuple angle = inputPose.U - templatePose.U;

                    HTuple _homMat2D;
                    HOperatorSet.HomMat2dIdentity(out _homMat2D);
                    HOperatorSet.HomMat2dRotate(_homMat2D, (HTuple)(angle), (HTuple)templatePose.X, (HTuple)templatePose.Y, out _homMat2D);
                    HOperatorSet.HomMat2dTranslate(_homMat2D, (HTuple)(Row), (HTuple)(Col), out _homMat2D);

                    //对预期线的起始点做放射变换
                    HOperatorSet.AffineTransPixel(_homMat2D, (HTuple)expectLineStartRow, (HTuple)expectLineStartCol, out newExpectLineStartRow, out newExpectLineStartCol);
                    HOperatorSet.AffineTransPixel(_homMat2D, (HTuple)expectLineEndRow, (HTuple)expectLineEndCol, out newExpectLineEndRow, out newExpectLineEndCol);
                }
                else
                {
                    newExpectLineStartRow = expectLineStartRow;
                    newExpectLineStartCol = expectLineStartCol;
                    newExpectLineEndRow   = expectLineEndRow;
                    newExpectLineEndCol   = expectLineEndCol;
                }

                HTuple handleID;
                HOperatorSet.CreateMetrologyModel(out handleID);
                HTuple width, height;
                HOperatorSet.GetImageSize(inputImage, out width, out height);
                HOperatorSet.SetMetrologyModelImageSize(handleID, width[0], height[0]);
                HTuple index;
                HOperatorSet.AddMetrologyObjectLineMeasure(handleID, newExpectLineStartRow, newExpectLineStartCol, newExpectLineEndRow, newExpectLineEndCol, new HTuple(50), new HTuple(20), new HTuple(1), new HTuple(30), new HTuple(), new HTuple(), out index);

                //参数在这里设置
                HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_transition"), new HTuple(polarity));
                HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("num_measures"), new HTuple(cliperNum));
                HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_length1"), new HTuple(length));
                HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_length2"), new HTuple(weidth));
                HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_threshold"), new HTuple(threshold));
                HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_select"), new HTuple(edgeSelect));
                HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_sigma"), new HTuple(sigma));
                HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("min_score"), new HTuple(minScore));
                HOperatorSet.ApplyMetrologyModel(inputImage, handleID);

                //显示所有卡尺
                HTuple pointRow, pointCol;
                HOperatorSet.GetMetrologyObjectMeasures(out contours, handleID, new HTuple("all"), new HTuple("all"), out pointRow, out pointCol);
                HOperatorSet.SetColor(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, new HTuple("blue"));
                HOperatorSet.DispObj(contours, FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow);
                FormFindLine.Instance.myHwindow.DispObj(contours, "blue");

                //显示指示找线方向的箭头
                HTuple arrowAngle;
                HOperatorSet.AngleLx(newExpectLineStartRow, newExpectLineStartCol, newExpectLineEndRow, newExpectLineEndCol, out arrowAngle);
                #region 测试箭头


                #endregion
                arrowAngle = arrowAngle + Math.PI / 2;
                arrowAngle = arrowAngle.TupleDeg();
                HTuple row = (newExpectLineStartRow + newExpectLineEndRow) / 2;
                HTuple column = (newExpectLineStartCol + newExpectLineEndCol) / 2;
                double drow, dcolumn;
                double arrowLength = length + 100;
                if (0 <= arrowAngle && arrowAngle <= 90)
                {
                    drow    = Math.Abs(arrowLength * Math.Sin(((HTuple)arrowAngle).TupleRad()));
                    dcolumn = Math.Abs(arrowLength * Math.Cos(((HTuple)arrowAngle).TupleRad()));
                    HOperatorSet.DispArrow(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, row, column, row - drow, column + dcolumn, 5.0);
                }
                else if (arrowAngle > 90 && arrowAngle <= 180)
                {
                    drow    = arrowLength * Math.Sin(((HTuple)(180 - arrowAngle)).TupleRad());
                    dcolumn = arrowLength * Math.Cos(((HTuple)(180 - arrowAngle)).TupleRad());
                    HOperatorSet.DispArrow(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, row, column, row - drow, column - dcolumn, 5.0);
                }
                else if (arrowAngle < 0 && arrowAngle >= -90)
                {
                    drow    = arrowLength * Math.Sin(((HTuple)arrowAngle * -1).TupleRad());
                    dcolumn = arrowLength * Math.Cos(((HTuple)arrowAngle * -1).TupleRad());
                    HOperatorSet.DispArrow(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, row, column, row + drow, column + dcolumn, 5.0);
                }
                else if (arrowAngle < -90 && arrowAngle >= -180)
                {
                    drow    = Math.Abs(arrowLength * Math.Sin(((HTuple)arrowAngle + 180).TupleRad()));
                    dcolumn = Math.Abs(arrowLength * Math.Cos(((HTuple)arrowAngle + 180).TupleRad()));
                    HOperatorSet.DispArrow(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, row, column, row + drow, column - dcolumn, 5.0);
                }

                //把点显示出来
                HObject cross;
                HOperatorSet.GenCrossContourXld(out cross, pointRow, pointCol, new HTuple(12), new HTuple(0));
                HOperatorSet.SetColor(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, new HTuple("orange"));
                HOperatorSet.DispObj(cross, FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow);
                FormFindLine.Instance.myHwindow.DispObj(cross, "orange");

                //得到所找到的线
                HTuple  parameter;
                HObject line;
                HOperatorSet.GetMetrologyObjectResult(handleID, new HTuple("all"), new HTuple("all"), new HTuple("result_type"), new HTuple("all_param"), out parameter);
                HOperatorSet.GetMetrologyObjectResultContour(out line, handleID, new HTuple("all"), new HTuple("all"), new HTuple(1.5));

                if (parameter.Length >= 4)
                {
                    ResultLineStartRow = parameter[0];
                    ResultLineStartCol = parameter[1];
                    ResultLineEndRow   = parameter[2];
                    ResultLineEndCol   = parameter[3];
                    Point start = new Point()
                    {
                        Row = ResultLineStartRow, Col = ResultLineStartCol
                    };
                    Point end = new Point()
                    {
                        Row = ResultLineEndRow, Col = ResultLineEndCol
                    };
                    resultLine = new Line()
                    {
                        StartPoint = start, EndPoint = end
                    };

                    //显示找到的线
                    HOperatorSet.SetColor(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, new HTuple("green"));
                    // HOperatorSet.DispObj(line, GetWindowHandle(jobName));
                    FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow.SetLineWidth(2);
                    FormFindLine.Instance.myHwindow.DispObj(line, "green");
                }
                HOperatorSet.AngleLx(ResultLineStartRow, ResultLineStartCol, ResultLineEndRow, ResultLineEndCol, out _angle);
                HOperatorSet.ClearMetrologyModel(handleID);

                FormFindLine.Instance.tbx_resultStartRow.Text = ResultLineStartRow.ToString();
                FormFindLine.Instance.tbx_resultStartCol.Text = ResultLineEndCol.ToString();
                FormFindLine.Instance.tbx_resultEndRow.Text   = ResultLineEndRow.ToString();
                FormFindLine.Instance.tbx_resultEndCol.Text   = ResultLineEndCol.ToString();
                FormFindLine.Instance.TextBoxMessageDisp("运行成功", System.Drawing.Color.Green);
                toolRunStatu = ToolRunStatu.Succeed;
            }
            catch (Exception ex)
            {
                FormFindLine.Instance.TextBoxMessageDisp("工具运行异常" + ex.Message, System.Drawing.Color.Red);
            }
        }
예제 #19
0
        public void Run(SoftwareRunState softwareRunState)
        {
            Stopwatch sw = new Stopwatch();

            sw.Restart();
            HTuple  homMat2DArrow = null;
            HObject arrow = null, arrowTrans = null;
            HObject drawLine = null, imageReducedLine;

            if (inputImage == null)
            {
                FormFindLine.Instance.SetToolStatus("工具输入图像为空", ToolRunStatu.Not_Input_Image);
                return;
            }
            try
            {
                UpdateImage();
                if (inputPose != null)
                {
                    HTuple Row   = inputPose.X - templatePose.X;
                    HTuple Col   = inputPose.Y - templatePose.Y;
                    HTuple angle = inputPose.U - templatePose.U;

                    HTuple _homMat2D;
                    HOperatorSet.HomMat2dIdentity(out _homMat2D);
                    HOperatorSet.HomMat2dRotate(_homMat2D, (HTuple)(angle), (HTuple)templatePose.X, (HTuple)templatePose.Y, out _homMat2D);
                    HOperatorSet.HomMat2dTranslate(_homMat2D, (HTuple)(Row), (HTuple)(Col), out _homMat2D);

                    //对预期线的起始点做放射变换
                    HOperatorSet.AffineTransPixel(_homMat2D, (HTuple)expectLineStartRow, (HTuple)expectLineStartCol, out newExpectLineStartRow, out newExpectLineStartCol);
                    HOperatorSet.AffineTransPixel(_homMat2D, (HTuple)expectLineEndRow, (HTuple)expectLineEndCol, out newExpectLineEndRow, out newExpectLineEndCol);
                }
                else
                {
                    newExpectLineStartRow = expectLineStartRow;
                    newExpectLineStartCol = expectLineStartCol;
                    newExpectLineEndRow   = expectLineEndRow;
                    newExpectLineEndCol   = expectLineEndCol;
                }

                HTuple handleID;
                HOperatorSet.CreateMetrologyModel(out handleID);
                HTuple width, height;
                HOperatorSet.GetImageSize(inputImage, out width, out height);
                HOperatorSet.SetMetrologyModelImageSize(handleID, width[0], height[0]);
                HTuple index;
                HOperatorSet.AddMetrologyObjectLineMeasure(handleID, newExpectLineStartRow, newExpectLineStartCol, newExpectLineEndRow, newExpectLineEndCol, new HTuple(50), new HTuple(20), new HTuple(1), new HTuple(30), new HTuple(), new HTuple(), out index);

                //参数在这里设置
                HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_transition"), new HTuple(polarity));
                HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("num_measures"), new HTuple(cliperNum));
                HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_length1"), new HTuple(length));
                HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_length2"), new HTuple(weidth));
                HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_threshold"), new HTuple(threshold));
                HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_select"), new HTuple(edgeSelect));
                HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_sigma"), new HTuple(sigma));
                HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("min_score"), new HTuple(minScore));
                HOperatorSet.ApplyMetrologyModel(inputImage, handleID);

                //显示所有卡尺
                HTuple pointRow, pointCol;
                HOperatorSet.GetMetrologyObjectMeasures(out contoursDisp, handleID, new HTuple("all"), new HTuple("all"), out pointRow, out pointCol);


                //显示指示找线方向的箭头

                #region 测试箭头
                HTuple arrowRow = null, arrowColumn = null;
                HOperatorSet.GenRegionLine(out drawLine, newExpectLineStartRow, newExpectLineStartCol, newExpectLineEndRow, newExpectLineEndCol);
                HOperatorSet.ReduceDomain(inputImage, drawLine, out imageReducedLine);
                HOperatorSet.GetRegionPoints(imageReducedLine, out arrowRow, out arrowColumn);
                if (arrowRow.Length < 200)
                {
                    CommonMethods.CommonMethods.gen_arrow_contour_xld(out arrow, arrowRow[0], arrowColumn[0], arrowRow[arrowRow.Length - 1], arrowColumn[arrowRow.Length - 1], 20, 20);
                }
                else
                {
                    CommonMethods.CommonMethods.gen_arrow_contour_xld(out arrow, arrowRow[0], arrowColumn[0], arrowRow[200], arrowColumn[200], 20, 20);
                }
                HOperatorSet.VectorAngleToRigid(newExpectLineStartRow, newExpectLineStartCol, 0, (newExpectLineStartRow + newExpectLineEndRow) / 2, (newExpectLineStartCol + newExpectLineEndCol) / 2, new HTuple(-90).TupleRad(), out homMat2DArrow);
                HOperatorSet.AffineTransContourXld(arrow, out arrowDisp, homMat2DArrow);
                #endregion

                //把点显示出来
                HOperatorSet.GenCrossContourXld(out crossDisp, pointRow, pointCol, new HTuple(12), new HTuple(0));

                //得到所找到的线
                HTuple parameter;
                HOperatorSet.GetMetrologyObjectResult(handleID, new HTuple("all"), new HTuple("all"), new HTuple("result_type"), new HTuple("all_param"), out parameter);
                HOperatorSet.GetMetrologyObjectResultContour(out LineDisp, handleID, new HTuple("all"), new HTuple("all"), new HTuple(1.5));

                if (parameter.Length >= 4)
                {
                    ResultLineStartRow = parameter[0];
                    ResultLineStartCol = parameter[1];
                    ResultLineEndRow   = parameter[2];
                    ResultLineEndCol   = parameter[3];
                    Point start = new Point()
                    {
                        Row = ResultLineStartRow, Col = ResultLineStartCol
                    };
                    Point end = new Point()
                    {
                        Row = ResultLineEndRow, Col = ResultLineEndCol
                    };
                    resultLine = new Line()
                    {
                        StartPoint = start, EndPoint = end
                    };
                }
                HOperatorSet.AngleLx(ResultLineStartRow, ResultLineStartCol, ResultLineEndRow, ResultLineEndCol, out _angle);
                if (softwareRunState == SoftwareRunState.Debug)
                {
                    DispMainWindow(FormFindLine.Instance.myHwindow);
                    FormFindLine.Instance.tbx_resultStartRow.Text = ResultLineStartRow.ToString();
                    FormFindLine.Instance.tbx_resultStartCol.Text = ResultLineEndCol.ToString();
                    FormFindLine.Instance.tbx_resultEndRow.Text   = ResultLineEndRow.ToString();
                    FormFindLine.Instance.tbx_resultEndCol.Text   = ResultLineEndCol.ToString();
                }
                HOperatorSet.ClearMetrologyModel(handleID);
                // 参数传递
                ParamsTrans();
                sw.Stop();
                runTime = $"运行时间: {sw.ElapsedMilliseconds} ms";
                FormFindLine.Instance.SetToolStatus("工具运行成功!", ToolRunStatu.Succeed);
            }
            catch (Exception ex)
            {
                FormFindLine.Instance.SetToolStatus($"工具运行异常,异常原因: {ex}", ToolRunStatu.Tool_Run_Error);
            }
            finally
            {
                //homMat2DArrow.Dispose();
                //arrow.Dispose();
                //arrowTrans.Dispose();
            }
        }
예제 #20
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();
            }
        }
예제 #21
0
        public bool Find_halcon_circle(ExecuteBuffer _executeBuffer, HWndCtrl hWndCtrl, 模板GVName_halcon Model_result, Dictionary <int, PointName> Point_temp_result, out string result_info, out HTuple Row, out HTuple Column, out HTuple Radius, bool show_info)
        {
            HTuple MetrologyHandle;
            HTuple pic_wid, pic_height;

            Row         = new HTuple();
            Column      = new HTuple();
            Radius      = new HTuple();
            result_info = "";
            HOperatorSet.CreateMetrologyModel(out MetrologyHandle);
            if (!_executeBuffer.imageBuffer.ContainsKey(this.Threshold_image.SelectedItem.ToString() + ".img"))
            {
                MessageBox.Show("查找圆: 输入图像已经不存在,请重置设置输入图像");
                result_info = " 查找圆: 输入图像已经不存在,请重置设置输入图像";
                return(false);
            }
            if (_executeBuffer.imageBuffer[this.Threshold_image.SelectedItem.ToString() + ".img"] == null)
            {
                MessageBox.Show("查找圆: image参数为空或者未赋值");
                result_info = " 查找圆: 输入图像已经不存在,请重置设置输入图像";
                return(false);
            }

            /*
             * if (xrow == null || xcol == null || xradius == null)
             * {
             *  MessageBox.Show("查找圆: 未确定圆位置");
             *  result_info = " 查找圆: 未确定圆位置";
             *  return false;
             * }*/
            HObject imagein = _executeBuffer.imageBuffer[this.Threshold_image.SelectedItem.ToString() + ".img"];

            HOperatorSet.GetImageSize(imagein, out pic_wid, out pic_height);
            HOperatorSet.SetMetrologyModelImageSize(MetrologyHandle, pic_wid, pic_height);
            HTuple Index;

            HTuple trow, tcol, tradius;

            if (this.checkBox_circle.Checked)
            {
                if (xrow == null || xcol == null || xradius == null)
                {
                    MessageBox.Show("查找直线:未确认线位置");
                    result_info = " 查找直线: 未确认线位置";
                    return(false);
                }
                else if (xrow.D == 0 && xcol.D == 0 && xradius.D == 0)
                {
                    MessageBox.Show("查找直线:未确认线位置");
                    result_info = " 查找直线: 未确认线位置";
                    return(false);
                }
                else
                {
                    trow    = xrow;
                    tcol    = xcol;
                    tradius = xradius;
                }
            }
            else
            {
                tradius = Convert.ToDouble(this.textBox_radius.Text);
                if (this.cbb_source1.SelectedIndex == 0)
                {
                    if (Model_result == null)
                    {
                        MessageBox.Show("查找圆:匹配列表为空,请设置");
                        result_info = " 查找圆: 匹配列表为空,请设置";
                        return(false);
                    }
                    if (Model_result.点X.TupleLength() < 1)
                    {
                        MessageBox.Show("查找圆:匹配列表为空,请设置");
                        result_info = " 查找圆: 匹配列表为空,请设置";
                        return(false);
                    }
                    trow = Model_result.点Y[0];
                    tcol = Model_result.点X[0];
                }
                else
                {
                    int number1 = Convert.ToInt32(this.tb_value1.Text);
                    if (!Point_temp_result.ContainsKey(number1))
                    {
                        MessageBox.Show("查找圆:全局列表点无此点,请设置");
                        result_info = " 查找圆: 全局列表无此点,请设置";
                        return(false);
                    }
                    else
                    {
                        trow = Point_temp_result[number1].点Y;
                        tcol = Point_temp_result[number1].点X;
                    }
                }
            }



            HOperatorSet.AddMetrologyObjectCircleMeasure(MetrologyHandle, trow, tcol, tradius, Convert.ToInt32(this.measure_length1.Text.ToString()), Convert.ToInt32(this.measure_length2.Text.ToString()), 1, Convert.ToInt32(this.measure_threshold.Text.ToString()), new HTuple(), new HTuple(), out Index);
            //    HOperatorSet.SetMetrologyObjectParam(MetrologyHandle,"all","measure_transition","negative");
            HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "measure_transition", this.cbb_transition.SelectedItem.ToString());
            HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "measure_select", this.cbb_measure_select.SelectedItem.ToString());
            HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "num_measures", Convert.ToInt32(this.num_measure.Text.ToString()));
            HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "num_instances", 40);
            HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "measure_sigma", 1);
            HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "measure_interpolation", "bicubic");
            //HOperatorSet.SetMetrologyObjectParam(MetrologyHandle,"all","measure_select","all");
            HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "min_score", 0.4);
            HOperatorSet.ApplyMetrologyModel(imagein, MetrologyHandle);
            HObject Contours, Cross;

            HOperatorSet.GetMetrologyObjectMeasures(out Contours, MetrologyHandle, "all", "all", out Row, out Column);
            HOperatorSet.GenCrossContourXld(out Cross, Row, Column, 6, 0.785398);
            HTuple hv_Parameter = null;

            //得到线的起点和终点坐标并显示出来
            HOperatorSet.GetMetrologyObjectResult(MetrologyHandle, "all", "all", "result_type",
                                                  "all_param", out hv_Parameter);
            if (hv_Parameter.TupleLength() == 0)
            {
                result_info = " 查找圆: 在指定位置,找圆失败";
                MessageBox.Show("未找到圆");
                return(false);
            }
            int Circle_number = hv_Parameter.TupleLength() / 3;

            double[] Row_out    = new double[Circle_number];
            double[] Column_out = new double[Circle_number];
            double[] Radius_out = new double[Circle_number];
            for (int i = 0; i < Circle_number; i++)
            {
                Row_out[i]    = hv_Parameter[i * 3 + 1].D;
                Column_out[i] = hv_Parameter[i * 3].D;
                Radius_out[i] = hv_Parameter[i * 3 + 2].D;
            }
            Row    = (HTuple)Row_out;
            Column = (HTuple)Column_out;
            Radius = (HTuple)Radius_out;

            HObject ho_Contour;

            HOperatorSet.GenEmptyObj(out ho_Contour);
            ho_Contour.Dispose();
            HOperatorSet.GetMetrologyObjectResultContour(out ho_Contour, MetrologyHandle,
                                                         "all", "all", 1.5);


            HOperatorSet.ClearMetrologyModel(MetrologyHandle);
            if (show_info)
            {
                hWndCtrl.changeGraphicSettings(GraphicsContext.GC_COLOR, this.comboBox1.SelectedItem.ToString());
                hWndCtrl.addIconicVar(Cross);
                hWndCtrl.addIconicVar(Contours);
            }
            hWndCtrl.changeGraphicSettings(GraphicsContext.GC_COLOR, this.comboBox1.SelectedItem.ToString());
            hWndCtrl.addIconicVar(ho_Contour);
            hWndCtrl.repaint();
            return(true);
        }
예제 #22
0
        private double ImageAlgorithmII(HObject ho_ResultImage)
        {
            // Local iconic variables

            HObject ho_ResultRegion = null;
            HObject ho_ResultConnectedRegions = null, ho_ResultSelectedRegions = null;
            HObject ho_ResultCircle = null, ho_ResultContour = null;

            // Local control variables

            HTuple hv_Width = null, hv_Height = null;
            HTuple hv_MetrologyHandle = null, hv_CircleRadiusTolerance = null;
            HTuple hv_NewRadius = null;
            HTuple hv_ResultArea = new HTuple(), hv_ResultRow = new HTuple();
            HTuple hv_ResultColumn = new HTuple(), hv_MetrologyCircleIndice = new HTuple();
            HTuple hv_CircleRadiusResult = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_ResultRegion);
            HOperatorSet.GenEmptyObj(out ho_ResultConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_ResultSelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_ResultCircle);
            HOperatorSet.GenEmptyObj(out ho_ResultContour);

            HOperatorSet.GetImageSize(ho_ResultImage, out hv_Width, out hv_Height);

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

            //设置测量对象图像大小
            HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandle, hv_Width, hv_Height);
            //
            hv_CircleRadiusTolerance = 100;

            //圆半径
            hv_NewRadius = 324;


            //阈值图像
            ho_ResultRegion.Dispose();
            HOperatorSet.Threshold(ho_ResultImage, out ho_ResultRegion, 0, 60);

            //区域连通处理
            ho_ResultConnectedRegions.Dispose();
            HOperatorSet.Connection(ho_ResultRegion, out ho_ResultConnectedRegions);

            //根据面积和圆度来过滤想要的圆区域
            ho_ResultSelectedRegions.Dispose();
            HOperatorSet.SelectShape(ho_ResultConnectedRegions, out ho_ResultSelectedRegions,
                                     (new HTuple("area")).TupleConcat("roundness"), "and", (new HTuple(250000)).TupleConcat(
                                         0.9), (new HTuple(320000)).TupleConcat(1));

            //获取圆的中心坐标和面积
            HOperatorSet.AreaCenter(ho_ResultSelectedRegions, out hv_ResultArea, out hv_ResultRow, out hv_ResultColumn);


            HOperatorSet.SetColor(WindowID, "green");

            //设置区域的填充模式
            HOperatorSet.SetDraw(WindowID, "margin");

            //显示图像
            HOperatorSet.DispObj(ho_ResultImage, WindowID);

            if (hv_ResultArea.Length != 0)
            {
                //生成圆
                ho_ResultCircle.Dispose();
                HOperatorSet.GenCircle(out ho_ResultCircle, hv_ResultRow, hv_ResultColumn, hv_NewRadius);
                //添加圆到测量模型里
                HOperatorSet.AddMetrologyObjectCircleMeasure(hv_MetrologyHandle, hv_ResultRow,
                                                             hv_ResultColumn, hv_NewRadius, hv_CircleRadiusTolerance, 5, 1.5, 30, (new HTuple("measure_transition")).TupleConcat(
                                                                 "min_score"), (new HTuple("all")).TupleConcat(0.4), out hv_MetrologyCircleIndice);

                //测量并拟合几何形状
                HOperatorSet.ApplyMetrologyModel(ho_ResultImage, hv_MetrologyHandle);

                //获取测量模型里的测量轮廓
                ho_ResultContour.Dispose();
                HOperatorSet.GetMetrologyObjectResultContour(out ho_ResultContour, hv_MetrologyHandle, "all", "all", 1.5);

                //获取测量模型里的测量结果
                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_MetrologyCircleIndice, "all", "result_type", "radius", out hv_CircleRadiusResult);

                //显示测量轮廓
                HOperatorSet.DispObj(ho_ResultContour, WindowID);

                //设置文字显示位置
                HOperatorSet.SetTposition(WindowID, 0, 0);
                //* 在窗口指定位置输出文字信息
                HOperatorSet.WriteString(WindowID, "圆外径:" + hv_CircleRadiusResult);


                //清除测量模型
                HOperatorSet.ClearMetrologyModel(hv_MetrologyHandle);

                ho_ResultRegion.Dispose();
                ho_ResultConnectedRegions.Dispose();
                ho_ResultSelectedRegions.Dispose();
                ho_ResultCircle.Dispose();
                ho_ResultContour.Dispose();

                return(hv_CircleRadiusResult.D);
            }
            else
            {
                ho_ResultRegion.Dispose();
                ho_ResultConnectedRegions.Dispose();
                ho_ResultSelectedRegions.Dispose();
                ho_ResultCircle.Dispose();
                ho_ResultContour.Dispose();

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

        HObject ho_Image, ho_ModelRegion, ho_TemplateImage;
        HObject ho_ModelContours, ho_RectifiedImage = null, ho_Regions = null;
        HObject ho_ConnectedRegions = null, ho_SelectedRegions = null;
        HObject ho_Contours = null, ho_Regions1 = null, ho_RegionLines = null;

        // Local control variables

        HTuple hv_ModelID = new HTuple(), hv_ModelRegionArea = new HTuple();
        HTuple hv_RefRow = new HTuple(), hv_RefColumn = new HTuple();
        HTuple hv_TestImages = new HTuple(), hv_T = new HTuple();
        HTuple hv_Row = new HTuple(), hv_Column = new HTuple();
        HTuple hv_Angle = new HTuple(), hv_Score = new HTuple();
        HTuple hv_I = new HTuple(), hv_RectificationHomMat2D = new HTuple();
        HTuple hv_Row1 = new HTuple(), hv_Column1 = new HTuple();
        HTuple hv_Radius = new HTuple(), hv_MetrologyHandle = new HTuple();
        HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
        HTuple hv_Index = new HTuple(), hv_Row3 = new HTuple();
        HTuple hv_Column3 = new HTuple(), hv_Parameter = new HTuple();
        HTuple hv_Row2 = new HTuple(), hv_Column2 = new HTuple();
        HTuple hv_Radius1 = new HTuple(), hv_MetrologyHandle1 = new HTuple();
        HTuple hv_Index1 = new HTuple(), hv_Parameter1 = new HTuple();
        HTuple hv_SmlC = new HTuple(), hv_SR = new HTuple(), hv_SC = new HTuple();
        HTuple hv_BR = new HTuple(), hv_BC = new HTuple(), hv_Distance = new HTuple();

        // Initialize local and output iconic variables
        HOperatorSet.GenEmptyObj(out ho_Image);
        HOperatorSet.GenEmptyObj(out ho_ModelRegion);
        HOperatorSet.GenEmptyObj(out ho_TemplateImage);
        HOperatorSet.GenEmptyObj(out ho_ModelContours);
        HOperatorSet.GenEmptyObj(out ho_RectifiedImage);
        HOperatorSet.GenEmptyObj(out ho_Regions);
        HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
        HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
        HOperatorSet.GenEmptyObj(out ho_Contours);
        HOperatorSet.GenEmptyObj(out ho_Regions1);
        HOperatorSet.GenEmptyObj(out ho_RegionLines);
        //
        //Matching 01: ************************************************
        //Matching 01: BEGIN of generated code for model initialization
        //Matching 01: ************************************************
        HOperatorSet.SetSystem("border_shape_models", "false");
        //
        //Matching 01: Obtain the model image
        ho_Image.Dispose();
        HOperatorSet.ReadImage(out ho_Image, "C:/Users/Chanru/Desktop/Class/bd/bd1.png");
        //
        //Matching 01: Build the ROI from basic regions
        ho_ModelRegion.Dispose();
        HOperatorSet.GenRectangle1(out ho_ModelRegion, 445.809, 637.592, 801.311, 1070.62);
        //
        //Matching 01: Reduce the model template
        ho_TemplateImage.Dispose();
        HOperatorSet.ReduceDomain(ho_Image, ho_ModelRegion, out ho_TemplateImage);
        //
        //Matching 01: Create the shape model
        hv_ModelID.Dispose();
        HOperatorSet.CreateShapeModel(ho_TemplateImage, 6, (new HTuple(0)).TupleRad()
                                      , (new HTuple(360)).TupleRad(), (new HTuple(0.4233)).TupleRad(), (new HTuple("point_reduction_medium")).TupleConcat(
                                          "no_pregeneration"), "use_polarity", ((new HTuple(10)).TupleConcat(14)).TupleConcat(
                                          35), 3, out hv_ModelID);
        //
        //Matching 01: Get the model contour for transforming it later into the image
        ho_ModelContours.Dispose();
        HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelID, 1);
        //
        //Matching 01: Get the reference position
        hv_ModelRegionArea.Dispose(); hv_RefRow.Dispose(); hv_RefColumn.Dispose();
        HOperatorSet.AreaCenter(ho_ModelRegion, out hv_ModelRegionArea, out hv_RefRow,
                                out hv_RefColumn);
        //
        //Matching 01: END of generated code for model initialization
        //Matching 01:  * * * * * * * * * * * * * * * * * * * * * * *
        //Matching 01: BEGIN of generated code for model application
        //
        //Matching 01: Loop over all specified test images
        hv_TestImages.Dispose();
        hv_TestImages    = new HTuple();
        hv_TestImages[0] = "C:/Users/Chanru/Desktop/Class/bd/bd1.png";
        hv_TestImages[1] = "C:/Users/Chanru/Desktop/Class/bd/bd10.png";
        hv_TestImages[2] = "C:/Users/Chanru/Desktop/Class/bd/bd2.png";
        hv_TestImages[3] = "C:/Users/Chanru/Desktop/Class/bd/bd3.png";
        hv_TestImages[4] = "C:/Users/Chanru/Desktop/Class/bd/bd4.png";
        hv_TestImages[5] = "C:/Users/Chanru/Desktop/Class/bd/bd5.png";
        hv_TestImages[6] = "C:/Users/Chanru/Desktop/Class/bd/bd6.png";
        hv_TestImages[7] = "C:/Users/Chanru/Desktop/Class/bd/bd7.png";
        hv_TestImages[8] = "C:/Users/Chanru/Desktop/Class/bd/bd8.png";
        hv_TestImages[9] = "C:/Users/Chanru/Desktop/Class/bd/bd9.png";
        for (hv_T = 0; (int)hv_T <= 9; hv_T = (int)hv_T + 1)
        {
            //
            //Matching 01: Obtain the test image
            ho_Image.Dispose();
            HOperatorSet.ReadImage(out ho_Image, hv_TestImages.TupleSelect(hv_T));
            //
            //Matching 01: Find the model
            hv_Row.Dispose(); hv_Column.Dispose(); hv_Angle.Dispose(); hv_Score.Dispose();
            HOperatorSet.FindShapeModel(ho_Image, hv_ModelID, (new HTuple(0)).TupleRad()
                                        , (new HTuple(360)).TupleRad(), 0.5, 0, 0.5, "least_squares", (new HTuple(6)).TupleConcat(
                                            1), 0.75, out hv_Row, out hv_Column, out hv_Angle, out hv_Score);
            //
            //Matching 01: Code for rectification of the image
            //Matching 01: Calculate an inverse hom_mat2d for each of the matching results
            for (hv_I = 0; (int)hv_I <= (int)((new HTuple(hv_Score.TupleLength())) - 1); hv_I = (int)hv_I + 1)
            {
                hv_RectificationHomMat2D.Dispose();
                HOperatorSet.HomMat2dIdentity(out hv_RectificationHomMat2D);
                {
                    HTuple ExpTmpOutVar_0;
                    HOperatorSet.HomMat2dTranslate(hv_RectificationHomMat2D, hv_RefRow - (hv_Row.TupleSelect(
                                                                                              hv_I)), hv_RefColumn - (hv_Column.TupleSelect(hv_I)), out ExpTmpOutVar_0);
                    hv_RectificationHomMat2D.Dispose();
                    hv_RectificationHomMat2D = ExpTmpOutVar_0;
                }
                {
                    HTuple ExpTmpOutVar_0;
                    HOperatorSet.HomMat2dRotate(hv_RectificationHomMat2D, -(hv_Angle.TupleSelect(
                                                                                hv_I)), hv_RefRow, hv_RefColumn, out ExpTmpOutVar_0);
                    hv_RectificationHomMat2D.Dispose();
                    hv_RectificationHomMat2D = ExpTmpOutVar_0;
                }
                ho_RectifiedImage.Dispose();
                HOperatorSet.AffineTransImage(ho_Image, out ho_RectifiedImage, hv_RectificationHomMat2D,
                                              "constant", "false");
                //
                //Matching 01: Insert your code using the rectified image here

                ho_Regions.Dispose();
                HOperatorSet.Threshold(ho_Image, out ho_Regions, 99, 255);
                ho_ConnectedRegions.Dispose();
                HOperatorSet.Connection(ho_Regions, out ho_ConnectedRegions);
                ho_SelectedRegions.Dispose();
                HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area",
                                         "and", 150, 800);

                hv_Row1.Dispose(); hv_Column1.Dispose(); hv_Radius.Dispose();
                HOperatorSet.SmallestCircle(ho_SelectedRegions, out hv_Row1, out hv_Column1,
                                            out hv_Radius);
                hv_MetrologyHandle.Dispose();
                HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle);
                hv_Width.Dispose(); hv_Height.Dispose();
                HOperatorSet.GetImageSize(ho_RectifiedImage, out hv_Width, out hv_Height);
                HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandle, hv_Width, hv_Height);
                hv_Index.Dispose();
                HOperatorSet.AddMetrologyObjectCircleMeasure(hv_MetrologyHandle, hv_Row1,
                                                             hv_Column1, hv_Radius, 20, 5, 1, 30, "num_measures", 30, out hv_Index);
                HOperatorSet.ApplyMetrologyModel(ho_RectifiedImage, hv_MetrologyHandle);
                ho_Contours.Dispose(); hv_Row3.Dispose(); hv_Column3.Dispose();
                HOperatorSet.GetMetrologyObjectMeasures(out ho_Contours, hv_MetrologyHandle,
                                                        "all", "all", out hv_Row3, out hv_Column3);
                hv_Parameter.Dispose();
                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type",
                                                      "all_param", out hv_Parameter);
                //*********

                ho_Regions1.Dispose();
                HOperatorSet.Threshold(ho_Image, out ho_Regions1, 22, 78);
                hv_Row2.Dispose(); hv_Column2.Dispose(); hv_Radius1.Dispose();
                HOperatorSet.SmallestCircle(ho_Regions1, out hv_Row2, out hv_Column2, out hv_Radius1);
                hv_MetrologyHandle1.Dispose();
                HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle1);
                HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandle1, hv_Width, hv_Height);
                hv_Index1.Dispose();
                HOperatorSet.AddMetrologyObjectCircleMeasure(hv_MetrologyHandle1, hv_Row2,
                                                             hv_Column2, hv_Radius1, 20, 5, 1, 30, new HTuple(), new HTuple(), out hv_Index1);
                HOperatorSet.ApplyMetrologyModel(ho_RectifiedImage, hv_MetrologyHandle1);
                hv_Parameter1.Dispose();
                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle1, "all", "all",
                                                      "result_type", "all_param", out hv_Parameter1);
                //*******

                hv_SmlC.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_SmlC = HTuple.TupleGenSequence(
                        0, (new HTuple(hv_Parameter.TupleLength())) - 1, 3);
                }
                hv_SR.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_SR = hv_Parameter.TupleSelect(
                        hv_SmlC);
                }
                hv_SC.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_SC = hv_Parameter.TupleSelect(
                        hv_SmlC + 1);
                }

                hv_BR.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_BR = (hv_SR * 0) + (hv_Parameter1.TupleSelect(
                                               0));
                }
                hv_BC.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_BC = (hv_SC * 0) + (hv_Parameter1.TupleSelect(
                                               1));
                }


                hv_Distance.Dispose();
                HOperatorSet.DistancePp(hv_SR, hv_SC, hv_BR, hv_BC, out hv_Distance);

                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.DispObj(ho_RectifiedImage, HDevWindowStack.GetActive());
                }
                ho_RegionLines.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLines, hv_SR, hv_SC, hv_BR, hv_BC);
                // stop(...); only in hdevelop
            }
        }
        //
        //Matching 01: *******************************************
        //Matching 01: END of generated code for model application
        //Matching 01: *******************************************
        //

        ho_Image.Dispose();
        ho_ModelRegion.Dispose();
        ho_TemplateImage.Dispose();
        ho_ModelContours.Dispose();
        ho_RectifiedImage.Dispose();
        ho_Regions.Dispose();
        ho_ConnectedRegions.Dispose();
        ho_SelectedRegions.Dispose();
        ho_Contours.Dispose();
        ho_Regions1.Dispose();
        ho_RegionLines.Dispose();

        hv_ModelID.Dispose();
        hv_ModelRegionArea.Dispose();
        hv_RefRow.Dispose();
        hv_RefColumn.Dispose();
        hv_TestImages.Dispose();
        hv_T.Dispose();
        hv_Row.Dispose();
        hv_Column.Dispose();
        hv_Angle.Dispose();
        hv_Score.Dispose();
        hv_I.Dispose();
        hv_RectificationHomMat2D.Dispose();
        hv_Row1.Dispose();
        hv_Column1.Dispose();
        hv_Radius.Dispose();
        hv_MetrologyHandle.Dispose();
        hv_Width.Dispose();
        hv_Height.Dispose();
        hv_Index.Dispose();
        hv_Row3.Dispose();
        hv_Column3.Dispose();
        hv_Parameter.Dispose();
        hv_Row2.Dispose();
        hv_Column2.Dispose();
        hv_Radius1.Dispose();
        hv_MetrologyHandle1.Dispose();
        hv_Index1.Dispose();
        hv_Parameter1.Dispose();
        hv_SmlC.Dispose();
        hv_SR.Dispose();
        hv_SC.Dispose();
        hv_BR.Dispose();
        hv_BC.Dispose();
        hv_Distance.Dispose();
    }