机器视觉11 —— CogGraphicCollection 图形集合

使用图形集合CogGraphicCollection在CogBlobTool工具找到的斑点周围画图形。

CogGraphicCollection 是康耐视(Cognex)VisionPro 中用于管理图形对象集合的工具。以下是其详细介绍:

功能特点

  • 图形管理:可用于存储和管理多个 CogGraphic 类型的图形对象,如 CogGraphicLabel、CogCircle、CogLine 等。通过该工具,能方便地对这些图形对象进行统一的添加、删除、遍历和操作。
  • 灵活组合:可以将不同类型的图形对象组合在一起,形成一个复杂的图形集合。例如,在一个检测场景中,可能同时需要在图像上显示标签、圆形标记和直线标注等,CogGraphicCollection 就可以将这些不同的图形组合到一起进行管理和显示。
  • 与其他工具集成:能与 VisionPro 中的其他工具,如 CogRecordDisplay、CogToolBlock 等进行集成。例如,可以将 CogGraphicCollection 中的图形对象添加到 CogRecordDisplay 中进行显示,或者在 CogToolBlock 的脚本中对 CogGraphicCollection 进行操作,以实现更复杂的视觉应用功能。

应用场景

  • 工业检测可视化:在工业检测中,用于将检测结果、标注信息等以图形的形式显示在图像上,方便操作人员直观地查看检测情况。例如,在检测零件的缺陷时,可将缺陷位置用圆形或矩形标记出来,同时添加标签注明缺陷类型。
  • 视觉引导辅助:在机器人视觉引导系统中,可用于显示目标物体的位置、姿态等信息的图形表示,为机器人的操作提供直观的参考。比如,用箭头表示目标物体的方向,用圆形表示目标物体的中心位置。
  • 数据展示与分析:在各种视觉应用中,将处理后的数据以图形化的方式展示在图像上,方便用户查看和分析。例如,在统计产品数量时,用柱状图或饼图表示不同类型产品的数量分布。

原图和代码在文末。

CogRectangleAffine:仿射矩形

CogGraphicCollection:图形集合

显示效果 

命名空间和私有字段

在运行工具之前需要先清空图形集合,不然切换图片后之前画的图案会一直保留。

参数 ( CogBlobAxisConstants.Principal ) :最小外接矩形

遍历blob2工具找到的所有斑点添加到图形集合中。

通过面积来判断矩形的颜色,并把绿色矩形的旋转角度设置为0,红色矩形角度没有设置。

最后添加输出显示

作业原图

脚本代码

#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.Blob;
#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  // 声明仿射矩形和圆变量
  private CogRectangleAffine rec1;
  private CogRectangleAffine rec2;
  private CogCircle cir;
  // 创建图形集合
  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)
  {
    // 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);
    
    // 先清空图形集合
    gc.Clear();
    
    // 映射工具
    CogBlobTool blob1 = mToolBlock.Tools["CogBlobTool1"] as CogBlobTool;
    CogBlobTool blob2 = mToolBlock.Tools["CogBlobTool2"] as CogBlobTool;
    CogBlobTool blob3 = mToolBlock.Tools["CogBlobTool3"] as CogBlobTool;
    
    // 在blob1工具找到的第一个斑点质心位置画出最小外接矩形
    if (blob1.Results.GetBlobs().Count > 0)
    {
      rec1 = new CogRectangleAffine();
      // 矩形rec1为blob1找到的第一个结果的(CogBlobAxisConstants.Principal)最小外接矩形
      rec1 = blob1.Results.GetBlobByID(0).GetBoundingBox(CogBlobAxisConstants.Principal);
      // 设置矩形颜色为蓝色
      rec1.Color = CogColorConstants.Blue;
      gc.Add(rec1);  //把矩形添加进入图形集合
    }
    
    // 在blob2工具找到的全部斑点质心位置画出最小外接矩形
    foreach (CogBlobResult b1 in blob2.Results.GetBlobs())
    {
      rec2 = new CogRectangleAffine();
      rec2 = b1.GetBoundingBox(CogBlobAxisConstants.Principal);
      
      if (b1.Area > 1000)  // 用面积来判断矩形颜色
      {
        rec2.Rotation = 0;  // 绿色矩形的角度
        rec2.Color = CogColorConstants.Green;
      }
      else
      {
        rec2.Color = CogColorConstants.Red;
      }
      gc.Add(rec2);  //把矩形添加进入图形集合
    }
    
    // 在blob3工具找到的第一个斑点的质心位置画圆
    if (blob3.Results.GetBlobs().Count > 0)
    {
      cir = new CogCircle();
      cir.Color = CogColorConstants.Magenta;
      // 设置圆的XY坐标位置为blob3找到的第一个结果的质心位置
      cir.CenterX = blob3.Results.GetBlobByID(0).CenterOfMassX;
      cir.CenterY = blob3.Results.GetBlobByID(0).CenterOfMassY;
      cir.Radius = 40;  // 圆半径
      gc.Add(cir);  //把圆添加进入图形集合
    }
    
    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, "CogBlobTool1.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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值