using HalconDotNet; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using LB_VisionControls; using static System.Windows.Forms.VisualStyles.VisualStyleElement; using System.Reflection; using System.Diagnostics.Contracts; using System.Drawing.Drawing2D; using Newtonsoft.Json.Linq; using System.Xml.Linq; using System.Diagnostics; using OpenVinoSharp; namespace LB_VisionProcesses.Alogrithms.Halcon { public partial class HFindMultiModelToolEdit : TAlgorithmEdit { public List listModelToolControls = new List(); public HFindMultiModelToolEdit(HFindMultiModelTool subject = null) { if (subject != null && subject is HFindMultiModelTool) Subject = subject; else Subject = new HFindMultiModelTool(); this.Dock = DockStyle.Fill; InitializeComponent(); } /// /// 控件加载事件 /// /// /// private void HFindModelToolEdit_Load(object sender, EventArgs e) { pnlInputImage.Controls.Add(inputImageHSmartWindowControl); inputImageHSmartWindowControl.Dock = DockStyle.Fill; pnlRecordImage.Controls.Add(recordImageHSmartWindowControl); recordImageHSmartWindowControl.Dock = DockStyle.Fill; // 设置显示样式 toolTip.AutoPopDelay = 5000;//提示信息的可见时间 toolTip.InitialDelay = 500;//事件触发多久后出现提示 toolTip.ReshowDelay = 500;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框 toolTip.ShowAlways = true;//是否显示提示框 //遍历可以选择的Roi类型枚举 foreach (var value in Enum.GetValues(typeof(RoiType))) cmbTypeRoi.Items.Add(value.ToString()); //遍历可以选择的Fixture枚举 cmbFixture.Items.Add(""); foreach (string value in IProcess.dicFixtures.Keys) cmbFixture.Items.Add(value.ToString()); cmbTypeRoi.Text = RoiType.None.ToString(); LoadParas(); if (Subject.Result) { lblResult.BackColor = Color.Green; lblResult.Text = "True"; } else { lblResult.BackColor = Color.Red; lblResult.Text = "False"; } lblMsg.Text = Msg.Length > 50 ? Msg.Substring(0, 50) : Msg; lblMsgToolTip.SetToolTip(BtmStatusStrip, Msg); lblRunTime.Text = $"{Subject.RunTime}ms"; UpdataInputsUI(); } private System.Windows.Forms.ToolTip toolTip = new System.Windows.Forms.ToolTip(); private void ShowToolTip(Control control, string message) => toolTip.SetToolTip(control, message); // 通过反射清除所有事件处理程序 public void ClearAllEventHandlers() { // 获取事件字段 FieldInfo field = typeof(Label).GetField("MouseHover", BindingFlags.NonPublic | BindingFlags.Instance); if (field != null) { // 将事件字段设为 null,移除所有事件处理程序 field.SetValue(this, null); } } void UpdataInputsUI() { this.Invoke(() => { #region 清空输入 this.lblInputParam1.Visible = false; this.lblInputParam2.Visible = false; this.lblInputParam3.Visible = false; this.lblInputParam4.Visible = false; this.lblInputParam5.Visible = false; this.lblInputParam6.Visible = false; this.lblInputParam7.Visible = false; this.lblInputParam8.Visible = false; this.lblInputParam9.Visible = false; this.lblInputParam10.Visible = false; this.lblInputParam11.Visible = false; this.lblInputParam12.Visible = false; this.lblInputParam13.Visible = false; this.lblInputParam14.Visible = false; this.lblInputParam15.Visible = false; this.lblInputParam16.Visible = false; this.lblInputParam17.Visible = false; this.lblInputParam18.Visible = false; this.dtxtInputParam1.Visible = false; this.dtxtInputParam2.Visible = false; this.dtxtInputParam3.Visible = false; this.dtxtInputParam4.Visible = false; this.dtxtInputParam5.Visible = false; this.dtxtInputParam6.Visible = false; this.dtxtInputParam7.Visible = false; this.dtxtInputParam8.Visible = false; this.dtxtInputParam9.Visible = false; this.dtxtInputParam10.Visible = false; this.dtxtInputParam11.Visible = false; this.dtxtInputParam12.Visible = false; this.dtxtInputParam13.Visible = false; this.dtxtInputParam14.Visible = false; this.dtxtInputParam15.Visible = false; this.dtxtInputParam16.Visible = false; this.dtxtInputParam17.Visible = false; this.dtxtInputParam18.Visible = false; #endregion //ClearAllEventHandlers(); //, AngleStart / 180.0 * Math.PI, AngleExtent / 180.0 * Math.PI //, ScaleRMin, ScaleRMax //, ScaleCMin, ScaleCMax //, MinScore, NumMatches, MaxOverlap, SubPixel //, NumLevels == "auto" ? NumLevels : Convert.ToInt32(NumLevels) //, Greediness #region 输入参数 //, AngleStart / 180.0 * Math.PI, AngleExtent / 180.0 * Math.PI this.lblInputParam1.Text = "起始角度"; this.dtxtInputParam1.Text = Subject.Params.Inputs["AngleStart"]?.ToString(); this.lblInputParam1.Visible = true; this.dtxtInputParam1.Visible = true; this.dtxtInputParam1.Enabled = true; lblInputParam1.MouseHover += (s, e) => ShowToolTip((Control)s, "搜索时的起始角度AngleStart"); this.lblInputParam2.Text = "角度范围"; this.dtxtInputParam2.Text = Subject.Params.Inputs["AngleExtent"]?.ToString(); this.lblInputParam2.Visible = true; this.dtxtInputParam2.Visible = true; this.dtxtInputParam2.Enabled = true; lblInputParam2.MouseHover += (s, e) => ShowToolTip((Control)s, "搜索时的角度范围AngleExtent,'0'表示无角度搜索"); //, ScaleRMin, ScaleRMax this.lblInputParam3.Text = "纵向缩放最小值"; this.dtxtInputParam3.Text = Subject.Params.Inputs["ScaleRMin"]?.ToString(); this.lblInputParam3.Visible = true; this.dtxtInputParam3.Visible = true; this.dtxtInputParam3.Enabled = true; lblInputParam3.MouseHover += (s, e) => ShowToolTip((Control)s, "纵向缩放最小值ScaleRMin"); this.lblInputParam4.Text = "纵向缩放最大值"; this.dtxtInputParam4.Text = Subject.Params.Inputs["ScaleRMax"]?.ToString(); this.lblInputParam4.Visible = true; this.dtxtInputParam4.Visible = true; this.dtxtInputParam4.Enabled = true; lblInputParam4.MouseHover += (s, e) => ShowToolTip((Control)s, "纵向缩放最大值ScaleRMax"); //, ScaleCMin, ScaleCMax this.lblInputParam5.Text = "横向缩放最小值"; this.dtxtInputParam5.Text = Subject.Params.Inputs["ScaleCMin"]?.ToString(); this.lblInputParam5.Visible = true; this.dtxtInputParam5.Visible = true; this.dtxtInputParam5.Enabled = true; lblInputParam5.MouseHover += (s, e) => ShowToolTip((Control)s, "横向缩放最小值ScaleCMin"); this.lblInputParam6.Text = "横向缩放最大值"; this.dtxtInputParam6.Text = Subject.Params.Inputs["ScaleCMax"]?.ToString(); this.lblInputParam6.Visible = true; this.dtxtInputParam6.Visible = true; this.dtxtInputParam6.Enabled = true; lblInputParam6.MouseHover += (s, e) => ShowToolTip((Control)s, "横向缩放最大值ScaleCMax"); //, MinScore, NumMatches, MaxOverlap, ResultType this.lblInputParam7.Text = "最小分数"; this.dtxtInputParam7.Text = Subject.Params.Inputs["MinScore"]?.ToString(); this.lblInputParam7.Visible = true; this.dtxtInputParam7.Visible = true; this.dtxtInputParam7.Enabled = true; lblInputParam7.MouseHover += (s, e) => ShowToolTip((Control)s, "被找到的模板最小分数MinScore"); this.lblInputParam8.Text = "期望个数"; this.dtxtInputParam8.Text = Subject.Params.Inputs["NumMatches"]?.ToString(); this.lblInputParam8.Visible = true; this.dtxtInputParam8.Visible = true; this.dtxtInputParam8.Enabled = true; lblInputParam8.MouseHover += (s, e) => ShowToolTip((Control)s, "要找到的模板最多的实例数NumMatches,'0'则找到所有可能的匹配"); this.lblInputParam9.Text = "最大重叠比例"; this.dtxtInputParam9.Text = Subject.Params.Inputs["MaxOverlap"]?.ToString(); this.lblInputParam9.Visible = true; this.dtxtInputParam9.Visible = true; this.dtxtInputParam9.Enabled = true; lblInputParam9.MouseHover += (s, e) => ShowToolTip((Control)s, "允许找到的模型实例的最大重叠比例MaxOverlap[较小的值会避免找到多个相似的匹配]"); this.lblInputParam10.Text = "计算精度"; this.dtxtInputParam10.Text = Subject.Params.Inputs["SubPixel"]?.ToString(); this.lblInputParam10.Visible = true; this.dtxtInputParam10.Visible = true; this.dtxtInputParam10.Enabled = false; lblInputParam10.MouseHover += (s, e) => ShowToolTip((Control)s, "计算精度的设置SubPixel['none'最大误差为半个像素]"); //, NumLevels == "auto" ? NumLevels : Convert.ToInt32(NumLevels) //, Greediness, , new HTuple(), new HTuple() this.lblInputParam11.Text = "搜索时金字塔的层级"; this.dtxtInputParam11.Text = "0"; this.lblInputParam11.Visible = true; this.dtxtInputParam11.Visible = true; this.dtxtInputParam11.Enabled = false; lblInputParam11.MouseHover += (s, e) => ShowToolTip((Control)s, "搜索时金字塔的层级"); this.lblInputParam12.Text = "贪婪度"; this.dtxtInputParam12.Text = Subject.Params.Inputs["Greediness"]?.ToString(); this.lblInputParam12.Visible = true; this.dtxtInputParam12.Visible = true; this.dtxtInputParam12.Enabled = true; lblInputParam12.MouseHover += (s, e) => ShowToolTip((Control)s, "贪婪度,越高速度越快[一般都设为0.8,太大容易出现找不到的情况]"); this.lblInputParam13.Text = "最小数量"; this.dtxtInputParam13.Text = Subject.Params.Inputs["MinCount"]?.ToString(); this.lblInputParam13.Visible = true; this.dtxtInputParam13.Visible = true; this.dtxtInputParam13.Enabled = true; lblInputParam13.MouseHover += (s, e) => ShowToolTip((Control)s, "匹配结果要求的最小数量"); this.lblInputParam14.Text = "最大数量"; this.dtxtInputParam14.Text = Subject.Params.Inputs["MaxCount"]?.ToString(); this.lblInputParam14.Visible = true; this.dtxtInputParam14.Visible = true; this.dtxtInputParam14.Enabled = true; lblInputParam14.MouseHover += (s, e) => ShowToolTip((Control)s, "匹配结果要求的最大数量"); this.lblInputParam15.Text = "模板滤波"; this.dtxtInputParam15.Text = Subject.Params.Inputs["ModelMask"]?.ToString(); this.lblInputParam15.Visible = true; this.dtxtInputParam15.Visible = true; this.dtxtInputParam15.Enabled = true; lblInputParam15.MouseHover += (s, e) => ShowToolTip((Control)s, "模板匹配前执行均值滤波"); #endregion //UpdataInputs(); //string NumLevels = "auto" //, double AngleStart = -5, double AngleExtent = 10, string AngleStep = "auto" //, double ScaleRMin = 0.9, double ScaleRMax = 1.1, string ScaleRStep = "auto" //, double ScaleCMin = 1.1, double ScaleCMax = 0.9, string ScaleCStep = "auto" //, string Optimization = "auto", string Metric = "ignore_local_polarity" //, string Contrast = "auto", int MinContrast = 10 }); } /// /// 更新运行参数 /// public override void UpdataInputs() { List hFindModelTools = new List(); foreach (var ModelToolControl in listModelToolControls) hFindModelTools.Add(ModelToolControl.HFindModelTool); ((HFindMultiModelTool)Subject).HFindModelTools = hFindModelTools; int iResult = 0; Subject.Params.Inputs["MinCount"] = int.TryParse(dtxtMinCount.Text, out iResult) ? iResult : Subject.Params.Inputs["MinCount"]; Subject.Params.Inputs["MaxCount"] = int.TryParse(dtxtMaxCount.Text, out iResult) ? iResult : Subject.Params.Inputs["MinCount"]; Subject.Params.Inputs["AngleStart"] = dtxtInputParam1.Text; Subject.Params.Inputs["AngleExtent"] = dtxtInputParam2.Text; Subject.Params.Inputs["ScaleRMin"] = dtxtInputParam3.Text; Subject.Params.Inputs["ScaleRMax"] = dtxtInputParam4.Text; Subject.Params.Inputs["ScaleCMin"] = dtxtInputParam5.Text; Subject.Params.Inputs["ScaleCMax"] = dtxtInputParam6.Text; Subject.Params.Inputs["MinScore"] = dtxtInputParam7.Text; Subject.Params.Inputs["NumMatches"] = dtxtInputParam8.Text; Subject.Params.Inputs["MaxOverlap"] = dtxtInputParam9.Text; Subject.Params.Inputs["SubPixel"] = dtxtInputParam10.Text; Subject.Params.Inputs["NumLevels"] = dtxtInputParam11.Text; Subject.Params.Inputs["Greediness"] = dtxtInputParam12.Text; Subject.Params.Inputs["MinCount"] = dtxtInputParam13.Text; Subject.Params.Inputs["MaxCount"] = dtxtInputParam14.Text; Subject.Params.Inputs["ModelMask"] = dtxtInputParam15.Text; Subject.Params.Outputs.Add("CenterX", new List()); Subject.Params.Outputs.Add("CenterY", new List()); Subject.Params.Outputs.Add("Angle", new List()); Subject.Params.Outputs.Add("Score", new List()); Subject.Params.Outputs.Add("Count", 0); if (cmbFixture.Text == "") Subject.Params.Fixture = new Fixture(); else if (IProcess.dicFixtures.ContainsKey(cmbFixture.Text)) Subject.Params.Fixture = IProcess.dicFixtures[cmbFixture.Text]; base.UpdataInputs(); } /// /// 加载运行参数 /// public override void LoadParas() { this.BeginInvoke(new Action(() => { dtxtMinCount.Text = Subject.Params.Inputs["MinCount"]?.ToString(); dtxtMaxCount.Text = Subject.Params.Inputs["MaxCount"]?.ToString(); dtxtInputParam1.Text = Subject.Params.Inputs["AngleStart"]?.ToString(); dtxtInputParam2.Text = Subject.Params.Inputs["AngleExtent"]?.ToString(); dtxtInputParam3.Text = Subject.Params.Inputs["ScaleRMin"]?.ToString(); dtxtInputParam4.Text = Subject.Params.Inputs["ScaleRMax"]?.ToString(); dtxtInputParam5.Text = Subject.Params.Inputs["ScaleCMin"]?.ToString(); dtxtInputParam6.Text = Subject.Params.Inputs["ScaleCMax"]?.ToString(); dtxtInputParam7.Text = Subject.Params.Inputs["MinScore"]?.ToString(); dtxtInputParam8.Text = Subject.Params.Inputs["NumMatches"]?.ToString(); dtxtInputParam9.Text = Subject.Params.Inputs["MaxOverlap"]?.ToString(); dtxtInputParam10.Text = Subject.Params.Inputs["SubPixel"]?.ToString(); dtxtInputParam11.Text = Subject.Params.Inputs["NumLevels"]?.ToString(); dtxtInputParam12.Text = Subject.Params.Inputs["Greediness"]?.ToString(); dtxtInputParam13.Text = Subject.Params.Inputs["MinCount"]?.ToString(); dtxtInputParam14.Text = Subject.Params.Inputs["MaxCount"]?.ToString(); dtxtInputParam15.Text = Subject.Params.Inputs["ModelMask"]?.ToString(); if (Subject.InputImage != null && Subject.InputImage is HObject) inputImageHSmartWindowControl.ShowHoImage((HObject)Subject.InputImage); Type type = Subject.Params.ROI?.GetType(); if (Subject.Params.ROI != null) { switch (type) { case Type t when t == typeof(HRectangle2): cmbTypeRoi.Text = RoiType.Rectangle2.ToString(); break; case Type t when t == typeof(HCircle): cmbTypeRoi.Text = RoiType.Circle.ToString(); break; case Type t when t == typeof(HSegment): cmbTypeRoi.Text = RoiType.Segment.ToString(); break; default: cmbTypeRoi.Text = RoiType.None.ToString(); break; } if (cmbTypeRoi.Text.ToString() != "None") ckbDrawRoi.Checked = true; else ckbDrawRoi.Checked = false; inputImageHSmartWindowControl.oRoi = Subject.Params.ROI; } if (Subject.Params.Fixture != null) cmbFixture.Text = Subject.Params.Fixture.strName; else cmbFixture.Text = ""; switch (type) { case Type t when t == typeof(HRectangle2): inputImageHSmartWindowControl.oRoi = new HRectangle2(Subject.Params.ROI.X + Subject.Params.Fixture.X, Subject.Params.ROI.Y + Subject.Params.Fixture.Y , Subject.Params.ROI.Phi + Subject.Params.Fixture.Phi, ((HRectangle2)Subject.Params.ROI).Width, ((HRectangle2)Subject.Params.ROI).Height); break; case Type t when t == typeof(HCircle): inputImageHSmartWindowControl.oRoi = new HCircle(Subject.Params.ROI.X + Subject.Params.Fixture.X, Subject.Params.ROI.Y + Subject.Params.Fixture.Y , ((HCircle)Subject.Params.ROI).Radius); break; case Type t when t == typeof(HSegment): inputImageHSmartWindowControl.oRoi = new HSegment(((HSegment)Subject.Params.ROI).StartX + Subject.Params.Fixture.X, ((HSegment)Subject.Params.ROI).StartY + Subject.Params.Fixture.Y , ((HSegment)Subject.Params.ROI).EndX + Subject.Params.Fixture.X, ((HSegment)Subject.Params.ROI).EndY + Subject.Params.Fixture.Y); break; default: inputImageHSmartWindowControl.oRoi = null; break; } if (!string.IsNullOrEmpty(Subject.Params.Inputs["ModelsFullPath"]?.ToString())) ShowModels((JArray.FromObject(Subject.Params.Inputs["ModelsFullPath"])).ToObject>()); })); } /// /// 更新输出结果 /// public override void UpdataOutputs() { this.BeginInvoke(new Action(() => { //存在反序列化错乱的情况需要使用自定义的转换器 dtxtCenterX.Text = ProcessParams.ConvertToString(Subject.Params.Outputs["CenterX"]); dtxtCenterY.Text = ProcessParams.ConvertToString(Subject.Params.Outputs["CenterY"]); dtxtAngle.Text = ProcessParams.ConvertToString(Subject.Params.Outputs["Angle"]); dtxtScore.Text = ProcessParams.ConvertToString(Subject.Params.Outputs["Score"]); dtxtCount.Text = ProcessParams.ConvertToString(Subject.Params.Outputs["Count"]); })); } /// /// 点击运行 /// /// /// public override void btnRun_Click(object sender, EventArgs e) { if (Subject.InputImage != null) InputImage = Subject.InputImage; DateTime StartTime = DateTime.Now; Run(); //更新日志与结果 this.BeginInvoke(new Action(() => { if (Subject.Result) { lblResult.BackColor = Color.Green; lblResult.Text = "True"; recordImageHSmartWindowControl.SetColor("green"); } else { lblResult.BackColor = Color.Red; lblResult.Text = "False"; recordImageHSmartWindowControl.SetColor("red"); } lblMsg.Text = Msg.Length > 50 ? Msg.Substring(0, 50) : Msg; lblMsgToolTip.SetToolTip(BtmStatusStrip, Msg); lblRunTime.Text = $"{(DateTime.Now - StartTime).TotalMilliseconds}ms"; UpdataOutputs(); imgTabControl.SelectedTab = tabPageRecordImage; if (Subject.InputImage != null && Subject.InputImage is HObject) { HOperatorSet.GetImageSize((HObject)Subject.InputImage, out HTuple ho_ImageWidth, out HTuple ho_ImageHeight); recordImageHSmartWindowControl.ShowHoImage((HObject)Subject.InputImage); } //先判断子类再判断父类 if (Subject.Record != null && Subject.Record is MsgRecord) { MsgRecord MsgRecord = (MsgRecord)Subject.Record; recordImageHSmartWindowControl.DispObj(MsgRecord.RecordObject_OK, true); recordImageHSmartWindowControl.DispObj(MsgRecord.RecordObject_NG, false); for (int i = 0; i < MsgRecord.Msg.Length; i++) recordImageHSmartWindowControl.ShowMsg(MsgRecord.Msg[i], 1 == MsgRecord.Result[i] ? true : false , MsgRecord.Column[i], MsgRecord.Row[i]); } else if (Subject.Record != null && Subject.Record is ObjectRecord record) { recordImageHSmartWindowControl.DispObj(record.RecordObject_OK); recordImageHSmartWindowControl.DispObj(record.RecordObject_NG); } GC.Collect(); })); } public override void btnLoadImage_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); // 设置文件对话框的属性 openFileDialog.Multiselect = false; // 不允许多选 // 设置文件过滤器,支持多种文件类型 openFileDialog.Filter = "Image Files (*.png;*.jpg;*.jpeg;*.bmp)|*.png;*.jpg;*.jpeg;*.bmp|All Files (*.*)|*.*"; // 显示文件对话框 DialogResult result = openFileDialog.ShowDialog(); // 处理对话框返回结果 if (result == DialogResult.OK) { // 获取用户选择的文件名 string[] selectedFiles = openFileDialog.FileNames; if (selectedFiles.Length > 0) { HOperatorSet.ReadImage(out HObject ho_Image, selectedFiles[0]); //判断是否为灰度图 using (HDevDisposeHelper dh = new HDevDisposeHelper()) { HOperatorSet.CountChannels(ho_Image, out HTuple hv_Channels); if (hv_Channels.TupleInt() != 1) { if (hv_Channels.TupleInt() == 4) { HOperatorSet.Decompose4(ho_Image, out HObject R, out HObject G, out HObject B, out _); HOperatorSet.Compose3(R, G, B, out ho_Image); } HOperatorSet.Rgb1ToGray(ho_Image, out ho_Image); //更新日志与结果 this.BeginInvoke(new Action(() => { lblMsg.Text = "导入图片非灰度图,自动转换为灰度图"; })); } InputImage = ho_Image; imgTabControl.SelectedTab = tabPageInputImage; foreach (var ModelToolControl in listModelToolControls) ModelToolControl.HFindModelTool.InputImage = ho_Image.CopyObj(1, -1); inputImageHSmartWindowControl.oRoi = inputImageHSmartWindowControl.oRoi; } } } } public override void btnSaveParas_Click(object sender, EventArgs e) { //保存前需要更新输入参数Inputs UpdataInputs(); // 创建 SaveFileDialog 实例 using (SaveFileDialog saveFileDialog = new SaveFileDialog()) { // 设置对话框标题 saveFileDialog.Title = "保存文件"; // 设置默认路径 saveFileDialog.InitialDirectory = Application.StartupPath; // 设置文件类型过滤器 saveFileDialog.Filter = "文本文件 (*.json)|*.json|所有文件 (*.*)|*.*"; // 设置默认文件名 saveFileDialog.FileName = Subject.strProcessName + ".json"; // 显示对话框并检查用户是否点击了保存按钮 if (saveFileDialog.ShowDialog() == DialogResult.OK) { // 获取用户选择的文件路径 string fullPath = saveFileDialog.FileName; Debug.WriteLine("选择的文件路径是: " + fullPath); Subject.strProcessName = Path.GetFileNameWithoutExtension(fullPath); ((HFindMultiModelTool)Subject).Save(Path.GetDirectoryName(fullPath)); } } } public override void btnLoadParas_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); // 设置文件对话框的属性 openFileDialog.Multiselect = false; // 不允许多选 // 设置文件过滤器,支持多种文件类型 openFileDialog.Filter = "Ini Files (*.json)|*.json|All Files (*.*)|*.*"; // 显示文件对话框 DialogResult result = openFileDialog.ShowDialog(); // 处理对话框返回结果 if (result == DialogResult.OK) { // 获取用户选择的文件名 string[] selectedFiles = openFileDialog.FileNames; if (selectedFiles.Length > 0) { ((HFindMultiModelTool)Subject).Load(selectedFiles[0]); LoadParas(); } } } public override void ckbDrawRoi_CheckedChanged(object sender, EventArgs e) { if (ckbDrawRoi.Checked) { inputImageHSmartWindowControl.bAollowDraw = true; imgTabControl.SelectedTab = tabPageInputImage; } else { inputImageHSmartWindowControl.bAollowDraw = false; Subject.Params.ROI = new ROI(); } } public override void cmbTypeRoi_SelectedIndexChanged(object sender, EventArgs e) { try { if (Enum.TryParse(cmbTypeRoi.Text.ToString(), out RoiType type)) { HTuple hv_imageWidth = 0; HTuple hv_imageHeight = 0; if (InputImage != null && InputImage is HObject) HOperatorSet.GetImageSize((HObject)InputImage, out hv_imageWidth, out hv_imageHeight); switch (type) { case RoiType.Rectangle2: inputImageHSmartWindowControl.oRoi = new HRectangle2(hv_imageWidth.TupleReal() / 2, hv_imageHeight.TupleReal() / 2, 0 , hv_imageWidth.TupleReal() / 4, hv_imageHeight.TupleReal() / 4); break; case RoiType.Circle: inputImageHSmartWindowControl.oRoi = new HCircle(hv_imageWidth.TupleReal() / 2, hv_imageHeight.TupleReal() / 2, hv_imageWidth.TupleReal() / 4); break; case RoiType.Segment: inputImageHSmartWindowControl.oRoi = new HSegment(0, 0, hv_imageWidth.TupleReal() / 4, hv_imageHeight.TupleReal() / 4); break; case RoiType.None: default: inputImageHSmartWindowControl.oRoi = null; break; } } } catch { } } public override void cmbFixture_SelectedIndexChanged(object sender, EventArgs e) { try { if (IProcess.dicFixtures.ContainsKey(cmbFixture.Text)) Subject.Params.Fixture = IProcess.dicFixtures[cmbFixture.Text]; else Subject.Params.Fixture = new Fixture(); } catch { } } public void ShowModels(List ModelsFullPath) { try { if (ModelsFullPath == null) return; splitContainer1.Panel1.Controls.Clear(); listModelToolControls.Clear(); foreach (string ModelFullPath in ModelsFullPath) { int index = listModelToolControls.Count + 1; string name = Path.GetFileNameWithoutExtension(ModelFullPath); HMulitModelControl hMulitModelControl = new HMulitModelControl(index, name); hMulitModelControl.HFindModelTool.Load(ModelFullPath); if (InputImage != null && InputImage is HObject image && image.IsInitialized()) hMulitModelControl.HFindModelTool.InputImage = image.CopyObj(1, -1); splitContainer1.Panel1.Controls.Add(hMulitModelControl); hMulitModelControl.Dock = DockStyle.Top; listModelToolControls.Add(hMulitModelControl); //刷新显示 hMulitModelControl.UpdataModelUI(); } } catch { } } private void btnAddModel_Click(object sender, EventArgs e) { int index = listModelToolControls.Count + 1; string name = "模板" + index.ToString(); HMulitModelControl hMulitModelControl = new HMulitModelControl(index, name); splitContainer1.Panel1.Controls.Add(hMulitModelControl); hMulitModelControl.Dock = DockStyle.Top; listModelToolControls.Add(hMulitModelControl); //刷新显示 if (InputImage != null && InputImage is HObject) hMulitModelControl.HFindModelTool.InputImage = ((HObject)InputImage).CopyObj(1, -1); ((HFindMultiModelTool)Subject).HFindModelTools.Add(hMulitModelControl.HFindModelTool); } private void btnDelModel_Click(object sender, EventArgs e) { int index = listModelToolControls.Count - 1; splitContainer1.Panel1.Controls.Remove(listModelToolControls[index]); listModelToolControls[index].Dispose(); listModelToolControls.RemoveAt(index); ((HFindMultiModelTool)Subject).HFindModelTools.RemoveAt(index); } private void HFindMultiModelToolEdit_SizeChanged(object sender, EventArgs e) { try { //刷新显示 foreach (HMulitModelControl ModelToolControl in listModelToolControls) ModelToolControl.UpdataModelUI(); } catch { } } } }