コード例 #1
0
ファイル: Pick.cs プロジェクト: trigrass2/visionsystem
        public void get_features(HObject ho_Region, out HTuple hv_Features)
        {
            // Local iconic variables
            HObject ho_SingleRegion, ho_Contours;
            // Local control variables
            HTuple hv_Circularity_xld = null, hv_ContLength = null;
            HTuple hv_Circularity = null, hv_Anisometry = null, hv_Bulkiness = null;
            HTuple hv_StructureFactor = null, hv_Distance = null, hv_Sigma = null;
            HTuple hv_Roundness = null, hv_Sides = null, hv_PSI1 = null;
            HTuple hv_PSI2 = null, hv_PSI3 = null, hv_PSI4 = null;

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_SingleRegion);
            HOperatorSet.GenEmptyObj(out ho_Contours);
            ho_SingleRegion.Dispose();
            HOperatorSet.SelectObj(ho_Region, out ho_SingleRegion, 1);
            ho_Contours.Dispose();
            HOperatorSet.GenContourRegionXld(ho_SingleRegion, out ho_Contours, "border");
            HOperatorSet.CircularityXld(ho_Contours, out hv_Circularity_xld);
            HOperatorSet.Contlength(ho_SingleRegion, out hv_ContLength);
            HOperatorSet.Circularity(ho_SingleRegion, out hv_Circularity);
            HOperatorSet.Eccentricity(ho_SingleRegion, out hv_Anisometry, out hv_Bulkiness, out hv_StructureFactor);
            HOperatorSet.Roundness(ho_SingleRegion, out hv_Distance, out hv_Sigma, out hv_Roundness, out hv_Sides);
            HOperatorSet.MomentsRegionCentralInvar(ho_SingleRegion, out hv_PSI1, out hv_PSI2, out hv_PSI3, out hv_PSI4);
            hv_Features = new HTuple();
            hv_Features = hv_Features.TupleConcat(hv_Circularity);
            hv_Features = hv_Features.TupleConcat(hv_Circularity_xld);
            //, PSI1, PSI2, PSI3, PSI4]
            ho_SingleRegion.Dispose();
            ho_Contours.Dispose();
            return;
        }
