コード例 #1
0
        // Local procedures
        //public void FitCirCle(HObject ho_image, HTuple hv_startangle, HTuple hv_endangle,
        //    HTuple hv_bw, HTuple hv_num, HTuple hv_Radius, HTuple hv_colcenter, HTuple hv_rowcenter,
        //    HTuple hv_len1, HTuple hv_len2, HTuple hv_sigma, HTuple hv_thresholdval, HTuple hv_transition,
        //    HTuple hv_select, HTuple hv_windowhandle, HTuple hv_showrow, HTuple hv_showcol,
        //    out HTuple hv_RowCircleCenterFit, out HTuple hv_ColumnCircleCenterFit, out HTuple hv_RadiusCircleCenterFit,
        //    out HTuple hv_StartPhiCircleCenterFit, out HTuple hv_EndPhiCircleCenterFit,
        //    out HTuple hv_PointOrderCircleCenterFit)
        //{
        //    // Local iconic variables

        //    HObject ho_Rectangle = null, ho_RectangleSM = null;
        //    HObject ho_ImageReduced = null, ho_Contour = null;

        //    // Local control variables

        //    HTuple hv_t1 = null, hv_rowedgearr = null;
        //    HTuple hv_coledgearr = null, hv_step = null, hv_angle = null;
        //    HTuple hv_x = new HTuple(), hv_y = new HTuple(), hv_anglerad = new HTuple();
        //    HTuple hv_Row1 = new HTuple(), hv_Column1 = new HTuple();
        //    HTuple hv_Row2 = new HTuple(), hv_Column2 = new HTuple();
        //    HTuple hv_width = new HTuple(), hv_high = new HTuple();
        //    HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
        //    HTuple hv_MeasureHandle = new HTuple(), hv_RowEdge = new HTuple();
        //    HTuple hv_ColumnEdge = new HTuple(), hv_Amplitude = new HTuple();
        //    HTuple hv_Distance = new HTuple(), hv_t2 = new HTuple();
        //    HTuple hv_t = new HTuple(), hv_msg = new HTuple();
        //    // Initialize local and output iconic variables
        //    HOperatorSet.GenEmptyObj(out ho_Rectangle);
        //    HOperatorSet.GenEmptyObj(out ho_RectangleSM);
        //    HOperatorSet.GenEmptyObj(out ho_ImageReduced);
        //    HOperatorSet.GenEmptyObj(out ho_Contour);
        //    hv_RowCircleCenterFit = new HTuple();
        //    hv_ColumnCircleCenterFit = new HTuple();
        //    hv_RadiusCircleCenterFit = new HTuple();
        //    hv_StartPhiCircleCenterFit = new HTuple();
        //    hv_EndPhiCircleCenterFit = new HTuple();
        //    hv_PointOrderCircleCenterFit = new HTuple();
        //    try
        //    {
        //        HOperatorSet.CountSeconds(out hv_t1);
        //        hv_rowedgearr = new HTuple();
        //        hv_coledgearr = new HTuple();
        //        hv_step = (((hv_startangle - hv_endangle)).TupleAbs()) / hv_num;
        //        HTuple end_val4 = hv_endangle;
        //        HTuple step_val4 = hv_step;
        //        for (hv_angle = hv_startangle; hv_angle.Continue(end_val4, step_val4); hv_angle = hv_angle.TupleAdd(step_val4))
        //        {
        //            HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "margin");
        //            hv_x = hv_colcenter + (hv_Radius * (((hv_angle.TupleRad())).TupleCos()));
        //            hv_y = hv_rowcenter - (hv_Radius * (((hv_angle.TupleRad())).TupleSin()));
        //            if ((int)(new HTuple(hv_bw.TupleEqual(1))) != 0)
        //            {
        //                hv_anglerad = ((hv_angle + 180)).TupleRad();
        //            }
        //            else
        //            {
        //                hv_anglerad = hv_angle.TupleRad();
        //            }

        //            ho_Rectangle.Dispose();
        //            HOperatorSet.GenRectangle2(out ho_Rectangle, hv_y, hv_x, hv_anglerad, hv_len1,
        //                hv_len2);
        //            HOperatorSet.SmallestRectangle1(ho_Rectangle, out hv_Row1, out hv_Column1,
        //                out hv_Row2, out hv_Column2);
        //            ho_RectangleSM.Dispose();
        //            HOperatorSet.GenRectangle1(out ho_RectangleSM, hv_Row1 - 1, hv_Column1 - 1, hv_Row2 + 1,
        //                hv_Column2 + 1);
        //            hv_width = (((hv_Column2 - hv_Column1)).TupleAbs()) + 2;
        //            hv_high = (((hv_Row2 - hv_Row1)).TupleAbs()) + 2;
        //            ho_ImageReduced.Dispose();
        //            HOperatorSet.ReduceDomain(ho_image, ho_RectangleSM, out ho_ImageReduced);
        //            HOperatorSet.GetImageSize(ho_ImageReduced, out hv_Width, out hv_Height);

        //            HOperatorSet.GenMeasureRectangle2(hv_y, hv_x, hv_anglerad, hv_len1, hv_len2,
        //                hv_Width, hv_Height, "nearest_neighbor", out hv_MeasureHandle);
        //            HOperatorSet.MeasurePos(ho_image, hv_MeasureHandle, hv_sigma, hv_thresholdval,
        //                hv_transition, hv_select, out hv_RowEdge, out hv_ColumnEdge, out hv_Amplitude,
        //                out hv_Distance);
        //            HOperatorSet.CloseMeasure(hv_MeasureHandle);
        //            HOperatorSet.DispRectangle2(hv_ExpDefaultWinHandle, hv_y, hv_x, hv_anglerad,
        //                hv_len1, hv_len2);
        //            if ((int)(new HTuple((new HTuple(hv_RowEdge.TupleLength())).TupleGreater(
        //                0))) != 0)
        //            {
        //                HOperatorSet.DispCross(hv_ExpDefaultWinHandle, hv_RowEdge, hv_ColumnEdge,
        //                    36, 0);
        //                hv_rowedgearr = hv_rowedgearr.TupleConcat(hv_RowEdge);
        //                hv_coledgearr = hv_coledgearr.TupleConcat(hv_ColumnEdge);
        //            }

        //        }
        //        if ((int)(new HTuple((new HTuple(hv_rowedgearr.TupleLength())).TupleGreater(
        //            3))) != 0)
        //        {
        //            ho_Contour.Dispose();
        //            HOperatorSet.GenContourPolygonXld(out ho_Contour, hv_rowedgearr, hv_coledgearr);
        //            HOperatorSet.FitCircleContourXld(ho_Contour, "atukey", -1, 0, 0, 3, 2, out hv_RowCircleCenterFit,
        //                out hv_ColumnCircleCenterFit, out hv_RadiusCircleCenterFit, out hv_StartPhiCircleCenterFit,
        //                out hv_EndPhiCircleCenterFit, out hv_PointOrderCircleCenterFit);
        //            HOperatorSet.DispCircle(hv_ExpDefaultWinHandle, hv_RowCircleCenterFit, hv_ColumnCircleCenterFit,
        //                hv_RadiusCircleCenterFit);

        //            HOperatorSet.CountSeconds(out hv_t2);
        //            hv_t = hv_t2 - hv_t1;
        //            hv_msg = (((hv_t + ",x:") + hv_ColumnCircleCenterFit) + ",y:") + hv_RowCircleCenterFit;
        //            disp_message(hv_ExpDefaultWinHandle, hv_msg, "window", hv_showrow, hv_showcol,
        //                "black", "true");
        //        }
        //        ho_Rectangle.Dispose();
        //        ho_RectangleSM.Dispose();
        //        ho_ImageReduced.Dispose();
        //        ho_Contour.Dispose();

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

        //        throw HDevExpDefaultException;
        //    }
        //}

        public bool FitCirCle(HObject ho_image, HTuple hv_startangle, HTuple hv_endangle,
                              HTuple hv_bw, HTuple hv_num, HTuple hv_Radius, HTuple hv_colcenter, HTuple hv_rowcenter,
                              HTuple hv_len1, HTuple hv_len2, HTuple hv_sigma, HTuple hv_thresholdval, HTuple hv_transition,
                              HTuple hv_select, HTuple hv_windowhandle, HTuple hv_showrow, HTuple hv_showcol,
                              out HTuple hv_RowCircleCenterFit, out HTuple hv_ColumnCircleCenterFit, out HTuple hv_RadiusCircleCenterFit,
                              out HTuple hv_StartPhiCircleCenterFit, out HTuple hv_EndPhiCircleCenterFit,
                              out HTuple hv_PointOrderCircleCenterFit)
        {
            // Local iconic variables
            HObject ho_Rectangle = null, ho_RectangleSM = null;
            HObject ho_ImageReduced = null, ho_Contour = null;

            // Local control variables

            HTuple hv_t1 = null, hv_rowedgearr = null;
            HTuple hv_coledgearr = null, hv_step = null, hv_angle = null;
            HTuple hv_anglerad = new HTuple(), hv_x = new HTuple();
            HTuple hv_y = new HTuple(), hv_Row1 = new HTuple(), hv_Column1 = new HTuple();
            HTuple hv_Row2 = new HTuple(), hv_Column2 = new HTuple();
            HTuple hv_width = new HTuple(), hv_high = new HTuple();
            HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
            HTuple hv_MeasureHandle = new HTuple(), hv_RowEdge = new HTuple();
            HTuple hv_ColumnEdge = new HTuple(), hv_Amplitude = new HTuple();
            HTuple hv_Distance = new HTuple(), hv_t2 = new HTuple();
            HTuple hv_t = new HTuple(), hv_msg = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_RectangleSM);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Contour);
            hv_RowCircleCenterFit        = new HTuple();
            hv_ColumnCircleCenterFit     = new HTuple();
            hv_RadiusCircleCenterFit     = new HTuple();
            hv_StartPhiCircleCenterFit   = new HTuple();
            hv_EndPhiCircleCenterFit     = new HTuple();
            hv_PointOrderCircleCenterFit = new HTuple();
            hv_RowCircleCenterFit        = 0;
            hv_ColumnCircleCenterFit     = 0;
            hv_RadiusCircleCenterFit     = 0;
            try
            {
                if (ho_image == null || !ho_image.IsInitialized())
                {
                    return(false);
                }
                HOperatorSet.CountSeconds(out hv_t1);
                hv_rowedgearr = new HTuple();
                hv_coledgearr = new HTuple();
                hv_step       = (((hv_startangle - hv_endangle)).TupleAbs()) / hv_num;
                HTuple end_val4  = hv_endangle;
                HTuple step_val4 = hv_step;
                for (hv_angle = hv_startangle; hv_angle.Continue(end_val4, step_val4); hv_angle = hv_angle.TupleAdd(step_val4))
                {
                    HOperatorSet.SetDraw(hv_windowhandle, "margin");
                    try
                    {
                        hv_x = hv_colcenter + (hv_Radius * (((hv_angle.TupleRad())).TupleCos()));
                        hv_y = hv_rowcenter - (hv_Radius * (((hv_angle.TupleRad())).TupleSin()));
                        if ((int)(new HTuple(hv_bw.TupleEqual(1))) != 0)
                        {
                            hv_anglerad = ((hv_angle + 180)).TupleRad();
                        }
                        else
                        {
                            hv_anglerad = hv_angle.TupleRad();
                        }
                        ho_Rectangle.Dispose();
                        HOperatorSet.GenRectangle2(out ho_Rectangle, hv_y, hv_x, hv_anglerad, hv_len1,
                                                   hv_len2);
                        HOperatorSet.SmallestRectangle1(ho_Rectangle, out hv_Row1, out hv_Column1,
                                                        out hv_Row2, out hv_Column2);
                        ho_RectangleSM.Dispose();
                        HOperatorSet.GenRectangle1(out ho_RectangleSM, hv_Row1 - 1, hv_Column1 - 1, hv_Row2 + 1,
                                                   hv_Column2 + 1);
                        hv_width = (((hv_Column2 - hv_Column1)).TupleAbs()) + 2;
                        hv_high  = (((hv_Row2 - hv_Row1)).TupleAbs()) + 2;
                        ho_ImageReduced.Dispose();
                        HOperatorSet.ReduceDomain(ho_image, ho_RectangleSM, out ho_ImageReduced);
                        HOperatorSet.GetImageSize(ho_ImageReduced, out hv_Width, out hv_Height);

                        HOperatorSet.GenMeasureRectangle2(hv_y, hv_x, hv_anglerad, hv_len1, hv_len2,
                                                          hv_Width, hv_Height, "nearest_neighbor", out hv_MeasureHandle);
                        HOperatorSet.MeasurePos(ho_ImageReduced, hv_MeasureHandle, hv_sigma, hv_thresholdval,
                                                hv_transition, hv_select, out hv_RowEdge, out hv_ColumnEdge, out hv_Amplitude,
                                                out hv_Distance);
                        HOperatorSet.CloseMeasure(hv_MeasureHandle);
                        //disp_rectangle2 (windowhandle, y, x, anglerad, len1, len2)
                        if ((int)(new HTuple((new HTuple(hv_RowEdge.TupleLength())).TupleGreater(
                                                 0))) != 0)
                        {
                            //disp_cross (windowhandle, RowEdge, ColumnEdge, 36, 0)
                            hv_rowedgearr = hv_rowedgearr.TupleConcat(hv_RowEdge);
                            hv_coledgearr = hv_coledgearr.TupleConcat(hv_ColumnEdge);
                        }
                    }
                    catch
                    {
                        continue;
                    }
                }
                bool brtn = true;
                if ((int)(new HTuple((new HTuple(hv_rowedgearr.TupleLength())).TupleGreater(
                                         3))) != 0)
                {
                    if (hv_rowedgearr.Length * 0.7 > 3)
                    {
                        ho_Contour.Dispose();

                        HOperatorSet.SetColor(hv_windowhandle, "green");
                        HOperatorSet.GenContourPolygonXld(out ho_Contour, hv_rowedgearr, hv_coledgearr);
                        HOperatorSet.FitCircleContourXld(ho_Contour, "atukey", -1, 0, 0, 15, 6, out hv_RowCircleCenterFit,
                                                         out hv_ColumnCircleCenterFit, out hv_RadiusCircleCenterFit, out hv_StartPhiCircleCenterFit,
                                                         out hv_EndPhiCircleCenterFit, out hv_PointOrderCircleCenterFit);
                        HOperatorSet.DispCircle(hv_windowhandle, hv_RowCircleCenterFit, hv_ColumnCircleCenterFit,
                                                hv_RadiusCircleCenterFit);

                        HOperatorSet.CountSeconds(out hv_t2);
                        hv_t   = hv_t2 - hv_t1;
                        hv_msg = (((hv_t + ",x:") + hv_ColumnCircleCenterFit) + ",y:") + hv_RowCircleCenterFit;
                        //disp_message(hv_ExpDefaultWinHandle, hv_msg, "window", hv_showrow, hv_showcol,
                        //    "black", "true");
                        HalconExternFunExport.disp_message(hv_windowhandle, hv_msg, "window", 100, 200, "green", "false");
                        _logger.Warn(m_strStepName + "找圆成功:" + hv_msg.S);
                        brtn = true;
                    }
                    else
                    {
                        brtn = false;
                    }
                }
                else
                {
                    brtn = false;
                }
                ho_Rectangle.Dispose();
                ho_RectangleSM.Dispose();
                ho_ImageReduced.Dispose();
                ho_Contour.Dispose();

                return(brtn);
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_Rectangle.Dispose();
                ho_RectangleSM.Dispose();
                ho_ImageReduced.Dispose();
                ho_Contour.Dispose();
                _logger.Warn(m_strStepName + "找圆失败:" + HDevExpDefaultException.Message);
                // throw HDevExpDefaultException;
                return(false);
            }
        }
