public void NewDoProject(LD.Config.PlcDataItem PlcItem, Object Obj) { LD.Common.PlcDevice plcDevice = PlcItem.PlcDevice; if (plcDevice == LD.Common.PlcDevice.ArtTestCameraStart) // { //读取机台编号 int StageNum = (int)LD.Logic.PlcHandle.Instance.ReadValue(LD.Common.PlcDevice.ArtTestStageNumRead); //读取拍照位编号 int GrabNum = (int)LD.Logic.PlcHandle.Instance.ReadValue(LD.Common.PlcDevice.ArtTestGrabNum); //获取视觉示教的定位参数 NowProjectPara = ProjectParaManager.Instance.GetProjectPara(StageNum); //获取当前拍照点的视觉坐标 NowVisionPara = NowProjectPara.GetVisionPara(GrabNum); NowVisionProcess.SetVisionPara(NowVisionPara); NowVisionProcess.Do(); FirstLocalResult = NowVisionProcess.MyLocalResult; St_VectorAngle PosToRot = NowVisionProcess.PosToRot; St_VectorAngle TeachPosToRot = NowVisionProcess.TeachPosToRot; St_VectorAngle AddVector = new St_VectorAngle(); MyVisionBase.VectorAngleToMotionXYTh(PosToRot, TeachPosToRot, out AddVector); LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.ArtTestAddX, -AddVector.Col); LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.ArtTestAddY, AddVector.Row); LD.Logic.PlcHandle.Instance.WriteValue(LD.Common.PlcDevice.ArtTestAddTheta, -AddVector.Angle); ViewControl view1 = DisplaySystem.GetViewControl(CameraTest.UpCam1); } }
public LineCircRectRlt() { IsOk = true; NgContour = new HObject(); DetectContour = new HObject(); row = 0; col = 0; angle = 0; x = 0; y = 0; Theta = 0; TeachRow = 0; TeachCol = 0; TeachAngle = 0; TeachX = 0; TeachY = 0; TeachTheta = 0; IsLocalOk = false; ShowContour = new HObject(); NgContour = new HObject(); DetectContour = new HObject(); TeachPosToRot = new St_VectorAngle(); PosToRot = new St_VectorAngle(); }
/// <summary> /// 像素坐标转换到机械坐标或者世界坐标系,当标定模式为CaliModelEnum.HandEyeCali时,算出的坐标为相对于旋转中心的坐标, /// 物体相对于机械坐标原点的坐标:计算出的坐标+当前拍照的坐标。 /// </summary> /// <param name="PixelVector">图像处理定位出来的像素坐标 </param> /// <param name="CaliValueIn">标定参数,包含标定的起点,标定的终点,标定矩阵,相机是否挂在XY轴上</param> /// <param name="GrabImgPosPt3d">采集图像时的机械坐标</param> /// <param name="WorldVector"></param> public static void TransEyeToHandPos(St_VectorAngle PixelVector, CaliValue CaliValueIn, VectorAngle GrabImgPosPt3d, out St_VectorAngle WorldVector) { WorldVector = new St_VectorAngle(); double X = 0, Y = 0; HTuple HomMat = new HTuple(); MyVisionBase.MyHomMatToHalcon(CaliValueIn.HomMat, out HomMat); // VectorAngle CaliMidPt = new VectorAngle(); //标定起始点到标定终点的中点 CaliMidPt.X = 0.5 * (CaliValueIn.StartCaliPt.x + CaliValueIn.EndCaliPt.x); CaliMidPt.Y = 0.5 * (CaliValueIn.StartCaliPt.y + CaliValueIn.EndCaliPt.y); double AddX = 0, AddY = 0; //拍照坐标不和标定中心坐标重合时,带来的偏移量 if (CaliValueIn.IsMoveX && !CaliValueIn.IsMoveY) //相机随着X轴移动 { AddX = GrabImgPosPt3d.X - CaliMidPt.X; AddY = -(GrabImgPosPt3d.Y - CaliMidPt.Y); } else if (!CaliValueIn.IsMoveX && CaliValueIn.IsMoveY) //相机随着Y轴移动 { AddX = -(GrabImgPosPt3d.X - CaliMidPt.X); AddY = (GrabImgPosPt3d.Y - CaliMidPt.Y); } else if (!CaliValueIn.IsMoveX && !CaliValueIn.IsMoveY) //相机静止 { AddX = -(GrabImgPosPt3d.X - CaliMidPt.X); AddY = -(GrabImgPosPt3d.Y - CaliMidPt.Y); } else if (CaliValueIn.IsMoveX && CaliValueIn.IsMoveY) //相机随着XY轴移动 { AddX = (GrabImgPosPt3d.X - CaliMidPt.X); AddY = (GrabImgPosPt3d.Y - CaliMidPt.Y); } //像素坐标转换成当前的世界坐标, MyVisionBase.AffineTransPt(PixelVector.Col, PixelVector.Row, HomMat, out X, out Y); switch (CaliValueIn.caliModel) { case CaliModelEnum.HandEyeCali: X = X + AddX; Y = Y + AddY; break; case CaliModelEnum.Cali9PtCali: X = X + AddX; Y = Y + AddY; break; default: X = X + AddX; Y = Y + AddY; break; } WorldVector.Angle = PixelVector.Angle; WorldVector.Col = X; WorldVector.Row = Y; }
public St_InspectImageSetting(bool isInit = true) { LinePara = new St_LinesParam(4); CirclePara = new St_CirclesParam(true); VectorAngle0 = new St_VectorAngle(true); NowVectorAngle = new St_VectorAngle(true); InspectLinePara = new St_InsepctLinePara(2, true); InspectCirclePara = new St_InspectCirclePara(2, true); InspectRectPara = new St_InspectRectanglePara(1, true); }
public void UpdateCurrImage(HObject img, St_VectorAngle TempFindVectorAngleIn, LocalPara LocalParaIn) { if (GrabedImg != null) { GrabedImg.Dispose(); } HOperatorSet.CopyImage(img, out GrabedImg); TeachFindVectorAngle = TempFindVectorAngleIn; LocalPara0 = LocalParaIn; }
private void btnFindTemplate_Click(object sender, EventArgs e) { DialogResult rlt = MessageBox.Show("是否重新示教模板,点击YES,需要重新示教后面的线和圆?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (rlt != DialogResult.Yes) { return; } if (CurrentImage == null) { Logger.PopError("请先采集图片!", true); return; } RectangleF roi = new RectangleF(); roi.X = Setting.SearchAreaX; roi.Y = Setting.SearchAreaY; roi.Width = Setting.SearchWidth; roi.Height = Setting.SearchHeight; MatchingResult result; TemplateParam.FindSharpTemplate(CurrentImage, roi, TemplateParam, out result); if (result.mScore != null && result.mScore > 0) { XYOffset.Col = result.mCol - TemplateParam.CenterX; XYOffset.Row = result.mRow - TemplateParam.CenterY; TemplateParam.CenterX = result.mCol; TemplateParam.CenterY = result.mRow; TemplateParam.TemplateAngle = result.mAngle; TempFindVecvtorAngle = new St_VectorAngle(TemplateParam.CenterY, TemplateParam.CenterX, TemplateParam.TemplateAngle); view1.Refresh(); this.view1.AddImage(this.CurrentImage); view1.Repaint(); Thread.Sleep(100); HXLD contour; TemplateParam.GetDetectionContour(out contour); view1.SetDraw("blue", "margin"); view1.AddViewObject(contour.CopyObj(1, -1)); view1.Repaint(); Thread.Sleep(500); string str = string.Format("匹配度:{0}%", (100 * result.mScore.D).ToString("f2")); view1.SetString(100, 500, "red", str); str = string.Format("耗时:{0}ms", (result.mTime).ToString("f1")); view1.SetString(100, 250, "red", str); contour.Dispose(); } else { view1.SetString(50, 100, "red", "匹配失败!"); } }
private void TryDebugBtn_Click(object sender, EventArgs e) { Stopwatch sw = new Stopwatch(); sw.Start(); try{ #region Point2Db MotionPos = new Point2Db(0, 0); HObject CirContour = new HObject(); HObject circle = new HObject(); HObject centerCont = new HObject(); //模板的VectorAngle double TempTeachRow = LocalPara0.Template.CenterY; double TempTeachCol = LocalPara0.Template.CenterX; double TempTeachAngle = LocalPara0.Template.TemplateAngle; view1.Refresh(); HTuple Y = new HTuple(), X = new HTuple(); //找出要定位的的圆 St_CirclesParam AdjCirclePara = new St_CirclesParam(2); St_VectorAngle VectorAngle0 = new St_VectorAngle(LocalPara0.Template.CenterY, LocalPara0.Template.CenterX, LocalPara0.Template.TemplateAngle); HTuple CircleStartPhi = new HTuple(), CircleEndPhi = new HTuple(); CircleTypePos.FindCircle(GrabedImg, AdjCirclePara, out Y, out X, out FindCircleR, out CircleStartPhi, out CircleEndPhi, out circle, out CirContour, out centerCont); FindCircleRow = Y; FindCircleCol = X; view1.AddImage(GrabedImg); Thread.Sleep(100); view1.SetDraw("green", "margin"); view1.AddViewObject(circle); view1.SetDraw("blue", "margin"); view1.AddViewObject(CirContour); view1.SetDraw("red", "margin"); view1.AddViewObject(centerCont); HObject CirCenterCross = new HObject(); HOperatorSet.GenCrossContourXld(out CirCenterCross, Y, X, 120, 0.6); view1.AddViewObject(CirCenterCross); #endregion } catch (Exception e0) { Logger.PopError(e0.Message.ToString(), true); } }
private void TempFindTestBtn_Click(object sender, EventArgs e) { if (CurrentImage == null) { Logger.PopError("请先采集图片!", true); return; } RectangleF roi = new RectangleF(); roi.X = Setting.SearchAreaX; roi.Y = Setting.SearchAreaY; roi.Width = Setting.SearchWidth; roi.Height = Setting.SearchHeight; MatchingResult result; TemplateParam.FindSharpTemplate(CurrentImage, roi, TemplateParam, out result); view1.Refresh(); view1.AddImage(CurrentImage); if (result.mScore != null && result.mScore > 0) { TempFindVecvtorAngle = new St_VectorAngle(result.mRow, result.mCol, result.mAngle); HXLD contour; TemplateParam.GetDetectionContour(out contour); view1.SetDraw("blue", "margin"); view1.AddViewObject(contour.CopyObj(1, -1)); view1.Repaint(); string str = string.Format("匹配度:{0}%", (100 * result.mScore.D).ToString("f2")); view1.SetString(100, 150, "red", str); str = string.Format("耗时:{0}ms", (result.mTime).ToString("f1")); view1.SetString(100, 200, "red", str); str = string.Format("Row:{0}", (result.mRow.D).ToString("f1")); view1.SetString(100, 250, "red", str); str = string.Format("Col:{0}", (result.mCol.D).ToString("f1")); view1.SetString(100, 300, "red", str); str = string.Format("Angle:{0}", (result.mAngle.D * 180.0 / Math.PI).ToString("f1")); view1.SetString(100, 350, "red", str); contour.Dispose(); } else { view1.SetString(50, 100, "red", "匹配失败!"); } }
public LocalSettingPara() { SearchAreaX = 0; SearchAreaY = 0; SearchWidth = 1000; SearchHeight = 1000; localModel = LocalModelEnum.Temp; GrabPosTeach = new Point3Db(); TeachImgLocal = new St_VectorAngle(); TeachCoordi = CoordiEmum.Coordi0; CoordiCam = CoordiCamHandEyeMatEnum.Coordi0Cam0; Offset_x_range = 1; Offset_y_range = 1; Offset_theta_range = 1; Offset_x = 0; Offset_y = 0; Offset_theta = 0; PixelSize = 1; }
public BlobLocalRlt() { ListRow = new List <double>(); ListCol = new List <double>(); ListWid = new List <double>(); ListHei = new List <double>(); ListArea = new List <double>(); row = 0; col = 0; angle = 0; x = 0; y = 0; Theta = 0; TeachRow = 0; TeachCol = 0; TeachAngle = 0; TeachX = 0; TeachY = 0; TeachTheta = 0; IsLocalOk = false; ShowContour = new HObject(); TeachPosToRot = new St_VectorAngle(); PosToRot = new St_VectorAngle(); }
public void UpdatePara(St_LinesParam LineParaIn, St_CirclesParam CircleParaIn, St_VectorAngle VectorAngle0In) { InspectImageSetting.VectorAngle0 = VectorAngle0In; InspectImageSetting.CirclePara = CircleParaIn; InspectImageSetting.LinePara = LineParaIn; }
public St_PosDesignMsg(bool IsInit = true) { vectorAngle = new St_VectorAngle(); PixelSize = 0.02; }
private void btnNextStep_Click(object sender, EventArgs e) { SubFormIndex++; if (SubFormIndex == VisionSubFormList.Count) { DialogResult rlt = MessageBox.Show("即将保存参数 &&退出,是否继续?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); foreach (var item in panelOperator.Controls) { if (item is SubFrmTemplate) { TeachLocalPara.localSetting.SearchAreaX = ((SubFrmTemplate)item).Setting.SearchAreaX; TeachLocalPara.localSetting.SearchAreaY = ((SubFrmTemplate)item).Setting.SearchAreaY; TeachLocalPara.localSetting.SearchWidth = ((SubFrmTemplate)item).Setting.SearchWidth; TeachLocalPara.localSetting.SearchHeight = ((SubFrmTemplate)item).Setting.SearchHeight; TeachLocalPara.Template = ((SubFrmTemplate)item).TemplateParam; TemplateFindVectorAngle = ((SubFrmTemplate)item).TempFindVecvtorAngle; } else if (item is SubFrmBlobLocal) { TeachLocalPara.Blobs = ((SubFrmBlobLocal)item).BlobInspectPara; break; } else if (item is SubFrmFindLine) { TeachLocalPara.Lines = ((SubFrmFindLine)item).LinesParam; } else if (item is SubFrmFindCircle) { TeachLocalPara.Circles = ((SubFrmFindCircle)item).CircleParam; } else if (item is SubFrmRecheck) { TeachLocalPara.LineCirRectInspParam = ((SubFrmRecheck)item).RecheckParam; } else if (item is SubFrmFinish) { TeachLocalPara.localSetting = ((SubFrmFinish)item).LocalPara0.localSetting; } else { } } if (rlt == DialogResult.Yes) { IsSaveVisionPara = true; this.Close(); this.Dispose(); return; } else { SubFormIndex--; return; } } else { foreach (var item in panelOperator.Controls) { if (item is SubFrmTemplate) { TeachLocalPara.localSetting.SearchAreaX = ((SubFrmTemplate)item).Setting.SearchAreaX; TeachLocalPara.localSetting.SearchAreaY = ((SubFrmTemplate)item).Setting.SearchAreaY; TeachLocalPara.localSetting.SearchWidth = ((SubFrmTemplate)item).Setting.SearchWidth; TeachLocalPara.localSetting.SearchHeight = ((SubFrmTemplate)item).Setting.SearchHeight; TeachLocalPara.Template = ((SubFrmTemplate)item).TemplateParam; TemplateFindVectorAngle = ((SubFrmTemplate)item).TempFindVecvtorAngle; } else if (item is SubFrmBlobLocal) { TeachLocalPara.Blobs = ((SubFrmBlobLocal)item).BlobInspectPara; break; } else if (item is SubFrmFindLine) { TeachLocalPara.Lines = ((SubFrmFindLine)item).LinesParam; } else if (item is SubFrmFindCircle) { TeachLocalPara.Circles = ((SubFrmFindCircle)item).CircleParam; } else if (item is SubFrmRecheck) { TeachLocalPara.LineCirRectInspParam = ((SubFrmRecheck)item).RecheckParam; } else if (item is SubFrmFinish) { TeachLocalPara.localSetting = ((SubFrmFinish)item).LocalPara0.localSetting; } else { } } SwitchSubFrm(SubFormIndex); SwitchStepButton(SubFormIndex); } SwitchStepButton(SubFormIndex); if (CurrentImage != null) { if (CurrentImage.IsInitialized()) { UpdateImageToSubForm(); } } UpdateParaToSubForm(); }
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; }
private void FindLineBtn_Click(object sender, EventArgs e) { txtFindTime.Clear(); Stopwatch sw = new Stopwatch(); sw.Start(); if (GrabedImg == null) { Logger.PopError("请先采集图片!", true); return; } //1.0模板匹配 St_TemplateParam TemplateParam = VisionPara0.localPara.Template; RectangleF roi = new RectangleF(); LocalSettingPara Setting = VisionPara0.localPara.localSetting; roi.X = Setting.SearchAreaX; roi.Y = Setting.SearchAreaY; roi.Width = Setting.SearchWidth; roi.Height = Setting.SearchHeight; MatchingResult result; TemplateParam.FindSharpTemplate(GrabedImg, roi, TemplateParam, out result); //2.0调整图像位置 St_VectorAngle VectorAngle0 = new St_VectorAngle(VisionPara0.localPara.Template.CenterY, VisionPara0.localPara.Template.CenterX, VisionPara0.localPara.Template.TemplateAngle); St_VectorAngle TempFindVectorAngle = new St_VectorAngle(result.mRow, result.mCol, result.mAngle);//找到的模板坐标 HTuple HomMat = new HTuple(); HOperatorSet.VectorAngleToRigid(result.mRow, result.mCol, result.mAngle, VisionPara0.localPara.Template.CenterY, VisionPara0.localPara.Template.CenterX, VisionPara0.localPara.Template.TemplateAngle, out HomMat); HObject AffineImg = new HObject(); HOperatorSet.AffineTransImage(GrabedImg, out AffineImg, HomMat, "constant", "false"); HOperatorSet.ClearWindow(ShowWindow); MyVisionBase.hDispObj(ShowWindow, GrabedImg); //3.0找出Pin针坐标 HObject RoiContour = new HObject(); MyVisionBase.hDispObj(ShowWindow, AffineImg); HTuple PinRows = new HTuple(), PinCols = new HTuple(); PinInsepct.FindPinPos(TeachInspectPinParam, AffineImg, out PinRows, out PinCols); HObject ShowContour = new HObject(); HOperatorSet.GenCrossContourXld(out ShowContour, PinRows, PinCols, 50, 0); MyVisionBase.hSetColor(ShowWindow, "red"); MyVisionBase.hDispObj(ShowWindow, ShowContour); //4.0 计算出Pin针偏移量 HTuple OffSetRows = new HTuple(), OffSetCols = new HTuple(), OffSetDists = new HTuple();; PinInsepct.CalculatePinOffset(TeachInspectPinParam, PinRows, PinCols, out OffSetRows, out OffSetCols, out OffSetDists); HTuple Max = OffSetDists.TupleMax(); HTuple MaxIndex = OffSetDists.TupleFind(Max); HOperatorSet.GenCircleContourXld(out ShowContour, PinRows[MaxIndex[0].I], PinCols[MaxIndex[0].I], 50, 0, Math.PI * 2, "positive", 1.0); MyVisionBase.hSetColor(ShowWindow, "red"); MyVisionBase.hDispObj(ShowWindow, ShowContour); txtFindTime.Text = sw.ElapsedMilliseconds.ToString(); }
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 static bool CircleLocation(HObject ImgIn, St_CirclesParam TeachCirclePara0, out St_VectorAngle VectorAngleOut, out HObject circleCont, out HObject CirContour, out HObject centerCont) { St_CirclesParam TeachCirclePara; TeachCirclePara = TeachCirclePara0; HObject ObjContour = new HObject(); CirContour = new HObject(); HOperatorSet.GenEmptyObj(out CirContour); HObject CircleObj = new HObject(); centerCont = new HObject(); circleCont = new HObject(); VectorAngleOut = new St_VectorAngle(true); HTuple CircleRows = new HTuple(), CircleCols = new HTuple(); HObject RoiContour = new HObject(); HTuple ResultRows = new HTuple(), ResultCols = new HTuple(), ArcType0 = new HTuple(); HTuple CircleCenterRow = new HTuple(), CircleCenterCol = new HTuple(), CircleRadius = new HTuple(); HTuple StartPhi = new HTuple(), EndPhi = new HTuple(), PointOrder = new HTuple(), IsTrueFlag = new HTuple(); int cirCount; try { #region for (int i = 0; i < TeachCirclePara.Count; i++) { //2.0利用存下来的圆的信息,创建圆的边缘点坐标 MyVisionBase.GenCirclePts2(TeachCirclePara.CenterRows[i], TeachCirclePara.CenterCols[i], TeachCirclePara.CircleRs[i], TeachCirclePara.StartPhis[i], TeachCirclePara.EndPhis[i] , TeachCirclePara.PointOrders[i], out CircleRows, out CircleCols); //3.0找出边缘点用来拟合圆 MyVisionBase.spoke2(ImgIn, TeachCirclePara.Elements[i], TeachCirclePara.DetectHeights[i], 2, 2, TeachCirclePara.Thresholds[i], "all", "first", CircleRows, CircleCols, TeachCirclePara.Directs[i], out ResultRows, out ResultCols, out ArcType0); HOperatorSet.GenCrossContourXld(out CircleObj, ResultRows, ResultCols, TeachCirclePara.CircleRs[i] / 10, 0.6); HOperatorSet.ConcatObj(CircleObj, CirContour, out CirContour); cirCount = CirContour.CountObj(); HTuple ElectRows = ResultRows, ElectCols = ResultCols; int lengthResultRows = ResultRows.Length; int StartNum = (int)(lengthResultRows * 0.3); int RemoveNum = (int)(lengthResultRows * 0.2); for (int j = StartNum; j < StartNum + RemoveNum; j++) { if (j < lengthResultRows && StartNum + RemoveNum < lengthResultRows) { ElectRows = ElectRows.TupleRemove(StartNum); ElectCols = ElectCols.TupleRemove(StartNum); } } //4.拟合圆找到圆的中心 HObject CircleContour = new HObject(); HTuple FitRow = new HTuple(), FitCol = new HTuple(), FitR = new HTuple(), SartP = new HTuple(), EndP = new HTuple(), IsFlag = new HTuple(); MyVisionBase.PtsToBestCircle1(out CircleObj, ElectRows, ElectCols, 4, "arc", out FitRow, out FitCol, out FitR, out SartP, out EndP, out IsFlag); if (Math.Abs(TeachCirclePara.CircleRs[i] - FitR.D) > TeachCirclePara.CircleRs[i] * 0.2) { HOperatorSet.ConcatObj(CircleObj, CirContour, out CirContour); Logger.PopError("找出圆的半径大于理论值。"); return(false); } cirCount = CircleObj.CountObj(); HOperatorSet.ConcatObj(CircleObj, CirContour, out CirContour); CircleCenterRow[i] = FitRow.D; CircleCenterCol[i] = FitCol.D; CircleRadius[i] = FitR.D; StartPhi[i] = SartP.D; EndPhi[i] = EndP.D; IsTrueFlag[i] = IsFlag[0]; } HTuple AngleLxPixel = new HTuple(), AngleLx = new HTuple(); if (CircleCenterRow.TupleLength() != TeachCirclePara0.Count) { Logger.PopError("CircleTypePos.FindCenter" + "没有找到圆"); return(false); } #region //生成圆心十字轮廓,非功能性代码 HTuple cRow = new HTuple(), cColumn = new HTuple(); int count = CircleCenterRow.TupleLength(); cRow = 0; cColumn = 0; if (count < 4) { for (int i = 0; i < count; i++) { cRow += CircleCenterRow[i]; cColumn += CircleCenterCol[i]; } HOperatorSet.GenCrossContourXld(out centerCont, cRow / count, cColumn / count, 150, 0); } else if (count == 4) { for (int i = 0; i < 2; i++) { cRow += CircleCenterRow[i]; cColumn += CircleCenterCol[i]; } HOperatorSet.GenCrossContourXld(out centerCont, cRow / 2, cColumn / 2, 150, 0); } #endregion //5.0X轴到圆的中心连线的角度 HTuple CircleRowAdj = new HTuple(); CircleRowAdj = CircleCenterRow.Clone(); //5.1调整行坐标 MyVisionBase.AdjImgRow(ImgIn, ref CircleRowAdj); if (CircleRowAdj.Length >= 2) { if (CircleCenterCol[0].D < CircleCenterCol[1].D) { HOperatorSet.AngleLl(0, 0, 1, 0, CircleCenterCol[0].D, CircleRowAdj[0].D, CircleCenterCol[1].D, CircleRowAdj[1].D, out AngleLxPixel); //圆中心连线与相机坐标X轴之间的夹角 } else { HOperatorSet.AngleLl(0, 0, 1, 0, CircleCenterCol[1].D, CircleRowAdj[1].D, CircleCenterCol[0].D, CircleRowAdj[0].D, out AngleLxPixel); } } else if (CircleRowAdj.Length == 1) { AngleLxPixel = 0; } VectorAngleOut = new St_VectorAngle(cRow / count, cColumn / count, AngleLxPixel); if (double.IsNaN(cColumn / count) || double.IsNaN(cRow / count)) { Logger.PopError("计算出错,非数字"); return(false); } return(true); #endregion } catch (Exception e0) { Logger.PopError(e0.Message + e0.Source); return(false); } }