VisionPro:轴承错位标识
找出轴承缺陷并标记:效果
1.打开导入图片
2.添加CogToolblock:方便后续写代码
3.对零件进行模板匹配
4.对图片进行预处理(重点)
5.添加找圆工具和展开工具
根据下图将线链接
6.添加模板匹配工具并运行
7.训练模板
好的全找出来
8.代码编写
红色框为添加的代码(下面有完整代码)
添加代码完成
#region namespace imports
using System;
using System.Collections;
using System.Collections.Generic;
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;
using Cognex.VisionPro.PixelMap;
#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{#region Private Member Variablesprivate Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;#endregionCogGraphicCollection col = new CogGraphicCollection();/// <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>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// 清空集合col.Clear();CogPMAlignTool pma2 = mToolBlock.Tools["CogPMAlignTool2"] as CogPMAlignTool;CogPolarUnwrapTool polar = mToolBlock.Tools["CogPolarUnwrapTool1"] as CogPolarUnwrapTool;//声明集合存储X坐标List<double> listX = new List<double> ();// Run each tool using the RunTool functionforeach(ICogTool tool in mToolBlock.Tools)mToolBlock.RunTool(tool, ref message, ref result);double y=0,yTotal = 0;for (int i = 0; i < pma2.Results.Count; i++){//给集合添加X坐标listX.Add(pma2.Results[i].GetPose().TranslationX);yTotal += pma2.Results[i].GetPose().TranslationY;}y = yTotal / pma2.Results.Count;//排序 升序listX.Sort();// 计算中间NG点信息for (int i = 0; i < listX.Count - 1; i++){if (listX[i + 1] - listX[i] > 60){double x = (listX[i + 1] + listX[i]) / 2;double xIn,yIn;//从输出图像得坐标获得输入图像得坐标polar.RunParams.GetInputPointFromOutputPoint(polar.InputImage, polar.Region, x, y, out xIn, out yIn);col.Add(CreateCircle(xIn, yIn));}}//判断最后一位坐标位置if (listX[listX.Count - 1] < 750 ){double x = listX[listX.Count - 1] + 40;double xIn,yIn;//从输出图像得坐标获得输入图像得坐标polar.RunParams.GetInputPointFromOutputPoint(polar.InputImage, polar.Region, x, y, out xIn, out yIn);col.Add(CreateCircle(xIn, yIn));}//判断最后一位坐标位置if (listX[0] > 40 ){double x = listX[1] - 40;double xIn,yIn;//从输出图像得坐标获得输入图像得坐标polar.RunParams.GetInputPointFromOutputPoint(polar.InputImage, polar.Region, x, y, out xIn, out yIn);col.Add(CreateCircle(xIn, yIn));}return false;}/// <summary>/// 创建圆形标签/// </summary>/// <param name="x">X坐标</param>/// <param name="y">Y坐标</param>/// <returns>返回一个圆</returns>private CogCircle CreateCircle(double x, double y){CogCircle c = new CogCircle();c.CenterX = x;c.CenterY = y;c.Radius = 5;c.LineWidthInScreenPixels = 5;c.Color = CogColorConstants.Red;return c;}#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){foreach (ICogGraphic g in col){mToolBlock.AddGraphicToRunRecord(g, lastRecord, "CogIPOneImageTool1.InputImage", "script");//mToolBlock.AddGraphicToRunRecord(g, lastRecord, "CogPolarUnwrapTool1.OutputImage", "script");}}#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}
完成