Visionpro-blob工具-骰子的应用

张开发
2026/5/3 6:32:33 15 分钟阅读
Visionpro-blob工具-骰子的应用
斑点寻找工具BlobBlob的概述CogBlobTool 工具俗称斑点工具 经常应用于工业的一些检测类型的项目。斑点分析 探测并且分析图像中的二维形状Blob 是先根据用户设定好的灰阶范围对图像进行分割然后对目标进行查找和分析面积质心周长主轴......应用场景——Blob 分析非常合适以下场合的应用:—— 对象在尺寸、形状/或方向上差异很大训练模型很难或者不可能—— 对象在背景中找不到的截然不同的灰度—— 对象没有重叠或者接触—— 应用案例—— 检查环氧树脂点分配的数量、尺寸和形状—— 检查表示坏薄片模型的墨水点的正常位置和大小—— 检查药片的破碎和大小—— 根据对象的尺寸、形状或位置整理或者分类对象Blob_参数主要用于检测和分析图像中的斑点特征:SegmentationParams.Mode设置图像分割模式包括固定阈值HardFixedThreshold、动态阈值HardDynamicThreshold、相对阈值HardRelativeThreshold等。SegmentationParam.Polarity设置斑点的极性包括白底黑点DarkBlobs和黑底白点LightBlobsConnectivityCleanup设置连通性清理模式、包括修剪Prune、填充Fill等ConnectivityMinPixels设置最小面积用于过滤过小的斑点ConnectivityMode设置连通性包括已标记Labeled、灰度GreyScale等Region设置检测区域的形状、包括圆形CogCircle、椭圆CogEllipse、多边形CogPolygon等RunTimeMeasures设置测试属性包括面积Area、质心坐标CenterMassX、CenterMassY等阈值模式选择合适的阈值模式固定阈值、动态阈值、相对阈值来分割图像中的斑点和背景极性设置根据斑点的灰度特征选择白底黑点或黑底白点的极性连通性处理通过设置连通性模式和清理模式处理斑点的连通性问题区域设置指定检测区域、以限制斑点检测的范围面积过滤通过设置最小面积和最大面积过滤不符合要求的斑点形状筛选通过设置形状参数如非环形值、筛选出特定形状的斑点骰子案例工具使用CogBlobTool脚本声明label集合private CogGraphicCollection dt new CogGraphicCollection(); dt.Clear();封装labelprivate 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 namemessageSets the Message in the tools RunStatus./param /// param nameresultSets the Result in the tools RunStatus/param /// returnsTrue 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 namecurrentRecord /// 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 namelastRecord /// 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 namehostThe 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;创建labelprivate 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 namemessageSets the Message in the tools RunStatus./param /// param nameresultSets the Result in the tools RunStatus/param /// returnsTrue 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 namecurrentRecord /// 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 namelastRecord /// 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 namehostThe 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 }

更多文章