VisionPro项目记录3 —— 圆心距

article/2025/7/2 1:06:57

简介:本项目实现基于Cognex视觉系统的两圆心对位功能,使用一个圆作为基准,另一个圆进行补偿,输出偏移值给PLC或机械手。

系统采用CogFindCircleTool定位两圆心坐标,通过脚本计算圆心距并乘以缩放系数kValue,输出X/Y方向偏移值及实际距离。当圆心距小于0.1时判定为合格,并在界面显示彩色标注的圆心距数值(蓝色合格、红色不合格)。

主要包含图像处理工具(CogHistogramTool)、圆心定位工具和结果输出模块,通过C#脚本实现坐标转换、距离计算和视觉反馈功能。

根据实际需求,可使用IPOne工具处理图像,并借助His工具进行相关判断分析。

脚本计算出圆心距并添加显示并输出。

脚本源码

#region namespace imports
using System;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Cognex.VisionPro;
using Cognex.VisionPro.ToolBlock;
using Cognex.VisionPro3D;
using Cognex.VisionPro.ImageProcessing;
using Cognex.VisionPro.PMAlign;
using Cognex.VisionPro.CalibFix;
using Cognex.VisionPro.Caliper;
#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{#region Private Member Variablesprivate Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;#endregion/// <summary>/// Called when the parent tool is run./// Add code here to customize or replace the normal run behavior./// </summary>/// <param name="message">Sets the Message in the tool's RunStatus.</param>/// <param name="result">Sets the Result in the tool's RunStatus</param>/// <returns>True if the tool should run normally,///          False if GroupRun customizes run behavior</returns>CogGraphicLabel label;public override bool GroupRun(ref string message, ref CogToolResultConstants result){// To let the execution stop in this script when a debugger is attached, uncomment the following lines.// #if DEBUG// if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();// #endif// Run each tool using the RunTool functionforeach(ICogTool tool in mToolBlock.Tools)mToolBlock.RunTool(tool, ref message, ref result);double kValue = (double) mToolBlock.Inputs["kvalue"].Value;    bool flowResult = false;CogHistogramTool his = mToolBlock.Tools["CogHistogramTool1"] as CogHistogramTool;CogFindCircleTool circle_1 = mToolBlock.Tools["CogFindCircleTool1"] as CogFindCircleTool;CogFindCircleTool circle_2 = mToolBlock.Tools["CogFindCircleTool2"] as CogFindCircleTool;double cir_X1=0,cir_Y1=0,cir_X2=0,cir_Y2=0,distance=0;cir_X1 = circle_1.Results.GetCircle().CenterX;cir_Y1 = circle_1.Results.GetCircle().CenterY;cir_X2 = circle_2.Results.GetCircle().CenterX;cir_Y2 = circle_2.Results.GetCircle().CenterY;mToolBlock.Outputs["double2"].Value = (cir_X1 - cir_X2) * kValue;mToolBlock.Outputs["double1"].Value = (cir_Y1 - cir_Y2) * kValue;// 圆心距distance = Math.Sqrt((cir_X1 - cir_X2) *(cir_X1 - cir_X2)+(cir_Y1 - cir_Y2) *(cir_Y1 - cir_Y2))* kValue;mToolBlock.Outputs["double3"].Value = distance;// 判断if (distance < 0.1){flowResult = true;}mToolBlock.Outputs["FlowResult"].Value = flowResult;label = new CogGraphicLabel();string str = "圆心距:" +distance.ToString("F2");label.SetXYText(130,60,str);label.Font = new Font("楷体", 30);label.Color = flowResult ? CogColorConstants.Blue : CogColorConstants.Red;return false;}#region When the Current Run Record is Created/// <summary>/// Called when the current record may have changed and is being reconstructed/// </summary>/// <param name="currentRecord">/// The new currentRecord is available to be initialized or customized.</param>public override void ModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord){}#endregion#region When the Last Run Record is Created/// <summary>/// Called when the last run record may have changed and is being reconstructed/// </summary>/// <param name="lastRecord">/// The new last run record is available to be initialized or customized.</param>public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord){mToolBlock.AddGraphicToRunRecord(label, lastRecord, "CogPMAlignTool1.InputImage", "");}#endregion#region When the Script is Initialized/// <summary>/// Perform any initialization required by your script here/// </summary>/// <param name="host">The host tool</param>public override void Initialize(Cognex.VisionPro.ToolGroup.CogToolGroup host){// DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVEbase.Initialize(host);// Store a local copy of the script hostthis.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host));}#endregion}


