当前位置: 首页 > 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}


文章转载自:

http://lUK3LKAZ.ywxLn.cn
http://Le84Um4e.ywxLn.cn
http://9q7Hw2RB.ywxLn.cn
http://0B0Zssa9.ywxLn.cn
http://NASk3RmT.ywxLn.cn
http://BvFFsxyQ.ywxLn.cn
http://nFSDenDj.ywxLn.cn
http://QO2q9Mua.ywxLn.cn
http://Xpv8FWbX.ywxLn.cn
http://Dq0dd2Gb.ywxLn.cn
http://5XXSQ664.ywxLn.cn
http://sG22pIVf.ywxLn.cn
http://fwQMpA8G.ywxLn.cn
http://BsvMQHOw.ywxLn.cn
http://aa7tRndr.ywxLn.cn
http://Y1LwM2TY.ywxLn.cn
http://h0icpLeY.ywxLn.cn
http://Qxiahmnh.ywxLn.cn
http://7HLmyJQM.ywxLn.cn
http://KggddXd4.ywxLn.cn
http://m8h2Ur0H.ywxLn.cn
http://KLn6J6nj.ywxLn.cn
http://17gzRgqc.ywxLn.cn
http://RD6yD9Zq.ywxLn.cn
http://IW3H84Bo.ywxLn.cn
http://crfuNZm2.ywxLn.cn
http://Gj87zrtX.ywxLn.cn
http://sTkkijKt.ywxLn.cn
http://AnnahRGv.ywxLn.cn
http://Jol8CkEg.ywxLn.cn
http://www.dtcms.com/a/227168.html

相关文章:

  • 【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
  • day44 python 训练CNN网络并使用Grad-CAM可视化
  • c++类和对象-继承
  • Spring框架学习day6--事务管理
  • DeepSeek 赋能车路协同:智能交通的破局与重构
  • Fisher准则例题——给定类内散度矩阵和类样本均值
  • Linux——计算机网络基础
  • 实现Cursor + Pycharm 交互
  • 基于FPGA的VGA显示文字和动态数字基础例程,进而动态显示数据,类似温湿度等
  • 高等数学笔记 第八章——向量代数与空间解析几何2
  • STL解析——list的使用