using HalconDotNet; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Community; using Model; using Model.Vision; using System.Windows; using System.Collections.ObjectModel; namespace ViewModel.Vision { /// /// 测量VM /// partial class HalconVisual { /// /// 边缘测量编辑 /// public DelegateCommand EdgeMeasureEditCommand { get; set; } /// /// 边缘测量测试 /// public DelegateCommand EdgeMeasureTestCommand { get; set; } /// /// 测量数据保存 /// public DelegateCommand MeasureSaveCommand { get; set; } #region 高斯平滑的Sigma private double m_Sigma; /// /// 高斯平滑的Sigma /// public double M_Sigma { get { return m_Sigma; } set { m_Sigma = value; if (ContinuousChange) { SelectModel.MyROI.M_Sigma = value; MeasureLineEdge(SelectModel); } this.RaisePropertyChanged("M_Sigma"); } } #endregion #region 最小边缘幅度 private double m_Threshold; /// /// 最小边缘幅度 /// public double M_Threshold { get { return m_Threshold; } set { m_Threshold = value; if (ContinuousChange) { SelectModel.MyROI.M_Threshold = value; MeasureLineEdge(SelectModel); } this.RaisePropertyChanged("M_Threshold"); } } #endregion #region 边缘分组方式 private string m_Transition; /// /// 边缘分组方式,值列表:'all','positive','negative','all_strongest','positive_strongest','negative_strongest' /// public string M_Transition { get { return m_Transition; } set { m_Transition = value; if (ContinuousChange) { SelectModel.MyROI.M_Transition = value; MeasureLineEdge(SelectModel); } this.RaisePropertyChanged("M_Transition"); } } private ObservableCollection m_TransitionList; /// /// 边缘分组类型List /// public ObservableCollection M_TransitionList { get { return m_TransitionList; } set { m_TransitionList = value; this.RaisePropertyChanged("M_TransitionList"); } } #endregion #region 选择边缘对的方式 private string m_Select; /// /// 选择边缘对的方式 值列表:'all','first','last' /// public string M_Select { get { return m_Select; } set { m_Select = value; if (ContinuousChange) { SelectModel.MyROI.M_Select = value; MeasureLineEdge(SelectModel); } this.RaisePropertyChanged("M_Select"); } } private ObservableCollection m_SelectList; /// /// 边缘选择方式List /// public ObservableCollection M_SelectList { get { return m_SelectList; } set { m_SelectList = value; this.RaisePropertyChanged("M_SelectList"); } } #endregion #region 插值方法 private string m_Interpolation; /// /// 插值方法 默认值是 nearest_neighbor。是最近点的意思。 还可以写bilinear(双线性),bicubic(双三次) /// public string M_Interpolation { get { return m_Interpolation; } set { m_Interpolation = value; if (ContinuousChange) { SelectModel.MyROI.M_Interpolation = value; MeasureLineEdge(SelectModel); } this.RaisePropertyChanged("M_Interpolation"); } } private ObservableCollection m_InterpolationList; /// /// 插值方法List /// public ObservableCollection M_InterpolationList { get { return m_InterpolationList; } set { m_InterpolationList = value; this.RaisePropertyChanged("M_Interpolation"); } } #endregion /// /// 是否可以进行,参数变化实时调整 /// bool ContinuousChange { get; set; } #region 测量初始化 /// /// 测量初始化 /// public void MeasureInitialize() { M_Sigma = 1; M_Threshold = 30; M_Transition = "all"; M_Select = "all"; M_Interpolation = "nearest_neighbor"; //值列表:'all','positive','negative','all_strongest','positive_strongest','negative_strongest' M_TransitionList = new ObservableCollection() { new CommonClass() { ID=1, CommonName="all", ShortCode= "all" }, new CommonClass() { ID=1, CommonName="positive", ShortCode= "positive" }, new CommonClass() { ID=1, CommonName="negative", ShortCode= "negative" }, new CommonClass() { ID=1, CommonName="all_strongest", ShortCode= "all_strongest" }, new CommonClass() { ID=1, CommonName="positive_strongest", ShortCode= "positive_strongest" }, new CommonClass() { ID=1, CommonName="negative_strongest", ShortCode= "negative_strongest" }}; //值列表:'all','first','last' M_SelectList = new ObservableCollection() { new CommonClass() { ID=1, CommonName="all", ShortCode= "all" }, new CommonClass() { ID=1, CommonName="first", ShortCode= "first" }, new CommonClass() { ID=1, CommonName="last", ShortCode= "last" }}; //提取边缘最近点作为参考像素, 默认值是 nearest_neighbor。是最近点的意思。 还可以写bilinear(双线性),bicubic(双三次) M_InterpolationList = new ObservableCollection() { new CommonClass() { ID=1, CommonName="nearest_neighbor", ShortCode= "nearest_neighbor" }, new CommonClass() { ID=1, CommonName="bilinear", ShortCode= "bilinear" }, new CommonClass() { ID=1, CommonName="bicubic", ShortCode= "bicubic" }}; EdgeMeasureEditCommand = new DelegateCommand(); EdgeMeasureEditCommand.ExecuteAction = new Action(EdgeMeasureEdit); EdgeMeasureTestCommand = new DelegateCommand(); EdgeMeasureTestCommand.ExecuteAction = new Action(EdgeMeasureTest); MeasureSaveCommand = new DelegateCommand(); MeasureSaveCommand.ExecuteAction = new Action(MeasureSave); } #endregion #region 边缘测量编辑 /// /// 边缘测量编辑 /// /// private void EdgeMeasureEdit(object obj) { if (SelectModel.ID <= 0) { if (SaveAll(SelectModel)) { SelectModel = Vdal.GetRectangleModel(SelectModel.VeiID, SelectModel.AreaType); if (SelectModel.ID > 0) { CreateLineEdgeMeasure(SelectModel); ContinuousChange = true; } else { MessageBox.Show("数据读取错误"); } } } else { CreateLineEdgeMeasure(SelectModel); ContinuousChange = true; } } #endregion #region 边缘测量测试 /// /// 边缘测量测试 /// /// private void EdgeMeasureTest(object obj) { if (SelectModel.ID > 0) { ImplementMeasure(SelectVEI); ContinuousChange = true; } } #endregion #region 测量参数保存 /// /// 测量参数保存 /// /// private void MeasureSave(object obj) { SelectModel.MyROI.MeasureParMerge(); if (SaveAll(SelectModel)) { SelectModel = Vdal.GetRectangleModel(SelectModel.VeiID, SelectModel.AreaType); } } #endregion #region 创建直线边缘测量 /// /// 创建直线边缘测量 /// /// private void CreateLineEdgeMeasure(V_RectangleModel RM) { if (RM == null || RM.MyROI == null) { ShowTextPer("测量对象不能为空", "red"); return; } //1、获取图像 if (!GetImage()) { CreateModeling = false; return; } if (!ImageCorrecting(Ho_Image, SelectVEI.ImageCorrectionRM)) { HOperatorSet.WriteString(HWindowWPF.HalconWindow, "图像校正失败"); return; } Ho_Image = CorrectingImage; ShowObj(CorrectingImage); #region 2、绘制ROI ClearWindowAndData(); ShowObj(Ho_Image); //绘制区域 RM = DrawArea(Ho_Image, RM); //强制选择任意矩形绘制ROI DrawRoiShape = DrawRoiEmn.R2; //绘制ROI DrawROI(RM); #endregion MeasureLineEdge(RM); } #endregion #region 执行测量 /// /// 执行测量 /// /// public void ImplementMeasure(VisualEngineeringItems VEI) { V_RectangleModel RM = VEI.MyRectangleModel; #region 1、获取图像 if (!GetImage()) { CreateModeling = false; return; } #endregion if (!ImageCorrecting(Ho_Image, VEI.ImageCorrectionRM)) { HOperatorSet.WriteString(HWindowWPF.HalconWindow, "图像校正失败"); return; } Ho_Image = CorrectingImage; RM.MyROI.MeasureParSplit(); MeasureLineEdge(RM); } #endregion #region 直线边缘测量 /// /// 直线边缘测量 /// private void MeasureLineEdge(V_RectangleModel RM) { HObject ho_ROI, ho_EdgeFirst = null, ho_ImageTemp = null; HObject ho_EdgeSecond = null; //实际测量数据 HTuple WorldRow1 = null, WorldColumn1 = null, WorldRow2 = null, WorldColumn2 = null, WorldWidthMeasure = null, WorldInterDistance = null; HTuple hv_Width = null, hv_Height = null; HTuple hv_Row = null, hv_Column = null, hv_Length1 = null; HTuple hv_Length2 = null, hv_Phi = null, hv_MeasureHandle = null; HTuple hv_RowEdgeFirst = null, hv_ColumnEdgeFirst = null; HTuple hv_AmplitudeFirst = null, hv_RowEdgeSecond = null; HTuple hv_ColumnEdgeSecond = null, hv_AmplitudeSecond = null; HTuple hv_WidthMeasure = null, hv_InterDistance = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_ImageTemp); HOperatorSet.GenEmptyObj(out ho_ROI); HOperatorSet.GenEmptyObj(out ho_EdgeFirst); HOperatorSet.GenEmptyObj(out ho_EdgeSecond); try { HOperatorSet.GetImageSize(Ho_Image, out hv_Width, out hv_Height); hv_Row = RM.MyROI.hv_Row; hv_Column = RM.MyROI.hv_Column; hv_Phi = RM.MyROI.hv_Phi; hv_Length1 = RM.MyROI.hv_length1; hv_Length2 = RM.MyROI.hv_length2; //创建测量对象 //'nearest_neighbor',//提取边缘最近点作为参考像素, 默认值是 nearest_neighbor。是最近点的意思。 还可以写bilinear(双线性),bicubic(双三次) //HOperatorSet.GenMeasureRectangle2(hv_Row, hv_Column, hv_Phi, hv_Length1, hv_Length2, hv_Width, hv_Height, "bilinear", out hv_MeasureHandle); HOperatorSet.GenMeasureRectangle2(hv_Row, hv_Column, hv_Phi, hv_Length1, hv_Length2, hv_Width, hv_Height, RM.MyROI.M_Interpolation, out hv_MeasureHandle); //测量 //HOperatorSet.MeasurePairs(Ho_Image, hv_MeasureHandle, 1, 1, "negative", "all", out hv_RowEdgeFirst, out hv_ColumnEdgeFirst, out hv_AmplitudeFirst, out hv_RowEdgeSecond, // out hv_ColumnEdgeSecond, out hv_AmplitudeSecond, out hv_IntraDistance, // out hv_InterDistance); HOperatorSet.MeasurePairs(Ho_Image, hv_MeasureHandle, RM.MyROI.M_Sigma, RM.MyROI.M_Threshold, RM.MyROI.M_Transition, RM.MyROI.M_Select, out hv_RowEdgeFirst, out hv_ColumnEdgeFirst, out hv_AmplitudeFirst, out hv_RowEdgeSecond, out hv_ColumnEdgeSecond, out hv_AmplitudeSecond, out hv_WidthMeasure, out hv_InterDistance); //是否显示像素测量值 bool showPixPar = true; //读取已标定文件 if (RM.ReadCalibrationPar(Camera.CamName)) { //像素坐标转换成世界坐标 HOperatorSet.AffineTransPoint2d(RM.hv_JiuDianCalibrationPar, hv_RowEdgeFirst, hv_ColumnEdgeFirst, out WorldRow1, out WorldColumn1); HOperatorSet.AffineTransPoint2d(RM.hv_JiuDianCalibrationPar, hv_RowEdgeSecond, hv_ColumnEdgeSecond, out WorldRow2, out WorldColumn2); //获取边缘对内之间的间距 HOperatorSet.DistancePp(WorldRow1, WorldColumn1, WorldRow2, WorldColumn2, out WorldWidthMeasure); //获取边缘对 对于对之间的间距 int count = WorldRow2.TupleLength(); if (count > 0) { HTuple hv_RowFrom, hv_ColumnFrom, hv_RowTo, hv_ColumnTo; HOperatorSet.TupleSelectRange(WorldRow2, 0, count - 2, out hv_RowFrom); HOperatorSet.TupleSelectRange(WorldColumn2, 0, count - 2, out hv_ColumnFrom); HOperatorSet.TupleSelectRange(WorldRow1, 1, count - 1, out hv_RowTo); HOperatorSet.TupleSelectRange(WorldColumn1, 1, count - 1, out hv_ColumnTo); HOperatorSet.DistancePp(hv_RowFrom, hv_ColumnFrom, hv_RowTo, hv_ColumnTo, out WorldInterDistance); } showPixPar = false; } //显示结果 ClearWindowAndData(); ShowObj(Ho_Image); for (int i = 0; i < hv_RowEdgeFirst.TupleLength(); i++) { HTuple hv_RowStart = hv_RowEdgeFirst[i] + (hv_Length2 * (hv_Phi.TupleCos())); HTuple hv_RowEnd = hv_RowEdgeFirst[i] - (hv_Length2 * (hv_Phi.TupleCos())); HTuple hv_ColStart = hv_ColumnEdgeFirst[i] + (hv_Length2 * (hv_Phi.TupleSin())); HTuple hv_ColEnd = hv_ColumnEdgeFirst[i] - (hv_Length2 * (hv_Phi.TupleSin())); ho_EdgeFirst.Dispose(); HOperatorSet.GenContourPolygonXld(out ho_EdgeFirst, hv_RowStart.TupleConcat(hv_RowEnd), hv_ColStart.TupleConcat(hv_ColEnd)); hv_RowStart = hv_RowEdgeSecond[i] + (hv_Length2 * (hv_Phi.TupleCos())); hv_RowEnd = hv_RowEdgeSecond[i] - (hv_Length2 * (hv_Phi.TupleCos())); hv_ColStart = hv_ColumnEdgeSecond[i] + (hv_Length2 * (hv_Phi.TupleSin())); hv_ColEnd = hv_ColumnEdgeSecond[i] - (hv_Length2 * (hv_Phi.TupleSin())); ho_EdgeSecond.Dispose(); HOperatorSet.GenContourPolygonXld(out ho_EdgeSecond, hv_RowStart.TupleConcat(hv_RowEnd), hv_ColStart.TupleConcat(hv_ColEnd)); HOperatorSet.SetLineWidth(HWindowWPF.HalconWindow, 2); ShowObj(ho_EdgeFirst, "cyan"); ShowObj(ho_EdgeSecond, "magenta"); if (showPixPar) { ShowText("width:" + hv_WidthMeasure[i] + " pix", hv_RowEdgeFirst[i], hv_ColumnEdgeFirst[i] + 20, "blue"); } else { ShowText("width:" + WorldWidthMeasure[i] + " mm", hv_RowEdgeFirst[i], hv_ColumnEdgeFirst[i] + 20, "blue"); } } HOperatorSet.CloseMeasure(hv_MeasureHandle); } catch (HalconException HDevExpDefaultException) { ho_ImageTemp.Dispose(); ho_ROI.Dispose(); ho_EdgeFirst.Dispose(); ho_EdgeSecond.Dispose(); HTuple err = new HTuple(); err[0] = "测量失败"; err[1] = HDevExpDefaultException.Message; ShowText(err, "red"); } ho_ImageTemp.Dispose(); ho_ROI.Dispose(); ho_EdgeFirst.Dispose(); ho_EdgeSecond.Dispose(); } #endregion } }