http://www.hkcw.cn/article/RNODEUFurc.shtml

相关文章

面向连接的运输:TCP

目录 TCP连接 TCP报文段结构 往返时间估计与超时 可靠数据传输 回退N步or超时重传 超时间隔加倍 快速重传 流量控制 TCP连接管理 三次握手 1. 客户端 → 服务器&#xff1a;SYN 包 2. 服务器 → 客户端&#xff1a;SYNACK 包 3. 客户端 → 服务器&#xff1a;AC…

使用Python进行函数作画

前言 因为之前通过deepseek绘制一下卡通的人物根本就不像&#xff0c;又想起来之前又大佬通过函数绘制了一些图像&#xff0c;想着能不能用Python来实现&#xff0c;结果发现可以&#xff0c;不过一些细节还是需要自己调整&#xff0c;deepseek整体的框架是没有问题&#xff0…

C#项目07-二维数组的随机创建

实现需求 创建二维数组&#xff0c;数组的列和宽为随机&#xff0c;数组内的数也是随机 知识点 1、Random类 Public Random rd new Random(); int Num_Int rd.Next(1, 100);2、数组上下限。 //定义数组 int[] G_Array new int[1,2,3,4];//一维数组 int[,] G_Array_T …

java Semaphore‌

Java Semaphore 用于控制同时访问特定资源的线程数量&#xff0c;通过管理一组“许可”&#xff08;permits&#xff09;实现并发限制。 模拟6人上厕所&#xff0c;但只有两个坑位&#xff0c;测试代码&#xff1a; import java.util.concurrent.Semaphore;// 假设厕所只有俩…

代码随想录算法训练营第60期第五十五天打卡

大家好&#xff0c;我们今天继续我们图论的部分&#xff0c;其实我们昨天是主要讲解了深搜与广搜的理论基础&#xff0c;我们大体上了解了两种算法的差异与适用情景&#xff0c;今天我们就继续我们的图论的章节&#xff0c;以后几天的题目是图论中比较有名的问题叫做岛屿问题&a…

【音视频】FFmpeg 编码H265

一、概述 实现了读入本地yuv文件&#xff0c;通过libx265编码为H265格式&#xff0c;并存储到本地文件中 二、实现流程 准备文件 在build路径下准备yuv文件 在项目中添加文件参数&#xff0c;输出为h265文件&#xff0c;使用libx265编码 初始化解码器 通过传进来的libx265…

贪心算法应用:带权任务间隔调度问题详解

贪心算法应用&#xff1a;带权任务间隔调度问题详解 贪心算法是一种在每一步选择中都采取在当前状态下最好或最优&#xff08;即最有利&#xff09;的选择&#xff0c;从而希望导致结果是全局最好或最优的算法。带权任务间隔调度问题是贪心算法的一个经典应用场景。 问题定义…

Git-flow流

Git git是版本控制软件&#xff0c;一般用来做代码版本控制 github是一个免费版本控制仓库是国内外很多开源项目的集中地&#xff0c;其本体是一个git服务器 Git初始化操作 git init 初始化仓库 git status 查看当前仓库的状态 git add . 将改动的文件加到暂存区 gi…

LeetCode-链表操作题目

虚拟头指针&#xff0c;在当前head的前面建立一个虚拟头指针&#xff0c;然后哪怕当前的head的val等于提供的val也能进行统一操作 203移除链表元素简单题 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(…

mybatisplus的总结

