private void GrabImgBtn_Click(object sender, EventArgs e) { if (GrabedImg != null) { GrabedImg.Dispose(); } CameraCtrl.Instance.GrabImg(TeachCaliPara.cam, out GrabedImg); //采集图像 view1.Refresh(); view1.AddViewImage(GrabedImg); view1.Repaint(); }
public bool StartGrabImg() { //1.打开光源 FileLib.Logger.Pop(" 打开光源:", false, "FOF运行日志"); foreach (LightPara item in MyVisionPara.camLightPara.lightPara) { LightCtrlManager.Instance.SetLightValue(item); } //2.采图 FileLib.Logger.Pop(" 开始采图:", false, "FOF运行日志"); if (GrabImg != null) { GrabImg.Dispose(); } CameraCtrl.Instance.SetExpos(MyVisionPara.camLightPara.CamName, MyVisionPara.camLightPara.Exposure); CameraCtrl.Instance.GrabImg(MyVisionPara.camLightPara.CamName, out GrabImg); ViewControl view0 = new ViewControl(); view0 = DisplaySystem.GetViewControl(CameraTest.UpCam2); if (MyVisionPara.camLightPara.CamName == CameraEnum.Cam2) { view0 = DisplaySystem.GetViewControl(CameraTest.UpCam3); } view0.ResetView(); view0.Refresh(); //3.关闭光源 LightCtrlManager.Instance.SetAllLightTo0(); FileLib.Logger.Pop(" 关闭光源:", false, "FOF运行日志"); return(true); }
private void ContinueGrabBtn_Click(object sender, EventArgs e) //连续采集 { ContinueGrabBtn.Enabled = false; IsContinnueGrab = true; System.Threading.Tasks.Task.Factory.StartNew(new Action(() => { int i = 0; while (IsContinnueGrab) { i++; if (GrabedImg != null) { GrabedImg.Dispose(); } if (CameraCtrl.Instance.GrabImg(TeachCamLightPara.CamName, out GrabedImg)) { view1.Refresh(); view1.AddViewImage(GrabedImg); view1.Repaint(); } System.Threading.Thread.Sleep(100); if (i > 500) { break; } } })); }
private void StartTeachBtn1_Click(object sender, EventArgs e) { HObject RoiContour = new HObject(); HTuple ArcType = new HTuple(); if (StartTeachBtn1.Text == "开始示教") { GroupBoxEnable(groupBox3, false); StartTeachBtn1.Enabled = true; StartTeachBtn1.Text = "保存"; view1.Refresh(); view1.AddImage(GrabedImg); ActionIsDrawingRoi(true); view1.roiController.reset(); view1.roiController.setROIShape(new ViewROI.ROICircularArc()); } else if (StartTeachBtn1.Text == "保存") { ActionIsDrawingRoi(false); GroupBoxEnable(groupBox3, true); ViewROI.ROICircularArc arc = new ViewROI.ROICircularArc(); if (view1.roiController.getActiveROI() is ViewROI.ROICircularArc) { StartTeachBtn1.Enabled = false; StartTeachBtn1.Text = "开始示教"; arc = (ViewROI.ROICircularArc)view1.roiController.getActiveROI(); arc.GetCircleArc(out TeachCenterRow, out TeachCenterCol, out TeachCircleR, out TeachStartPhi, out TeachEndPhi); if (ArcType.Length > 0) { TeachArcType = ArcType.S; } Thread TeachThd = new Thread(TeachProcess); TeachThd.IsBackground = true; TeachThd.Start(); } else { MessageBox.Show("请选中形状"); return; } } }
private void DrawRectBtn_Click(object sender, EventArgs e) { if (Img == null) { MessageBox.Show("请先加载一张图片"); return; } DrawRectBtn.Enabled = false; view.DrawRect1(out row1, out col1, out row2, out col2); txtSearchX.Text = col1.ToString(); txtSearchY.Text = row1.ToString(); txtSearchWidth.Text = (col2 - col1).ToString(); txtSearchHeight.Text = (row2 - row1).ToString(); DrawRectBtn.Enabled = true; HObject Rect, RectContour; HOperatorSet.GenRectangle1(out Rect, row1, col1, row2, col2); HOperatorSet.GenContourRegionXld(Rect, out RectContour, "border"); view.Refresh(); view.AddViewImage(Img); view.AddViewObject(RectContour); view.Repaint(); }
private void btnLoadLocalImage_Click(object sender, EventArgs e) { try { string fileName; OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "All files (*.*)|*.*|bmp files (*.bmp)|*.bmp"; openFileDialog1.Multiselect = false; openFileDialog1.FilterIndex = 1; openFileDialog1.Title = "打开图片文件"; openFileDialog1.RestoreDirectory = false; openFileDialog1.InitialDirectory = @"D:\Image\"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { fileName = openFileDialog1.FileName; HObject img; HTuple wid, hei; HOperatorSet.GenEmptyObj(out img); HOperatorSet.ReadImage(out img, fileName); HOperatorSet.GetImageSize(img, out wid, out hei); //hWindowControl1.ImagePart = new Rectangle(0, 0, wid, hei); OrgImageWid = wid; OrgImageHei = hei; TeachLocalPara.Template.SetTrainImage(fileName); if (CurrentImage != null) { CurrentImage.Dispose(); } if (TempSrcImage != null) { TempSrcImage.Dispose(); } HOperatorSet.CopyImage(img, out CurrentImage); HOperatorSet.CopyImage(img, out TempSrcImage); view1.Refresh(); view1.ResetView(); view1.AddViewImage(CurrentImage); view1.Repaint(); UpdateImageToSubForm(); if (img != null) { img.Dispose(); } } openFileDialog1.Dispose(); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } }
private void RectTeachBtn_Click(object sender, EventArgs e) { int SelectIndex = CaliPtModelCbx.SelectedIndex; if (GrabedImg == null || !GrabedImg.IsInitialized()) { MessageBox.Show("标定前,请先采集图片"); return; } System.Threading.Tasks.Task.Factory.StartNew(new Action(() => { try{ int row1 = 0, col1 = 0, row2, col2; if (SelectIndex == 0) { TeachCaliPara.ListRectRegion = new List <RectangleF>(); for (int i = 0; i < 4; i++) { view1.SetString(100, 100, "red", "左击拖动绘制矩形,右击释放"); view1.SetString(100, 150, "red", "绘制第" + i.ToString() + "矩形"); view1.DrawRect1(out row1, out col1, out row2, out col2); HObject Rect, RectContour; HOperatorSet.GenRectangle1(out Rect, row1, col1, row2, col2); HOperatorSet.GenContourRegionXld(Rect, out RectContour, "border"); view1.Refresh(); view1.AddViewImage(GrabedImg); view1.AddViewObject(RectContour); view1.Repaint(); RectangleF rect = new RectangleF(col1, row1, col2 - col1, row2 - row1); TeachCaliPara.ListRectRegion.Add(rect); } } } catch { } })); }
public override void Do() { //1.打开光源 foreach (LightPara item in MyVisionPara.camLightPara.lightPara) { LightCtrlManager.Instance.SetLightValue(item); } //2.采图 if (GrabImg != null) { GrabImg.Dispose(); } CameraCtrl.Instance.SetExpos(MyVisionPara.camLightPara.CamName, MyVisionPara.camLightPara.Exposure); if (CameraCtrl.Instance.GrabImg(MyVisionPara.camLightPara.CamName, out GrabImg)) { LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.FOF_Grabed_ok, 1); //拍照OK } else { LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.FOF_Grabed_ok, 2); //拍照NG } ViewControl view1 = DisplaySystem.GetViewControl(CameraTest.UpCam1); if (MyVisionPara.camLightPara.CamName == CameraEnum.Cam0) { view1 = DisplaySystem.GetViewControl(CameraTest.UpCam1); } if (MyVisionPara.camLightPara.CamName == CameraEnum.Cam1) { view1 = DisplaySystem.GetViewControl(CameraTest.UpCam2); } if (MyVisionPara.camLightPara.CamName == CameraEnum.Cam2) { view1 = DisplaySystem.GetViewControl(CameraTest.UpCam3); } view1.ResetView(); view1.Refresh(); view1.AddViewImage(GrabImg); view1.Repaint(); MyVisionBase.SaveImg(GrabImg, "扎针对位图片"); //3.关闭光源 LightCtrlManager.Instance.SetAllLightTo0(); //4.0设置定位模式 MyLocal.SetLocalModel(MyVisionPara.localPara.localSetting.localModel); //5.0设置定位参数 MyLocal.SetParam(GrabImg, MyVisionPara.localPara); //6.执行定位 try { MyLocal.doLocal(); LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.FOF_Grabed_result, 1); //告诉PLC定位结果OK } catch (Exception e0) { Logger.PopError1(e0, false, "视觉日志"); LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.FOF_Grabed_result, 2); //告诉PLC定位结果OK } //7.告诉PLC定位结果完成 MyLocalResult = MyLocal.GetResult(); view1.AddViewObject(MyLocalResult.ShowContour); //结合标定坐标计算出产品的实际位置 MyHomMat2D HomMat = new MyHomMat2D(); HomMat = CaliParaManager.Instance.GetHomMAT(MyVisionPara.localPara.localSetting.CoordiCam); //获取示教的标定矩阵 HalconDotNet.HTuple HHomMat = new HalconDotNet.HTuple(); //标定矩阵的转换 MyVisionBase.MyHomMatToHalcon(HomMat, out HHomMat); //图像坐标系的原点由左上角变到左下角 MyVisionBase.AdjImgRow(GrabImg, ref MyLocalResult.row); St_VectorAngle PixelVector = new St_VectorAngle(MyLocalResult.row, MyLocalResult.col, MyLocalResult.angle); CaliValue CaliPara = CaliParaManager.Instance.GetCaliValue(MyVisionPara.localPara.localSetting.CoordiCam); //将像素坐标转换到标定坐标 //MyLocalResult.AffineTransResult() HalconDotNet.HTuple HomMatCali = new HalconDotNet.HTuple(); MyVisionBase.MyHomMatToHalcon(CaliPara.HomMat, out HomMatCali); MyVisionBase.AffineTransPt(MyLocalResult.col, MyLocalResult.row, HomMatCali, out MyLocalResult.x, out MyLocalResult.y); //MyVisionBase.AffineTransPt() //获取当前坐标 //double X = 0, Y = 0, Z = 0, Theta = 0; //if (CaliParaManager.Instance.GetCaliMode(MyVisionPara.localPara.localSetting.TeachCoordi) == CaliModelEnum.HandEyeCali) //{ // MotionManager.Instance.SetCoordi(MyVisionPara.localPara.localSetting.TeachCoordi); // MotionManager.Instance.GetCoordiPos(out X, out Y, out Z, out Theta); //} //VectorAngle GrabPos = new VectorAngle(X, Y, Theta); ////9 计算出当前产品距离旋转中心的坐标 //EyeToHandPos.TransEyeToHandPos(PixelVector, CaliPara, GrabPos, out PosToRot); ////10计算出示教产品到旋转中心的坐标 //St_VectorAngle TeachPixelPos = new St_VectorAngle(MyLocalResult.TeachRow, MyLocalResult.TeachCol, MyLocalResult.TeachAngle); //VectorAngle TeachGrabPos = new VectorAngle(MyLocalResult.TeachX, MyLocalResult.TeachY, MyLocalResult.TeachTheta); //MyVisionBase.AdjImgRow(GrabImg, ref TeachPixelPos.Row); //EyeToHandPos.TransEyeToHandPos(TeachPixelPos, CaliPara, TeachGrabPos, out TeachPosToRot); //view1.Repaint(); //view1.SetString(100, 100, "red", "PosToRot: " + " X: " + PosToRot.Col.ToString("f3") + " Y: " + // PosToRot.Row.ToString("f3") + " Theta: " + PosToRot.Angle.ToString("f3")); //MyLocalResult.PosToRot = PosToRot; //MyLocalResult.TeachPosToRot = TeachPosToRot; }
public virtual void Do() { //1.打开光源 FileLib.Logger.Pop(" 打开光源:", false, "运行日志"); foreach (LightPara item in MyVisionPara.camLightPara.lightPara) { LightCtrlManager.Instance.SetLightValue(item); } //2.采图 if (GrabImg != null) { GrabImg.Dispose(); } FileLib.Logger.Pop(" 开始采图:", false, "运行日志"); CameraCtrl.Instance.SetExpos(MyVisionPara.camLightPara.CamName, MyVisionPara.camLightPara.Exposure); //10.获取当前的机械坐标 double X = 0, Y = 0, Z = 0, Theta = 0; if (CaliParaManager.Instance.GetCaliMode(MyVisionPara.localPara.localSetting.CoordiCam) == CaliModelEnum.HandEyeCali) { MotionManager.Instance.SetCoordi(MyVisionPara.localPara.localSetting.TeachCoordi); FileLib.Logger.Pop("当前的坐标系为:" + MyVisionPara.localPara.localSetting.TeachCoordi.ToString(), false, "运行日志"); MotionManager.Instance.GetCoordiPos(out X, out Y, out Z, out Theta); FileLib.Logger.Pop("当前的机械坐标", false, "运行日志"); FileLib.Logger.Pop("X:" + X.ToString("f4") + " Y:" + Y.ToString("f4") + " Z:" + Z.ToString("f4") + " Theta:" + Theta.ToString("f4"), false, "运行日志"); } if (CameraCtrl.Instance.GrabImg(MyVisionPara.camLightPara.CamName, out GrabImg)) { LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.ArtTestCameraGrabed, 1); //拍照OK FileLib.Logger.Pop(" 采图OK:", false, "运行日志"); } else { LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.ArtTestCameraGrabed, 2); //拍照NG FileLib.Logger.Pop(" 采图NG:", false, "运行日志"); } ViewControl view1 = DisplaySystem.GetViewControl(CameraTest.UpCam1); if (MyVisionPara.camLightPara.CamName == CameraEnum.Cam0) { view1 = DisplaySystem.GetViewControl(CameraTest.UpCam1); } if (MyVisionPara.camLightPara.CamName == CameraEnum.Cam1) { view1 = DisplaySystem.GetViewControl(CameraTest.UpCam2); } if (MyVisionPara.camLightPara.CamName == CameraEnum.Cam2) { view1 = DisplaySystem.GetViewControl(CameraTest.UpCam3); } view1.ResetView(); view1.Refresh(); view1.AddViewImage(GrabImg); view1.Repaint(); MyVisionBase.SaveImg(GrabImg, "扎针对位图片"); //3.关闭光源 LightCtrlManager.Instance.SetAllLightTo0(); FileLib.Logger.Pop(" 关闭光源:", false, "运行日志"); //4.0设置定位模式 MyLocal.SetLocalModel(MyVisionPara.localPara.localSetting.localModel); //5.0设置定位参数 MyLocal.SetParam(GrabImg, MyVisionPara.localPara); //6.执行定位 try { MyLocal.doLocal(); //7.告诉PLC定位结果完成 if (MyVisionPara.ProjectVisionItem == ProjectVisionEnum.ProjectVision1) { FileLib.Logger.Pop(" 扎针右侧视觉定位完成,先不发给PLC信号,等计算出偏移量后,将信号发给PLC:", false, "运行日志"); } else { LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.ArtTestCamLocalResult, 1); //告诉PLC定位结果OK FileLib.Logger.Pop(" 告诉PLC拍照结果OK(告诉PLC定位结果OK):", false, "运行日志"); } } catch (Exception e0) { Logger.PopError1(e0, false, "视觉错误日志"); LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.ArtTestCamLocalResult, 2); //告诉PLC定位结果NG FileLib.Logger.Pop(" 告诉PLC拍照结果OK(告诉PLC定位结果NG):", false, "运行日志"); MyLocalResult.IsLocalOk = false; } MyLocalResult = MyLocal.GetResult(); view1.AddViewObject(MyLocalResult.ShowContour); //结合标定坐标计算出产品的实际位置 //8.获取标定矩阵 MyHomMat2D HomMat = new MyHomMat2D(); HomMat = CaliParaManager.Instance.GetHomMAT(MyVisionPara.localPara.localSetting.CoordiCam); //获取示教的标定矩阵 FileLib.Logger.Pop(" 获取示教的标定矩阵:" + MyVisionPara.localPara.localSetting.CoordiCam.ToString(), false, "运行日志"); FileLib.Logger.Pop(HomMat.ToString(), false, "运行日志"); HalconDotNet.HTuple HHomMat = new HalconDotNet.HTuple(); //标定矩阵的转换 MyVisionBase.MyHomMatToHalcon(HomMat, out HHomMat); //9.图像坐标系的原点由左上角变到左下角 MyVisionBase.AdjImgRow(GrabImg, ref MyLocalResult.row); St_VectorAngle PixelVector = new St_VectorAngle(MyLocalResult.row, MyLocalResult.col, MyLocalResult.angle); FileLib.Logger.Pop("当前的像素坐标", false, "运行日志"); FileLib.Logger.Pop("Col:" + MyLocalResult.col.ToString("f4") + " Row:" + MyLocalResult.row.ToString("f4") + " Theta:" + MyLocalResult.angle.ToString("f4"), false, "运行日志"); CaliValue CaliPara = CaliParaManager.Instance.GetCaliValue(MyVisionPara.localPara.localSetting.CoordiCam); VectorAngle GrabPos = new VectorAngle(X, Y, Theta); //9 计算出当前产品距离旋转中心的坐标 EyeToHandPos.TransEyeToHandPos(PixelVector, CaliPara, GrabPos, out PosToRot); //10计算出示教产品到旋转中心的坐标 St_VectorAngle TeachPixelPos = new St_VectorAngle(MyLocalResult.TeachRow, MyLocalResult.TeachCol, MyLocalResult.TeachAngle); VectorAngle TeachGrabPos = new VectorAngle(MyLocalResult.TeachX, MyLocalResult.TeachY, MyLocalResult.TeachTheta); MyVisionBase.AdjImgRow(GrabImg, ref TeachPixelPos.Row); EyeToHandPos.TransEyeToHandPos(TeachPixelPos, CaliPara, TeachGrabPos, out TeachPosToRot); view1.Repaint(); view1.SetString(100, 100, "red", "PosToRot: " + " X: " + PosToRot.Col.ToString("f3") + " Y: " + PosToRot.Row.ToString("f3") + " Theta: " + PosToRot.Angle.ToString("f3")); MyLocalResult.PosToRot = PosToRot; MyLocalResult.TeachPosToRot = TeachPosToRot; FileLib.Logger.Pop("示教的产品相对旋转中心的偏移量", false, "运行日志"); FileLib.Logger.Pop("PosToRot: " + " X: " + PosToRot.Col.ToString("f3") + " Y: " + PosToRot.Row.ToString("f3") + " Theta: " + PosToRot.Angle.ToString("f3"), false, "运行日志"); FileLib.Logger.Pop("当前的产品相对旋转中心的偏移量", false, "运行日志"); FileLib.Logger.Pop("PosToRot: " + " X: " + TeachPosToRot.Col.ToString("f3") + " Y: " + TeachPosToRot.Row.ToString("f3") + " Theta: " + TeachPosToRot.Angle.ToString("f3"), false, "运行日志"); //11.计算出X Y Theta的补偿量, ////图像坐标转到机械坐标系 //MyVisionBase.AffineTransPt(MyLocalResult.col, MyLocalResult.row, HHomMat, out MyLocalResult.x, out MyLocalResult.y); //// //MyVisionBase.AffineTransPt(MyLocalResult.TeachCol, MyLocalResult.TeachRow, HHomMat, out MyLocalResult.TeachX, out MyLocalResult.TeachY); }
private void btnCreateTemplate_Click(object sender, EventArgs e) { if (btnCreateTemplate.Text == "新建") { btnCreateTemplate.Enabled = true; panelOperator.Enabled = true; groupBox1.Enabled = true; this.Enabled = true; btnCreateTemplate.Text = "保存"; view1.roiController.reset(); view1.roiController.NotifyRCObserver += UpdateViewData; view1.roiController.setROISign(ViewROI.ROIController.MODE_ROI_POS); if (CurrentImage == null) { Logger.PopError("请先采集图片!", true); return; } view1.SetDraw("red", "margin"); //1.0界面去使能 this.BeginInvoke(new Action(() => { ActionIsDrawingRoi(true); })); view1.Refresh(); view1.AddImage(CurrentImage); if (RoiStr.Equals("Rect1")) { view1.SetString(12, 12, "red", "绘制矩形区域作为模板区域"); Thread.Sleep(10); view1.roiController.setROIShape(new ViewROI.ROIRectangle1(200)); } #region //else if (RoiStr.Equals("Rect2")) //{ // HOperatorSet.ClearWindow(ShowWindow); // MyVisionBase.hDispObj(ShowWindow, CurrentImage); // MyVisionBase.disp_message(ShowWindow, "绘制矩形区域作为模板区域", "window", 12, 12, "red", "false"); // HTuple CenterRow, CenterCol, Rect2Phi, RectL1, RectL2; // HOperatorSet.DrawRectangle2(ShowWindow, out CenterRow, out CenterCol, out Rect2Phi, out RectL1, out RectL2); // ModelRoi = new HRegion(); // ModelRoi.GenRectangle2(CenterRow, CenterCol, Rect2Phi, RectL1, RectL2); //} //else if (RoiStr.Equals("Circle")) //{ // MyVisionBase.disp_message(ShowWindow, "绘制圆形区域作为模板区域", "window", 12, 12, "red", "false"); // HTuple CircleRow, CircleCol, CircleR; // HOperatorSet.DrawCircle(ShowWindow, out CircleRow, out CircleCol, out CircleR); // ModelRoi = new HRegion(); // ModelRoi.GenCircle(CircleRow, CircleCol, CircleR); //} //else if (RoiStr.Equals("CircleMod")) //{ // HTuple CircleRowIn, CircleColIn, CircleRIn, CircleRow, CircleCol, CircleR; // MyVisionBase.disp_message(ShowWindow, "绘制外圆区域作为圆环模板区域的外圆", "window", 12, 12, "red", "false"); // HOperatorSet.DrawCircle(ShowWindow, out CircleRowIn, out CircleColIn, out CircleRIn); // HOperatorSet.ClearWindow(ShowWindow); // MyVisionBase.hDispObj(ShowWindow, CurrentImage); // MyVisionBase.disp_message(ShowWindow, "绘制内圆区域作为圆环模板区域的内圆", "window", 12, 12, "red", "false"); // HOperatorSet.DrawCircleMod(ShowWindow, CircleRowIn, CircleColIn, CircleRIn, out CircleRow, out CircleCol, out CircleR); // HRegion InnerCirRegion = new HRegion(); // ModelRoi = new HRegion(); // InnerCirRegion.GenCircle(CircleRowIn, CircleColIn, CircleRIn); // ModelRoi.GenCircle(CircleRow, CircleCol, CircleR); // ModelRoi.Difference(InnerCirRegion); //} //else if (RoiStr.Equals("Rect2+Rect2")) //{ // HTuple CenterRow, CenterCol, Rect2Phi, RectL1, RectL2; // HRegion Rect2ROI = new HRegion(); // MyVisionBase.disp_message(ShowWindow, "绘制矩形1作为矩形联合区域的第一个矩形区域", "window", 12, 12, "red", "false"); // HOperatorSet.DrawRectangle2(ShowWindow, out CenterRow, out CenterCol, out Rect2Phi, out RectL1, out RectL2); // ModelRoi = new HRegion(); // ModelRoi.GenRectangle2(CenterRow, CenterCol, Rect2Phi, RectL1, RectL2); // HOperatorSet.ClearWindow(ShowWindow); // MyVisionBase.hDispObj(ShowWindow, CurrentImage); // MyVisionBase.disp_message(ShowWindow, "绘制矩形2作为矩形联合区域的第二个矩形区域", "window", 12, 12, "red", "false"); // HOperatorSet.DrawRectangle2(ShowWindow, out CenterRow, out CenterCol, out Rect2Phi, out RectL1, out RectL2); // Rect2ROI.GenRectangle2(CenterRow, CenterCol, Rect2Phi, RectL1, RectL2); // ModelRoi = ModelRoi.Union2(Rect2ROI); //} //else //{ // HOperatorSet.DrawRectangle1(ShowWindow, out row1, out column1, out row2, out column2); // ModelRoi = new HRegion(row1, column1, row2, column2); //} #endregion } else if (btnCreateTemplate.Text == "保存") { btnCreateTemplate.Text = "新建"; view1.roiController.NotifyRCObserver -= UpdateViewData; view1.roiController.reset(); this.BeginInvoke(new Action(() => { ActionIsDrawingRoi(false); })); string files = TemplateParam.TemplatePath; if (false) { SaveFileDialog saveFileDialog1 = new SaveFileDialog(); if (saveFileDialog1.ShowDialog() == DialogResult.OK) { files = saveFileDialog1.FileName; TemplateParam.Save(files); } } else { TemplateParam.Save(files); TemplateParam.Load(files); } } }
public override void Do() { //1.打开光源 FileLib.Logger.Pop(" 打开光源:", false, "FOF运行日志"); foreach (LightPara item in MyVisionPara.camLightPara.lightPara) { LightCtrlManager.Instance.SetLightValue(item); } //2.采图 FileLib.Logger.Pop(" 开始采图:", false, "FOF运行日志"); if (GrabImg != null) { GrabImg.Dispose(); } if (FilterImg != null) { FilterImg.Dispose(); } CameraCtrl.Instance.SetExpos(MyVisionPara.camLightPara.CamName, MyVisionPara.camLightPara.Exposure); if (CameraCtrl.Instance.GrabImg(MyVisionPara.camLightPara.CamName, out GrabImg)) { if (MyVisionPara.camLightPara.CamName == CameraEnum.Cam2) { LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.FOF_Grabed_ok, 1); //拍照OK } FileLib.Logger.Pop(" 采图OK:", false, "FOF运行日志"); } else { if (MyVisionPara.camLightPara.CamName == CameraEnum.Cam2) { LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.FOF_Grabed_ok, 2); //拍照NG } FileLib.Logger.Pop(" 采图NG:", false, "FOF运行日志"); } ViewControl view1 = new ViewControl(); view1 = DisplaySystem.GetViewControl(CameraTest.UpCam2); if (MyVisionPara.camLightPara.CamName == CameraEnum.Cam2) { view1 = DisplaySystem.GetViewControl(CameraTest.UpCam3); } view1.ResetView(); view1.Refresh(); //3.关闭光源 //LightCtrlManager.Instance.SetAllLightTo0(); //FileLib.Logger.Pop(" 关闭光源:", false, "FOF运行日志"); //4.0设置定位模式 MyLocal.SetLocalModel(MyVisionPara.localPara.localSetting.localModel); //5.0设置定位参数 FileLib.Logger.Pop(" 开始偏移检测:", false, "FOF运行日志"); MyVisionBase.SaveImg(GrabImg, "FOF偏移检测图片"); if (MyVisionPara.camLightPara.IsFilter) { FileLib.Logger.Pop(" 开始频域滤波,FilterC:" + MyVisionPara.camLightPara.FilterC.ToString(), false, "FOF运行日志"); MyVisionBase.FilterImg(GrabImg, out FilterImg, MyVisionPara.camLightPara.FilterC); view1.AddViewImage(FilterImg); MyVisionBase.SaveImg(FilterImg, "FOF偏移检测图片"); MyLocal.SetParam(FilterImg, MyVisionPara.localPara); } else { view1.AddViewImage(GrabImg); MyLocal.SetParam(GrabImg, MyVisionPara.localPara); } view1.Repaint(); //6.执行定位 try { FileLib.Logger.Pop(" 开始定位:", false, "FOF运行日志"); MyLocal.doLocal(); if (MyVisionPara.camLightPara.CamName == CameraEnum.Cam2) { LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.FOF_Grabed_result, 1); } FileLib.Logger.Pop(" 告诉PLC拍照结果OK(告诉PLC定位结果OK):", false, "FOF运行日志"); } catch (Exception e0) { if (MyVisionPara.camLightPara.CamName == CameraEnum.Cam2) { LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.FOF_Grabed_result, 2); } Logger.PopError1(e0, false, "视觉错误日志"); FileLib.Logger.Pop(" 告诉PLC拍照结果OK(告诉PLC定位结果NG):", false, "FOF运行日志"); } MyLocalResult = MyLocal.GetResult(); view1.AddViewObject(MyLocalResult.ShowContour); view1.Repaint(); //8.结合标定坐标计算出产品的实际位置 MyHomMat2D HomMat = new MyHomMat2D(); HomMat = CaliParaManager.Instance.GetHomMAT(MyVisionPara.localPara.localSetting.CoordiCam); //获取示教的标定矩阵 FileLib.Logger.Pop(" 获取标定矩阵(CoordiEnumItem):" + MyVisionPara.localPara.localSetting.CoordiCam.ToString() , false, "FOF运行日志"); FileLib.Logger.Pop(HomMat.ToString(), false, "FOF运行日志"); HalconDotNet.HTuple HHomMat = new HalconDotNet.HTuple(); //9.标定矩阵的转换 MyVisionBase.MyHomMatToHalcon(HomMat, out HHomMat); //10.图像坐标系的原点由左上角变到左下角 MyVisionBase.AdjImgRow(GrabImg, ref MyLocalResult.row); MyVisionBase.AffineTransPt(MyLocalResult.col, MyLocalResult.row, HHomMat, out MyLocalResult.x, out MyLocalResult.y); FileLib.Logger.Pop("col: " + MyLocalResult.col.ToString("f3") + " row: " + MyLocalResult.row.ToString("f3"), false, "FOF运行日志"); FileLib.Logger.Pop("X: " + MyLocalResult.x.ToString("f3") + " Y: " + MyLocalResult.y.ToString("f3"), false, "FOF运行日志"); MyLocalResult.Theta = MyLocalResult.angle; if (FilterImg != null) { FilterImg.Dispose(); } if (GrabImg != null) { GrabImg.Dispose(); } }
private void CaliRotCenterBtn_Click(object sender, EventArgs e) { DialogResult dr = MessageBox.Show("是否示开始旋转中心标定?", "提示", MessageBoxButtons.YesNo); if (dr == DialogResult.No) { return; } System.Threading.Tasks.Task.Factory.StartNew(new Action(() => { ListMarkCenterU = new List <Point2Db>(); HObject GrabedImg = new HObject(); LocalManager MyLocal = new LocalManager(); //定位类初始化 LocalResult MyResult = new LocalResult(); MyLocal.SetLocalModel(TeachCaliPara.localPara.localSetting.localModel); HOperatorSet.GenEmptyObj(out FindCenterCont); //旋转一个角度拍一次照片,用定位的点拟合圆,计算出0度时标定点到旋转中心的偏移量 for (int i = 0; i < TeachCaliPara.AngleStep; i++) { if (MotionManager.Instance.SetCoordiPos(TeachCaliPara.StartRotPt.x, TeachCaliPara.StartRotPt.y, TeachCaliPara.StartRotPt.angle + TeachCaliPara.AngleRange / TeachCaliPara.AngleStep * i)) { if (GrabedImg != null) { GrabedImg.Dispose(); } CameraCtrl.Instance.GrabImg(TeachCaliPara.cam, out GrabedImg); //采集图像 MyResult = new LocalResult(); MyLocal.SetParam(GrabedImg, TeachCaliPara.localPara); MyLocal.doLocal(); MyResult = MyLocal.GetResult(); if (!MyResult.IsLocalOk) { MessageBox.Show("定位失败"); return; } view1.ResetView(); view1.Refresh(); view1.AddViewImage(GrabedImg); view1.AddViewObject(MyResult.ShowContour); view1.Repaint(); //view1.Refresh(); //this.Refresh(); Thread.Sleep(50); Point2Db NowPt = new Point2Db(); NowPt.Col = MyResult.col; NowPt.Row = MyResult.row; ListMarkCenterU.Add(NowPt); FindCenterCont = FindCenterCont.ConcatObj(MyResult.ShowContour); } else { MessageBox.Show("移动失败"); return; } } #region 零度时候的定位坐标 Point2Db ZeoroDegreePos = new Point2Db(); if (MotionManager.Instance.SetCoordiPos(TeachCaliPara.StartRotPt.x, TeachCaliPara.StartRotPt.y, 0.0)) { if (GrabedImg != null) { GrabedImg.Dispose(); } CameraCtrl.Instance.GrabImg(TeachCaliPara.cam, out GrabedImg); //采集图像 MyResult = new LocalResult(); MyLocal.SetParam(GrabedImg, TeachCaliPara.localPara); MyLocal.doLocal(); MyResult = MyLocal.GetResult(); if (!MyResult.IsLocalOk) { MessageBox.Show("定位失败"); return; } ZeoroDegreePos = new Point2Db(MyResult.col, MyResult.row); } #endregion //通过找到的标定mark中心点集合,拟合出旋转中心 HTuple CircleRows = new HTuple(); HTuple CircleCols = new HTuple(); MyVisionBase.ListPt2dToHTuple(ListMarkCenterU, out CircleRows, out CircleCols); HObject CircleContourU = new HObject(); HTuple CenterRow = new HTuple(), CenterCol = new HTuple(); HTuple RotRadius = new HTuple(), StartPhi = new HTuple(), EndPhi = new HTuple(), FitFlag = new HTuple(); if (FindCirCenterCbx.SelectedValue.ToString() == "拟合圆") { MyVisionBase.PtsToBestCircle1(out CircleContourU, CircleRows, CircleCols, 4, "arc", out CenterRow, out CenterCol, out RotRadius, out StartPhi, out EndPhi, out FitFlag);//通过边界点拟合圆 RotR = RotRadius.D; RotCenterRow = CenterRow.D; RotCenterCol = CenterCol.D; } else if (FindCirCenterCbx.SelectedValue.ToString() == "中垂线找圆心") { FindCircleCenter(ListMarkCenterU, out RotCenterRow, out RotCenterCol, out RotR); HOperatorSet.GenCircleContourXld(out CircleContourU, RotCenterRow, RotCenterCol, RotR, 0, Math.PI, "positive", 1); } HObject PtContour = new HObject(); HOperatorSet.GenCrossContourXld(out PtContour, CircleRows, CircleCols, 20, 0.6); HOperatorSet.ConcatObj(PtContour, CircleContourU, out CircleContourU); // MyVisionBase.hDispObj(hWindowControl1.HalconWindow, CircleContourU); view1.AddViewObject(CircleContourU); view1.Repaint(); MyVisionBase.HTupleToListPt2d(CircleRows, CircleCols, out ListMarkCenterU); //计算0度标定Mark中心到旋转中心的偏移量 VectorCaliMarkToRot.Row = (CenterRow - ZeoroDegreePos.Row); VectorCaliMarkToRot.Col = (CenterCol - ZeoroDegreePos.Col); HObject MarkToRotArrow = new HObject(); MyVisionBase.GenArrowContourXld(out MarkToRotArrow, ZeoroDegreePos.Row, ZeoroDegreePos.Col, CenterRow, CenterCol, 30, 30); view1.AddViewObject(MarkToRotArrow); view1.Repaint(); })); }
/// <summary> /// 拟合参数的示教函数 /// </summary> public void TeachProcess() { ShowRoiFlag = true; HObject RoiContour = new HObject(); //1.0获取图像,调节曝光时间 HTuple ResultRows = new HTuple(), ResultCols = new HTuple(); //2.0示教参数 #region //直线参数示教 //2.1画出直线的检测区域 bool CurIsOk = true; view1.AddViewImage(GrabedImg); while (ShowRoiFlag) { //2.2显示直线的检测ROI,调整ROI的参数 Thread.Sleep(50); view1.Refresh(); view1.AddImage(GrabedImg); CurIsOk = MyVisionBase.gen_rake_ROI(GrabedImg, out RoiContour, Element, DetectHeight, 2, LineRow1, LineCol1, LineRow2, LineCol2); if (!CurIsOk) { break; } view1.SetDraw("blue", "margin"); view1.AddViewObject(RoiContour); //2.3利用卡尺工具找出边界点 CurIsOk = MyVisionBase.Rake(GrabedImg, out RoiContour, Element, DetectHeight, 2, 2, Threshold, "all", "first", LineRow1, LineCol1, LineRow2, LineCol2, out ResultRows, out ResultCols); //减去中间不要的点 int Count = ResultRows.Length; int ClipLength = (int )(Count * CenterClip); int StartClipNo = (int)((Count - ClipLength) * 0.5); if ((Count - ClipLength) > 4 && ClipLength > 0) { for (int i = 0; i < ClipLength; i++) { ResultRows = ResultRows.TupleRemove(StartClipNo); ResultCols = ResultCols.TupleRemove(StartClipNo); } } if (!CurIsOk) { break; } //2.4 HObject LinePtCross = new HObject(); //2.5显示拟合的点 HOperatorSet.GenCrossContourXld(out LinePtCross, ResultRows, ResultCols, 12, 0.7); view1.SetDraw("green", "margin"); view1.AddViewObject(LinePtCross); //2.6直线拟合 HObject FitLineCotour = new HObject(); HTuple FitLineRow1 = new HTuple(), FitLineCol1 = new HTuple(), FitLineRow2 = new HTuple(), FitLineCol2 = new HTuple(), PtToLineDist = new HTuple(); CurIsOk = MyVisionBase.PtsToBestLine(out FitLineCotour, ResultRows, ResultCols, 3, out FitLineRow1, out FitLineCol1, out FitLineRow2, out FitLineCol2, out PtToLineDist); if (CurIsOk) { HTuple AngleLx = new HTuple(); HOperatorSet.AngleLl(0, 0, 1, 0, FitLineCol1, FitLineRow1, FitLineCol2, FitLineRow2, out AngleLx); //直线1与X轴之间的夹角 HTuple DistP1L = new HTuple(), DistP2L = new HTuple(); HOperatorSet.DistancePl(LineRow10, LineCol10, FitLineRow1, FitLineCol1, FitLineRow2, FitLineCol2, out DistP1L); HOperatorSet.DistancePl(LineRow20, LineCol20, FitLineRow1, FitLineCol1, FitLineRow2, FitLineCol2, out DistP2L); AngleLx = AngleLx.D / Math.PI * 180.0; if (Math.Abs(AngleLx.D) % 90 < 10 && DistP1L.D < 10 && DistP2L.D < 10) { HOperatorSet.ProjectionPl(LineRow1, LineCol1, FitLineRow1, FitLineCol1, FitLineRow2, FitLineCol2, out LineRow1, out LineCol1); HOperatorSet.ProjectionPl(LineRow2, LineCol2, FitLineRow1, FitLineCol1, FitLineRow2, FitLineCol2, out LineRow2, out LineCol2); } } if (!CurIsOk) { break; } //2.7显示拟合直线 view1.SetDraw("red", "margin"); view1.AddViewObject(FitLineCotour); view1.Repaint(); Thread.Sleep(1500); } #endregion }