Visionpro-blob工具-骰子的应用

斑点寻找工具Blob

Blob的概述

CogBlobTool 工具,俗称斑点工具 ;经常应用于工业的一些检测类型的项目。

  • 斑点分析 探测并且分析图像中的二维形状
  • Blob 是先根据用户设定好的灰阶范围对图像进行分割,然后对目标进行查找和分析
    • 面积
    • 质心
    • 周长
    • 主轴
    • ......

应用场景

  • ------>Blob 分析非常合适以下场合的应用:

    • ------> 对象在尺寸、形状/或方向上差异很大(训练模型很难或者不可能)
    • ------> 对象在背景中找不到的截然不同的灰度
    • ------> 对象没有重叠或者接触
  • ------> 应用案例:

    • ------> 检查环氧树脂点分配的数量、尺寸和形状
    • ------> 检查表示坏薄片模型的墨水点的正常位置和大小
    • ------> 检查药片的破碎和大小
    • ------> 根据对象的尺寸、形状或位置整理或者分类对象

Blob_参数

主要用于检测和分析图像中的斑点特征:

  1. **SegmentationParams.Mode:**设置图像分割模式,包括固定阈值(HardFixedThreshold)、动态阈值(HardDynamicThreshold)、相对阈值(HardRelativeThreshold)等。
  2. **SegmentationParam.Polarity:**设置斑点的极性,包括白底黑点(DarkBlobs)和黑底白点(LightBlobs)
  3. ConnectivityCleanup: 设置连通性清理模式、包括修剪(Prune)、填充(Fill)等
  4. ConnectivityMinPixels: 设置最小面积,用于过滤过小的斑点
  5. ConnectivityMode: 设置连通性,包括已标记(Labeled)、灰度(GreyScale)等
  6. Region: 设置检测区域的形状、包括圆形(CogCircle)、椭圆(CogEllipse)、多边形(CogPolygon)等
  7. RunTimeMeasures: 设置测试属性,包括面积(Area)、质心坐标(CenterMassX、CenterMassY)等
  • **阈值模式:**选择合适的阈值模式(固定阈值、动态阈值、相对阈值)来分割图像中的斑点和背景
  • 极性设置: 根据斑点的灰度特征,选择白底黑点或黑底白点的极性
  • 连通性处理: 通过设置连通性模式和清理模式,处理斑点的连通性问题
  • 区域设置: 指定检测区域、以限制斑点检测的范围
  • 面积过滤: 通过设置最小面积和最大面积,过滤不符合要求的斑点
  • 形状筛选: 通过设置形状参数,如非环形值、筛选出特定形状的斑点

骰子案例:

工具使用(CogBlobTool)

脚本

声明label集合
cs 复制代码
private CogGraphicCollection dt = new CogGraphicCollection();
dt.Clear();
封装label
cs 复制代码
private CogGraphicLabel createlabel(string text, float size, double x, double y, CogColorConstants color)
  {
    CogGraphicLabel label = new CogGraphicLabel();
    label.Font = new Font("Arial", size, FontStyle.Bold, GraphicsUnit.Pixel);
    label.Color = color;
    label.Alignment = CogGraphicLabelAlignmentConstants.TopLeft;
    label.BackgroundColor = CogColorConstants.White;
    label.SetXYText(x, y, text);
    return label;
  }
实例化转黑白图像
cs 复制代码
CogImageConvertTool imageconvert = new CogImageConvertTool();
    imageconvert.InputImage = null;
    imageconvert.InputImage = mToolBlock.Inputs["InputImage"].Value as ICogImage;
    imageconvert.RunParams.RunMode = CogImageConvertRunModeConstants.Intensity;
    imageconvert.Run();
声明工具,清空变量
cs 复制代码
CogBlobTool blob = mToolBlock.Tools["CogBlobTool1"]as CogBlobTool;
    mToolBlock.Outputs["ResultContent"].Value = "";
    mToolBlock.Outputs["Count"].Value = "";
赋值
cs 复制代码
if(imageconvert.RunStatus.Result == CogToolResultConstants.Accept)
    {
      blob.InputImage = imageconvert.OutputImage;
      blob.Run();
      
      foreach(ICogTool tool in mToolBlock.Tools)
      if(tool.RunStatus.Result == CogToolResultConstants.Accept)
      {
          mToolBlock.RunTool(tool, ref message, ref result);
          mToolBlock.Outputs["Count"].Value = blob.Results.GetBlobs().Count;
          mToolBlock.Outputs["ResultContent"].Value ="骰子的点数是:"+mToolBlock.Outputs["Count"].Value;
          string text = mToolBlock.Outputs["ResultContent"].Value as string;
        dt.Add(createlabel(text, 25, 0, 0, CogColorConstants.Blue));
          dt.Add(createlabel(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), 25, 0, 120, CogColorConstants.Green));
      }
    }
