using HalconDotNet;
|
using LB_VisionControls;
|
using System.Data.Common;
|
using LB_VisionProcesses.Alogrithms;
|
using System.Windows.Forms;
|
using static LB_VisionProcesses.Alogrithms.PointsCloudConvertTool;
|
using OpenCvSharp;
|
using System;
|
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
using System.Diagnostics;
|
|
namespace LB_VisionProcesses.Alogrithms
|
{
|
public partial class PointsCloudConvertToolEdit : TAlgorithmEdit
|
{
|
HWindowControl inputImageHSmartWindowControl = new HWindowControl();
|
HWindowControl recordImageHSmartWindowControl = new HWindowControl();
|
|
public PointsCloudConvertToolEdit(PointsCloudConvertTool subject = null)
|
{
|
if (subject != null && subject is PointsCloudConvertTool)
|
{
|
Subject = subject;
|
}
|
else
|
{
|
Subject = new PointsCloudConvertTool();
|
}
|
|
this.Dock = DockStyle.Fill;
|
InitializeComponent();
|
}
|
|
/// <summary>
|
/// 控件加载事件
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void PointsCloudConvertToolEdit_Load(object sender, EventArgs e)
|
{
|
pnlInputImage.Controls.Add(inputImageHSmartWindowControl);
|
inputImageHSmartWindowControl.Dock = DockStyle.Fill;
|
|
pnlRecordImage.Controls.Add(recordImageHSmartWindowControl);
|
recordImageHSmartWindowControl.Dock = DockStyle.Fill;
|
|
//遍历可以选择的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());
|
}
|
|
//遍历可以选择的图片格式枚举
|
foreach (var value in Enum.GetValues(typeof(ImageType)))
|
{
|
cmbInputType.Items.Add(value.ToString());
|
cmbOutputType.Items.Add(value.ToString());
|
}
|
|
//foreach (var value in Enum.GetValues(typeof(PixelType)))
|
// cmbAfterType.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";
|
|
// 把窗口对象推窗口栈中
|
HDevWindowStack.Push(inputImageHSmartWindowControl.HalconWindow);
|
HDevWindowStack.Push(recordImageHSmartWindowControl.HalconWindow);
|
}
|
|
/// <summary>
|
/// 更新运行参数
|
/// </summary>
|
public override void UpdataInputs()
|
{
|
double dResult = 0;
|
|
//设置运行参数
|
Subject.Params.Inputs["输入格式"] = cmbInputType.SelectedItem.ToString();
|
Subject.Params.Inputs["输出格式"] = cmbOutputType.SelectedItem.ToString();
|
Subject.Params.Inputs["是否后处理"] = ckbAfter.Checked;
|
|
if (cmbFixture.Text == "")
|
{
|
Subject.Params.Fixture = new Fixture();
|
}
|
else if (IProcess.dicFixtures.ContainsKey(cmbFixture.Text))
|
{
|
Subject.Params.Fixture = IProcess.dicFixtures[cmbFixture.Text];
|
}
|
|
//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(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 override void LoadParas()
|
{
|
this.BeginInvoke(new Action(() =>
|
{
|
cmbInputType.Text = Subject.Params.Inputs["输入格式"].ToString();
|
cmbOutputType.Text = Subject.Params.Inputs["输出格式"].ToString();
|
ckbAfter.Checked = Convert.ToBoolean(Subject.Params.Inputs["是否后处理"]);
|
//cmbAfterType.Text = Subject.Params.Inputs["后处理格式"].ToString();
|
|
if (Subject.InputImage != null)
|
{
|
if (Subject.InputImage is HObject)
|
{
|
TAlgorithm.DispImage((HObject)Subject.InputImage, inputImageHSmartWindowControl.HalconWindow);
|
}
|
else if (Subject.InputImage is HTuple)
|
{
|
btnLoadImage.Enabled = false;
|
btnLoadParas.Enabled = false;
|
HOperatorSet.ClearWindow(inputImageHSmartWindowControl.HalconWindow);
|
// 显示3D模型
|
Task.Run(() =>
|
{
|
HTuple hv_Pose = new HTuple();
|
HTuple hv_CamParam = new HTuple();
|
HTuple hv_GenParamName = new HTuple();
|
HTuple hv_GenParamValue = new HTuple();
|
hv_GenParamName = new HTuple("lut", "intensity");
|
hv_GenParamValue = new HTuple("color1", "coord_z");
|
TAlgorithm.visualize_object_model_3d(inputImageHSmartWindowControl.HalconWindow
|
, (HTuple)Subject.InputImage, hv_CamParam,
|
hv_Pose, hv_GenParamName, hv_GenParamValue, "显示3D点云模型",
|
new HTuple(), new HTuple(), out HTuple poseOut);
|
btnLoadImage.Enabled = true;
|
btnLoadParas.Enabled = true;
|
});
|
}
|
}
|
|
|
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;
|
//}
|
}));
|
}
|
|
/// <summary>
|
/// 更新输出结果
|
/// </summary>
|
public override void UpdataOutputs()
|
{
|
this.BeginInvoke(new Action(() =>
|
{
|
|
}));
|
}
|
|
/// <summary>
|
/// 点击运行
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
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.OutputImage != null)
|
{
|
if (Subject.OutputImage is HObject)
|
{
|
TAlgorithm.DispImage((HObject)Subject.OutputImage, recordImageHSmartWindowControl.HalconWindow);
|
}
|
else if (Subject.OutputImage is HTuple)
|
{
|
btnRun.Enabled = false;
|
HOperatorSet.ClearWindow(recordImageHSmartWindowControl.HalconWindow);
|
// 显示3D模型
|
Task.Run(() =>
|
{
|
HTuple hv_Pose = new HTuple();
|
HTuple hv_CamParam = new HTuple();
|
HTuple hv_GenParamName = new HTuple();
|
HTuple hv_GenParamValue = new HTuple();
|
//hv_GenParamName = new HTuple("lut", "intensity");
|
//hv_GenParamValue = new HTuple("color1", "coord_z");
|
|
hv_GenParamName = new HTuple("disp_pose", "color_attrib", "lut");
|
hv_GenParamValue = new HTuple("true", "coord_z", "color1");
|
TAlgorithm.visualize_object_model_3d(recordImageHSmartWindowControl.HalconWindow
|
, (HTuple)Subject.OutputImage, hv_CamParam,
|
hv_Pose, hv_GenParamName, hv_GenParamValue, "显示3D点云模型",
|
new HTuple(), new HTuple(), out HTuple poseOut);
|
btnRun.Enabled = true;
|
});
|
}
|
}
|
|
//先判断子类再判断父类
|
//if (Subject.Record != null && Subject.Record is MsgRecord)
|
//{
|
// MsgRecord MsgRecord = (MsgRecord)Subject.Record;
|
// recordImageHSmartWindowControl.DispObj(MsgRecord.RecordObject);
|
|
// for (int i = 0; i < MsgRecord.Msg.Length; i++)
|
// recordImageHSmartWindowControl.ShowMsg(MsgRecord.Msg[i], Subject.Result, MsgRecord.Column[i], MsgRecord.Row[i]);
|
//}
|
//else if (Subject.Record != null && Subject.Record is ObjectRecord)
|
// recordImageHSmartWindowControl.DispObj(((ObjectRecord)Subject.Record).RecordObject);
|
|
GC.Collect();
|
}));
|
}
|
|
public override void btnLoadImage_Click(object sender, EventArgs e)
|
{
|
OpenFileDialog openFileDialog = new OpenFileDialog();
|
|
// 设置文件对话框的属性
|
openFileDialog.Multiselect = false; // 不允许多选
|
// 设置文件过滤器,支持多种文件类型
|
bool bEnumParseSuccess = Enum.TryParse(cmbInputType.Text, out ImageType eInputImageType);
|
|
if (bEnumParseSuccess)
|
{
|
switch (eInputImageType)
|
{
|
case ImageType.DeepImage:
|
{
|
openFileDialog.Filter = "Image Files (*.tif;*.tiff)|*.tif;*.tiff|All Files (*.*)|*.*";
|
break;
|
}
|
case ImageType.PointsCloud:
|
{
|
openFileDialog.Filter = "Image Files (*.om3)|*.om3|All Files (*.*)|*.*";
|
break;
|
}
|
default:
|
{
|
return;
|
}
|
}
|
}
|
else
|
{
|
return;
|
}
|
// 显示文件对话框
|
DialogResult result = openFileDialog.ShowDialog();
|
|
// 处理对话框返回结果
|
if (result == DialogResult.OK)
|
{
|
// 获取用户选择的文件名
|
string[] selectedFiles = openFileDialog.FileNames;
|
if (selectedFiles.Length > 0)
|
{
|
if (bEnumParseSuccess)
|
{
|
switch (eInputImageType)
|
{
|
case ImageType.DeepImage:
|
{
|
HOperatorSet.ReadImage(out HObject ho_Image, selectedFiles[0]);
|
if (ho_Image == null)
|
{
|
lblResult.BackColor = Color.Red;
|
lblResult.Text = "False";
|
lblMsg.Text = $"图片导入失败,路径为{selectedFiles[0]}";
|
return;
|
}
|
Subject.InputImage = ho_Image;
|
imgTabControl.SelectedTab = tabPageInputImage;
|
TAlgorithm.DispImage(ho_Image, inputImageHSmartWindowControl.HalconWindow);
|
break;
|
}
|
case ImageType.PointsCloud:
|
{
|
HOperatorSet.ReadObjectModel3d(selectedFiles[0], "m", new HTuple(), new HTuple()
|
, out HTuple hv_ObjectModel3D, out HTuple hv_Status);
|
if (hv_ObjectModel3D == null)
|
{
|
lblResult.BackColor = Color.Red;
|
lblResult.Text = "False";
|
lblMsg.Text = $"图片导入失败,路径为{selectedFiles[0]}";
|
return;
|
}
|
Subject.InputImage = hv_ObjectModel3D;
|
imgTabControl.SelectedTab = tabPageInputImage;
|
btnLoadImage.Enabled = false;
|
HOperatorSet.ClearWindow(inputImageHSmartWindowControl.HalconWindow);
|
// 显示3D模型
|
Task.Run(() =>
|
{
|
HTuple hv_Pose = new HTuple();
|
HTuple hv_CamParam = new HTuple();
|
HTuple hv_GenParamName = new HTuple();
|
HTuple hv_GenParamValue = new HTuple();
|
//hv_GenParamName = new HTuple("lut", "intensity");
|
//hv_GenParamValue = new HTuple("color1", "coord_z");
|
|
hv_GenParamName = new HTuple("disp_pose", "color_attrib", "lut");
|
hv_GenParamValue = new HTuple("true", "coord_z", "color1");
|
|
TAlgorithm.visualize_object_model_3d(inputImageHSmartWindowControl.HalconWindow
|
, hv_ObjectModel3D, hv_CamParam,
|
hv_Pose, hv_GenParamName, hv_GenParamValue, "显示3D点云模型",
|
new HTuple(), new HTuple(), out HTuple poseOut);
|
btnLoadImage.Enabled = true;
|
});
|
break;
|
}
|
default:
|
{
|
lblResult.BackColor = Color.Red;
|
lblResult.Text = "False";
|
lblMsg.Text = $"不支持输入格式[{cmbInputType.Text}]";
|
return;
|
}
|
}
|
}
|
}
|
}
|
}
|
|
public override void btnSaveParas_Click(object sender, EventArgs e) { base.btnSaveParas_Click(sender, e); }
|
|
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 = null;
|
//}
|
}
|
|
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 { }
|
}
|
|
private void ckbAfter_CheckedChanged(object sender, EventArgs e)
|
{
|
cmbAfterType.Enabled = ckbAfter.Checked;
|
}
|
|
private void btnSaveImage_Click(object sender, EventArgs e)
|
{
|
// 创建 SaveFileDialog 实例
|
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
|
{
|
// 设置对话框标题
|
saveFileDialog.Title = "保存文件";
|
|
// 设置默认路径
|
saveFileDialog.InitialDirectory = Application.StartupPath;
|
|
bool bEnumParseSuccess = Enum.TryParse(cmbOutputType.Text, out ImageType eOutputImageType);
|
|
if (bEnumParseSuccess && Subject.OutputImage != null)
|
{
|
switch (eOutputImageType)
|
{
|
case ImageType.DeepImage:
|
{
|
// 设置文件类型过滤器
|
saveFileDialog.Filter = "图像文件 (*.tif)|*.tif|所有文件 (*.*)|*.*";
|
// 设置默认文件名
|
saveFileDialog.FileName = "output.tif";
|
break;
|
}
|
case ImageType.PointsCloud:
|
{
|
// 设置文件类型过滤器
|
saveFileDialog.Filter = "图像文件 (*.om3)|*.om3|所有文件 (*.*)|*.*";
|
// 设置默认文件名
|
saveFileDialog.FileName = "output.om3";
|
break;
|
}
|
default:
|
{
|
return;
|
}
|
}
|
}
|
else
|
{
|
return;
|
}
|
|
|
// 显示对话框并检查用户是否点击了保存按钮
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
{
|
// 获取用户选择的文件路径
|
string fullPath = saveFileDialog.FileName;
|
Debug.WriteLine("选择的文件路径是: " + fullPath);
|
|
if (!string.IsNullOrEmpty(fullPath))
|
{
|
if (bEnumParseSuccess)
|
{
|
switch (eOutputImageType)
|
{
|
case ImageType.DeepImage:
|
{
|
try
|
{
|
if (Subject.OutputImage is HObject ho_Image)
|
HOperatorSet.WriteImage(ho_Image, "bmp", 0, fullPath);
|
}
|
catch { }
|
break;
|
}
|
case ImageType.PointsCloud:
|
{
|
try
|
{
|
if (Subject.OutputImage is HTuple hv_Image)
|
HOperatorSet.WriteObjectModel3d(hv_Image
|
, "om3", fullPath, new HTuple(), new HTuple());
|
}
|
catch { }
|
break;
|
}
|
default:
|
{
|
return;
|
}
|
}
|
}
|
}
|
}
|
}
|
}
|
}
|
}
|