コード例 #2
0
        public override bool GenObj(HObject image, string savePath, VisionControl visionControl)
        {
            // HOperatorSet.CreateAnisoShapeModel()
            HTuple hv_Row1 = null, hv_Column1 = null, hv_Row2 = null;
            HTuple hv_Column2 = null, hv_ModelID = null, hv_Row = null;
            HTuple hv_Column = null, hv_Angle = null, hv_ScaleR = null;
            HTuple hv_ScaleC = null, hv_Score = null;
            string strPathRoi = visionShapParam.RoiRegionPath;

            if (!File.Exists(strPathRoi))
            {
                MessageBox.Show("Roi 不存在,请画ROI", "Err", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            if (image == null || !image.IsInitialized())
            {
                MessageBox.Show("图片不存在,请先读取图片", "Err", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            HObject ho_ImageReduced = null, ho_Rectangle = null;

            try
            {
                string strPathMode    = "";
                string strPathModeImg = "";
                if (visionShapParam.ModeType.ToString() == "形状")
                {
                    HOperatorSet.ReadRegion(out ho_Rectangle, strPathRoi);
                    HOperatorSet.ReduceDomain(image, ho_Rectangle, out ho_ImageReduced);
                    HOperatorSet.EdgesSubPix(ho_ImageReduced, out HObject edges, "canny", 2, visionShapParam.ContrastLow, visionShapParam.ContrastHigh);
                    if (visionShapParam.CratePyramid == 0)
                    {
                        HOperatorSet.CreateShapeModelXld(edges, "auto", (new HTuple(visionShapParam.AngleStart)).TupleRad(), (new HTuple(visionShapParam.AngleExtent)).TupleRad(), new HTuple(0.05), "auto",
                                                         new HTuple(visionShapParam.strPolaritySel), 5, out hv_ModelID);
                    }
                    else
                    {
                        HOperatorSet.CreateShapeModelXld(edges, visionShapParam.CratePyramid, (new HTuple(visionShapParam.AngleStart)).TupleRad()
                                                         , (new HTuple(visionShapParam.AngleExtent)).TupleRad(), new HTuple(0.05), "auto", new HTuple(visionShapParam.strPolaritySel), 5, out hv_ModelID);
                    }
                    HOperatorSet.GetShapeModelParams(hv_ModelID, out HTuple numlevels, out HTuple startangle,
                                                     out HTuple endextend, out HTuple anglestep, out HTuple scaleMin, out HTuple scaleMax, out HTuple scalestep, out HTuple metric, out HTuple minContrast);
                    visionShapParam.MatchPyamidHigh = numlevels.I;

                    strPathMode = savePath + "\\" + m_strStepName + "_Mode.shm";
                    visionShapParam.ModeShmPath = strPathMode;
                    HOperatorSet.WriteShapeModel(hv_ModelID, strPathMode);
                }
                else
                {
                    HOperatorSet.CreateNccModel(ho_ImageReduced, "auto", (new HTuple(visionShapParam.AngleStart)).TupleRad(), (new HTuple(visionShapParam.AngleExtent)).TupleRad(), "auto", new HTuple(visionShapParam.strPolaritySel),
                                                out hv_ModelID);
                }
                // strPathModeImg = savePath.Substring(0, index);
                strPathModeImg = savePath + "\\" + m_strStepName + "_ModeImg" + ".bmp";
                // string strPathModeImg = VisionMgr.GetInstance().CurrentVisionProcessDir + "\\" + m_strStepName + "\\" + m_strStepName + "_ModeImg" + ".bmp";
                visionShapParam.ModeImgPath = strPathModeImg;
                HOperatorSet.WriteImage(image, "bmp", 0, strPathModeImg);
                //if (ModeID != null)
                //    if (visionShapParam.ModeType == "形状")
                //    {
                //        HOperatorSet.ClearShapeModel(ModeID);
                //    }
                //    else
                //    {
                //        HOperatorSet.ClearNccModel(ModeID);
                //    }
                ModeID = hv_ModelID;
                Save();
                if (Process_image(image, visionControl) && visionShapParam.ResultCol.Count > 0)
                {
                    visionShapParam.ModlePoint.x = visionShapParam.ResultCol[0];
                    visionShapParam.ModlePoint.y = visionShapParam.ResultRow[0];
                    visionShapParam.ModlePoint.u = visionShapParam.ResultAngle[0];
                    if (visionControl != null && visionControl.isOpen())
                    {
                        HalconExternFunExport.disp_message(visionControl.GetHalconWindow(), "匹配成功", "window", 100, 100, "green", "false");
                    }
                    if (!visionShapParam.bSetOutPoint)
                    {
                        visionShapParam.OutPointInModleImage = new Point2d(visionShapParam.ModlePoint.x, visionShapParam.ModlePoint.y);
                    }
                }
                else
                {
                    MessageBox.Show(m_strStepName + ":创建模板失败-", "Err", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(false);
                }
            }
            catch (HalconException e)
            {
                MessageBox.Show(m_strStepName + ":创建模板失败-" + e.Message, "Err", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            finally
            {
                if (GenFinished != null)
                {
                    GenFinished();
                }
                ho_Rectangle?.Dispose();
                ho_ImageReduced?.Dispose();
                GC.Collect();
            }

            return(true);
        }
コード例 #3
0
ファイル: 1dCode.cs プロジェクト: yuanshichaocn/HeadSetTest
        public override bool Process_image(HObject ho_Image, VisionControl visionControl)
        {
            if (Mode1dCode == null)
            {
                MessageBox.Show(m_strStepName + "1CodeModle不存在", "Err", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            if (visionControl == null)
            {
                MessageBox.Show(m_strStepName + "显示窗体不存在", "Err", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            if (ho_Image == null || !ho_Image.IsInitialized())
            {
                MessageBox.Show(m_strStepName + "图片不存在", "Err", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            HTuple  hv_DataCodeHandleHigh = null;
            HTuple  hv_ResultHandles      = new HTuple();
            HTuple  hv_DecodedDataStrings = new HTuple();
            HObject ho_SymbolRegions      = null;
            HObject ReduceImg             = null;

            try
            {
                if (visionControl != null && visionControl.isOpen())
                {
                    HOperatorSet.SetDraw(visionControl.GetHalconWindow(), "margin");
                }
                if (Mode1dCodeSearch != null && Mode1dCodeSearch.IsInitialized())
                {
                    HOperatorSet.ReduceDomain(ho_Image, Mode1dCodeSearch, out ReduceImg);
                }
                //HOperatorSet.SetBarCodeParam(Mode1dCode, "meas_thresh_abs", 0.0);
                // HOperatorSet.SetBarCodeParam(Mode1dCode, "element_size_min", 1.0);
                // HOperatorSet.SetBarCodeParam(Mode1dCode, "meas_param_estimation", "true");
                if (ReduceImg != null && ReduceImg.IsInitialized())
                {
                    HOperatorSet.FindBarCode(ReduceImg, out ho_SymbolRegions, Mode1dCode,
                                             "auto", out hv_DecodedDataStrings);
                }
                else
                {
                    HOperatorSet.FindBarCode(ho_Image, out ho_SymbolRegions, Mode1dCode,
                                             "auto", out hv_DecodedDataStrings);
                }
                if (hv_DecodedDataStrings != null && hv_DecodedDataStrings.Length > 0)
                {
                    if (visionControl != null && visionControl.isOpen())
                    {
                        HOperatorSet.SetColor(visionControl.GetHalconWindow(), "green");
                        HOperatorSet.DispObj(ho_SymbolRegions, visionControl.GetHalconWindow());
                        HalconExternFunExport.disp_message(visionControl.GetHalconWindow(), hv_DecodedDataStrings, "window", 100, 200, "green", "false");
                        vision1dCodeParam.RusultCode = hv_DecodedDataStrings.S;
                        if (Mode1dCodeSearch != null && Mode1dCodeSearch.IsInitialized())
                        {
                            HOperatorSet.DispObj(Mode1dCodeSearch, visionControl.GetHalconWindow());
                        }
                    }
                }
                else
                {
                    if (visionControl != null && visionControl.isOpen())
                    {
                        HOperatorSet.SetColor(visionControl.GetHalconWindow(), "red");
                        if (Mode1dCodeSearch != null && Mode1dCodeSearch.IsInitialized())
                        {
                            HOperatorSet.DispObj(Mode1dCodeSearch, visionControl.GetHalconWindow());
                        }
                        HalconExternFunExport.disp_message(visionControl.GetHalconWindow(), "读码失败", "window", 100, 200, "red", "false");
                    }
                }
            }
            catch (HalconException e)
            {
                // MessageBox.Show(m_strStepName + "图片不存在", "Err", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            finally
            {
                ReduceImg?.Dispose();
                ho_SymbolRegions?.Dispose();
                // GC.SuppressFinalize(ReduceImg);
            }

            return(true);
        }
コード例 #4
0
        public override bool Process_image(HObject ho_Image, VisionControl visionControl)
        {
            if (ModeID == null)
            {
                MessageBox.Show(m_strStepName + ":模板不存在,请登录模板", "Err", MessageBoxButtons.OK, MessageBoxIcon.Error);
                _logger.Warn(m_strStepName + ":模板不存在,请登录模板");
                return(false);
            }
            if (ho_Image == null || !ho_Image.IsInitialized())
            {
                _logger.Warn(m_strStepName + ":匹配图片不存在,请读取或采集图片");
                MessageBox.Show(m_strStepName + ":匹配图片不存在,请读取或采集图片", "Err", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            HObject ReduceImg = null;

            try
            {
                visionShapParam.ResultRow.Clear();
                visionShapParam.ResultCol.Clear();
                visionShapParam.ResultAngle.Clear();
                visionShapParam.OutPointInResultImg.Clear();
                HTuple hv_Row1 = null, hv_Column1 = null, hv_Row2 = null;
                HTuple hv_Column2 = null, hv_Row = null;
                HTuple hv_Column = null, hv_Angle = null, hv_ScaleR = null;
                HTuple hv_ScaleC = null, hv_Score = null;

                // HOperatorSet.SetSystem("border_shape_models", "true");
                if (RegionSearch != null && RegionSearch.IsInitialized())
                {
                    HOperatorSet.ReduceDomain(ho_Image, RegionSearch, out ReduceImg);
                }

                if (visionShapParam.ModeType.ToString() == "形状")
                {
                    if (ReduceImg != null && ReduceImg.IsInitialized())
                    {
                        HOperatorSet.FindScaledShapeModel(ReduceImg, ModeID,
                                                          (new HTuple(visionShapParam.AngleStart)).TupleRad(), (new HTuple(visionShapParam.AngleExtent)).TupleRad(),
                                                          visionShapParam.MinRowScale, visionShapParam.MaxRowScale, visionShapParam.dSorce, visionShapParam.nNum, visionShapParam.MaxOverlap, "least_squares", (new HTuple(visionShapParam.MatchPyamidLow)).TupleConcat(new HTuple(visionShapParam.MatchPyamidHigh)), 0.7,
                                                          out hv_Row, out hv_Column, out hv_Angle, out hv_ScaleR, out hv_Score);
                    }
                    else
                    {
                        HOperatorSet.FindScaledShapeModel(ho_Image, ModeID,
                                                          (new HTuple(visionShapParam.AngleStart)).TupleRad(), (new HTuple(visionShapParam.AngleExtent)).TupleRad(),
                                                          visionShapParam.MinRowScale, visionShapParam.MaxRowScale, visionShapParam.dSorce, visionShapParam.nNum, visionShapParam.MaxOverlap, "least_squares", (new HTuple(visionShapParam.MatchPyamidLow)).TupleConcat(new HTuple(visionShapParam.MatchPyamidHigh)), 0.9,
                                                          out hv_Row, out hv_Column, out hv_Angle, out hv_ScaleR, out hv_Score);
                    }
                }
                else
                {
                    if (ReduceImg != null && ReduceImg.IsInitialized())
                    {
                        HOperatorSet.FindNccModel(ReduceImg, ModeID, (new HTuple(visionShapParam.AngleStart)).TupleRad(), (new HTuple(visionShapParam.AngleExtent)).TupleRad(),
                                                  visionShapParam.dSorce, visionShapParam.nNum, visionShapParam.MaxOverlap, "true", 0, out hv_Row, out hv_Column, out hv_Angle, out hv_Score);
                    }
                    else
                    {
                        HOperatorSet.FindNccModel(ho_Image, ModeID, (new HTuple(visionShapParam.AngleStart)).TupleRad(), (new HTuple(visionShapParam.AngleExtent)).TupleRad(),
                                                  visionShapParam.dSorce, visionShapParam.nNum, visionShapParam.MaxOverlap, "true", 0, out hv_Row, out hv_Column, out hv_Angle, out hv_Score);
                    }
                }

                if (hv_Row.Length > 0)
                {
                    for (int i = 0; i < hv_Row.Length; i++)
                    {
                        visionShapParam.ResultRow.Add(hv_Row[i].D);
                        visionShapParam.ResultCol.Add(hv_Column[i].D);
                        visionShapParam.ResultAngle.Add(hv_Angle[i].D);
                        HOperatorSet.VectorAngleToRigid(visionShapParam.ModlePoint.y, visionShapParam.ModlePoint.x, visionShapParam.ModlePoint.u,
                                                        hv_Row[i].D, hv_Column[i].D, hv_Angle[i].D, out HTuple hom2d);
                        if (visionShapParam.bSetOutPoint)
                        {
                            HOperatorSet.VectorAngleToRigid(visionShapParam.ModlePoint.y, visionShapParam.ModlePoint.x, visionShapParam.ModlePoint.u,
                                                            hv_Row[i].D, hv_Column[i].D, hv_Angle[i].D, out hom2d);

                            HOperatorSet.AffineTransPixel(hom2d, visionShapParam.OutPointInModleImage.y, visionShapParam.OutPointInModleImage.x, out HTuple rowTrans, out HTuple colTrans);
                            if (visionControl != null && visionControl.isOpen())
                            {
                                HOperatorSet.SetColor(visionControl.GetHalconWindow(), "blue");
                                HOperatorSet.DispCross(visionControl.GetHalconWindow(), rowTrans, colTrans, 80, 0);
                            }

                            visionShapParam.OutPointInResultImg.Add(new Point2d(colTrans.D, rowTrans.D));
                        }
                        else
                        {
                            if (visionControl != null && visionControl.isOpen())
                            {
                                HOperatorSet.DispCross(visionControl.GetHalconWindow(), hv_Row[i].D, hv_Column[i].D, 20, 0);
                            }
                        }
                        int indexsel = 0;
                        shapeparamResults?.Clear();
                        HTuple     rowTransUser = 0; HTuple colTransUser = 0;
                        shapeparam shapeparamInstance;
                        foreach (var temp in shapeslist)
                        {
                            shapeparamInstance = temp.Clone();

                            switch (temp.shapeType)
                            {
                            case ShapeType.点:

                                HOperatorSet.AffineTransPixel(hom2d,
                                                              ((UsrShapePoint)temp.usrshape).Y,
                                                              ((UsrShapePoint)temp.usrshape).X,
                                                              out rowTransUser, out colTransUser);
                                ((UsrShapePoint)shapeparamInstance.usrshape).X = colTransUser[0].D;
                                ((UsrShapePoint)shapeparamInstance.usrshape).Y = rowTransUser[0].D;

                                shapeparamResults.Add(shapeparamInstance);
                                break;

                            case ShapeType.圆形:

                                ((UsrShapeCircle)shapeparamInstance.usrshape).CircleRadius = ((UsrShapeCircle)temp.usrshape).CircleRadius;
                                HOperatorSet.AffineTransPixel(hom2d,
                                                              ((UsrShapeCircle)temp.usrshape).CircleCenterY,
                                                              ((UsrShapeCircle)temp.usrshape).CircleCenterX,
                                                              out rowTransUser, out colTransUser);
                                ((UsrShapeCircle)shapeparamInstance.usrshape).CircleCenterX = colTransUser[0].D;
                                ((UsrShapeCircle)shapeparamInstance.usrshape).CircleCenterY = rowTransUser[0].D;
                                ((UsrShapeCircle)shapeparamInstance.usrshape).CircleRadius  = ((UsrShapeCircle)temp.usrshape).CircleRadius;
                                shapeparamResults.Add(shapeparamInstance);
                                try
                                {
                                    HOperatorSet.DispCircle(visionControl.GetHalconWindow(), ((UsrShapeCircle)shapeparamInstance.usrshape).CircleCenterY,
                                                            ((UsrShapeCircle)shapeparamInstance.usrshape).CircleCenterX, ((UsrShapeCircle)shapeparamInstance.usrshape).CircleRadius);

                                    //  HOperatorSet.DispObj(rectobj, visionControl.GetHalconWindow());
                                }
                                catch (Exception e)
                                {
                                }
                                break;

                            case ShapeType.仿射矩形:

                                ((UsrShapeRect2)shapeparamInstance.usrshape).Len1 = ((UsrShapeRect2)temp.usrshape).Len1;
                                ((UsrShapeRect2)shapeparamInstance.usrshape).Len2 = ((UsrShapeRect2)temp.usrshape).Len2;
                                ((UsrShapeRect2)shapeparamInstance.usrshape).Phi  = ((UsrShapeRect2)temp.usrshape).Phi + hv_Angle[i].D;
                                HOperatorSet.AffineTransPixel(hom2d,
                                                              ((UsrShapeRect2)temp.usrshape).CenterY,
                                                              ((UsrShapeRect2)temp.usrshape).CenterX,
                                                              out rowTransUser, out colTransUser);
                                ((UsrShapeRect2)shapeparamInstance.usrshape).CenterX = colTransUser[0].D;
                                ((UsrShapeRect2)shapeparamInstance.usrshape).CenterY = rowTransUser[0].D;
                                shapeparamResults.Add(shapeparamInstance);
                                try
                                {
                                    HOperatorSet.DispRectangle2(visionControl.GetHalconWindow(), ((UsrShapeRect2)shapeparamInstance.usrshape).CenterY,
                                                                ((UsrShapeRect2)shapeparamInstance.usrshape).CenterX, ((UsrShapeRect2)shapeparamInstance.usrshape).Phi,
                                                                ((UsrShapeRect2)shapeparamInstance.usrshape).Len1,
                                                                ((UsrShapeRect2)shapeparamInstance.usrshape).Len2
                                                                );
                                    //  HOperatorSet.DispObj(rectobj, visionControl.GetHalconWindow());
                                }
                                catch (Exception e)
                                {
                                }
                                break;

                            case ShapeType.矩形:

                                HOperatorSet.AffineTransPixel(hom2d,
                                                              ((UsrShapeRect)temp.usrshape).Y1,
                                                              ((UsrShapeRect)temp.usrshape).X1,
                                                              out rowTransUser, out colTransUser);
                                ((UsrShapeRect)shapeparamInstance.usrshape).X1 = colTransUser[0].D;
                                ((UsrShapeRect)shapeparamInstance.usrshape).Y1 = rowTransUser[0].D;
                                HOperatorSet.AffineTransPixel(hom2d,
                                                              ((UsrShapeRect)temp.usrshape).Y2,
                                                              ((UsrShapeRect)temp.usrshape).X2,
                                                              out rowTransUser, out colTransUser);
                                ((UsrShapeRect)shapeparamInstance.usrshape).X2 = colTransUser[0].D;
                                ((UsrShapeRect)shapeparamInstance.usrshape).Y2 = rowTransUser[0].D;
                                shapeparamResults.Add(shapeparamInstance);
                                try
                                {
                                    //   HOperatorSet.GenRectangle1(out HObject rectobj, ((UsrShapeRect)shapeparamInstance.usrshape).Y1,
                                    // ((UsrShapeRect)shapeparamInstance.usrshape).X1,

                                    //((UsrShapeRect)shapeparamInstance.usrshape).Y2,
                                    //       ((UsrShapeRect)shapeparamInstance.usrshape).X2);
                                    //   HOperatorSet.DispObj(rectobj, visionControl.GetHalconWindow());
                                    if (((UsrShapeRect)shapeparamInstance.usrshape).Y2 > ((UsrShapeRect)shapeparamInstance.usrshape).Y1)
                                    {
                                        HOperatorSet.DispRectangle1(visionControl.GetHalconWindow(),
                                                                    ((UsrShapeRect)shapeparamInstance.usrshape).Y1,
                                                                    ((UsrShapeRect)shapeparamInstance.usrshape).X1, ((UsrShapeRect)shapeparamInstance.usrshape).Y2,
                                                                    ((UsrShapeRect)shapeparamInstance.usrshape).X2
                                                                    );
                                        // HOperatorSet.DispObj(rectobj, visionControl.GetHalconWindow());
                                    }
                                    else
                                    {
                                        HOperatorSet.DispRectangle1(visionControl.GetHalconWindow(), ((UsrShapeRect)shapeparamInstance.usrshape).Y2,
                                                                    ((UsrShapeRect)shapeparamInstance.usrshape).X2,
                                                                    ((UsrShapeRect)shapeparamInstance.usrshape).Y1,
                                                                    ((UsrShapeRect)shapeparamInstance.usrshape).X1);
                                        //  HOperatorSet.DispObj(rectobj, visionControl.GetHalconWindow());
                                    }
                                    //   HOperatorSet.DispRectangle1(visionControl.GetHalconWindow(),
                                    //((UsrShapeRect)shapeparamInstance.usrshape).Y1,
                                    // ((UsrShapeRect)shapeparamInstance.usrshape).X1,

                                    //((UsrShapeRect)shapeparamInstance.usrshape).Y2,
                                    //       ((UsrShapeRect)shapeparamInstance.usrshape).X2
                                    //);
                                }
                                catch (Exception e)
                                {
                                }

                                break;
                            }
                        }
                    }
                    Save();
                    if (visionControl != null && visionControl.isOpen())
                    {
                        HOperatorSet.SetColor(visionControl.GetHalconWindow(), "green");
                    }
                    if (RegionSearch != null && RegionSearch.IsInitialized() && visionControl != null && visionControl.isOpen())
                    {
                        HOperatorSet.DispObj(RegionSearch, visionControl.GetHalconWindow());
                    }
                    if (visionControl != null && visionControl.isOpen())
                    {
                        HalconExternFunExport.dev_display_shape_matching_results(ModeID, visionControl.GetHalconWindow(), "green", hv_Row, hv_Column, hv_Angle, 1, 1, 0);
                        for (int i = 0; i < hv_Row.Length; i++)
                        {
                            HTuple hTuple = string.Format("x:{0},y:{1},u:{2},score{3}", hv_Column[i].D, hv_Row[i].D, hv_Angle[i].D / Math.PI * 180, hv_Score[i].D);
                            string strmsg = string.Format("x:{0},y:{1},u:{2},score{3},Numlevels", hv_Column[i].D.ToString("F2"), hv_Row[i].D.ToString("F2"), (hv_Angle[i].D / Math.PI * 180).ToString("F2"), hv_Score[i].D.ToString("F2"), visionShapParam.MatchPyamidHigh);
                            HalconExternFunExport.disp_message(visionControl.GetHalconWindow(), strmsg, "window", 0 + i * 20, 100, "green", "false");
                        }
                    }
                }
                else
                {
                    if (visionControl != null && visionControl.isOpen())
                    {
                        HOperatorSet.SetColor(visionControl.GetHalconWindow(), "red");
                    }
                    if (RegionSearch != null && RegionSearch.IsInitialized() && visionControl != null && visionControl.isOpen())
                    {
                        HOperatorSet.DispObj(RegionSearch, visionControl.GetHalconWindow());
                    }
                    if (visionControl != null && visionControl.isOpen())
                    {
                        HalconExternFunExport.disp_message(visionControl.GetHalconWindow(), "没有找到", "window", 100, 100, "red", "false");
                    }

                    return(false);
                }
            }
            catch (HalconException e)
            {
                _logger.Warn(m_strStepName + ": 寻找模板失败:" + e.Message);
                MessageBox.Show(m_strStepName + ": 寻找模板失败:" + e.Message, "Err", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            finally
            {
                ReduceImg?.Dispose();
                GC.Collect();
            }

            return(true);
        }