/// <summary> /// 检测函数 /// </summary> private void Detect() { pbImage.Visible = false; hWindowCtrl.Visible = true; if (imgOri != null) { Int32 nDefects = 0; Int32 nMode = 0; // detection method selection String strFullFileName; if (string.IsNullOrEmpty(ParamSetting.ImageSavePath)) { ParamSetting.ImageSavePath = Application.StartupPath; } strFullFileName = ParamSetting.ImageSavePath + "\\" + strFileName; // save temp image imgOri.Save(strFullFileName); switch (nMode) { case 0: // method 0: dynamic thresholding detectInstanceDyn = new HDevelopExport(); detectInstanceDyn.InitHalcon(); if (File.Exists(strFullFileName)) { detectInstanceDyn.RunHalcon(hWindowCtrl.HalconWindow, strFullFileName, ParamSetting.MeasureParam.MinArea, ParamSetting.MeasureParam.FilterSize, ParamSetting.MeasureParam.DynamicRange, out nDefects); } break; case 1: // method 1: normal thresholding detectInstanceNorm = new HDevelopExport1(); detectInstanceNorm.InitHalcon(); if (File.Exists(strFullFileName)) { detectInstanceNorm.RunHalcon(hWindowCtrl.HalconWindow, strFullFileName, ParamSetting.MeasureParam.MinArea, ParamSetting.MeasureParam.GrayThreshold, out nDefects); } break; } if (nDefects > 0) { lblResult.Text = "NG"; lblResult.BackColor = Color.Red; if (!string.IsNullOrEmpty(ParamSetting.ImageSavePath)) { strSavePathNG = ParamSetting.ImageSavePath + "\\" + DateTime.Now.ToShortDateString().Replace("/", "-") + "\\NG"; CheckAndCreatePath(strSavePathNG); if (pbImage.Image != null) { pbImage.Image.Save(strSavePathNG + "\\" + strFileName); } } } else { lblResult.Text = "OK"; lblResult.BackColor = Color.Green; if (!string.IsNullOrEmpty(ParamSetting.ImageSavePath)) { strSavePathOK = ParamSetting.ImageSavePath + "\\" + DateTime.Now.ToShortDateString().Replace("/", "-") + "\\OK"; CheckAndCreatePath(strSavePathOK); if (pbImage.Image != null) { pbImage.Image.Save(strSavePathOK + "\\" + strFileName); } } } } pbImage.Image = imgOri; }
/* 检测按钮事件 */ private void btnDetect_Click(object sender, EventArgs e) { pbImage.Visible = false; hWindowCtrl.Visible = true; if (imgOri != null) { Int32 nDefects = 0; Int32 nMode = 1; String strFullFileName; if (strSavePath == "") { strSavePath = Application.StartupPath; } strFullFileName = strSavePath + strFileName; // save temp image imgOri.Save(strFullFileName); switch (nMode) { case 0: // method 0: dynamic thresholding detectInstanceDyn = new HDevelopExport(); detectInstanceDyn.InitHalcon(); if (File.Exists(strFullFileName)) { detectInstanceDyn.RunHalcon(hWindowCtrl.HalconWindow, strFullFileName, myConfig.面积, myConfig.滤波尺寸, myConfig.动态二值化阈值, out nDefects); } break; case 1: // method 1: normal thresholding detectInstanceNorm = new HDevelopExport1(); detectInstanceNorm.InitHalcon(); if (File.Exists(strFullFileName)) { detectInstanceNorm.RunHalcon(hWindowCtrl.HalconWindow, strFullFileName, myConfig.面积, myConfig.灰度值, out nDefects); } break; } if (nDefects > 0) { lblResult.Text = "NG"; lblResult.BackColor = Color.Red; if (strSavePath != null && strSavePath.Length != 0) { strSavePathNG = strSavePath + "\\NG"; CheckAndCreatePath(strSavePathNG); if (pbImage.Image != null) { pbImage.Image.Save(strSavePathNG + "\\" + strFileName); } } } else { lblResult.Text = "OK"; lblResult.BackColor = Color.Green; if (strSavePath != null && strSavePath.Length != 0) { strSavePathOK = strSavePath + "\\OK"; CheckAndCreatePath(strSavePathOK); if (pbImage.Image != null) { pbImage.Image.Save(strSavePathOK + "\\" + strFileName); } } } } pbImage.Image = imgOri; }