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}