using Community; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using HalconDotNet; using System.Collections.ObjectModel; using System.IO; namespace Model.Vision { /// /// 矩形区域模型 /// public class V_RectangleModel : NotificationObject { public int ID { get; set; } #region 区域轮廓参数 /// /// 项目ID /// public int VeiID { get; set; } /// /// 区域类型,1:自由区域 2:图像校正区域 /// public int AreaType { get; set; } /// /// 起点纵向坐标 /// public double Point_Row1 { get; set; } /// /// 起点横向坐标 /// public double Point_Column1 { get; set; } /// /// 终点纵向坐标 /// public double Point_Row2 { get; set; } /// /// 终点横向坐标 /// public double Point_Column2 { get; set; } /// /// 矩形区域轮廓 /// public HObject ho_RectangleModel; #endregion /// /// 支持文件名称 /// public string HelpFileName { get; set; } /// /// 是否读取标定文件 /// public bool WhetherReadFile { get; set; } /// /// 九点标定参数 /// public HTuple hv_JiuDianCalibrationPar = null; #region 光源各通道亮度 /// /// 光源通道1亮度 /// public long PassagewayBrightness1 { get; set; } /// /// 光源通道2亮度 /// public long PassagewayBrightness2 { get; set; } /// /// 光源通道3亮度 /// public long PassagewayBrightness3 { get; set; } /// /// 光源通道4亮度 /// public long PassagewayBrightness4 { get; set; } #endregion #region 我的ROI private V_ROI myROI; /// /// 我的ROI /// public V_ROI MyROI { get { return myROI; } set { myROI = value; this.RaisePropertyChanged("MyROI"); } } #endregion #region 我的ROI集合 private ObservableCollection myRoiList; /// /// 我的ROI集合 /// public ObservableCollection MyRoiList { get { return myRoiList; } set { myRoiList = value; this.RaisePropertyChanged("MyRoiList"); } } #endregion #region 我的角点 private V_CornerPoint myCorner; /// /// 我的角点 /// public V_CornerPoint MyCorner { get { return myCorner; } set { myCorner = value == null ? new V_CornerPoint() : value; this.RaisePropertyChanged("MyROI"); } } #endregion /// /// 模板图片 /// public HObject ModelImage; public V_RectangleModel() { WhetherReadFile = true; MyROI = new V_ROI() { ROIType = 1 }; MyRoiList = new ObservableCollection(); MyCorner = new V_CornerPoint(); } #region 实例化矩形轮廓 /// /// 实例化矩形轮廓 /// public void Instantiation() { WhetherReadFile = true; HOperatorSet.GenEmptyObj(out ho_RectangleModel); //获取产品模型 ho_RectangleModel.Dispose(); HOperatorSet.GenRectangle1(out ho_RectangleModel, Point_Row1, Point_Column1, Point_Row2, Point_Column2); } #endregion #region 标定文件的存取 /// /// 保存标定文件 /// /// public bool SaveCalibrationFile(HTuple hv_CalibrationPar) { this.HelpFileName = VeiID + "_" + this.ID; //先确保标定参数文件已存在 string filePath = @"Calibration/" + HelpFileName + ".tup"; //相机标定参数保存路径 if (!Directory.Exists(@"Calibration/")) { Directory.CreateDirectory(@"Calibration/"); } try { HOperatorSet.WriteTuple(hv_CalibrationPar, filePath); this.hv_JiuDianCalibrationPar = hv_CalibrationPar; return true; } catch (Exception) { return false; } #region 第二种保存文件的方法 //先确保标定参数文件已存在 // string filePath = @"Calibration/" + HelpFileName + ".dat"; //相机标定参数保存路径 //if (!Directory.Exists(@"Calibration/")) //{ // Directory.CreateDirectory(@"Calibration/"); //} //if (!File.Exists(filePath)) //{ // File.Create(filePath).Dispose(); //} //if (File.Exists(filePath)) //{ // HTuple hv_SerializedItemHandle, hv_FileHandle; // //保存标定参数 // HOperatorSet.SerializeHomMat2d(hv_CalibrationPar, out hv_SerializedItemHandle); // HOperatorSet.OpenFile(filePath, "output_binary", out hv_FileHandle); // HOperatorSet.FwriteSerializedItem(hv_FileHandle, hv_SerializedItemHandle); // HOperatorSet.CloseFile(hv_FileHandle); // this.hv_JiuDianCalibrationPar = hv_CalibrationPar; // this.WhetherReadFile = true; // return true; //} //else //{ // return false; //} #endregion } /// /// 读取标定参数 /// /// public bool ReadCalibrationPar() { if (!WhetherReadFile && hv_JiuDianCalibrationPar != null) { return true; } this.HelpFileName = VeiID + "_" + this.ID; string filePath = @"Calibration/" + HelpFileName + ".tup"; //相机标定参数保存路径 bool bl = false; try { if (File.Exists(filePath)) { HOperatorSet.ReadTuple(filePath, out hv_JiuDianCalibrationPar); bl = true; } } catch (Exception) { bl = false; } WhetherReadFile = !bl; return bl; #region 第二种读取文件的方法 //string filePath = @"Calibration/" + HelpFileName + ".dat"; //相机标定参数保存路径 //if (!File.Exists(filePath)) //{ // this.hv_JiuDianCalibrationPar = null; // return false; //} //try //{ // HTuple hv_FileHandle1 = null, hv_SerializedItemHandle1 = null; // HTuple hv_HomMat2D1 = null; // //读取 // HOperatorSet.OpenFile(filePath, "input_binary", out hv_FileHandle1); // HOperatorSet.FreadSerializedItem(hv_FileHandle1, out hv_SerializedItemHandle1); // HOperatorSet.DeserializeHomMat2d(hv_SerializedItemHandle1, out hv_HomMat2D1); // HOperatorSet.CloseFile(hv_FileHandle1); // this.WhetherReadFile = false; // this.hv_JiuDianCalibrationPar = hv_HomMat2D1; // return true; //} //catch (Exception) //{ // return false; //} #endregion } #endregion } }