/// <summary> /// 获得点的实际坐标 /// </summary> /// <param name="markCenter"></param> /// <returns></returns> public PointH GetMarkAxis(PointH markCenter) { HTuple hv_Tx = null, hv_Ty = null; try { HOperatorSet.AffineTransPoint2d(hv_HomMat2D, markCenter.X, markCenter.Y, out hv_Tx, out hv_Ty); return(new PointH(hv_Tx[0].D, hv_Ty[0].D)); } catch { return(new PointH()); } }
/// <summary> /// 九点校正的CAD坐标 /// </summary> /// <param name="strPath"></param> /// <returns></returns> public bool GetCADData(string strPath) { CADAllData = new PointH[11, 11]; cadCenterData = new PointH(); try { using (FileStream fs = new FileStream(strPath, FileMode.Open, FileAccess.Read)) { using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("utf-8"))) { string strReadline; while ((strReadline = sr.ReadLine()) != null) { if (strReadline.IndexOf(CCDName) >= 0) { string camindex = strReadline.Substring(3, 1); for (int i = 0; i < 11; i++) { strReadline = sr.ReadLine(); string[] strx = strReadline.Split(','); strReadline = sr.ReadLine(); string[] stry = strReadline.Split(','); if (strx.Length != stry.Length || strx.Length != 11) { MessageBox.Show(CCDName + "读取CAD坐标失败"); halconCCD1_event_StatusText("读取CAD坐标失败", true); return(false); } for (int j = 0; j < strx.Length; j++) { double xx = strx[j] == "" ? 0.0 : Convert.ToDouble(strx[j]); double yy = stry[j] == "" ? 0.0 : Convert.ToDouble(stry[j]); PointH pointh = new PointH(xx, yy); CADAllData[j, i] = pointh; } } } } } } cadCenterData = CADAllData[5, 5]; return(true); } catch (Exception ex) { halconCCD1_event_StatusText("获取CAD坐标异常:" + ex.ToString()); ShowStatusForm("获取CAD坐标异常", Color.Red); CADAllData = new PointH[11, 11]; cadCenterData = new PointH(); return(false); } }
private void DispMMPixel() { try { this.Invoke(new EventHandler(delegate { lbl_X.Text = "X:0.0000 um/pixel"; lbl_Y.Text = "Y:0.0000 um/pixel"; PointH pm = halconHelp.GetPixelMM(); lbl_X.Text = "X:" + (pm.X * 1000).ToString("0.0000") + " um/pixel"; lbl_Y.Text = "Y:" + (pm.Y * 1000).ToString("0.0000") + " um/pixel"; })); } catch (Exception ex) { } }
public void InitCCD() { if (m_nCameraType == 1) //AVT相机 { if (m_AVTName == "") { halconCCD1_event_StatusText("未设置AVT相机编号,初始化失败", true); ShowStatusForm(this.CCDName + "-" + "未设置AVT相机编号,初始化失败", Color.Red); return; } } halconCCD1.InitCCD(); halconCCD1.SetCCDExposure(m_ExposureProduct); //初始化相机,设置曝光值 point1 = new PointH(halconCCD1.SearchRec_x1, halconCCD1.SearchRec_y1); point2 = new PointH(halconCCD1.SearchRec_x2, halconCCD1.SearchRec_y2); }
/// <summary> /// 模板中心补偿 /// </summary> /// <param name="offset">补偿值</param> /// <param name="Center">模板中心坐标</param> /// <param name="angle">旋转角度</param> /// <param name="isClockwise">true:顺时针(默认)/false:逆时针</param> /// <returns>旋转后坐标</returns> public static PointH DeviationCalc(PointH offset, PointH Center, double angle) { try { if (angle > 180) { angle -= 360; } PointH _P = new PointH(offset.X, offset.Y); //待旋转点 PointH _Center = new PointH(Center.X, Center.Y); //旋转中心 bool isClockwise = (angle >= 0 && angle <= 180) ? false : true; //顺时、逆时针旋转 double rad = Math.PI / 180 * (isClockwise ? -angle : angle); PointH newP = RotatePoint(_P, _Center, rad, isClockwise); PointH point = new PointH((float)newP.X, (float)newP.Y); return(point); //Y轴坐标系再取反 } catch { return(new PointH()); } }
/// <summary> /// 返回点P围绕点A旋转弧度rad后的坐标 /// </summary> /// <param name="P">待旋转点坐标</param> /// <param name="Center">旋转中心坐标</param> /// <param name="rad">旋转弧度</param> /// <param name="isClockwise">true:顺时针(默认)/false:逆时针</param> /// <returns>旋转后坐标</returns> private static PointH RotatePoint(PointH P, PointH Center, double rad, bool isClockwise = true) { //点Temp1 PointH Temp1 = new PointH(P.X - Center.X, P.Y - Center.Y); //点Temp1到原点的长度 double lenO2Temp1 = Temp1.DistanceTo(new PointH(0, 0)); //∠T1OX弧度 double angT1OX = radPOX(Temp1.X, Temp1.Y); //∠T2OX弧度(T2为T1以O为圆心旋转弧度rad) double angT2OX = angT1OX - (isClockwise ? 1 : -1) * rad; //点Temp2 PointH Temp2 = new PointH( lenO2Temp1 * Math.Cos(angT2OX), lenO2Temp1 * Math.Sin(angT2OX)); //点Q return(new PointH(Temp2.X + Center.X, Temp2.Y + Center.Y)); }
//该点到指定点pTarget的距离 public double DistanceTo(PointH p) { return(Math.Sqrt((p.X - X) * (p.X - X) + (p.Y - Y) * (p.Y - Y))); }
private void btn_config_Click(object sender, EventArgs e) { if (GlobalVar.m_bChangeForm) { if (this.changeForm) { this.changeForm = false; this.BackColor = Color.DarkGray; btn_config.BackColor = Color.DarkGray; return; } this.changeForm = true; this.BackColor = Color.Green; btn_config.BackColor = Color.Green; if (m_eventChangeForm != null) { m_eventChangeForm(sequence); } return; } CCDParaConfig paraconfig = new CCDParaConfig(this); if (paraconfig.ShowDialog() == DialogResult.OK) { m_AVTName = paraconfig.AVTName; AreaMark[0] = paraconfig.MarkMinArea; AreaMark[1] = paraconfig.MarkMaxArea; AreaPoint[0] = paraconfig.PointMinArea; AreaPoint[1] = paraconfig.PointMaxArea; AreaProduct[0] = paraconfig.ProductMinArea; AreaProduct[1] = paraconfig.ProductMaxArea; m_ExposureProduct = paraconfig.m_ExposureProduct; m_ExposureModel = paraconfig.m_ExposureModel; m_UmPixel = paraconfig.m_UmPixel; point1 = new PointH(paraconfig.m_roiX1, paraconfig.m_roiY1); point2 = new PointH(paraconfig.m_roiX2, paraconfig.m_roiY2); halconCCD1.SearchRec_x1 = paraconfig.m_roiX1; halconCCD1.SearchRec_y1 = paraconfig.m_roiY1; halconCCD1.SearchRec_x2 = paraconfig.m_roiX2; halconCCD1.SearchRec_y2 = paraconfig.m_roiY2; WriteINI(); if (m_bIn9Point) { halconHelp.SetMarkArea(AreaMark); halconHelp.SetPointArea(AreaPoint); halconCCD1.SetCCDExposure(m_ExposureModel); //修改曝光值-校准片 } else { if (GlobalVar.m_bInCalibMode) { halconHelp.SetMarkArea(AreaMark); halconHelp.SetPointArea(AreaPoint); halconCCD1.SetCCDExposure(m_ExposureModel); //修改曝光值-校准片 } else { halconHelp.SetMarkArea(AreaProduct); halconHelp.SetPointArea(AreaPoint); halconCCD1.SetCCDExposure(m_ExposureProduct); //修改曝光值-制品 } } } }
public void StartHanle(HImage himage) { //m_bInHandleImage = true; //2018.05.25 改为多线程,在拍照前置为True m_MarkCenter = new PointH(); m_MarkAxis = new PointH(); try { HImage handleImage = himage; //if (GlobalVar.gl_strProductModel.ToUpper().IndexOf("A85IFLEX") >= 0) if (!GlobalVar.m_bUseXLD) { m_MarkCenter = halconHelp.GetMarkCenter(handleImage); } else { m_MarkCenter = halconHelp.GetMarkCenterXLD(handleImage); } if (m_MarkCenter.X == 0.0 || m_MarkCenter.Y == 0.0) { m_MarkAxis = new PointH(); } else { m_MarkAxis = halconHelp.GetMarkAxis(m_MarkCenter); } if (m_bIn9Point) { #region 九点标定处理 m_bHomMat2DOK = false; try { if (m_MarkCenter.X == 0.0 || m_MarkCenter.Y == 0.0) { m_bHomMat2DOK = false; return; } HTuple hv_Row, hv_Column, hv_AxisX, hv_AxisY, hv_Height, hv_Width, hv_newRow, hv_newColumn; halconHelp.Get9Points(handleImage, out hv_Row, out hv_Column, out hv_Height, out hv_Width); GetAxisData(hv_Row, hv_Column, hv_Height, hv_Width, out hv_newRow, out hv_newColumn, out hv_AxisY, out hv_AxisX); //int length = hv_newRow.Length; //hv_newRow[length] = m_MarkCenter.Y; //hv_newColumn[length] = m_MarkCenter.X; //hv_AxisY[length] = cadCenterData.Y; //hv_AxisX[length] = cadCenterData.X; for (int i = 0; i < hv_newRow.Length; i++) { halconHelp.disp_message(hv_DispWindown, (i + 1), "image", hv_newRow[i].D, hv_newColumn[i].D, "red", "true"); } int countMY = hv_newRow.Length, countMX = hv_newColumn.Length, countAY = hv_AxisY.Length, countAX = hv_AxisX.Length; if (countMY < 9 || countMX < 9 || countAY < 9 || countAX < 9 || countAX != countAY || countAX != countMX || countMX != countMY) { m_bHomMat2DOK = false; halconCCD1_event_StatusText("标定相机异常: 标定点过少或个数不匹配", true); GlobalVar.m_bNeedReCalc = true; //需要重新标定相机 GlobalVar.m_timeLast9Point = GlobalVar.m_timeLast9Point.AddHours((GlobalVar.m_CalibSpace + 12) * -1); string strfile = Application.StartupPath + "\\CONFIG\\Config.ini"; CommonFunc.Write(ini_Section_TestInfo, ini_Key_Last9Point, GlobalVar.m_timeLast9Point.ToString("yyyy-MM-dd HH:mm:ss.fff"), strfile); return; } HOperatorSet.WriteTuple(hv_newRow, m_pathConfigTup + "\\" + CCDName + "MarkY.tup"); HOperatorSet.WriteTuple(hv_newColumn, m_pathConfigTup + "\\" + CCDName + "MarkX.tup"); HOperatorSet.WriteTuple(hv_AxisY, m_pathConfigTup + "\\" + CCDName + "AxisY.tup"); HOperatorSet.WriteTuple(hv_AxisX, m_pathConfigTup + "\\" + CCDName + "AxisX.tup"); GetHomMat2D(); } catch (Exception ex) { m_bHomMat2DOK = false; halconCCD1_event_StatusText("标定相机异常: " + ex.ToString(), true); ShowStatusForm("标定相机异常", Color.Red); } #endregion } } catch (Exception ex) { halconCCD1_event_StatusText("图片处理异常: " + ex.ToString(), true); } finally { m_bInHandleImage = false; DispCenterPoint(); } }