コード例 #2
0
        public void FitCicrle(HObject ho_Image, HTuple hv_WindowHandle, HTuple hv_CircleRow,
                              HTuple hv_CircleColumn, HTuple hv_CircleRadius, HTuple hv_RectRow, HTuple hv_RectColumn,
                              HTuple hv_RectPhi, HTuple hv_RectLength1, HTuple hv_RectLength2, HTuple hv_Transition,
                              HTuple hv_Sigma, HTuple hv_Threshold, out HTuple hv_CenterRow, out HTuple hv_CenterColumn,
                              out HTuple hv_CenterRaduis, out HTuple hv_StartPhi, out HTuple hv_EndPhi, out HTuple hv_Circularity)
        {
            // Local iconic variables

            HObject ho_Rectangle, ho_Contour;

            // Local control variables

            HTuple hv_Width = null, hv_Height = null, hv_sr = null;
            HTuple hv_sc = null, hv_lenth1 = null, hv_lenth2 = null;
            HTuple hv_val = null, hv_PointNum = null, hv_mr = null;
            HTuple hv_mc = null, hv_ag = null, hv_i = null, hv_lr = null;
            HTuple hv_lc = null, hv_j = null, hv_MeasureHandle = new HTuple();
            HTuple hv_RowEdge = new HTuple(), hv_ColumnEdge = new HTuple();
            HTuple hv_Amplitude = new HTuple(), hv_Distance = new HTuple();
            HTuple hv_RowEdgeCount = new HTuple(), hv_RowEdgeVal = new HTuple();
            HTuple hv_Indices = new HTuple(), hv_RowEdgeValMin = new HTuple();
            HTuple hv_rc = null, hv_zr = null, hv_zc = null, hv_PointOrder = null;
            HTuple hv_CircleColumn_COPY_INP_TMP = hv_CircleColumn.Clone();
            HTuple hv_CircleRow_COPY_INP_TMP    = hv_CircleRow.Clone();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_Contour);
            hv_CenterRow    = new HTuple();
            hv_CenterColumn = new HTuple();
            hv_CenterRaduis = new HTuple();
            hv_StartPhi     = new HTuple();
            hv_EndPhi       = new HTuple();
            hv_Circularity  = new HTuple();
            try
            {
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin");
                }
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green");
                }
                HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
                //
                //tuple_deg (RectPhi, Deg)
                //
                //gen_circle_contour_xld (ContCircle, CircleRow, CircleColumn, CircleRadius, 0, 6.28318/2, 'positive', 1)
                //
                hv_sr = hv_CircleRow_COPY_INP_TMP - hv_CircleRadius;
                hv_sc = hv_CircleColumn_COPY_INP_TMP.Clone();
                //
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red");
                }
                //
                hv_lenth1 = hv_CircleRadius / 2.5;
                hv_lenth2 = hv_CircleRadius / 8;
                //lenth1 := RectLength1
                //lenth2 := RectLength2
                hv_val      = 5;
                hv_PointNum = (6.28318 * hv_CircleRadius) / ((hv_lenth2 + hv_val) * 2);
                //
                hv_mr = hv_sr.Clone();
                hv_mc = hv_sc.Clone();
                //
                ho_Rectangle.Dispose();
                HOperatorSet.GenRectangle2(out ho_Rectangle, hv_mr, hv_mc, (new HTuple(0 + 90)).TupleRad()
                                           , hv_lenth1, hv_lenth2);
                //
                hv_ag = 10;
                //
                HOperatorSet.DispImage(ho_Image, 3600);
                //tuple_rad (ag, rd)
                //
                HTuple end_val30  = 360 / hv_ag;
                HTuple step_val30 = 1;
                for (hv_i = 0; hv_i.Continue(end_val30, step_val30); hv_i = hv_i.TupleAdd(step_val30))
                {
                    hv_mr = hv_sr + (hv_CircleRadius * (1 - ((((hv_ag.TupleRad()) * hv_i)).TupleCos()
                                                             )));
                    hv_mc = hv_sc - (hv_CircleRadius * ((((hv_ag.TupleRad()) * hv_i)).TupleSin()));
                    ho_Rectangle.Dispose();
                    HOperatorSet.GenRectangle2(out ho_Rectangle, hv_mr, hv_mc, (((hv_ag * hv_i) + 90)).TupleRad()
                                               , hv_lenth1, hv_lenth2);
                }
                //
                //stop ()
                //
                hv_lr = new HTuple();
                hv_lc = new HTuple();
                //
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
                }
                hv_j = 0;
                if (HDevWindowStack.IsOpen())
                {
                    //dev_set_draw ('fill')
                }
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 1);
                }
                //
                HTuple end_val46  = 360 / hv_ag;
                HTuple step_val46 = 1;
                for (hv_i = 0; hv_i.Continue(end_val46, step_val46); hv_i = hv_i.TupleAdd(step_val46))
                {
                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green");
                    }
                    //
                    hv_mr = hv_sr + (hv_CircleRadius * (1 - ((((hv_ag.TupleRad()) * hv_i)).TupleCos()
                                                             )));
                    hv_mc = hv_sc - (hv_CircleRadius * ((((hv_ag.TupleRad()) * hv_i)).TupleSin()));
                    //
                    ho_Rectangle.Dispose();
                    HOperatorSet.GenRectangle2(out ho_Rectangle, hv_mr, hv_mc, (((hv_ag * hv_i) + 90)).TupleRad()
                                               , hv_lenth1, hv_lenth2);
                    //
                    HOperatorSet.GenMeasureRectangle2(hv_mr, hv_mc, (((hv_ag * hv_i) + 90)).TupleRad()
                                                      , hv_lenth1, hv_lenth2, hv_Width, hv_Height, "nearest_neighbor", out hv_MeasureHandle);
                    //Transition = 'positive' :dark-to-light;  Transition = 'negative': light-to-dark
                    HOperatorSet.MeasurePos(ho_Image, hv_MeasureHandle, hv_Sigma, hv_Threshold,
                                            hv_Transition, "all", out hv_RowEdge, out hv_ColumnEdge, out hv_Amplitude,
                                            out hv_Distance);
                    //
                    if ((int)(new HTuple((new HTuple(hv_RowEdge.TupleLength())).TupleEqual(0))) != 0)
                    {
                        if (HDevWindowStack.IsOpen())
                        {
                            HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red");
                        }
                        HOperatorSet.DispCross(hv_WindowHandle, hv_mr, hv_mc, 20, (new HTuple(45)).TupleRad()
                                               );
                        //stop ()
                    }
                    else if ((int)(new HTuple((new HTuple(hv_RowEdge.TupleLength())).TupleEqual(
                                                  1))) != 0)
                    {
                        if (HDevWindowStack.IsOpen())
                        {
                            HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green");
                        }
                        HOperatorSet.DispCross(hv_WindowHandle, hv_RowEdge, hv_ColumnEdge, 20,
                                               (new HTuple(45)).TupleRad());
                        if (hv_lr == null)
                        {
                            hv_lr = new HTuple();
                        }
                        hv_lr[hv_j] = hv_RowEdge.TupleSelect(0);
                        if (hv_lc == null)
                        {
                            hv_lc = new HTuple();
                        }
                        hv_lc[hv_j] = hv_ColumnEdge.TupleSelect(0);
                        hv_j        = hv_j + 1;
                    }
                    else if ((int)(new HTuple((new HTuple(hv_RowEdge.TupleLength())).TupleNotEqual(
                                                  1))) != 0)
                    {
                        //By position near DrawLine
                        hv_RowEdgeCount = new HTuple(hv_RowEdge.TupleLength());
                        hv_RowEdgeVal   = hv_RowEdge - hv_mr;
                        HOperatorSet.TupleAbs(hv_RowEdgeVal, out hv_RowEdgeVal);
                        //tuple_sort_index (RowEdgeVal, Indices)
                        HOperatorSet.TupleMin(hv_RowEdgeVal, out hv_RowEdgeValMin);
                        HOperatorSet.TupleFind(hv_RowEdgeVal, hv_RowEdgeValMin, out hv_Indices);
                        if ((int)(new HTuple((new HTuple(hv_Indices.TupleLength())).TupleEqual(
                                                 1))) != 0)
                        {
                            if (hv_lr == null)
                            {
                                hv_lr = new HTuple();
                            }
                            hv_lr[hv_j] = hv_RowEdge.TupleSelect(hv_Indices);
                            if (hv_lc == null)
                            {
                                hv_lc = new HTuple();
                            }
                            hv_lc[hv_j] = hv_ColumnEdge.TupleSelect(hv_Indices);
                        }
                        else
                        {
                            if (hv_lr == null)
                            {
                                hv_lr = new HTuple();
                            }
                            hv_lr[hv_j] = hv_RowEdge.TupleSelect(hv_Indices.TupleSelect(0));
                            if (hv_lc == null)
                            {
                                hv_lc = new HTuple();
                            }
                            hv_lc[hv_j] = hv_ColumnEdge.TupleSelect(hv_Indices.TupleSelect(0));
                        }
                        hv_j = hv_j + 1;
                        if (HDevWindowStack.IsOpen())
                        {
                            HOperatorSet.SetColor(HDevWindowStack.GetActive(), "blue");
                        }
                        HOperatorSet.DispCross(hv_WindowHandle, hv_RowEdge.TupleSelect(hv_Indices),
                                               hv_ColumnEdge.TupleSelect(hv_Indices), 20, (new HTuple(45)).TupleRad()
                                               );
                        //
                        //By Threshold
                    }
                    HOperatorSet.CloseMeasure(hv_MeasureHandle);
                }
                //
                //stop ()
                //
                hv_rc = new HTuple();
                hv_zr = new HTuple();
                hv_zc = new HTuple();
                //
                hv_CircleRow_COPY_INP_TMP    = hv_lr.Clone();
                hv_CircleColumn_COPY_INP_TMP = hv_lc.Clone();
                //
                //
                if ((int)(new HTuple((new HTuple(hv_CircleRow_COPY_INP_TMP.TupleLength())).TupleLess(
                                         4))) != 0)
                {
                    disp_message(hv_WindowHandle, "FitLine Fail", "image", 10, 10, "black", "true");
                    ho_Rectangle.Dispose();
                    ho_Contour.Dispose();

                    return;
                }
                ho_Contour.Dispose();
                HOperatorSet.GenContourPolygonXld(out ho_Contour, hv_CircleRow_COPY_INP_TMP,
                                                  hv_CircleColumn_COPY_INP_TMP);
                //
                HOperatorSet.FitCircleContourXld(ho_Contour, "algebraic", -1, 0, 0, 3, 2, out hv_CenterRow,
                                                 out hv_CenterColumn, out hv_CenterRaduis, out hv_StartPhi, out hv_EndPhi,
                                                 out hv_PointOrder);
                HOperatorSet.CircularityXld(ho_Contour, out hv_Circularity);
                //
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetColor(HDevWindowStack.GetActive(), "blue");
                }
                HOperatorSet.DispCircle(hv_WindowHandle, hv_CenterRow, hv_CenterColumn, hv_CenterRaduis);
                //
                ho_Rectangle.Dispose();
                ho_Contour.Dispose();

                return;
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_Rectangle.Dispose();
                ho_Contour.Dispose();

                throw HDevExpDefaultException;
            }
        }