当前位置: 首页 > article >正文

C#Halcon找线封装

       利用CreateMetrologyModel封装找线工具时,在后期实际应用调试时容易把检测极性搞混乱,造成检测偏差,基于此,此Demo增加画线后检测极性的指引,首先看一下效果

加载测试图片

d4eb66a6bc484cb3948a1f92b60831c8.png

画线

114504d9427649f9a650ea1704869154.png

确定后指引效果

b158feb806d744119881cf4636c18e91.png

找线效果

bdf95d037ab5428a87d8b765329195d4.png

修改显示

fa27f92450cb49b9a9015a0634867b6b.png

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);
            }
        }

    }
}

 

 


http://www.kler.cn/a/488499.html

相关文章:

  • 计算机的错误计算(二百零七)
  • 为深度学习引入张量
  • imageio 图片转mp4 保存mp4
  • 测试ip端口-telnet开启与使用
  • Linux服务器网络不通问题排查及常用命令使用
  • Linux存储管理之核心秘密(The Core Secret of Linux Storage Management)
  • 关于地平线开发板使用nhwc格式的前向传播输出格式的理解
  • 【FlutterDart】tolyui_feedback组件例子效果(23 /100)
  • vue3 初体验
  • 学习通过几何约束从单个图像预测 3D 车道形状和相机姿态 | 论文解读
  • 前端哪些内容最好添加专属前缀?
  • 嵌入式系统 tensorflow
  • HarmonyOS开发:ArkTS初识
  • Windows使用AutoHotKey解决鼠标键连击现象(解决鼠标连击、单击变双击的故障)
  • package包机制详解
  • HTML实战课堂之倒计时页面
  • 如何使用Scala和Selenium爬取知乎视频并保存到本地
  • 分布式ID—雪花算法
  • 【python翻译软件V1.0】
  • 计算机毕业设计hadoop+spark+hive新能源汽车推荐系统 汽车数据分析可视化大屏 新能源汽车推荐系统 汽车爬虫 汽车大数据 机器学习
  • istio-proxy oom问题排查步骤
  • JVM 触发类加载的条件有哪些?
  • 修改sshd默认配置,提升安全
  • Elasticsearch—索引库操作(增删查改)
  • word论文排版常见问题汇总
  • 【JAVA】时间戳和日期时间互转