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

VisionPro项目记录3 —— 圆心距

简介:本项目实现基于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}

相关文章:

  • 【Linux】权限chmod命令+Linux终端常用快捷键
  • 机器学习知识图谱——逻辑回归算法(Logistic Regression)
  • 安装 Hugo
  • 【LeetCode 题解】两数之和(C++/Python 双解法):从语法到算法的全面解析
  • 35.x64汇编写法(二)
  • dvwa4——File Inclusion
  • [Java恶补day13] 53. 最大子数组和
  • 【LeetCode 热题100】回溯:括号生成 组合总和(力扣22 / 39 )(Go语言版)
  • 12.1 GUI 事件处理
  • 截图工具 Snipaste V2.10.7(2025.06.2更新)
  • java Semaphore‌
  • 题目 3225: 蓝桥杯2024年第十五届省赛真题-回文字符串
  • 数据库MySQL集群MGR
  • 【Block总结】LRSA,局部区域自注意力|即插即用
  • 爬虫的几种方式(使用什么技术来进行一个爬取数据)
  • Android第十二次面试GetX库渲染机制
  • ThinkPrune:在RL中引入长度限制,在保持性能一致或略有提升下,显著提升推理效率
  • vue router详解和用法
  • 极客时间:用 FAISS、LangChain 和 Google Colab 模拟 LLM 的短期与长期记忆
  • ubuntu安装devkitPro
  • 开发app商城软件的公司/平台优化是什么意思
  • 网站建设 广/拉新推广渠道
  • 网站做301对优化有影响/考证培训机构报名网站
  • 外包一个企业网站多少钱/宁德市医院东侨院区
  • 做一个京东网站怎么做的/手机优化大师官方免费下载
  • 邢台建设银行官方网站/广东seo网站优化公司