All脚本
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.Dimensioning;
using Cognex.VisionPro.ImageProcessing;
using Cognex.VisionPro.Blob;
#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  #endregion
  private CogGraphicCollection dt = new CogGraphicCollection();

  /// <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
    dt.Clear();
    CogImageConvertTool imageconvert = new CogImageConvertTool();
    imageconvert.InputImage = null;
    imageconvert.InputImage = mToolBlock.Inputs["InputImage"].Value as ICogImage;
    imageconvert.RunParams.RunMode = CogImageConvertRunModeConstants.Intensity;
    imageconvert.Run();
    CogBlobTool blob = mToolBlock.Tools["CogBlobTool1"]as CogBlobTool;
    mToolBlock.Outputs["ResultContent"].Value = "";
    mToolBlock.Outputs["Count"].Value = "";


    // Run each tool using the RunTool function
    if(imageconvert.RunStatus.Result == CogToolResultConstants.Accept)
    {
      blob.InputImage = imageconvert.OutputImage;
      blob.Run();
      
      foreach(ICogTool tool in mToolBlock.Tools)
      if(tool.RunStatus.Result == CogToolResultConstants.Accept)
      {
          mToolBlock.RunTool(tool, ref message, ref result);
          mToolBlock.Outputs["Count"].Value = blob.Results.GetBlobs().Count;
          mToolBlock.Outputs["ResultContent"].Value ="骰子的点数是:"+mToolBlock.Outputs["Count"].Value;
          string text = mToolBlock.Outputs["ResultContent"].Value as string;
        dt.Add(createlabel(text, 25, 0, 0, CogColorConstants.Blue));
          dt.Add(createlabel(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), 25, 0, 120, CogColorConstants.Green));
      }
    }

    return false;
  }
  private CogGraphicLabel createlabel(string text, float size, double x, double y, CogColorConstants color)
  {
    CogGraphicLabel label = new CogGraphicLabel();
    label.Font = new Font("Arial", size, FontStyle.Bold, GraphicsUnit.Pixel);
    label.Color = color;
    label.Alignment = CogGraphicLabelAlignmentConstants.TopLeft;
    label.BackgroundColor = CogColorConstants.White;
    label.SetXYText(x, y, text);
    return label;
  }

  #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 s in dt)
    {
      mToolBlock.AddGraphicToRunRecord(s, lastRecord, "CogBlobTool1.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

}

载具判断案例

工具

Blob

脚本

声明工具· 并添加集合
cs 复制代码
private CogGraphicCollection dt = new CogGraphicCollection();
 dt.Clear();
    mToolBlock.Outputs["Count"].Value = "";
    mToolBlock.Outputs["Result"].Value = "";
    CogPMAlignTool pma = mToolBlock.Tools["CogPMAlignTool1"]as CogPMAlignTool;
    CogToolBlock tb = mToolBlock.Tools["CogTranform"]as CogToolBlock;
    CogBlobTool small = mToolBlock.Tools["small"]as CogBlobTool;
    CogBlobTool big = mToolBlock.Tools["big"]as CogBlobTool;
    CogHistogramTool histogram = mToolBlock.Tools["CogHistogramTool1"]as CogHistogramTool;
 pma.Run();
    int count = 0;
    bool state = true;
    double x = (double)tb.Outputs["X"].Value;
    double y = (double)tb.Outputs["Y"].Value;
判断值并圈写框
cs 复制代码
if(pma.RunStatus.Result == CogToolResultConstants.Accept)
    {
      foreach(ICogTool tool in mToolBlock.Tools)
        mToolBlock.RunTool(tool, ref message, ref result);
       count = small.Results.GetBlobs().Count + big.Results.GetBlobs().Count;
      if(histogram.Result.Mean < 110)
      {
        state = false;
        CogTransform2DLinear transform1 = new CogTransform2DLinear();
        
        CogRectangleAffine res = histogram.Region as CogRectangleAffine;
        transform1.TranslationX = res.CenterX + x;
        transform1.TranslationY = res.CenterY + y;
        res.Color = CogColorConstants.Red;
        
        CogHistogramTool his = new CogHistogramTool();
        his.Name = histogram.Name;
        his.Region = res;
        dt.Add(res);
        dt.Add(createlabel("此处图像NG", 15, transform1.TranslationX, transform1.TranslationY, CogColorConstants.Red));
      }
      
    }
声明结果在图像
cs 复制代码
dt.Add(createlabel(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), 25, 0, 0, CogColorConstants.Green));
    dt.Add(createlabel("物料状态:" + (state ? "OK" : "NG"), 25, 0, 110, state ? CogColorConstants.Blue : CogColorConstants.Red));
    dt.Add(createlabel("产品孔洞:" + count, 25, 0, 200, CogColorConstants.Purple));
    mToolBlock.Outputs["Count"].Value = count;
    mToolBlock.Outputs["Result"].Value = state ? "OK" : "NG";
