CogPatInspectTool是康耐视中的一种模板比对的视觉检测工具,主要用于产品不良检测。其核心功能是通过将输入图像与预先训练好的模板进行对比,识别出两者之间的差异,并生成高亮差异图,从而判断产品是否存在缺陷。
效果图

CogPatInspectTool工具块概念
CogPatInspectTool通过比对输入图像与训练模板的特征差异,检测产品的不良。其特点包括:
-
模板比对与缺陷检测 CogPatInspectTool通过提取输入图像中特定区域的特征(如边缘、形状等),与训练好的模板进行匹配,计算两者之间的偏差。差异超过设定阈值的区域会被标记为缺陷,并以高亮形式呈现。
-
依赖外部工具协同工作 该工具通常需要与CogPMAlignTool (模板匹配工具)、CogFixture(定位工具)等配合使用。例如,CogPMAlignTool提供模板的位置和角度信息,CogFixture调整坐标系,确保检测区域的精确定位。
CogPatInspectTool工具应用场景
-
工业质检:如电子元件焊接缺陷、印刷品瑕疵检测等。
-
高精度匹配:适用于需要快速比对模板与实时图像的场景,例如零件定位后的二次质量验证。
CogPatInspectTool工具应用流程
步骤1:
名称 | 描述 |
---|---|
InputImage | 待检测的图像输入 |
Pose | 从CogPMAlignTool的输出结果中获取定位信息(如Results.Item(0).GetPose ) |
Pattern.TrainImage | 训练好的模板图像。 |
Pattern.Origin | 模板的坐标系原点。 |
步骤2:
抓取训练图像与原点-->Current.TrainImage-->训练新模式-->统计训练当前模式:在次步骤点击当前模式会在左边区域显示训练次数-->结束统计训练;如果对这次训练不满意可以取消训练模式进行重新抓取训练结果。

步骤3:
根据图像来源进行适当调整。

步骤4:
规范化模式常见选项及定义
名称 | 定义 | 使用条件 |
---|---|---|
None(无规范化 | 不对输入图像进行任何预处理,直接使用原始图像进行模板匹配和缺陷检测。 | 适用于光照条件稳定、图像对比度高的场景,无需额外调整。 |
Linear(线性规范化) | 通过线性变换调整图像的亮度和对比度,使图像灰度分布更均匀。 | 适用于光照轻微变化但整体亮度分布可控的情况。 |
Histogram Equalization(直方图均衡化) | 增强图像全局对比度,通过扩展灰度分布范围突出细节。 | 适用于低对比度图像或需要增强局部特征的场景。 |
Local Contrast(局部对比度增强) | 基于局部区域的对比度调整,抑制全局光照变化的影响。 | 适用于光照不均匀或存在局部阴影的场景。 |
Adaptive Thresholding(自适应阈值) | 根据图像局部区域的灰度动态计算阈值,用于二值化处理。 | 适用于光照变化显著或背景复杂的图像。 |
Edge-Based(基于边缘的规范化) | 仅保留图像边缘信息,弱化纹理和光照干扰。 | 适用于需要依赖形状或边缘特征进行缺陷检测的场景。 |
Gradient Magnitude(梯度幅值规范化) | 计算图像梯度幅值,突出边缘和纹理变化。 | 适用于高噪声图像或需强化边缘差异的检测任务。 |
使用条件与选择建议
光照稳定性 :若光照条件稳定,选择None 或Linear 模式可简化计算;若光照波动较大,优先使用Local Contrast 或Adaptive Thresholding。
图像对比度 :低对比度图像需结合Histogram Equalization 或Edge-Based模式以增强特征。
噪声抑制 :对于高噪声环境,Gradient Magnitude模式可通过梯度计算减少噪声干扰。
实时性要求 :Linear 和Edge-Based 模式计算量较低,适合实时性要求高的场景;Adaptive Thresholding 和Local Contrast可能需要更高算力 。

步骤5:

项目:识别充电器镭雕字符检测
步骤1:
步骤2:

步骤3:
步骤4:

脚本
cs
#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.PatInspect;
using Cognex.VisionPro.Blob;
#endregion
public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
#endregion
//定义图形集合
//声明图形集合 CogGraphicCollection 一些关于 图形的 都可以放进去
CogGraphicCollection col = new CogGraphicCollection();
//声明 图形标签
CogGraphicLabel mLabel = new CogGraphicLabel();
//圆
CogCircle mCircle = new CogCircle();
/// <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
//找到工具
CogBlobTool blob = new CogBlobTool();
blob = mToolBlock.Tools["CogBlobTool1"] as CogBlobTool;
// Run each tool using the RunTool function
//qingkong
col.Clear();
foreach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool, ref message, ref result);
//判断 是否 ng
if(blob.Results.GetBlobs().Count > 0){
//ok
mLabel.SetXYText(200,200,"NG");
mLabel.Color = CogColorConstants.Red;
mLabel.Font = new Font("宋体",30);
//设置颜色
mCircle.Color = CogColorConstants.Red;
//设置半径
mCircle.Radius = 15;
//设置圆心
mCircle.CenterX = blob.Results.GetBlobs()[0].CenterOfMassX;
mCircle.CenterY = blob.Results.GetBlobs()[0].CenterOfMassY;
//设置圆线条 的像素宽度
mCircle.LineWidthInScreenPixels = 2;
col.Add(mCircle);
col.Add(mLabel);
}else{
mLabel.SetXYText(200, 200, "ok");
mLabel.Color = CogColorConstants.Green;
mLabel.Font = new Font("宋体", 30);
col.Add(mLabel);
}
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)
{
for (int i = 0; i < col.Count; i++)
{
mToolBlock.AddGraphicToRunRecord(col[i], lastRecord, "CogPMAlignTool1.InputImage", "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 REMOVE
base.Initialize(host);
// Store a local copy of the script host
this.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host));
}
#endregion
}
注意事项
-
模板质量:训练模板需避免模糊或光照不均,否则可能影响检测准确性。
-
多工具协同:建议结合CogPMAlignTool进行初始定位,再使用CogPatInspectTool进行精细化缺陷检测。
-
参数调优:需根据实际场景调整对比度阈值和检测区域,平衡误检率与漏检率。