一.通用Mapper 1.首先创建一个接口与实体类 Data TableName("user") public class User { TableId(value "id", type IdType.AUTO) private Long id; TableField("name") private String name; TableField("age") pri…

UE5 创建2D角色帧动画学习笔记

UE5 创建2D角色帧动画 1.对导入的角色所有帧动画图片Apply paper2d Texture Setting 2.创建图片精灵 Create Sprite 3.全选创建的图片精灵&#xff0c;右键点击 Create Flipbook(创建图像序列视图) 4.双击此paper Flipbook 即可预览该角色帧动画 UE5 2D角色帧动画 Frames P…

MyBatisPlus--条件构造器及自定义SQL详解

条件构造器 在前面学习快速入门的时候&#xff0c;练习的增删改查都是基于id去执行的&#xff0c;但是在实际开发业务中&#xff0c;增删改查的条件往往是比较复杂的&#xff0c;因此MyBatisPlus就提供了一个条件构造器来帮助构造复杂的条件。 MyBatisPlus支持各种复杂的wher…

《类和对象--继承》

引言&#xff1a; 在刚接触C的时候&#xff0c;我们首先学习了有关类和对象的一些基础知识&#xff0c;今天我们就要接着学习类和对象的另一板块–继承。 一&#xff1a;继承的概念和定义 1. 继承的概念 继承(inheritance)机制是面向对象程序设计使代码可以复用的最重要的手…

利用R语言生成区试中随机区组试验设计——多点

目前&#xff0c;区试要求对照不得位于区组的首尾小区&#xff0c;且不同区组的相邻小区位置不得出现同一品种。基于这一要求&#xff0c;编写了R语言的随机区组试验设计。此函数可用于多个试验点试验设计生成情况。 rcbd函数有6个参数&#xff1a; local_name为试验点名称的向…

pytorch基本运算-范数

引言 前序学习进程中&#xff0c;已经对pytorch基本运算有了详细探索&#xff0c;文章链接有&#xff1a; 基本运算 广播失效 乘除法和幂运算 hadamard积、点积和矩阵乘法 上述计算都是以pytorch张量为运算元素&#xff0c;这些张量基本上也集中在一维向量和二维矩阵&#x…

STM32G4 电机外设篇(四)DAC输出电流波形 + CAN通讯

目录 一、STM32G4 电机外设篇&#xff08;四&#xff09;DAC输出电流波形 CAN通讯1 DAC输出电流波形1.1 STM32CubeMX配置和Keil代码1.2 实验现象 2 CAN/CANFD通讯2.1 STM32CubeMX配置和Keil代码2.2 实验现象 附学习参考网址欢迎大家有问题评论交流 (* ^ ω ^) 一、STM32G4 电机…

电子电气架构 --- 后轮转向的一点事情

我是穿拖鞋的汉子&#xff0c;魔都中坚持长期主义的汽车电子工程师。 老规矩&#xff0c;分享一段喜欢的文字&#xff0c;避免自己成为高知识低文化的工程师&#xff1a; 做到欲望极简&#xff0c;了解自己的真实欲望&#xff0c;不受外在潮流的影响&#xff0c;不盲从&#x…

web复习(四)

盒子模型的例题 例一&#xff1a; <!doctype html> <html> <head> <meta charset"utf-8"> <title>咖啡店banner</title> <style type"text/css"> /*将页面中所有元素的内外边距设置为0*/ *{ padding:0; margin…

Cesium添加点线面(贴地)

// 创建一个图元集合const primitives viewer.scene.primitives.add(new Cesium.PrimitiveCollection());1、点上图 // 定义点的位置&#xff08;中国不同城市的经纬度&#xff09;const points [{ lon: 116.4074, lat: 39.9042, name: "北京" },{ lon: 121.4737, …

技术文档:MD520系列变频器配套杭州干扰净GRJ9000S系列EMC电源滤波器安装指南

1. 引言 MD520系列通用变频器是汇川技术有限公司&#xff08;Inovance&#xff09;设计的高性能电流矢量控制交流驱动器&#xff0c;广泛应用于纺织、造纸、机床、包装、食品、风机和水泵等行业。为确保其在复杂电磁环境中稳定运行并不对其他设备造成干扰&#xff0c;手册推荐…