创建label
cs 复制代码
 private CogGraphicLabel createlabel(string text, float size, double x, double y, CogColorConstants color)
  {
    CogGraphicLabel label = new CogGraphicLabel();
    label.Color = color;
    if(!text.Contains("此处图像"))
    {
      label.Alignment = CogGraphicLabelAlignmentConstants.TopLeft;
    }
    label.BackgroundColor = CogColorConstants.White;
    label.Font = new Font("Arial", size, FontStyle.Bold, GraphicsUnit.Pixel);
    label.SetXYText(x, y, text);
    return label;
  }
输出集合
cs 复制代码
foreach(ICogGraphic s in dt)
    {
      mToolBlock.AddGraphicToRunRecord(s, lastRecord, "CogPMAlignTool1.InputImage", "lastRecord");
    }
All_脚本
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.CalibFix;
using Cognex.VisionPro.Blob;
using Cognex.VisionPro.ImageProcessing;
#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  #endregion
  private CogGraphicCollection dt = new CogGraphicCollection();

  /// <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
    dt.Clear();
    mToolBlock.Outputs["Count"].Value = "";
    mToolBlock.Outputs["Result"].Value = "";
    CogPMAlignTool pma = mToolBlock.Tools["CogPMAlignTool1"]as CogPMAlignTool;
    CogToolBlock tb = mToolBlock.Tools["CogTranform"]as CogToolBlock;
    CogBlobTool small = mToolBlock.Tools["small"]as CogBlobTool;
    CogBlobTool big = mToolBlock.Tools["big"]as CogBlobTool;
    CogHistogramTool histogram = mToolBlock.Tools["CogHistogramTool1"]as CogHistogramTool;


    // Run each tool using the RunTool function
    pma.Run();
    int count = 0;
    bool state = true;
    double x = (double)tb.Outputs["X"].Value;
    double y = (double)tb.Outputs["Y"].Value;
    if(pma.RunStatus.Result == CogToolResultConstants.Accept)
    {
      foreach(ICogTool tool in mToolBlock.Tools)
        mToolBlock.RunTool(tool, ref message, ref result);
       count = small.Results.GetBlobs().Count + big.Results.GetBlobs().Count;
      if(histogram.Result.Mean < 110)
      {
        state = false;
        CogTransform2DLinear transform1 = new CogTransform2DLinear();
        
        CogRectangleAffine res = histogram.Region as CogRectangleAffine;
        transform1.TranslationX = res.CenterX + x;
        transform1.TranslationY = res.CenterY + y;
        res.Color = CogColorConstants.Red;
        
        CogHistogramTool his = new CogHistogramTool();
        his.Name = histogram.Name;
        his.Region = res;
        dt.Add(res);
        dt.Add(createlabel("此处图像NG", 15, transform1.TranslationX, transform1.TranslationY, CogColorConstants.Red));
      }
      
    }
    dt.Add(createlabel(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), 25, 0, 0, CogColorConstants.Green));
    dt.Add(createlabel("物料状态:" + (state ? "OK" : "NG"), 25, 0, 110, state ? CogColorConstants.Blue : CogColorConstants.Red));
    dt.Add(createlabel("产品孔洞:" + count, 25, 0, 200, CogColorConstants.Purple));
    mToolBlock.Outputs["Count"].Value = count;
    mToolBlock.Outputs["Result"].Value = state ? "OK" : "NG";

    return false;
  }
  private CogGraphicLabel createlabel(string text, float size, double x, double y, CogColorConstants color)
  {
    CogGraphicLabel label = new CogGraphicLabel();
    label.Color = color;
    if(!text.Contains("此处图像"))
    {
      label.Alignment = CogGraphicLabelAlignmentConstants.TopLeft;
    }
    label.BackgroundColor = CogColorConstants.White;
    label.Font = new Font("Arial", size, FontStyle.Bold, GraphicsUnit.Pixel);
    label.SetXYText(x, y, text);
    return label;
  }

  #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 s in dt)
    {
      mToolBlock.AddGraphicToRunRecord(s, lastRecord, "CogPMAlignTool1.InputImage", "lastRecord");
    }
  }
  #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

}
相关推荐
PyHaVolask2 小时前
图片处理基础-下
人工智能·计算机视觉
Agent产品评测局2 小时前
企业 Agent 流程上线后,如何实现持续优化与迭代?——2026年企业级智能体长效运营全景指南
人工智能·ai·chatgpt
东离与糖宝2 小时前
Spring AI RAG生产方案:Java对接Gemma 4构建企业知识库
java·人工智能
小赖同学啊2 小时前
LLM 自动化测试平台 企业级架构图
人工智能
U-Mail邮件系统2 小时前
企业邮箱本地私有化部署:构建自主可控、安全高效的邮件体系
大数据·人工智能·安全
天天进步20152 小时前
源码级优化:Graphiti 的并发处理与分布式记忆存储架构
人工智能·分布式·架构
盐水冰2 小时前
【SpringAI】认识与应用开发
人工智能·springai
hughnz2 小时前
钻头技术持续突飞猛进:地热钻探领域的创新
人工智能·算法
剑穗挂着新流苏3122 小时前
209_深度学习的生存哲学:数值稳定性、梯度爆炸与 Xavier 初始化
人工智能·深度学习