C#Halcon找线封装
利用CreateMetrologyModel封装找线工具时,在后期实际应用调试时容易把检测极性搞混乱,造成检测偏差,基于此,此Demo增加画线后检测极性的指引,首先看一下效果
加载测试图片
画线
确定后指引效果
找线效果
修改显示
UI代码
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 ViewControl;
using HalconDotNet;
using System.Reflection.Emit;
using static System.Net.Mime.MediaTypeNames;
namespace DeepLearningTest1
{
public partial class Form1 : Form
{
HalconView HW;
public static MyDraw myDraw =new MyDraw();
HObject HIMage = new HObject(), ho_Arrows=new HObject();
HObject ho_Clip, ho_Cross, ho_Fit;
Double R1, R2, C1, C2;
bool DisDraw=false, DisClip=false, DisCross=false, DisFit=false;
public Form1()
{
InitializeComponent();
HW = new HalconView();
HW.HWindowControl.BackColor = Color.White;
splitContainer1.Panel1.Controls.Add(HW);
HW.Dock = DockStyle.Fill;
}
/// <summary>
/// 加载图像
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "图片文件(*.bmp;*.jpg;*.gif;*.png;*.tiff;*.tif)|*.bmp;*.jpg;*.gif;*.png;*.tiff;*.tif";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
label3.Text = openFileDialog.FileName;
HOperatorSet.ReadImage(out HIMage, label3.Text);
HW.DispImage(HIMage, true);
}
}
catch (Exception ex)
{
MessageBox.Show("加载图片失败 " + ex.ToString());
}
}
/// <summary>
/// 画线
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_DrawROI_Click(object sender, EventArgs e)
{
try
{
if (!HIMage.IsInitialized()) { MessageBox.Show("图片为空"); return; }
HSystem.SetSystem("clip_region", "false");
HW.Focus();
HW.DrawLine(out HObject Line,out HTuple r1, out HTuple c1, out HTuple r2, out HTuple c2);
R1 = r1;
R2 = r2;
C1 = c1;
C2 = c2;
tBx_R1.Text = R1.ToString("f3");
tBx_C1.Text = C1.ToString("f3");
tBx_R2.Text = R2.ToString("f3");
tBx_C2.Text = C2.ToString("f3");
myDraw.myDrawCoordinateSystem(out ho_Arrows, r1, c1, r2, c2, Convert.ToInt32(tBx_perpendicular.Text), Convert.ToInt32(tBx_Arrow.Text));
HW.HalconWindow.ClearWindow();
HW.HalconWindow.SetColor("green");
HW.DispObject(HIMage);
HW.DispObject(ho_Arrows);
}
catch (Exception ex)
{
MessageBox.Show("设置区域失败 " + ex.ToString());
}
}
/// <summary>
/// 找线
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_FindLine_Click(object sender, EventArgs e)
{
HTuple hv_L1 = new HTuple(), hv_L2 = new HTuple(), hv_NumMatch = new HTuple();
HTuple hv_Threshold = new HTuple(), hv_Select = new HTuple();
HTuple hv_Type = new HTuple(), hv_MetrologyHandle = new HTuple();
HTuple hv_Index = new HTuple(), hv_Row1 = new HTuple();
HTuple hv_Column1 = new HTuple(), hv_Parameter = new HTuple();
HOperatorSet.GenEmptyObj(out ho_Clip);
HOperatorSet.GenEmptyObj(out ho_Cross);
HOperatorSet.GenEmptyObj(out ho_Fit);
hv_L1.Dispose();
hv_L1 = Convert.ToInt32(tBx_L1.Text);
hv_L2.Dispose();
hv_L2 = Convert.ToInt32(tBx_L2.Text);
hv_NumMatch.Dispose();
hv_NumMatch = Convert.ToInt32(tBx_num.Text);
hv_Threshold.Dispose();
hv_Threshold = Convert.ToInt32(tBx_Thre.Text);
hv_Select.Dispose();
hv_Select = cBx_select.Text;
hv_Type.Dispose();
hv_Type = cBx_P.Text;
hv_MetrologyHandle.Dispose();
HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle);
hv_Index.Dispose();
HOperatorSet.AddMetrologyObjectLineMeasure(hv_MetrologyHandle, R1, C1,
R2, C2, 1, 1, 1, 30, new HTuple(), new HTuple(), out hv_Index);
HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "measure_length1",
hv_L1);
HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "measure_length2",
hv_L2);
HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "num_measures",
hv_NumMatch);
//边缘阈值
HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "measure_threshold",
hv_Threshold);
//边缘选择
HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "measure_select",
hv_Select);
//过度方式
if ((int)(new HTuple(hv_Type.TupleEqual("由白到黑"))) != 0)
{
HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "measure_transition",
"positive");
}
else if ((int)(new HTuple(hv_Type.TupleEqual("由黑到白"))) != 0)
{
HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "measure_transition",
"negative");
}
else if ((int)(new HTuple(hv_Type.TupleEqual("全部"))) != 0)
{
HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "measure_transition",
"all");
}
HOperatorSet.ApplyMetrologyModel(HIMage, hv_MetrologyHandle);
try
{
//测量点
ho_Clip.Dispose(); hv_Row1.Dispose(); hv_Column1.Dispose();
HOperatorSet.GetMetrologyObjectMeasures(out ho_Clip, hv_MetrologyHandle,
"all", "all", out hv_Row1, out hv_Column1);
ho_Cross.Dispose();
HOperatorSet.GenCrossContourXld(out ho_Cross, hv_Row1, hv_Column1, 6, 0.785398);
//测量直线
hv_Parameter.Dispose();
HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, 0, "all", "result_type",
(((new HTuple("row_begin")).TupleConcat("column_begin")).TupleConcat("row_end")).TupleConcat(
"column_end"), out hv_Parameter);
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
ho_Fit.Dispose();
HOperatorSet.GenContourPolygonXld(out ho_Fit, ((hv_Parameter.TupleSelect(
0))).TupleConcat(hv_Parameter.TupleSelect(2)), ((hv_Parameter.TupleSelect(
1))).TupleConcat(hv_Parameter.TupleSelect(3)));
}
HOperatorSet.ClearMetrologyModel(hv_MetrologyHandle);
}
catch (Exception)
{
MessageBox.Show("找线失败");
}
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
if (checkBox4.Checked)
{
DisFit = true;
}
else
{
DisFit = false;
}
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
if (checkBox3.Checked)
{
DisCross = true;
}
else
{
DisCross = false;
}
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked)
{
DisClip = true;
}
else
{
DisClip = false;
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
DisDraw = true;
}
else
{
DisDraw = false;
}
}
/// <summary>
/// 显示结果
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_DisResult_Click(object sender, EventArgs e)
{
if (DisDraw)
{
HW.HalconWindow.SetColor("white");
HW.DispObject(ho_Arrows);
}
if (DisClip)
{
HW.HalconWindow.SetColor("blue");
HW.DispObject(ho_Clip);
}
if (DisCross)
{
HW.HalconWindow.SetColor("yellow");
HW.DispObject(ho_Cross);
}
if (DisFit)
{
HW.HalconWindow.SetColor("orange red");
HW.DispObject(ho_Fit);
}
}
}
}