private void btnFindTemp_Click(object sender, EventArgs e) { CPKTools tools = new CPKTools(); VisionImage align = this.image_Edit.Image; VisionImage image = Form_Main.Instance.imageSet.Image; Roi roi = Form_Main.Instance.imageSet.Roi; short Score = 600; double MinR = -10; double MaxR = 10; short rtn = 0; Variable.CamReturn camReturn = new Variable.CamReturn(); // 初步匹配 rtn = Form_Main.Instance.CamDetect_SearchGeometric(image, align, roi, Score, 1, MinR, MaxR, 100, 100, 0, 25, ref camReturn, 0, 0); if (rtn != 0) { MessageBox.Show("寻找模板失败,请检查参数!!!"); } // 记录ROI 的坐标 if (roi.Count > 0) { RectangleContour rect = (RectangleContour)roi[0].Shape; // save rect string strRoi = string.Format("{0},{1},{2},{3}", rect.Top, rect.Left, rect.Width, rect.Height); this.cpkIni.IniWriteValue("vision", "TemplateRoi", strRoi); } }
private void btnFindTemp2_Click(object sender, EventArgs e) { Variable.CamReturn cam = new Variable.CamReturn(); if (VisionHelper.FindNccTemplate(Form_Main.Instance.imageSet.Image, Form_Main.Instance.imageSet.Roi, ModelID, 1, 0.6, 0, 0, 1, 1, ref cam)) { VisionHelper.ShowResult(Form_Main.Instance.imageSet, cam); btnFindTemp2.BackColor = Color.Transparent; } else { btnFindTemp2.BackColor = Color.Red; } }
private void button1_Click(object sender, EventArgs e) { PointF curPos = Form_Main.VariableSys.pReadyPoint; CameraDefine.Instance[CAM.Bottom1]._Session.Snap(parent.imageSet.Image); Variable.CamReturn cam = Form_Main.Instance.Auto_Detect1(ref this.Feeder.Label, parent.imageSet.Image, CAM.Bottom1, 0); if (cam.IsOK) { PointF temp = parent.Point2CCDCenter(curPos, new PointContour(cam.X, cam.Y), CAM.Bottom1, 0); PointF rotateCenter = parent.Point2CCDCenter(curPos, Form_Main.VariableSys.pDownRotateCenter[0], CAM.Bottom1, 0); suckPos.X = temp.X - rotateCenter.X; suckPos.Y = temp.Y - rotateCenter.Y; this.nSuckPosX.Value = (decimal)suckPos.X; this.nSuckPosY.Value = (decimal)suckPos.Y; } }
public static void ShowResult(ImageViewer View, Variable.CamReturn camreturn) { PointF Center = new PointF(); Center.X = (float)camreturn.X; Center.Y = (float)camreturn.Y; double R = camreturn.Angle; //center cross View.Image.Overlays.Default.AddLine(new LineContour(new PointContour(Center.X - 30, Center.Y), new PointContour(Center.X + 30, Center.Y)), Rgb32Value.YellowColor); View.Image.Overlays.Default.AddLine(new LineContour(new PointContour(Center.X, Center.Y - 30), new PointContour(Center.X, Center.Y + 30)), Rgb32Value.YellowColor); PointF a = new PointF(); PointF A = new PointF(); a.X = Center.X + 500; a.Y = Center.Y; Form_Main.Instance.PtRotate(a, Center, R, out A); //Line Direction View.Image.Overlays.Default.AddLine(new LineContour(new PointContour(Center.X, Center.Y), new PointContour(A.X, A.Y)), Rgb32Value.BlueColor); }
private void bStartChkCailb_Jig_Click(object sender, EventArgs e) { if (!bRun) { this.bStartChkCailb_Jig.Text = "正在测量..."; ChkPoint_Jig_Act = new List <PointF>(); bRun = true; double col = double.Parse(this.textCol_Jig.Text); double row = double.Parse(this.textRow_Jig.Text); double rMin = double.Parse(this.tMinR.Text); double rMax = double.Parse(this.tMaxR.Text); double gian = double.Parse(this.tGainValue.Text); double offset = double.Parse(this.tOffsetValue.Text); if (!Directory.Exists(Variable.sPath_SYS_Jig)) { Directory.CreateDirectory(Variable.sPath_SYS_Jig); } if (File.Exists(Variable.sPath_SYS_Jig + "Pt_Act.txt")) { File.Delete(Variable.sPath_SYS_Jig + "Pt_Act.txt"); } if (File.Exists(Variable.sPath_SYS_Jig + "Pt_Machine.txt")) { File.Delete(Variable.sPath_SYS_Jig + "Pt_Machine.txt"); } if (Thd_Cailb == null || !Thd_Cailb.IsAlive) { Thd_Cailb = new Thread(() => { Variable.CamReturn cam = new Variable.CamReturn(); Form_Main.Instance.StartGrabImage(); for (int i = 0; i < chkPoint_Jig.Count; i++) { #region 开始测量 try { if (!bRun) { MessageBox.Show("停止侦测", "提示"); return; } PointF Pt_Act = new PointF(); Pt_Act.X = (float)Math.Floor(i / col); Pt_Act.Y = (float)(i - col * Pt_Act.X); ChkPoint_Jig_Act.Add(Pt_Act); PointF Pt_Machine = new PointF(); Form_Main.Instance.XYGoPosTillStop(20 * 1000, this.chkPoint_Jig[i], variable); // 获得上相机图像 VisionImage image = Form_Main.Instance.GrabImage2View(Camera.CAM.Top); image = Form_Main.Instance.GainOffset(image, gian, offset); Algorithms.Copy(image, Form_Main.Instance.imageSet.Image); PointContour centr = new PointContour(); double rad = 0; if (VisionHelper.DetectCircle(Form_Main.Instance.imageSet.Image, Form_Main.Instance.imageSet.Roi, rMin, rMax, out centr, out rad)) { cam.X = centr.X; cam.Y = centr.Y; cam.IsOK = true; VisionHelper.ShowResult(Form_Main.Instance.imageSet, cam); Pt_Machine = Form_Main.Instance.Point2CCDCenter(this.chkPoint_Jig[i], new PointContour(cam.X, cam.Y), 0, 0); } else { throw new Exception("寻找模板失败"); } //if (VisionHelper.FindNccTemplate(Form_Main.Instance.imageSet.Image, Form_Main.Instance.imageSet.Roi, ModelID, 1, 0.5, 0, 0, 1, 1, ref cam)) //{ // VisionHelper.ShowResult(Form_Main.Instance.imageSet, cam); // Pt_Machine = Form_Main.Instance.Point2CCDCenter(this.chkPoint_Jig[i], new PointContour(cam.X, cam.Y), 0); //} //else //{ // throw new Exception("寻找模板失败"); //} ChkPoint_Jig_Act.Add(Pt_Machine); StreamWriter sw = File.AppendText(Variable.sPath_SYS_Jig + "Pt_Act.txt"); sw.Write(Pt_Act.X.ToString("F3") + "," + Pt_Act.Y.ToString("F3") + "\r\n"); sw.Close(); StreamWriter sw1 = File.AppendText(Variable.sPath_SYS_Jig + "Pt_Machine.txt"); sw1.Write(Pt_Machine.X.ToString("F3") + "," + Pt_Machine.Y.ToString("F3") + "\r\n"); sw1.Close(); } catch (Exception ex) { if (MessageBox.Show($"侦测失败:{ex.Message} 是否重新识别 Y/N ", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes) { i--; continue; } else { MessageBox.Show("玻璃杯校验停止!!!"); bRun = false; return; } } #endregion } }); Thd_Cailb.Start(); } } else { if (MessageBox.Show("是否停止测量", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes) { this.bStartChkCailb_Jig.Text = "开始测量"; bRun = false; } } }
public static void FindShapeModel(HObject image, RectangleContour roi, HTuple modeID, ref Variable.CamReturn cam, HTuple windowTuple = null, double minScore = 0.7, int matchNum = 1) { minScore = 0.5; HTuple row, column, angle, scaleR, scaleC, score; HObject reduceImage = new HObject(); try { HRegion region = new HRegion(); if (roi == null) { reduceImage = image; } else { region = new HRegion(roi.Top, roi.Left, roi.Top + roi.Height, roi.Left + roi.Width); HOperatorSet.ReduceDomain(image, region, out reduceImage); } HOperatorSet.FindAnisoShapeModel(reduceImage, modeID, startAngle, endAngle, 0.8, 1.2, 0.8, 1.2, minScore, matchNum, 0.5, "least_squares", 0, 0.9, out row, out column, out angle, out scaleR, out scaleC, out score); image?.Dispose(); if (score.Length > 0) { cam.XScale = scaleC.D; cam.YScale = scaleR.D; cam.Y = row[0].D; cam.X = column[0].D; cam.Angle = new HTuple(angle[0].D).TupleDeg(); if (Math.Abs(cam.Angle) > 45 /*90 */) { if (cam.Angle > 0) { cam.Angle -= 180; } else { cam.Angle += 180; } } cam.Angle = -cam.Angle; cam.IsOK = true; if (windowTuple != null) { HTuple hv_HomMat2D; HObject ho_Contours, ho_TransContours; HOperatorSet.VectorAngleToRigid(0, 0, 0, row[0], column[0], angle[0], out hv_HomMat2D); HOperatorSet.GetShapeModelContours(out ho_Contours, modeID, 1); HOperatorSet.AffineTransContourXld(ho_Contours, out ho_TransContours, hv_HomMat2D); HOperatorSet.SetColor(windowTuple, "red"); HOperatorSet.DispObj(ho_TransContours, windowTuple); ho_Contours?.Dispose(); ho_TransContours?.Dispose(); } } else { cam.IsOK = false; } reduceImage?.Dispose(); image?.Dispose(); } catch { cam.IsOK = false; reduceImage?.Dispose(); image?.Dispose(); } }
private void bAutoSet_Click(object sender, EventArgs e) { if (this.cbLabelIndex.SelectedIndex < 0) { return; } int labelIndex = this.cbLabelIndex.SelectedIndex; //Task.Factory.StartNew(() => //{ try { var vel = Form_Main.VariableSys.VelMode_Current_Manual; parent.All_ZGoSafeTillStop(3000, vel); parent.XYGoPosTillStop(5000, Form_Main.VariableSys.pReadyPoint, vel); parent.Turn.GoPosTillStop(5000, Form_Main.VariableSys.dTurnXIAngle, vel); PointF camPt = new PointF(); camPt.X = float.Parse(this.FeederCtrl.Rows[labelIndex].Cells[0].Value.ToString()); camPt.Y = float.Parse(this.FeederCtrl.Rows[labelIndex].Cells[1].Value.ToString()); PointF nzPt = parent.Nz1ToOther(camPt, this.Nozzle); double zHeight = this.Feeder.XIHeight[labelIndex][this.Nozzle]; double xiAngle = this.Feeder.XI_Degree[this.Nozzle]; Z_RunParam zParam = parent.Z_RunParamMap[(uint)this.Nozzle]; R_RunParam rParam = parent.R_RunParamMap[(uint)this.Nozzle]; zParam.PO_vaccum.ResetIO(); zParam.XI_vaccum.ResetIO(); parent.XYGoPosTillStop(5000, nzPt, vel); rParam.GoPosTillStop(5000, xiAngle, vel); if (this.Feeder.bReachXI) { zParam.XI_vaccum.SetIO(); } else { zParam.XI_vaccum.ResetIO(); } zParam.GoPosTillStop(5000, zHeight, vel); zParam.XI_vaccum.SetIO(); Thread.Sleep(Form_Main.VariableSys.iDelay_BeforeXI); var camera = Form_Main.Nozzle2Cam(this.Nozzle); parent.LightON_Down_PASTE1(ref this.Feeder.Label); parent.SetShutter((int)this.Feeder.Label.Shutter1, camera.Item1); parent.All_ZGoSafeTillStop(5000, vel); if (!parent.All_ZReachOrg()) { return; } parent.XYGoPosTillStop(5000, Form_Main.VariableSys.pReadyPoint, vel); parent.Turn.GoPosTillStop(5000, Form_Main.VariableSys.dTurnPasteAngle, vel); // 拍照 Thread.Sleep(Form_Main.VariableSys.iDownCamDelay); PointF curPos = Form_Main.VariableSys.pReadyPoint; CameraDefine.Instance[camera.Item1]._Session.Snap(parent.imageSet.Image); Variable.CamReturn cam = Form_Main.Instance.Auto_Detect1(ref this.Feeder.Label, parent.imageSet.Image, camera.Item1, this.Nozzle); if (cam.IsOK) { // 识别出来的位置 PointF temp = parent.Point2CCDCenter(curPos, new PointContour(cam.X, cam.Y), camera.Item1, camera.Item2); PointF rotateCenter = parent.Point2CCDCenter(curPos, Form_Main.VariableSys.pDownRotateCenter[0], camera.Item1, camera.Item2); /// 识别与吸嘴旋转中心的误差 PointF offset = new PointF(); offset.X = temp.X - rotateCenter.X; offset.Y = temp.Y - rotateCenter.Y; this.nOffsetX.Value = (decimal)offset.X; this.nOffsetY.Value = (decimal)offset.Y; } } catch { } //}); }
/// <summary> /// NCC模板匹配-Roi-HObject /// </summary> /// <param name="img"></param> /// <param name="Roi"></param> /// <param name="ModelID"></param> /// <param name="MaxReturns"></param> /// <param name="MinScore"></param> /// <param name="MinAngle"></param> /// <param name="MaxAngle"></param> /// <param name="MinScal"></param> /// <param name="MaxScal"></param> /// <param name="matchresults"></param> /// <returns></returns> public static bool FindNccTemplate(VisionImage img, Roi Roi, HTuple ModelID, int MaxReturns, double MinScore, double MinAngle, double MaxAngle, double MinScal, double MaxScal, ref Variable.CamReturn camreturn)//, double MaxOverlap, double Greediness) { img.Type = ImageType.U8; HObject Himage = null, HRoi = null; HObject imgreduced = null; HTuple FindRow, FindColumn, FindAngle, FindScore; try { img.BorderWidth = 0; HOperatorSet.GenImage1(out Himage, "byte", img.Width, img.Height, img.StartPtr); //HOperatorSet.WriteImage(Himage, "bmp", 0, "D:\\1.bmp"); HOperatorSet.GenRectangle1(out HRoi, ((RectangleContour)Roi[0].Shape).Top, ((RectangleContour)Roi[0].Shape).Left, ((RectangleContour)Roi[0].Shape).Top + ((RectangleContour)Roi[0].Shape).Height, ((RectangleContour)Roi[0].Shape).Left + ((RectangleContour)Roi[0].Shape).Width); HOperatorSet.ReduceDomain(Himage, HRoi, out imgreduced);//image_rectified HOperatorSet.FindNccModel(imgreduced, ModelID, (new HTuple(MinAngle)).TupleRad(), (new HTuple(MaxAngle)).TupleRad(), MinScore, MaxReturns, 0.5, "true", 0, out FindRow, out FindColumn, out FindAngle, out FindScore); if (FindRow.Length == 0) { camreturn.X = 0; camreturn.Y = 0; camreturn.Angle = 0; camreturn.IsOK = false; Himage.Dispose(); HRoi.Dispose(); imgreduced.Dispose(); return(false); } else { camreturn.X = float.Parse(FindColumn[0].D.ToString()); camreturn.Y = float.Parse(FindRow[0].D.ToString()); camreturn.Angle = FindAngle[0].D; camreturn.IsOK = true; Himage.Dispose(); HRoi.Dispose(); imgreduced.Dispose(); return(true); } } catch (Exception a) { //PutInLog("玻璃板错误:" + a.Message); camreturn.X = 0; camreturn.Y = 0; camreturn.Angle = 0; camreturn.IsOK = false; //HOperatorSet.ClearObj(Himage); //HOperatorSet.ClearObj(HRoi); //HOperatorSet.ClearObj(imgreduced); if (Himage != null) { Himage.Dispose(); } if (HRoi != null) { HRoi.Dispose(); } if (imgreduced != null) { imgreduced.Dispose(); } return(false); } }