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 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) { 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 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 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 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 REMOVE base.Initialize(host); // Store a local copy of the script host this.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host)); } #endregion }