机器视觉8 —— CogPatInspectTool 缺陷对比工具(加画轮廓)
CogPatInspectTool主要用于缺陷检测和复杂模式分析功能特点模式检测能够根据图像中的特征和模式来检测目标物体即使在复杂背景下也能准确识位置和角度检测可以检测并识别目标物体的位置和角度信息为后续的分析和处理提供基多目标识别支持同时检测和识别多个目标物体提高检测效率和准确结果分析输出检测结果的位置信息和匹配度方便用户对检测结果进行评估和判工作原理过将当前图像与“训练图像进行对比获取“原始差异图像。再将“原始差异图像”与“阈值图像”进行对比进而获取“阈值差异图像最约一到当前图像与训练图像的差异通常差异区域即为缺陷所在。简介内容摘自豆包AI原图和脚本代码见文末工具搭建注意要把CogPMAlignTool1工具的Pose给到CogPatInspectTool1工具结果显示效果画出有缺陷的部分点击抓取训练图像与原点切换到Current.TrainImage调整训练区域点击训练新模式运行工具得到已训练的结果编写脚本画出缺陷轮廓创建图形集合循环blob工具找到的斑点创建图形对象把斑点轮廓画出来并添加到图形集合中创建文本根据blob斑点个数来修改文本的内容和颜色添加文本到图像集合添加显示#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.PMAlign; using Cognex.VisionPro.CalibFix; using Cognex.VisionPro.PatInspect; using Cognex.VisionPro.Blob; #endregion public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase { #region Private Member Variables private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock; private CogGraphicCollection gc new CogGraphicCollection(); // 创建图形集合 #endregion /// summary /// Called when the parent tool is run. /// Add code here to customize or replace the normal run behavior. /// /summary /// param namemessageSets the Message in the tools RunStatus./param /// param nameresultSets the Result in the tools RunStatus/param /// returnsTrue if the tool should run normally, /// False if GroupRun customizes run behavior/returns public override bool GroupRun(ref string message, ref CogToolResultConstants result) { foreach(ICogTool tool in mToolBlock.Tools) mToolBlock.RunTool(tool, ref message, ref result); gc.Clear(); // 清除图形集合 CogBlobTool blob1 mToolBlock.Tools[CogBlobTool1] as CogBlobTool; // 循环找到的blob斑点并画出其轮廓 for (int i 0; i blob1.Results.GetBlobs().Count; i) { CogPolygon p new CogPolygon(); p blob1.Results.GetBlobs()[i].GetBoundary(); p.Color CogColorConstants.Red; gc.Add(p); } // 创建label根据找到的blob斑点个数修改其颜色的文本内容 CogGraphicLabel label1 new CogGraphicLabel(); label1.Font new Font(楷体,30); if (blob1.Results.GetBlobs().Count 0) { label1.Color CogColorConstants.Red; label1.SetXYText(200, 200, NG); } else { label1.Color CogColorConstants.Green; label1.SetXYText(200, 200, OK); } gc.Add(label1); 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 namecurrentRecord /// 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 namelastRecord /// The new last run record is available to be initialized or customized./param public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord) { foreach (ICogGraphic item in gc) { mToolBlock.AddGraphicToRunRecord(item, lastRecord, CogPMAlignTool1.InputImage, ); } } #endregion #region When the Script is Initialized /// summary /// Perform any initialization required by your script here /// /summary /// param namehostThe host tool/param public override void Initialize(Cognex.VisionPro.ToolGroup.CogToolGroup host) { // DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE base.Initialize(host); // Store a local copy of the script host this.mToolBlock ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host)); } #endregion }