VisionPro正课四

1、脚本实现零件检测

零件边缘检测

按照图片所示使用部件,使用CogPatInspectTool框选零件检测区域,再对模板进行训练(注意训练模板所用的图像一定要是标准件)

使用脚本编写器,将以下代码添加到脚本中。

cs 复制代码
 //1.添加一个"图形"集合
  CogGraphicCollection gc=new CogGraphicCollection();
  //2.添加一个文本
  CogGraphicLabel label = new CogGraphicLabel();
cs 复制代码
    CogBlobTool blob = mToolBlock.Tools["CogBlobTool1"] as CogBlobTool;
    //对blob的结果数进行判断
    if(blob.Results.GetBlobs().Count > 0)
    {
      label.SetXYText(200, 200, "NG");
      label.Color = CogColorConstants.Red;
    }
    else
    {
      label.SetXYText(200, 200, "OK");
      label.Color = CogColorConstants.Green;
    }
    
    gc.Clear();//清空集合
    //向集合中添加文本标签
    gc.Add(label);
    //向集合中添加框选点
    foreach(CogBlobResult a in blob.Results.GetBlobs())
    {
      //实例化每个斑点的边界
      CogPolygon p = a.GetBoundary();
      p.Color = CogColorConstants.Purple;//设置斑点边界颜色
      gc.Add(p);//将斑点边界添加到集合中
    }
cs 复制代码
 //将集合的内容显示到图层中
    foreach( ICogGraphic c in gc)
    {
      mToolBlock.AddGraphicToRunRecord(c,lastRecord,"CogImageConvertTool1.InputImage","");
    }

2、蓝色工件缺陷检测

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.ImageProcessing;
using Cognex.VisionPro.PMAlign;
using Cognex.VisionPro.CalibFix;
using Cognex.VisionPro.ColorExtractor;
using Cognex.VisionPro.Blob;
#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  #endregion
  //1.添加一个"图形"集合
  CogGraphicCollection gc = new CogGraphicCollection();
  //2.添加一个文本
  CogGraphicLabel label = new CogGraphicLabel();

  /// <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


    // Run each tool using the RunTool function
    foreach(ICogTool tool in mToolBlock.Tools)
      mToolBlock.RunTool(tool, ref message, ref result);
    
    CogBlobTool blob = mToolBlock.Tools["CogBlobTool1"] as CogBlobTool;
    //对blob的结果数进行判断
    if(blob.Results.GetBlobs().Count > 0)
    {
      label.SetXYText(1000, 600, "NG");
      label.Color = CogColorConstants.Red;
       label.Font = new Font("微软雅黑",30);
    }
    else
    {
      label.SetXYText(1000, 600, "OK");
      label.Color = CogColorConstants.Green;
       label.Font = new Font("微软雅黑",30);
    }
    
    gc.Clear();//清空集合
    //向集合中添加文本标签
    gc.Add(label);
    //向集合中添加框选点
    foreach(CogBlobResult a in blob.Results.GetBlobs())
    {
      //实例化每个斑点的边界
      CogPolygon p = a.GetBoundary();
      p.Color = CogColorConstants.Purple;//设置斑点边界颜色
      gc.Add(p);//将斑点边界添加到集合中
    }


    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 c in gc)
    {
      mToolBlock.AddGraphicToRunRecord(c, lastRecord, "CogImageConvertTool1.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

}

3、