using Community; using HalconDotNet; using Model.Vision; using System; using System.Collections.Generic; using System.Data.OleDb; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Model.Vision { /// /// ROI对象 /// public class V_ROI : NotificationObject { private int id; /// /// ID /// public int ID { get { return id; } set { id = value; this.RaisePropertyChanged("ID"); } } /// /// 所属那个产品模型 /// public int ModelID { get; set; } #region ROI类型 1:自由ROI 2:OCR字符识别ROI /// /// ROI类型 1:自由ROI 2:OCR字符识别ROI /// public int ROIType { get; set; } #endregion #region 扫码参数 private int codeTypeID; /// /// 码型 1:二维码 2:条形码 /// public int CodeTypeID { get { return codeTypeID; } set { codeTypeID = value; RaisePropertyChanged("CodeTypeID"); } } private string codeStr; /// /// 码制 /// public string CodeStr { get { return codeStr; } set { codeStr = value; RaisePropertyChanged("CodeStr"); } } private bool multiCode; /// /// 多码扫描 /// public bool MultiCode { get { return multiCode; } set { multiCode = value; this.RaisePropertyChanged("MultiCode"); } } #endregion #region ROI区域形状参数 /// /// 起点纵向坐标 /// public double hv_Row { get; set; } /// /// 起点横向坐标 /// public double hv_Column { get; set; } /// /// 终点纵向坐标 /// public double hv_Row1 { get; set; } /// /// 终点横向坐标 /// public double hv_Column1 { get; set; } /// /// 任意矩形Phi角度值,与水平方向的夹角 /// public double hv_Phi { get; set; } /// /// 任意矩形的宽 /// public double hv_length1 { get; set; } /// /// 任意矩形的高 /// public double hv_length2 { get; set; } /// /// 圆形半径 /// public double hv_Radius { get; set; } #endregion #region 测量参数 private string measurePar; /// /// 测量参数(这里把多个参数合并到一起 进行存储) /// public string MeasurePar { get { return measurePar; } set { measurePar = value; MeasureParSplit(); } } public int MyProperty { get; set; } /// /// 高斯平滑的Sigma /// public double M_Sigma { get; set; } /// /// 最小边缘幅度 /// public double M_Threshold { get; set; } /// /// 边缘分组方式,值列表:'all','positive','negative','all_strongest','positive_strongest','negative_strongest' /// public string M_Transition { get; set; } /// /// 选择边缘对的方式 值列表:'all','first','last' /// public string M_Select { get; set; } /// /// 插值方法 默认值是 nearest_neighbor。是最近点的意思。 还可以写bilinear(双线性),bicubic(双三次) /// public string M_Interpolation { get; set; } /// /// 测量参数拆分 /// public void MeasureParSplit() { if (string.IsNullOrEmpty(MeasurePar) || MeasurePar.Length < 5) { return; } string[] str = MeasurePar.Split(','); M_Sigma = Convert.ToDouble(str[0]); M_Threshold = Convert.ToDouble(str[1]); M_Transition = str[2]; M_Select = str[3]; M_Interpolation = str[4]; } /// /// 测量参数合并 /// public void MeasureParMerge() { measurePar = M_Sigma + "," + M_Threshold + "," + M_Transition + "," + M_Select + "," + M_Interpolation; } #endregion #region OCR参数 /// /// OCR训练文件中的字符名称 /// public HTuple hv_CharacterNames = null; /// /// OCR训练文件中的字符个数 /// public HTuple hv_CharacterCount = null; /// /// 训练文件路径 /// public string TrainFilePath { get; set; } /// /// 是否还原OCR分类器 /// public bool ReductionOcrClassMlp { get; set; } /// /// OCR分类器 /// public HTuple hv_OcrHandle = null; /// /// OCR字体模型 /// public HTuple hv_TextModel = null; #endregion #region 检查参数 private int thresholdMin; /// /// 灰度值(最小值) /// public int ThresholdMin { get { return thresholdMin; } set { thresholdMin = value; this.RaisePropertyChanged("ThresholdMin"); } } private int thresholdMax; /// /// 灰度值(最大值) /// public int ThresholdMax { get { return thresholdMax; } set { thresholdMax = value; this.RaisePropertyChanged("ThresholdMax"); } } private int shapeA_Min; /// /// 形状A(面积最小值) /// public int ShapeA_Min { get { return shapeA_Min; } set { shapeA_Min = value; this.RaisePropertyChanged("ShapeA_Min"); } } private int shapeA_Max; /// /// 形状A(面积最大值) /// public int ShapeA_Max { get { return shapeA_Max; } set { shapeA_Max = value; this.RaisePropertyChanged("ShapeA_Max"); } } private int shapeB_Min; /// /// 形状B(面积最小值) /// public int ShapeB_Min { get { return shapeB_Min; } set { shapeB_Min = value; this.RaisePropertyChanged("ShapeB_Min"); } } private int shapeB_Max; /// /// 形状B(面积最大值) /// public int ShapeB_Max { get { return shapeB_Max; } set { shapeB_Max = value; this.RaisePropertyChanged("ShapeB_Max"); } } private int shapeC_Min; /// /// 形状C(面积最小值) /// public int ShapeC_Min { get { return shapeC_Min; } set { shapeC_Min = value; this.RaisePropertyChanged("ShapeC_Min"); } } private int shapeC_Max; /// /// 形状C(面积最大值) /// public int ShapeC_Max { get { return shapeC_Max; } set { shapeC_Max = value; this.RaisePropertyChanged("ShapeC_Max"); } } private int shapeD_Min; /// /// 形状D(面积最小值) /// public int ShapeD_Min { get { return shapeD_Min; } set { shapeD_Min = value; this.RaisePropertyChanged("ShapeD_Min"); } } private int shapeD_Max; /// /// 形状D(面积最大值) /// public int ShapeD_Max { get { return shapeD_Max; } set { shapeD_Max = value; this.RaisePropertyChanged("ShapeD_Max"); } } #endregion #region 其他参数 /// /// 支持文件名称 /// public string HelpFileName { get; set; } private bool enable; /// /// 启用 /// public bool Enable { get { return enable; } set { enable = value; this.RaisePropertyChanged("Enable"); } } private string notes; /// /// 备注 /// public string Notes { get { return notes; } set { notes = value; this.RaisePropertyChanged("Notes"); } } #endregion #region 执行结果是否成功 private string matchOK; /// /// 执行结果是否成功 /// public string MatchOK { get { return matchOK; } set { matchOK = value; this.RaisePropertyChanged("MatchOK"); } } #endregion #region 模型匹配相关参数 /// /// 是否要读取特征模型文件 /// public bool ReductionModel { get; set; } /// /// ROI模型参数 /// public HTuple hv_ModelID = null; private V_Piont modelFeaturePiont; /// /// 校正模型的特征坐标 /// public V_Piont ModelFeaturePiont { get { return modelFeaturePiont; } set { modelFeaturePiont = value == null ? new V_Piont() : value; } } private double recognitionCoefficient; /// /// 模型识别系数 /// public double RecognitionCoefficient { get { return recognitionCoefficient; } set { recognitionCoefficient = value; updateRC(); this.RaisePropertyChanged("RecognitionCoefficient"); } } #endregion /// /// ROI模型轮廓 /// public HObject ho_OutlineROI; #region ROI的形状 private DrawRoiEmn roiShape; /// /// ROI的形状 /// public DrawRoiEmn RoiShape { get { return roiShape; } set { roiShape = value; this.RaisePropertyChanged("RoiShape"); } } #endregion public V_ROI() { ReductionOcrClassMlp = true; MeasurePar = ""; ReductionModel = true; Enable = true; Notes = ""; ROIType = 1; CodeStr = ""; HelpFileName = ""; ModelFeaturePiont = new V_Piont(); RecognitionCoefficient = 0.5; //测量参数 M_Sigma = 1; M_Threshold = 30; M_Transition = "all"; M_Select = "all"; M_Interpolation = "nearest_neighbor"; ThresholdMin = 30; ThresholdMax = 50; } /// /// 初始化ROI /// public void Initialize() { HOperatorSet.GenEmptyObj(out ho_OutlineROI); switch (RoiShape) { case DrawRoiEmn.C: //获取轮廓 ho_OutlineROI.Dispose(); HOperatorSet.GenCircle(out ho_OutlineROI, hv_Row, hv_Column, hv_Radius); break; case DrawRoiEmn.R1: //获取轮廓 ho_OutlineROI.Dispose(); HOperatorSet.GenRectangle1(out ho_OutlineROI, hv_Row, hv_Column, hv_Row1, hv_Column1); break; case DrawRoiEmn.R2: //获取轮廓 ho_OutlineROI.Dispose(); HOperatorSet.GenRectangle2(out ho_OutlineROI, hv_Row, hv_Column, hv_Phi, hv_length1, hv_length2); break; } } #region 更新识别系数 bool bl = true; /// /// 更新识别系数 /// private void updateRC() { try { if (this.RecognitionCoefficient > 0 && this.ID > 0) { if (bl) { bl = false; return; } string sqlstr = "update V_ROI set RecognitionCoefficient=@RecognitionCoefficient where ID=@ID"; AccHelpers.ExecuteNonQuery(sqlstr, new OleDbParameter("@RecognitionCoefficient", this.RecognitionCoefficient), new OleDbParameter("@ID", this.ID)); } } catch (Exception) { } } #endregion #region 保存特征模型 /// /// 保存特征模型 /// /// public bool SaveFeatureModel() { HelpFileName = ModelID + "_" + ID; string filePathFIle = @"ModelFile/" + HelpFileName + ".shm"; try { //创建文件夹 if (!Directory.Exists(@"ModelFile/")) { Directory.CreateDirectory(@"ModelFile/"); } //保存特征模型文件 HOperatorSet.WriteShapeModel(hv_ModelID, filePathFIle); return true; } catch (Exception) { return false; } } #endregion #region 读取特征模型文件 /// /// 读取特征模型文件 /// public bool ReadFeatureModel() { //如果不需要读取 if (!ReductionModel || hv_ModelID != null) { return true; } this.HelpFileName = this.ModelID + "_" + this.ID; string filePath = @"ModelFile/" + this.HelpFileName + ".shm"; bool bl = false; try { if (File.Exists(filePath)) { //读取特征文件 HOperatorSet.ReadShapeModel(filePath, out hv_ModelID); bl = true; } } catch (Exception) { hv_ModelID = null; } ReductionModel = !bl; return bl; } #endregion #region 保存OCR分类器 /// /// 保存OCR分类器 /// /// public bool SaveOcrClassMlp(HTuple OcrHandle) { HelpFileName = ModelID + "_" + ID; ////创建OMC文件 string FilePath = @"OCR/" + HelpFileName + ".OMC"; try { if (!Directory.Exists(@"OCR/")) { Directory.CreateDirectory(@"OCR/"); } //保存OCR分类器 HOperatorSet.WriteOcrClassMlp(OcrHandle, FilePath); return true; } catch (Exception) { return false; } } #endregion #region 读取OCR分类器 /// /// 读取OCR分类器 /// /// public bool ReadOcrOcrClassMlp() { HelpFileName = ModelID + "_" + ID; ////创建OMC文件 string FilePath = @"OCR/" + HelpFileName + ".OMC"; try { if (File.Exists(FilePath)) { HOperatorSet.CreateTextModelReader("auto", FilePath, out hv_TextModel); //获取字体模型 HOperatorSet.ReadOcrClassMlp(FilePath, out hv_OcrHandle); //获取分类模型 } return true; } catch (Exception) { return false; } } #endregion #region 将OCR训练字符存储到文件中 /// /// 将OCR训练字符存储到文件中 /// /// 要训练的对象特征 /// 字符的灰度值 /// 字符的名称 /// public bool AccessOcrTrainModel(HObject character, HObject image, HTuple classVal) { HelpFileName = ModelID + "_" + ID; //创建训练文件 TrainFilePath = @"OCR/" + HelpFileName + ".trf"; try { if (!Directory.Exists(@"OCR/")) { Directory.CreateDirectory(@"OCR/"); } if (!File.Exists(TrainFilePath)) { //如果文件不存在 则运行此行代码 这会覆盖原来的字符 HOperatorSet.WriteOcrTrainf(character, image, classVal, TrainFilePath); } else { //将图像区域与字符标识关联,保存到训练文件 HOperatorSet.AppendOcrTrainf(character, image, classVal, TrainFilePath); } //读取OCR文字模型 HOperatorSet.ReadOcrTrainfNames(TrainFilePath, out hv_CharacterNames, out hv_CharacterCount); return true; } catch (Exception) { return false; } } #endregion } /// /// AI对象模型 /// public class AiModel:NotificationObject { } }