예제 #1
0
 public virtual void RunRef()
 {
     RunStart();
     ImageTestIn = ImageRefIn;
     if ((ImageTestIn == null || ImageTestIn.IsInitialized() == false) && (this is CreateImage.CreateImageTool == false))
     {
         return;
     }
     RunAct();
     RunEnd();
 }
        //主执行程序
        protected override void RunAct()
        {
            base.RunAct();
            if (!engineIsnitial)
            {
                MyEngine = new HDevEngine();
                InitialEngine();
            }
            #region 初始化
            RuningFinish  = false;
            base.Result   = string.Empty;
            base.IsOk     = false;
            base.isRealOk = false;
            //功能块
            bFindShapeMode         = false;
            bGrabPointSetting      = false;
            bPZT_Detection_Result  = false;
            bGrab_Intervene_Result = false;
            bLM_DP_Result          = false;
            bFangDai_Result        = false;
            bLanQuan_Result        = false;
            #endregion

            if (ImageTestIn == null || !ImageTestIn.IsInitialized())
            {
                return;
            }
            HImage R, G, B;
            R = ImageTestIn.Decompose3(out G, out B);

            #region 创建模板
            if (MCreateShapeModel.hShapeModel == null || !MCreateShapeModel.hShapeModel.IsInitialized())
            {
                if (!MCreateShapeModel.CreateShapeModelAct(ImageRefIn))
                {
                    Util.Notify("创建模板异常");
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo("创建模板异常");
                    }
                    return;
                }
            }
            #endregion

            HTuple t1;
            HOperatorSet.CountSeconds(out t1);

            #region 模板匹配

            bFindShapeMode = MFindShapeMode.FindShapeModeAct(ImageRefIn, MCreateShapeModel, ImageTestIn);
            HTuple t2;
            HOperatorSet.CountSeconds(out t2);
            double time = (t2 - t1).D * 1000;
            Util.Notify("模板匹配用时:" + time.ToString("F2") + "ms");
            if (NotifyExcInfo != null)
            {
                NotifyExcInfo("模板匹配用时:" + time.ToString("F2") + "ms");
            }

            if (!bFindShapeMode)
            {
                Util.Notify("查找模板异常");
                if (NotifyExcInfo != null)
                {
                    NotifyExcInfo("查找模板异常");
                }
                return;
            }
            List <HHomMat2D> homMat2Ds = new List <HHomMat2D>();
            homMat2Ds = MFindShapeMode.GetHHomMat2Ds();

            if (MFindShapeMode.row == null || MFindShapeMode.row.Length < 1 || homMat2Ds == null || homMat2Ds.Count < 1)
            {
                if (NotifyExcInfo != null)
                {
                    NotifyExcInfo("未找到模板");
                }
                base.IsOk     = true;
                base.isRealOk = true;
                return;
            }
            if (bFindShapeMode)
            {
                StringBuilder strb = new StringBuilder();
                strb.Append("查找结果:");
                strb.Append(Environment.NewLine);
                int count = MFindShapeMode.row_temp.Length;
                for (int i = 0; i < count; i++)
                {
                    HTuple phi;
                    HOperatorSet.TupleDeg(MFindShapeMode.angle_temp[i], out phi);
                    strb.Append("第" + (i + 1) + "个:\r\n");
                    string mes = string.Format(
                        "Row:{0:F2}\r\n" + "Col:{1:F2}\r\n" + "角度:{2:F2}\r\n" + "得分:{3:F2}"
                        , MFindShapeMode.row_temp[i].D, MFindShapeMode.column_temp[i].D, phi.D, MFindShapeMode.score_temp[i].D);
                    strb.Append(mes);
                    strb.Append(Environment.NewLine);
                }

                int blob_feature_lengt = MFindShapeMode.LG_Area.Length;
                for (int i = 0; i < blob_feature_lengt; i++)
                {
                    strb.Append("第" + (i + 1) + "个:\r\n");
                    string mes = string.Format(
                        "螺杆面积:{0:F2}\r\n" + "螺杆长:{1:F2}\r\n" + "垫片宽:{2:F2}\r\n"
                        , MFindShapeMode.LG_Area[i].D, MFindShapeMode.LG_lenght[i].D, MFindShapeMode.DP_width[i].D);
                    strb.Append(mes);
                    strb.Append(Environment.NewLine);
                }

                if (NotifyExcInfo != null)
                {
                    NotifyExcInfo(strb.ToString());
                }
                Util.Notify(strb.ToString());
            }



            #endregion



            #region 抓取点
            bGrabPointSetting = mGrabPointSetting.setTarget(homMat2Ds);
            if (!bGrabPointSetting)
            {
                Util.Notify("抓取点异常");
                if (NotifyExcInfo != null)
                {
                    NotifyExcInfo("抓取点异常");
                }
            }
            else
            {
                StringBuilder strd = new StringBuilder();
                for (int i = 0; i < MGrabPointSetting.GrabRowTarget.Length; i++)
                {
                    string mes = string.Format("抓取点像素坐标:" + "第" + (i + 1) + "个像素坐标:\r\n" +
                                               "Row: {0:F2}\r\n" +
                                               "Col: {1:F2}"
                                               , MGrabPointSetting.GrabRowTarget[i].D, MGrabPointSetting.GrabColTarget[i].D);
                    strd.Append(mes);
                    strd.Append(Environment.NewLine);
                }
                Util.Notify(strd.ToString());
                if (NotifyExcInfo != null)
                {
                    NotifyExcInfo(strd.ToString());
                }
            }
            #endregion

            HTuple t3;
            HOperatorSet.CountSeconds(out t3);
            time = (t3 - t2).D * 1000;
            Util.Notify("抓取点用时:" + time.ToString("F2") + "ms");
            if (NotifyExcInfo != null)
            {
                NotifyExcInfo("抓取点用时:" + time.ToString("F2") + "ms");
            }

            #region 膨胀套检测
            if (bPZT_Detection_Enble)
            {
                bPZT_Detection_Result = MPZT_Detection.PZT_Detection_Act(R, homMat2Ds, MFindShapeMode.angle);
                if (bPZT_Detection_Result)
                {
                    StringBuilder strb = new StringBuilder();
                    strb.Append("膨胀套:");
                    strb.Append(Environment.NewLine);
                    int count = MPZT_Detection.dist_PZT.Length;
                    for (int i = 0; i < count; i++)
                    {
                        string mes = string.Format("第" + (i + 1) + "个:\r\n" +
                                                   "长度: {0:F2}"
                                                   , MPZT_Detection.dist_PZT[i].D);
                        strb.Append(mes);
                        strb.Append(Environment.NewLine);
                    }
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo(strb.ToString());
                    }
                    Util.Notify(strb.ToString());
                }
                else
                {
                    Util.Notify("膨胀套检测异常");
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo("膨胀套检测异常");
                    }
                }
            }
            else
            {
                bPZT_Detection_Result = true;
            }

            #endregion

            HTuple t4;
            HOperatorSet.CountSeconds(out t4);
            time = (t4 - t3).D * 1000;
            Util.Notify("膨胀套用时:" + time.ToString("F2"));
            if (NotifyExcInfo != null)
            {
                NotifyExcInfo("膨胀套用时:" + time.ToString("F2") + "ms");
            }

            #region 抓取干涉
            if (bGrab_Intervene_Enble)
            {
                bGrab_Intervene_Result = MGrab_Intervene.Grab_Intervene_Act(G, homMat2Ds);
                if (bGrab_Intervene_Result)
                {
                    StringBuilder strb = new StringBuilder();
                    strb.Append("抓取干涉:");
                    strb.Append(Environment.NewLine);
                    int count = MGrab_Intervene.grabInterveneOkNg.Length;
                    for (int i = 0; i < count; i++)
                    {
                        string mes = string.Format("第" + (i + 1) + "个:\r\n" +
                                                   "是否干涉: {0}"
                                                   , MGrab_Intervene.grabInterveneOkNg[i].I > 0 ? "不干涉" : "干涉");
                        strb.Append(mes);
                        strb.Append(Environment.NewLine);
                    }
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo(strb.ToString());
                    }
                    Util.Notify(strb.ToString());
                }
                else
                {
                    Util.Notify("抓取干涉检测异常");
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo("抓取干涉检测异常");
                    }
                }
            }
            else
            {
                bGrab_Intervene_Result = true;
            }

            #endregion

            HTuple t5;
            HOperatorSet.CountSeconds(out t5);
            time = (t5 - t4).D * 1000;
            Util.Notify("抓取干涉用时:" + time.ToString("F2") + "ms");
            if (NotifyExcInfo != null)
            {
                NotifyExcInfo("抓取干涉用时:" + time.ToString("F2") + "ms");
            }


            #region 螺母垫片有无检测
            if (bLM_DP_Enble)
            {
                bLM_DP_Result = MLM_DP.LM_DP_Act(G, homMat2Ds);
                if (bLM_DP_Result)
                {
                    StringBuilder strb = new StringBuilder();
                    strb.Append("螺母垫片:");
                    strb.Append(Environment.NewLine);
                    int count = MLM_DP.lM_DP_OkNg.Length;
                    for (int i = 0; i < count; i++)
                    {
                        string mes = string.Format("第" + (i + 1) + "个:\r\n" +
                                                   "螺母垫片有无: {0}", MLM_DP.lM_DP_OkNg[i].I > 0 ? "有" : "无");
                        strb.Append(mes);
                        strb.Append(Environment.NewLine);
                        string temp = string.Format("第" + (i + 1) + "个:\r\n" +
                                                    "区块数量{0}; 最大区块面积{1}", MLM_DP.lM_DP_Num[i].I.ToString(), MLM_DP.lM_DP_Area[i].I.ToString());
                        strb.Append(temp);
                        strb.Append(Environment.NewLine);
                    }
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo(strb.ToString());
                    }
                    Util.Notify(strb.ToString());
                }
                else
                {
                    Util.Notify("螺母垫片有无: 检测异常");
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo("螺母垫片有无: 检测异常");
                    }
                }
            }
            else
            {
                bLM_DP_Result = true;
            }
            #endregion

            HTuple t6;
            HOperatorSet.CountSeconds(out t6);
            time = (t6 - t5).D * 1000;
            Util.Notify("螺母垫片用时:" + time.ToString("F2") + "ms");
            if (NotifyExcInfo != null)
            {
                NotifyExcInfo("螺母垫片用时:" + time.ToString("F2") + "ms");
            }

            #region 防呆
            if (bFangDai_Enable)
            {
                bFangDai_Result = MFangDai.FangDai_Act(G, homMat2Ds);
                if (bFangDai_Result)
                {
                    StringBuilder strb = new StringBuilder();
                    strb.Append("防呆:");
                    strb.Append(Environment.NewLine);
                    int count = MFangDai.Area.Length;
                    for (int i = 0; i < count; i++)
                    {
                        string mes = string.Format("第" + (i + 1) + "个:\r\n" +
                                                   "面积: {0:F2}"

                                                   , MFangDai.Area[i].D);
                        strb.Append(mes);
                        strb.Append(Environment.NewLine);
                    }
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo(strb.ToString());
                    }
                    Util.Notify(strb.ToString());
                }
                else
                {
                    Util.Notify("防呆检测异常");
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo("防呆检测异常");
                    }
                }
            }
            else
            {
                bFangDai_Result = true;
            }
            #endregion
            HTuple t7;
            HOperatorSet.CountSeconds(out t7);
            time = (t7 - t6).D * 1000;
            Util.Notify("防呆用时:" + time.ToString("F2") + "ms");
            if (NotifyExcInfo != null)
            {
                NotifyExcInfo("防呆用时:" + time.ToString("F2") + "ms");
            }

            #region 蓝圈
            if (bLanQuan_Enable)
            {
                bLanQuan_Result = MLanQuan.LanQuan_Act(R, B, homMat2Ds);
                if (bLanQuan_Result)
                {
                    StringBuilder strb = new StringBuilder();
                    strb.Append("蓝圈:");
                    strb.Append(Environment.NewLine);
                    int count = MLanQuan.Area.Length;
                    for (int i = 0; i < count; i++)
                    {
                        string mes = string.Format("第" + (i + 1) + "个:\r\n" +
                                                   "面积: {0:F2}\r\n"
                                                   , MLanQuan.Area[i].D);
                        strb.Append(mes);
                        strb.Append(Environment.NewLine);
                    }
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo(strb.ToString());
                    }
                    Util.Notify(strb.ToString());
                }
                else
                {
                    Util.Notify("蓝圈检测异常");
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo("蓝圈检测异常");
                    }
                }
            }
            else
            {
                bLanQuan_Result = true;
            }
            #endregion
            HTuple t8;
            HOperatorSet.CountSeconds(out t8);
            time = (t8 - t7).D * 1000;
            Util.Notify("蓝圈用时:" + time.ToString("F2") + "ms");
            if (NotifyExcInfo != null)
            {
                NotifyExcInfo("蓝圈用时:" + time.ToString("F2") + "ms");
            }


            RuningFinish  = true;
            base.IsOk     = bFindShapeMode && bGrabPointSetting && bPZT_Detection_Result && bGrab_Intervene_Result && bLM_DP_Result && bFangDai_Result && bLanQuan_Result;
            base.isRealOk = true;
            if (R != null && R.IsInitialized())
            {
                R.Dispose();
            }
            if (G != null && G.IsInitialized())
            {
                G.Dispose();
            }
            if (B != null && B.IsInitialized())
            {
                B.Dispose();
            }
        }
        //主执行程序
        protected override void RunAct()
        {
            base.RunAct();


            #region 初始化

            if (!engineIsnitial)
            {
                MyEngine = new HDevEngine();
                InitialEngine();
            }
            RuningFinish  = false;
            base.Result   = string.Empty;
            base.IsOk     = false;
            base.isRealOk = false;
            //功能块
            bFindShapeMode    = false;
            bGrabPointSetting = false;

            if (ImageTestIn == null || !ImageTestIn.IsInitialized())
            {
                return;
            }
            int channels = ImageTestIn.CountChannels();

            HImage ImageGray;
            if (channels == 3)
            {
                ImageGray = ImageTestIn.Rgb1ToGray();
            }
            else
            {
                ImageGray = ImageTestIn.Clone();
            }

            #endregion

            #region 创建模板
            if (MCreateShapeModel.hShapeModel == null || !MCreateShapeModel.hShapeModel.IsInitialized() || MCreateShapeModel.createNewModelID)
            {
                if (!MCreateShapeModel.CreateShapeModelAct(ImageRefIn))
                {
                    Util.Notify("创建模板异常");
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo("创建模板异常");
                    }
                    return;
                }
            }
            #endregion

            #region 模板匹配
            bFindShapeMode = MFindShapeMode.FindShapeModeAct(ImageRefIn, MCreateShapeModel, ImageTestIn);
            if (!bFindShapeMode)
            {
                Util.Notify("查找模板异常");
                if (NotifyExcInfo != null)
                {
                    NotifyExcInfo("查找模板异常");
                }
                return;
            }
            List <HHomMat2D> homMat2Ds = new List <HHomMat2D>();
            homMat2Ds = MFindShapeMode.GetHHomMat2Ds();
            if (MFindShapeMode.row == null || MFindShapeMode.row.Length < 1 || homMat2Ds == null)
            {
                Util.Notify("未找到模板");
                if (NotifyExcInfo != null)
                {
                    NotifyExcInfo("未找到模板");
                }
                base.IsOk     = true;
                base.isRealOk = true;
                return;
            }
            if (bFindShapeMode)
            {
                StringBuilder strb = new StringBuilder();
                strb.Append("查找结果:" + Environment.NewLine);
                int count = MFindShapeMode.row.Length;
                for (int i = 0; i < count; i++)
                {
                    HTuple phi;
                    HOperatorSet.TupleDeg(MFindShapeMode.angle[i], out phi);
                    strb.Append("第" + (i + 1) + "个:\r\n");
                    string mes = string.Format(
                        "Row:{0:F2}\r\n" + "Col:{1:F2}\r\n" + "角度:{2:F2}\r\n" + "得分:{3:F2}"
                        , MFindShapeMode.row[i].D, MFindShapeMode.column[i].D, phi.D, MFindShapeMode.score[i].D);
                    strb.Append(mes + Environment.NewLine);
                }
                if (NotifyExcInfo != null)
                {
                    NotifyExcInfo(strb.ToString());
                }
                Util.Notify(strb.ToString());
            }


            #endregion

            #region 抓取点
            bGrabPointSetting = mGrabPointSetting.setTarget(homMat2Ds);
            if (!bGrabPointSetting)
            {
                Util.Notify("抓取点异常");
                if (NotifyExcInfo != null)
                {
                    NotifyExcInfo("抓取点异常");
                }
            }
            else
            {
                StringBuilder strd = new StringBuilder();
                for (int i = 0; i < MGrabPointSetting.GrabRowTarget.Length; i++)
                {
                    string mes = string.Format(
                        "第" + (i + 1) + "个抓取点像素坐标:\r\n" +
                        "Row: {0:F2}\r\n" +
                        "Col: {1:F2}",
                        MGrabPointSetting.GrabRowTarget[i].D,
                        MGrabPointSetting.GrabColTarget[i].D);
                    strd.Append(mes + Environment.NewLine);
                }
                Util.Notify(strd.ToString());
                if (NotifyExcInfo != null)
                {
                    NotifyExcInfo(strd.ToString());
                }
            }
            #endregion

            #region 防呆
            if (bFangDai_Enable)
            {
                bFangDai_Result = MFangDai.FangDai_Act(ImageGray, homMat2Ds);
                if (bFangDai_Result)
                {
                    StringBuilder strb = new StringBuilder();
                    strb.Append("防呆:" + Environment.NewLine);
                    int count = MFangDai.Area.Length;
                    for (int i = 0; i < count; i++)
                    {
                        string mes = string.Format("第" + (i + 1) + "个:\r\n" +
                                                   "面积: {0:F2}"

                                                   , MFangDai.Area[i].D);
                        strb.Append(mes);
                        strb.Append(Environment.NewLine);
                    }
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo(strb.ToString());
                    }
                    Util.Notify(strb.ToString());
                }
                else
                {
                    Util.Notify("防呆检测异常");
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo("防呆检测异常");
                    }
                }
            }
            else
            {
                bFangDai_Result = true;
            }

            #endregion

            RuningFinish  = true;
            base.IsOk     = bFindShapeMode && bGrabPointSetting && bFangDai_Result;
            base.isRealOk = true;
            if (ImageGray != null && ImageGray.IsInitialized())
            {
                ImageGray.Dispose();
            }
        }