C#Halcon找线封装

devtools/2025/1/11 21:53:06/

       利用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.ppmy.cn/devtools/149704.html

相关文章

探索Java爬虫:获取淘宝App商品详情原数据API接口的实现

在当今的互联网时代&#xff0c;数据是最宝贵的资源之一。对于电商平台而言&#xff0c;商品详情数据更是重中之重。本文将带你深入了解如何使用Java爬虫获取淘宝App商品详情的原数据API接口&#xff0c;帮助你掌握这一技术&#xff0c;提升数据获取能力。 一、为什么选择Java…

深入理解 JSON 数据传递方式:数组格式与对象包装格式的对比与选择

深入理解 JSON 数据传递方式&#xff1a;数组格式与对象包装格式的对比与选择 深入理解 JSON 数据传递方式&#xff1a;数组格式与对象包装格式的对比与选择一、JSON 数组格式与对象包装格式的概述1. JSON 数组格式2. JSON 对象包装格式 二、数组格式与对象包装格式的主要区别1…

有收到腾讯委托律师事务所向AppStore投诉带有【水印相机】主标题名称App的开发者吗

近期&#xff0c;有多名开发者反馈&#xff0c;收到来自腾讯科技 (深圳) 有限公司委托北京的一家**诚律师事务所卞&#xff0c;写给AppStore的投诉邮件。 邮件内容主要说的是&#xff0c;腾讯注册了【水印相机】这四个字的商标&#xff0c;所以你们这些在AppStore上的app&…

JAVA 冒泡排序算法

1.冒泡排序 冒泡排序是最基本的排序算法&#xff0c;通过对待排序序列从前向后依次比较相邻元素的值&#xff0c;如果发现逆序就进行交换&#xff0c;使值较大的元素从前向后移&#xff0c;就像水底下的气泡一样逐渐向上冒。 冒泡排序算法是重复地遍历要排序的数列&#xff0c…

每天40分玩转Django:Django Docker化学习指南

Django Docker化学习指南 1. 学习目标 理解Docker容器化的基本概念和优势掌握Django应用的Docker化过程学习使用Docker Compose管理多容器应用 2. 核心知识点 知识点重要程度掌握要求Dockerfile编写⭐⭐⭐⭐⭐熟练掌握Docker基本命令⭐⭐⭐⭐熟练掌握Docker Compose配置⭐⭐…

如何高效格式化输出 JSON 字符串

引言 JSON&#xff08;JavaScript Object Notation&#xff09;作为一种轻量级的数据交换格式&#xff0c;已经成为了各种编程语言间传递数据的标准。无论是在 Web 开发中与前端进行数据交互&#xff0c;还是在微服务架构中进行服务之间的通信&#xff0c;JSON 数据格式都有着…

C++ 实现简单多数法

以下是几种用 C 实现简单多数法的代码示例&#xff1a; 暴力遍历法 收起 cpp #include <iostream> #include <vector>char majorityElementBruteForce(const std::vector<char>& grades) {int n grades.size();for (int i 0; i < n; i) {int cou…

基于单片机的数字气压计设计

摘要:在嵌入式技术快速发展过程中&#xff0c;智能测量仪器被广泛应用于工业生产以及人们日常生活领域。数字气压计在实际应用中&#xff0c;利用气压传感器检测环境中的压力大小&#xff0c;便于实现对设备进行智能化的控制操作。数字气压计在气象监测、矿产开采、科学实验等环…