using Community;
using HalconDotNet;
using System;
using System.Collections.Generic;
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 == null ? new V_ROI() : value;
this.RaisePropertyChanged("MyROI");
}
}
public HTuple HomMat2D { get; set; }
#endregion
#region 我的ROI集合
private ObservableCollection myRoiList;
///
/// 我的ROI集合
///
public ObservableCollection MyRoiList
{
get { return myRoiList; }
set
{
myRoiList = value == null ? new ObservableCollection() : 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("MyCorner");
}
}
#endregion
///
/// 定位相对坐标
///
public List LocateList { get; set; }
public List RoiList { get; set; }
///
/// 模板图片
///
public HObject ModelImage;
public V_RectangleModel()
{
WhetherReadFile = true;
MyROI = new V_ROI() { ROIType = 1 };
MyRoiList = new ObservableCollection();
LocateList = new List();
}
#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,string cameraName)
{
this.HelpFileName = cameraName;
//先确保标定参数文件已存在
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(string camereName)
{
if (WhetherReadFile == false && hv_JiuDianCalibrationPar != null)
{
return true;
}
string filePath = @"Calibration/" + camereName + ".tup"; //相机标定参数保存路径
bool bl = false;
try
{
if (File.Exists(filePath))
{
HOperatorSet.ReadTuple(filePath, out hv_JiuDianCalibrationPar);
bl = true;
}
else
{
this.HelpFileName = VeiID + "_" + this.ID;
var oldfilePath = @"Calibration/" + HelpFileName + ".tup"; //相机标定参数保存路径
if (File.Exists(oldfilePath))
{
File.Copy(oldfilePath, filePath);
}
}
}
catch (Exception)
{
bl = false;
}
WhetherReadFile = !bl;
return bl;
}
#endregion
}
}