一、CogPathInspectTool工具简介
CogPathInspectTool是VisionPro重要的工具,主要用于缺陷检测,通过将当前图像与“训练图像”对比,获取“原始差异图像”,再将“原始差异图像”与“阈值图像”进行对比,进而获取“阈值差异图像”得到最终的当前图像与训练图像的差异,通常差异区域为缺陷所在。该工具通常与CogPMAlignTool工具、CogBolbTool工具进行使用。
二、主要功能
- 模式检测:根据图像中的特征和模式检测目标物体。
- 位置和角度检测:检测并识别目标物体的位置和角度。
- 多目标识别:支持同时检测和识别多个目标物体。
- 结果分析:输出检测结果的位置信息和匹配度。
三、CogPathInspectTool操作流程实例
1、配置添加图形集合添加工具块
2、获取PMA工具:通过PMA工具进行准确的模版匹配,可以对其旋转缩放的角度进行精确的匹配
添加终端
3、配置CogPathInspectTool:在VisionPro中配置CogPathInspectTool工具,连接PMA终端,设置比较模型、偏侧高度阈值等参数,以便进行特征比较和缺陷识别。
4、获取Blob工具:通过调用CogBlobTool工具,连接CogPatInspectTool输出的瑕疵图片Result.GetDifferenceImage(Absolute)
通过blob进行斑点检测
5、 通过代码进行显示检测出的瑕疵,并进行显示
选择 C# Advanced Script脚本方式
脚本如下:
#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;
private CogGraphicCollection cc = new CogGraphicCollection(); // 创建图形集合 保存多个图形 例如多个label
CogGraphicLabel label = new CogGraphicLabel();
#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)
{
// 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
cc.Clear(); // 清空集合
CogBlobTool blob = (CogBlobTool) mToolBlock.Tools[2];
// CogBlobTool blob = new CogBlobTool();
// blob = (CogBlobTool) mToolBlock.Tools[2];
// Run each tool using the RunTool function
foreach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool, ref message, ref result);
for(int i = 0;i < blob.Results.GetBlobs().Count;i++)
{
CogPolygon p = new CogPolygon(); // 创建多边形
p = blob.Results.GetBlobs()[i].GetBoundary(); // 设置多边形边界
p.Color = CogColorConstants.Red;
p.LineWidthInScreenPixels = 3;
cc.Add(p);
}
double dou = blob.Results.GetBlobs().Count;
if(dou == 0)
{
label.SetXYText(20, 20, "无瑕疵");
}
else
{
label.SetXYText(20, 20, "有瑕疵");
}
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 a in cc)
{
mToolBlock.AddGraphicToRunRecord(a, lastRecord, "CogPMAlignTool1.InputImage", "ss");
}
mToolBlock.AddGraphicToRunRecord(label, lastRecord, "CogPMAlignTool1.InputImage", "ss");
}
#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
}
6、运行结果如下