当前位置: 首页 > news >正文

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集合
private CogGraphicCollection dt = new CogGraphicCollection(); dt.Clear();
封装label
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; }
实例化转黑白图像
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 = "";
赋值
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脚本
#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

脚本

声明工具· 并添加集合
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;
判断值并圈写框
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";
创建label
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; }
输出集合
foreach(ICogGraphic s in dt) { mToolBlock.AddGraphicToRunRecord(s, lastRecord, "CogPMAlignTool1.InputImage", "lastRecord"); }
All_脚本
#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 }
http://www.jsqmd.com/news/584559/

相关文章:

  • 从零开发Shell补全脚本:学习git-flow-completion的代码架构
  • Lepton AI实时推理:低延迟服务构建终极指南
  • isowords性能优化:如何在高帧率下运行复杂的3D渲染
  • BRV下拉刷新与上拉加载:SmartRefreshLayout集成完整教程
  • 2026年口碑好的无锡砂浆设备可靠供应商推荐 - 品牌宣传支持者
  • 51单片机模拟IIC从机实战:手把手教你用逻辑分析仪调试主从机通信(附完整代码)
  • 2026年靠谱的云南冷库风机安装厂家推荐与选型指南 - 品牌宣传支持者
  • Apache NetBeans社区生态解析:如何参与贡献与获取支持
  • Linux实现简易版Shell的代码详解
  • 程序员如何去阅读开源项目的源码?
  • 从零开始:用Speakeasy为你的Node.js应用添加双因素认证
  • 低成本验证创意:星图OpenClaw沙盒+Qwen3.5-9B试玩图片转代码
  • 腾讯HY-OmniWeaving:全能视频生成新突破
  • Nunchaku FLUX.1 CustomV3实战教程:多LoRA并行加载与动态权重切换操作指南
  • Skydive流量分析实战:从数据包捕获到深度协议解析的完整流程
  • 如何快速安装 git-flow-completion:三大Shell环境完整指南
  • 如何快速上手GSS引擎:5步实现响应式网页布局
  • 基于单片机的电话计费系统的设计
  • 搞定PS 2022的DR5插件‘未正确签署’报错,一条注册表命令就够了(附各版本对应表)
  • 千问3.5-27B效果实测:低质量扫描件文字区域检测与内容还原
  • 科研助手打造:OpenClaw调用Qwen3-14B实现文献综述自动化
  • 玩转红外遥控与步进电机的电子积木
  • Linux dd命令的深度解析与应用实践
  • AI模型优化与部署:从知识蒸馏到模型合并的完整解决方案
  • 基于STM32单片机的无线胎压监测系统
  • WuliArt Qwen-Image Turbo效果对比:FP16黑图频发 vs BF16稳定出图实测
  • 基于51单片机的太阳能LED路灯智能控制器:Proteus仿真与实现(包含原理图、流程图、物料...
  • 终极Windows Defender禁用工具:一键提升系统性能的完整解决方案
  • OpenClaw成本优化实践:百川2-13B-4bits量化模型本地调用方案
  • Crank.js未来展望:框架路线图和新功能预告