720 lines
19 KiB
C#
720 lines
19 KiB
C#
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
|
||
{
|
||
/// <summary>
|
||
/// ROI对象
|
||
/// </summary>
|
||
public class V_ROI : NotificationObject
|
||
{
|
||
private int id;
|
||
/// <summary>
|
||
/// ID
|
||
/// </summary>
|
||
public int ID
|
||
{
|
||
get { return id; }
|
||
set
|
||
{
|
||
id = value;
|
||
this.RaisePropertyChanged("ID");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 所属那个产品模型
|
||
/// </summary>
|
||
public int ModelID { get; set; }
|
||
|
||
#region ROI类型 1:自由ROI 2:OCR字符识别ROI
|
||
/// <summary>
|
||
/// ROI类型 1:自由ROI 2:OCR字符识别ROI
|
||
/// </summary>
|
||
public int ROIType { get; set; }
|
||
#endregion
|
||
|
||
#region 扫码参数
|
||
private int codeTypeID;
|
||
/// <summary>
|
||
/// 码型 1:二维码 2:条形码
|
||
/// </summary>
|
||
public int CodeTypeID
|
||
{
|
||
get { return codeTypeID; }
|
||
set { codeTypeID = value;RaisePropertyChanged("CodeTypeID"); }
|
||
}
|
||
private string codeStr;
|
||
/// <summary>
|
||
/// 码制
|
||
/// </summary>
|
||
public string CodeStr
|
||
{
|
||
get { return codeStr; }
|
||
set { codeStr = value; RaisePropertyChanged("CodeStr"); }
|
||
}
|
||
|
||
|
||
|
||
private bool multiCode;
|
||
/// <summary>
|
||
/// 多码扫描
|
||
/// </summary>
|
||
public bool MultiCode
|
||
{
|
||
get { return multiCode; }
|
||
set
|
||
{
|
||
multiCode = value;
|
||
this.RaisePropertyChanged("MultiCode");
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region ROI区域形状参数
|
||
/// <summary>
|
||
/// 起点纵向坐标
|
||
/// </summary>
|
||
public double hv_Row { get; set; }
|
||
/// <summary>
|
||
/// 起点横向坐标
|
||
/// </summary>
|
||
public double hv_Column { get; set; }
|
||
/// <summary>
|
||
/// 终点纵向坐标
|
||
/// </summary>
|
||
public double hv_Row1 { get; set; }
|
||
/// <summary>
|
||
/// 终点横向坐标
|
||
/// </summary>
|
||
public double hv_Column1 { get; set; }
|
||
|
||
/// <summary>
|
||
/// 任意矩形Phi角度值,与水平方向的夹角
|
||
/// </summary>
|
||
public double hv_Phi { get; set; }
|
||
/// <summary>
|
||
/// 任意矩形的宽
|
||
/// </summary>
|
||
public double hv_length1 { get; set; }
|
||
/// <summary>
|
||
/// 任意矩形的高
|
||
/// </summary>
|
||
public double hv_length2 { get; set; }
|
||
|
||
/// <summary>
|
||
/// 圆形半径
|
||
/// </summary>
|
||
public double hv_Radius { get; set; }
|
||
|
||
#endregion
|
||
|
||
#region 测量参数
|
||
private string measurePar;
|
||
/// <summary>
|
||
/// 测量参数(这里把多个参数合并到一起 进行存储)
|
||
/// </summary>
|
||
public string MeasurePar
|
||
{
|
||
get { return measurePar; }
|
||
set
|
||
{
|
||
measurePar = value;
|
||
MeasureParSplit();
|
||
}
|
||
}
|
||
|
||
|
||
public int MyProperty { get; set; }
|
||
|
||
/// <summary>
|
||
/// 高斯平滑的Sigma
|
||
/// </summary>
|
||
public double M_Sigma { get; set; }
|
||
/// <summary>
|
||
/// 最小边缘幅度
|
||
/// </summary>
|
||
public double M_Threshold { get; set; }
|
||
|
||
/// <summary>
|
||
/// 边缘分组方式,值列表:'all','positive','negative','all_strongest','positive_strongest','negative_strongest'
|
||
/// </summary>
|
||
public string M_Transition { get; set; }
|
||
|
||
/// <summary>
|
||
/// 选择边缘对的方式 值列表:'all','first','last'
|
||
/// </summary>
|
||
public string M_Select { get; set; }
|
||
|
||
/// <summary>
|
||
/// 插值方法 默认值是 nearest_neighbor。是最近点的意思。 还可以写bilinear(双线性),bicubic(双三次)
|
||
/// </summary>
|
||
public string M_Interpolation { get; set; }
|
||
|
||
/// <summary>
|
||
/// 测量参数拆分
|
||
/// </summary>
|
||
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];
|
||
}
|
||
|
||
/// <summary>
|
||
/// 测量参数合并
|
||
/// </summary>
|
||
public void MeasureParMerge()
|
||
{
|
||
measurePar = M_Sigma + "," + M_Threshold + "," + M_Transition + "," + M_Select + "," + M_Interpolation;
|
||
}
|
||
|
||
|
||
#endregion
|
||
|
||
#region OCR参数
|
||
|
||
/// <summary>
|
||
/// OCR训练文件中的字符名称
|
||
/// </summary>
|
||
public HTuple hv_CharacterNames = null;
|
||
/// <summary>
|
||
/// OCR训练文件中的字符个数
|
||
/// </summary>
|
||
public HTuple hv_CharacterCount = null;
|
||
/// <summary>
|
||
/// 训练文件路径
|
||
/// </summary>
|
||
public string TrainFilePath { get; set; }
|
||
|
||
/// <summary>
|
||
/// 是否还原OCR分类器
|
||
/// </summary>
|
||
public bool ReductionOcrClassMlp { get; set; }
|
||
/// <summary>
|
||
/// OCR分类器
|
||
/// </summary>
|
||
public HTuple hv_OcrHandle = null;
|
||
/// <summary>
|
||
/// OCR字体模型
|
||
/// </summary>
|
||
public HTuple hv_TextModel = null;
|
||
|
||
|
||
#endregion
|
||
|
||
#region 检查参数
|
||
private int thresholdMin;
|
||
/// <summary>
|
||
/// 灰度值(最小值)
|
||
/// </summary>
|
||
public int ThresholdMin
|
||
{
|
||
get { return thresholdMin; }
|
||
set
|
||
{
|
||
thresholdMin = value;
|
||
this.RaisePropertyChanged("ThresholdMin");
|
||
}
|
||
}
|
||
|
||
private int thresholdMax;
|
||
/// <summary>
|
||
/// 灰度值(最大值)
|
||
/// </summary>
|
||
public int ThresholdMax
|
||
{
|
||
get { return thresholdMax; }
|
||
set
|
||
{
|
||
thresholdMax = value;
|
||
this.RaisePropertyChanged("ThresholdMax");
|
||
}
|
||
}
|
||
|
||
|
||
private int shapeA_Min;
|
||
/// <summary>
|
||
/// 形状A(面积最小值)
|
||
/// </summary>
|
||
public int ShapeA_Min
|
||
{
|
||
get { return shapeA_Min; }
|
||
set
|
||
{
|
||
shapeA_Min = value;
|
||
this.RaisePropertyChanged("ShapeA_Min");
|
||
}
|
||
}
|
||
|
||
private int shapeA_Max;
|
||
/// <summary>
|
||
/// 形状A(面积最大值)
|
||
/// </summary>
|
||
public int ShapeA_Max
|
||
{
|
||
get { return shapeA_Max; }
|
||
set
|
||
{
|
||
shapeA_Max = value;
|
||
this.RaisePropertyChanged("ShapeA_Max");
|
||
}
|
||
}
|
||
|
||
|
||
private int shapeB_Min;
|
||
/// <summary>
|
||
/// 形状B(面积最小值)
|
||
/// </summary>
|
||
public int ShapeB_Min
|
||
{
|
||
get { return shapeB_Min; }
|
||
set
|
||
{
|
||
shapeB_Min = value;
|
||
this.RaisePropertyChanged("ShapeB_Min");
|
||
}
|
||
}
|
||
|
||
private int shapeB_Max;
|
||
/// <summary>
|
||
/// 形状B(面积最大值)
|
||
/// </summary>
|
||
public int ShapeB_Max
|
||
{
|
||
get { return shapeB_Max; }
|
||
set { shapeB_Max = value;
|
||
this.RaisePropertyChanged("ShapeB_Max");
|
||
}
|
||
}
|
||
|
||
private int shapeC_Min;
|
||
/// <summary>
|
||
/// 形状C(面积最小值)
|
||
/// </summary>
|
||
public int ShapeC_Min
|
||
{
|
||
get { return shapeC_Min; }
|
||
set
|
||
{
|
||
shapeC_Min = value;
|
||
this.RaisePropertyChanged("ShapeC_Min");
|
||
}
|
||
}
|
||
|
||
private int shapeC_Max;
|
||
/// <summary>
|
||
/// 形状C(面积最大值)
|
||
/// </summary>
|
||
public int ShapeC_Max
|
||
{
|
||
get { return shapeC_Max; }
|
||
set
|
||
{
|
||
shapeC_Max = value;
|
||
this.RaisePropertyChanged("ShapeC_Max");
|
||
}
|
||
}
|
||
|
||
private int shapeD_Min;
|
||
/// <summary>
|
||
/// 形状D(面积最小值)
|
||
/// </summary>
|
||
public int ShapeD_Min
|
||
{
|
||
get { return shapeD_Min; }
|
||
set
|
||
{
|
||
shapeD_Min = value;
|
||
this.RaisePropertyChanged("ShapeD_Min");
|
||
}
|
||
}
|
||
|
||
private int shapeD_Max;
|
||
/// <summary>
|
||
/// 形状D(面积最大值)
|
||
/// </summary>
|
||
public int ShapeD_Max
|
||
{
|
||
get { return shapeD_Max; }
|
||
set
|
||
{
|
||
shapeD_Max = value;
|
||
this.RaisePropertyChanged("ShapeD_Max");
|
||
}
|
||
}
|
||
|
||
|
||
|
||
#endregion
|
||
|
||
|
||
#region 其他参数
|
||
/// <summary>
|
||
/// 支持文件名称
|
||
/// </summary>
|
||
public string HelpFileName { get; set; }
|
||
|
||
|
||
private bool enable;
|
||
/// <summary>
|
||
/// 启用
|
||
/// </summary>
|
||
public bool Enable
|
||
{
|
||
get { return enable; }
|
||
set
|
||
{
|
||
enable = value;
|
||
this.RaisePropertyChanged("Enable");
|
||
}
|
||
}
|
||
|
||
|
||
private string notes;
|
||
/// <summary>
|
||
/// 备注
|
||
/// </summary>
|
||
public string Notes
|
||
{
|
||
get { return notes; }
|
||
set
|
||
{
|
||
notes = value;
|
||
this.RaisePropertyChanged("Notes");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 执行结果是否成功
|
||
private string matchOK;
|
||
/// <summary>
|
||
/// 执行结果是否成功
|
||
/// </summary>
|
||
public string MatchOK
|
||
{
|
||
get { return matchOK; }
|
||
set
|
||
{
|
||
matchOK = value;
|
||
this.RaisePropertyChanged("MatchOK");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 模型匹配相关参数
|
||
|
||
/// <summary>
|
||
/// 是否要读取特征模型文件
|
||
/// </summary>
|
||
public bool ReductionModel { get; set; }
|
||
|
||
/// <summary>
|
||
/// ROI模型参数
|
||
/// </summary>
|
||
public HTuple hv_ModelID = null;
|
||
|
||
/// <summary>
|
||
/// 校正模型的特征坐标
|
||
/// </summary>
|
||
public V_Piont ModelFeaturePiont { get; set; }
|
||
|
||
private double recognitionCoefficient;
|
||
/// <summary>
|
||
/// 模型识别系数
|
||
/// </summary>
|
||
public double RecognitionCoefficient
|
||
{
|
||
get { return recognitionCoefficient; }
|
||
set
|
||
{
|
||
recognitionCoefficient = value;
|
||
updateRC();
|
||
this.RaisePropertyChanged("RecognitionCoefficient");
|
||
}
|
||
}
|
||
|
||
|
||
#endregion
|
||
|
||
/// <summary>
|
||
/// ROI模型轮廓
|
||
/// </summary>
|
||
public HObject ho_OutlineROI;
|
||
|
||
#region ROI的形状
|
||
private DrawRoiEmn roiShape;
|
||
/// <summary>
|
||
/// ROI的形状
|
||
/// </summary>
|
||
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;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 初始化ROI
|
||
/// </summary>
|
||
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;
|
||
/// <summary>
|
||
/// 更新识别系数
|
||
/// </summary>
|
||
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 保存特征模型
|
||
/// <summary>
|
||
/// 保存特征模型
|
||
/// </summary>
|
||
/// <param name="hv_ModelID"></param>
|
||
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 读取特征模型文件
|
||
/// <summary>
|
||
/// 读取特征模型文件
|
||
/// </summary>
|
||
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分类器
|
||
/// <summary>
|
||
/// 保存OCR分类器
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
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分类器
|
||
/// <summary>
|
||
/// 读取OCR分类器
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
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训练字符存储到文件中
|
||
/// <summary>
|
||
/// 将OCR训练字符存储到文件中
|
||
/// </summary>
|
||
/// <param name="character">要训练的对象特征</param>
|
||
/// <param name="image">字符的灰度值</param>
|
||
/// <param name="classVal">字符的名称</param>
|
||
/// <returns></returns>
|
||
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
|
||
|
||
}
|
||
}
|