using HalconDotNet;
|
using Newtonsoft.Json.Linq;
|
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 OpenCvSharp;
|
using System.Diagnostics;
|
using LB_VisionControl;
|
|
namespace LB_VisionProcesses.Alogrithms
|
{
|
public partial class TAlgorithmEdit : UserControl
|
{
|
public IProcess Subject;
|
public UserHSmartWindowControl inputImageHSmartWindowControl = new UserHSmartWindowControl();
|
public UserHSmartWindowControl recordImageHSmartWindowControl = new UserHSmartWindowControl();
|
|
public ToolTip lblMsgToolTip = new ToolTip();
|
|
public bool Result
|
{
|
get
|
{
|
if (Subject == null)
|
return false;
|
return Subject.Result;
|
}
|
set
|
{
|
if (Subject != null)
|
Subject.Result = value;
|
}
|
}
|
|
public string Msg
|
{
|
get
|
{
|
if (Subject == null)
|
return "算法为空";
|
return Subject.Msg.Trim();
|
}
|
}
|
|
public Object InputImage
|
{
|
get
|
{
|
if (Subject == null)
|
{
|
return null;
|
}
|
return Subject.InputImage;
|
}
|
set
|
{
|
Subject.InputImage = value;
|
if (InputImage == null)
|
{
|
return;
|
}
|
|
if (InputImage is HObject)
|
{
|
inputImageHSmartWindowControl.ShowHoImage((HObject)value);
|
}
|
else if (InputImage is Bitmap)
|
{
|
using (HImage image = TAlgorithm.Bitmap2HObject((Bitmap)value))
|
{
|
inputImageHSmartWindowControl.ShowHoImage(image);
|
}
|
}
|
else if (InputImage is Mat)
|
{
|
using (HImage image = TAlgorithm.Mat2HObject((Mat)value))
|
{
|
inputImageHSmartWindowControl.ShowHoImage(image);
|
}
|
}
|
}
|
}
|
|
public TAlgorithmEdit() { }
|
|
public TAlgorithmEdit(TAlgorithm subject = null)
|
{
|
if (subject != null)
|
Subject = subject;
|
else
|
Subject = new TAlgorithm();
|
this.Dock = DockStyle.Fill;
|
|
lblMsgToolTip.AutoPopDelay = 5000; // 提示显示5秒后消失
|
lblMsgToolTip.InitialDelay = 500; // 鼠标悬停500毫秒后显示提示
|
|
InitializeComponent();
|
}
|
|
/// <summary>
|
/// 运行算子
|
/// </summary>
|
/// <param name="minThreshold"></param>
|
/// <param name="maxThreshold"></param>
|
/// <param name="minArea"></param>
|
/// <param name="maxArea"></param>
|
/// <param name="angle"></param>
|
/// <returns></returns>
|
public virtual bool Run()
|
{
|
//运行前需要更新输入参数Paras
|
UpdataInputs();
|
Subject.Run();
|
return Subject.Result;
|
}
|
|
/// <summary>
|
/// 更新运行参数
|
/// </summary>
|
public virtual void UpdataInputs()
|
{
|
Type type = inputImageHSmartWindowControl.oRoi?.GetType();
|
switch (type)
|
{
|
case Type t when t == typeof(HRectangle2):
|
HRectangle2 hRectangle2 = (HRectangle2)inputImageHSmartWindowControl.oRoi;
|
Subject.Params.ROI
|
= new HRectangle2(hRectangle2.X - Subject.Params.Fixture.X, hRectangle2.Y - Subject.Params.Fixture.Y
|
, hRectangle2.Phi - Subject.Params.Fixture.Phi, hRectangle2.Width, hRectangle2.Height);
|
break;
|
case Type t when t == typeof(HCircle):
|
HCircle hCircle = (HCircle)inputImageHSmartWindowControl.oRoi;
|
Subject.Params.ROI
|
= new HCircle(hCircle.X - Subject.Params.Fixture.X, hCircle.Y - Subject.Params.Fixture.Y, hCircle.Radius);
|
break;
|
case Type t when t == typeof(HEllipse):
|
HEllipse hEllipse = (HEllipse)inputImageHSmartWindowControl.oRoi;
|
Subject.Params.ROI
|
= new HEllipse(hEllipse.X - Subject.Params.Fixture.X, hEllipse.Y - Subject.Params.Fixture.Y
|
, hEllipse.Phi, hEllipse.Radius1, hEllipse.Radius2, hEllipse.StartAngle, hEllipse.EndAngle);
|
break;
|
case Type t when t == typeof(HSegment):
|
HSegment hSegment = (HSegment)inputImageHSmartWindowControl.oRoi;
|
Subject.Params.ROI
|
= new HSegment(hSegment.StartX - Subject.Params.Fixture.X, hSegment.StartY - Subject.Params.Fixture.Y
|
, hSegment.EndX - Subject.Params.Fixture.X, hSegment.EndY - Subject.Params.Fixture.Y);
|
break;
|
default:
|
Subject.Params.ROI = new ROI();
|
break;
|
}
|
}
|
|
/// <summary>
|
/// 加载运行参数
|
/// </summary>
|
public virtual void LoadParas()
|
{
|
Type type = Subject.Params.ROI?.GetType();
|
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(HEllipse):
|
inputImageHSmartWindowControl.oRoi
|
= new HEllipse(Subject.Params.ROI.X + Subject.Params.Fixture.X, Subject.Params.ROI.Y + Subject.Params.Fixture.Y
|
, ((HEllipse)Subject.Params.ROI).Phi, ((HEllipse)Subject.Params.ROI).Radius1, ((HEllipse)Subject.Params.ROI).Radius2
|
, ((HEllipse)Subject.Params.ROI).StartAngle, ((HEllipse)Subject.Params.ROI).EndAngle);
|
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;
|
}
|
}
|
|
/// <summary>
|
/// 更新运行结果
|
/// </summary>
|
public virtual void UpdataOutputs() { }
|
|
public virtual void btnRun_Click(object sender, EventArgs e) { }
|
|
public virtual 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]);
|
InputImage = ho_Image;
|
}
|
}
|
}
|
|
public virtual void btnSaveParas_Click(object sender, EventArgs e)
|
{
|
UpdataInputs();
|
// 创建 SaveFileDialog 实例
|
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
|
{
|
// 设置对话框标题
|
saveFileDialog.Title = "保存文件";
|
|
// 设置默认路径
|
saveFileDialog.InitialDirectory = Application.StartupPath;
|
|
// 设置文件类型过滤器
|
saveFileDialog.Filter = "文本文件 (*.json)|*.json|所有文件 (*.*)|*.*";
|
|
// 设置默认文件名
|
saveFileDialog.FileName = "Algorithm_1.json";
|
|
// 显示对话框并检查用户是否点击了保存按钮
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
{
|
// 获取用户选择的文件路径
|
string fullPath = saveFileDialog.FileName;
|
Debug.WriteLine("选择的文件路径是: " + fullPath);
|
Subject.strProcessName = Path.GetFileNameWithoutExtension(fullPath);
|
//Subject.oRoi = inputImageHSmartWindowControl.oRoi;
|
Subject.Save(System.IO.Path.GetDirectoryName(fullPath));
|
}
|
}
|
}
|
|
public virtual 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)
|
{
|
Subject.Load(selectedFiles[0]);
|
LoadParas();
|
}
|
}
|
}
|
|
public virtual void ckbDrawRoi_CheckedChanged(object sender, EventArgs e) { }
|
|
public virtual void cmbTypeRoi_SelectedIndexChanged(object sender, EventArgs e) { }
|
|
public virtual void cmbFixture_SelectedIndexChanged(object sender, EventArgs e) { }
|
}
|
}
|