public void UpdateViewData(int val) { switch (val) { case ViewROI.ROIController.EVENT_CHANGED_ROI_SIGN: case ViewROI.ROIController.EVENT_DELETED_ACTROI: case ViewROI.ROIController.EVENT_UPDATE_ROI: bool genROI = view1.roiController.defineModelROI(); ModelRoi = view1.roiController.getModelRegion(); if (ModelRoi == null) { return; } if (!TemplateParam.CreateShapeModel(CurrentImage, ModelRoi)) { Logger.PopError("模板创建失败!", true); return; } view1.ResetWindow(); view1.AddViewObject(CurrentImage); view1.SetDraw("blue", "margin"); view1.AddViewObject(ModelRoi); HXLD ModelContour = new HXLD(); TemplateParam.GetShapeModelContour(out ModelContour); view1.SetDraw("green", "margin"); view1.AddViewObject(ModelContour); view1.Repaint(); break; } }
/// <summary> /// 拟合参数的示教函数 /// </summary> public void TeachProcess() { ShowRoiFlag = true; HObject RoiContour = new HObject(); HObject Roi = new HObject(); HObject RectContour = new HObject(); HObject ReduceImg = new HObject(); HObject ThresholdRegion = new HObject(); HObject ConnectRegion = new HObject(); HObject SelectRegion = new HObject(); HTuple Area = new HTuple(), Row = new HTuple(), Col = new HTuple(); HObject RegionContour = new HObject(); HTuple RectRow1 = new HTuple(), RectRow2 = new HTuple(), RectCol1 = new HTuple(), RectCol2 = new HTuple(); HObject MinRectContour = new HObject(); while (ShowRoiFlag) { try { #region view1.AddImage(GrabedImg); Thread.Sleep(50); HOperatorSet.GenRectangle1(out Roi, RectF.Y, RectF.X, RectF.Y + RectF.Height, RectF.X + RectF.Width);//生成检测区域 HOperatorSet.GenContourRegionXld(Roi, out RectContour, "border"); view1.SetDraw("green"); view1.AddViewObject(RectContour); view1.Repaint(); System.Threading.Thread.Sleep(100); HOperatorSet.ReduceDomain(GrabedImg, Roi, out ReduceImg); //裁剪图像 Roi.Dispose(); HOperatorSet.Threshold(ReduceImg, out ThresholdRegion, MinGray, MaxGray); //阈值分割 ReduceImg.Dispose(); HOperatorSet.Connection(ThresholdRegion, out ConnectRegion); //区域连接 ThresholdRegion.Dispose(); HOperatorSet.SelectShape(ConnectRegion, out SelectRegion, "area", "and", AreaMin, AreaMax); ConnectRegion.Dispose(); HOperatorSet.AreaCenter(SelectRegion, out Area, out Row, out Col); HOperatorSet.GenContourRegionXld(SelectRegion, out RegionContour, "border"); view1.SetDraw("red"); view1.AddViewObject(RegionContour); // HOperatorSet.SmallestRectangle2(SelectRegion,out Row ,out Col,out P) HOperatorSet.SmallestRectangle1(SelectRegion, out RectRow1, out RectCol1, out RectRow2, out RectCol2); HObject RectRegion = new HObject(); HOperatorSet.GenRectangle1(out RectRegion, RectRow1, RectCol1, RectRow2, RectCol2); HOperatorSet.GenContourRegionXld(RectRegion, out MinRectContour, "border"); view1.AddViewObject(MinRectContour); view1.Repaint(); System.Threading.Thread.Sleep(500); #endregion } catch { } } Thread.Sleep(500); }
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(); }
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 GetProductPixelPosBtn_Click(object sender, EventArgs e) //计算示教产品的像素的坐标,并保存到配置文件 { this.GetProductPixelPosBtn.Enabled = false; this.ProductPixelPosTeachBtn.Enabled = true; try { LocalResult localResult = new LocalResult(); LocalManager myLocal = new LocalManager(); //1.0设置定位模式 myLocal.SetLocalModel(LocalPara0.localSetting.localModel); //2.0设置定位参数 myLocal.SetParam(CurImg, LocalPara0); //3.执行定位 myLocal.doLocal(); localResult = myLocal.GetResult(); this.LocalPara0.localSetting.TeachImgLocal = new St_VectorAngle(localResult.row, localResult.col, localResult.angle); view1.ResetWindow(); view1.AddImage(CurImg.CopyObj(1, -1)); view1.SetDraw("blue", "margin"); view1.AddViewObject(localResult.ShowContour.CopyObj(1, -1)); view1.Repaint(); this.ProductPixelPosTbx.Text = "Col:" + localResult.col.ToString("f2") + " Row:" + localResult.row.ToString("f2") + " Angle:" + localResult.angle.ToString("f2"); } catch { } }
private void DrawLinesProcess() { ShowRoiFlag = true; HObject RoiContour = new HObject(); HObject DetectRegions = new HObject(); HTuple ResultRows = new HTuple(), ResultCols = new HTuple(); #region //直线参数示教 //2.1画出直线的检测区域 bool CurIsOk = true; this.myView.AddViewImage(GrabedImg); while (ShowRoiFlag) { try { //2.2显示直线的检测ROI,调整ROI的参数 Thread.Sleep(10); myView.ResetWindow(); myView.AddImage(GrabedImg); CurIsOk = MyVisionBase.gen_rake_ROI1(GrabedImg, out DetectRegions, out RoiContour, Element, DetectHeight, Threshold, LineRow1, LineCol1, LineRow2, LineCol2); myView.Repaint(); Thread.Sleep(100); myView.SetDraw("blue", "margin"); myView.AddViewObject(RoiContour); myView.Repaint(); Thread.Sleep(300); //2.5显示测量出每个示教小ROI的平均灰度,并保存到ListGrays变量中 HTuple MeanGrays = new HTuple(), GrayDivs = new HTuple(); HOperatorSet.Intensity(DetectRegions, GrabedImg, out MeanGrays, out GrayDivs); MyVisionBase.HTupleToList(MeanGrays, out ListGrays); } catch (Exception e0) { MessageBox.Show(e0.Message + e0.Source); StartTeachBtn1.Enabled = true; break; } } #endregion }
private void CamParaDgv_CellContentClick(object sender, DataGridViewCellEventArgs e) { try{ if (e.RowIndex >= 0) { //当前行对应的相机参数 CameraPara CurCamPara = (CameraPara)this.CamParaDgv.CurrentRow.DataBoundItem; if (CurCamPara == null) { return; } switch (CamParaDgv.Columns[e.ColumnIndex].Name) { case "DeleteBtnDgvCol": if (MessageBox.Show("是否删除该项设备? ", "删除提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) { return; } CameraParaManager.Instance.CameraParaList.Remove(CurCamPara); break; case "OpenCamBtnDgvCol": CameraPara camParaI; for (int i = 0; i < CameraParaManager.Instance.CameraParaList.Count() - 1; i++) { camParaI = CameraParaManager.Instance.CameraParaList[i]; if (CurCamPara.CameraName == camParaI.CameraName) { MessageBox.Show("相机已经示教,请重新选择相机"); break; } } CameraCtrl.Instance.Init(); break; case "GrabImgDgvCol": ImgOut.Dispose(); CameraCtrl.Instance.GrabImg(CurCamPara.CameraName, out ImgOut); view.ResetView(); view.AddViewImage(ImgOut); //view.Refresh(); view.Repaint(); break; } this.CamParaDgv.Refresh(); } } catch { } }
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 void TeachProcess() { ShowRoiFlag = true; HObject GrabImge = new HObject(); HObject RoiContour = new HObject(); HTuple ResultRows = new HTuple(); HTuple ResultCols = new HTuple(); #region //圆参数示教 HTuple ArcType = new HTuple(); HTuple DrawRows = new HTuple(), DrawCols = new HTuple(); while (ShowRoiFlag) { Thread.Sleep(50); //2.2显示圆的拟合ROI,条正边界检测参数 bool CurIsOk = true; this.view1.Refresh(); this.view1.AddImage(this.GrabedImg); CurIsOk = MyVisionBase.GenCirclePts2(TeachCenterRow, TeachCenterCol, TeachCircleR, TeachStartPhi, TeachEndPhi, TeachPointOrderStr, out DrawRows, out DrawCols); if (!CurIsOk) { StartTeachBtn1.Enabled = true; break; } CurIsOk = MyVisionBase.gen_spoke_ROI(GrabedImg, out RoiContour, Element, DetectHeight, 2, DrawRows, DrawCols, Direct); if (!CurIsOk) { StartTeachBtn1.Enabled = true; break; } view1.SetDraw("blue", "margin"); view1.AddViewObject(RoiContour); //2.3利用卡尺工具找出拟合边界点 CurIsOk = MyVisionBase.spoke(GrabedImg, out RoiContour, view1, Element, DetectHeight, 2, 2, Threshold, "all", "first", DrawRows, DrawCols, Direct, out ResultRows, out ResultCols, out ArcType); if (!CurIsOk) { break; } //2.4 HObject CirclePtCross = new HObject(); //2.5显示拟合的点 HOperatorSet.GenCrossContourXld(out CirclePtCross, ResultRows, ResultCols, 12, 0.7); view1.SetDraw("green", "margin"); view1.AddViewObject(CirclePtCross); //2.6圆拟合 HTuple CenterRow = new HTuple(), CenterCol = new HTuple(), hv_Radius = new HTuple(), hv_StartPhi = new HTuple(), hv_EndPhi = new HTuple(), hv_PointOrder = new HTuple(), hv_Flag = new HTuple(); HObject FitCircleContour = new HObject(); CurIsOk = MyVisionBase.PtsToBestCircle(out FitCircleContour, view1, ResultRows, ResultCols, 4, TeachArcType, out CenterRow, out CenterCol, out hv_Radius, out hv_StartPhi, out hv_EndPhi, out hv_PointOrder, out hv_Flag); if (!CurIsOk) { break; } //2.7显示拟合圆 view1.SetDraw("red", "margin"); view1.AddViewObject(FitCircleContour); view1.Repaint(); Thread.Sleep(1000); } #endregion }
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); }
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 }