模拟找到喷漆是否合格,如图及为合格Lable显示OK有瑕疵显示NG
1.声明个CogToolBlock方便写C#脚本
2.依次添加CogPMLingTool、CogPatInspectTool和CogBlobTool工具并把对比工具输出的灰度图传给斑点工具
3.添加脚本
#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;
//声明图形集合 CogGraphicCollection 一些关于 图形的 都可以放进去
CogGraphicCollection col = 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)
{
// 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
//Bloo工具
CogBlobTool mBlob = new CogBlobTool();
mBlob = mToolBlock.Tools["CogBlobTool1"] as CogBlobTool;
//声明一个圆
CogCircle mCircle;
//声明一个lable
CogGraphicLabel mLable;
//删除上张图片的圆
col.Clear();
// Run each tool using the RunTool function
foreach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool, ref message, ref result);
//OK或者NG
if(mBlob.Results.GetBlobs().Count == 0){
//实例化label
mLable = new CogGraphicLabel();
//颜色
mLable.Color = CogColorConstants.Green;
//字体位置
mLable.SetXYText(200, 200, "OK");
//累加
col.Add(mLable);
}else {
mLable = new CogGraphicLabel();
mLable.Color = CogColorConstants.Red;
mLable.SetXYText(200, 200, "NG");
col.Add(mLable);
}
//画圆
//圆的 个数 对应斑点工具的count
for(int i = 0; i < mBlob.Results.GetBlobs().Count; i++){
//初始化一个圆
mCircle = new CogCircle();
//颜色
mCircle.Color = CogColorConstants.Red;
//半径
mCircle.Radius = 25;
//线宽
mCircle.LineWidthInScreenPixels = 2;
//显示的位置 圆心的坐标
mCircle.CenterX = mBlob.Results.GetBlobs()[i].CenterOfMassX;
mCircle.CenterY = mBlob.Results.GetBlobs()[i].CenterOfMassY;
//对每个圆圈叠加
col.Add(mCircle);
}
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", "");
}
}
#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
}