C#Halcon找线封装

server/2025/1/11 8:38:03/

       利用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/server/157414.html

相关文章

详细分析 创建并上传到 GitHub 仓库

目录 前言1. 从零创建并上传代码到 GitHub2. 将现有的本地仓库推送到 GitHub 前言 &#x1f91f; 找工作&#xff0c;来万码优才&#xff1a;&#x1f449; #小程序://万码优才/r6rqmzDaXpYkJZF 创建仓库的时候&#xff0c;平台已经有所提供流程&#xff01; 1. 从零创建并上传…

机器学习之基本概念 - 数据集、训练集、特征向量、独立同分布的

机器学习是对能通过经验自动改进的计算机算法的研究. ——汤姆米切尔(Tom Mitchell)[Mitchell, 1997] 思考一个问题&#xff1a; 如何让计算机能自动识别手写的数字&#xff1f; ————------------------———————分割线—————————————————-------…

部署HugeGraph

部署HugeGraph 这里以hugegraph1.2.0为例子&#xff0c;演示一下如何安装部署hugegraph 一、下载并安装JDK11 下载JDK11 https://www.oracle.com/java/technologies/downloads/#java11 使用scp命令将安装包上传到服务器 scp /path/to/local/file usernameserver_ip:/path/…

ue5 GAS 从零开始00

技能属性GAS 技能 属性 创建一个项目c 插件搜索 gameplay 保证这里勾选上 把这三个弄上去 “GameplayAbilities”,“GameplayTags”,“GameplayTasks” 这样就加载了三个模块 一定要先关ue 先关掉ue 生成 如果没编过&#xff0c;你就检查模块名字是不是没写对 一定要…

网络安全:守护数字世界的防线

在信息化时代&#xff0c;网络已成为人们生活和工作的基础设施&#xff0c;从在线购物、社交互动到企业运营、政府服务&#xff0c;无处不在的网络连接着全球各地的人们和组织。然而&#xff0c;网络的便捷性也带来了诸多安全风险&#xff0c;网络安全问题日益凸显&#xff0c;…

EasyCVR视频汇聚平台如何配置webrtc播放地址?

EasyCVR安防监控视频系统采用先进的网络传输技术&#xff0c;支持高清视频的接入和传输&#xff0c;能够满足大规模、高并发的远程监控需求。平台支持多协议接入&#xff0c;能将接入到视频流转码为多格式进行分发&#xff0c;包括RTMP、RTSP、HTTP-FLV、WebSocket-FLV、HLS、W…

OpenCV的对比度受限的自适应直方图均衡化算法

OpenCV的对比度受限的自适应直方图均衡化&#xff08;CLAHE&#xff09;算法是一种图像增强技术&#xff0c;旨在改善图像的局部对比度&#xff0c;同时避免噪声的过度放大。以下是CLAHE算法的原理、步骤以及示例代码。 1 原理 CLAHE是自适应直方图均衡化&#xff08;AHE&…

TCP__滑动窗口__拥塞控制

目录 1. 滑动窗口1.1 滑动窗口在哪里&#xff1f;1.2 如何理解滑动窗口1.3 滑动窗口的大小会变化吗&#xff1f;1.4 其它问题 2. 拥塞控制2.1 拥塞避免算法2.1.1 怎么理解拥塞窗口2.1.2 避免拥塞算法&#xff08;慢启动&#xff09;的思想2.1.3 慢启动的阈值2.1.4 总结&#xf…