机器视觉9 —— CogHistogramTool 直方图工具 CogHistogramTool 是康耐视CognexVisionPro 中的一个用于计算和分析图像直方图的工具。以下是其详细介绍功能直方图计算能计算图像的灰度直方图统计图像中每个灰度值的像素数量也能计算颜色直方图统计图像中每种颜色值的像素数量。区域选择支持对整个图像或指定区域计算直方图方便用户根据具体需求分析图像不同部分的像素分布。统计分析提供直方图的统计数据如均值、方差、标准差、像素总数、中值、出现概率最大的像素值等帮助用户更全面地了解图像像素的分布特征。应用场景图像增强通过分析直方图来调整图像的对比度和亮度增强图像质量例如可以根据直方图的分布情况拉伸或压缩图像的灰度范围使图像的细节更加清晰。质量检测在工业自动化领域通过直方图分析产品图像的灰度分布检测质量缺陷。如产品表面的划痕、污渍等可能会导致图像灰度分布的异常通过分析直方图可以发现这些异常从而实现质量检测。使用方法在 VisionPro 中通常先打开工具栏通过双击或点击鼠标拖拽添加 CogHistogramTool 工具。然后添加输入图像可以通过点击鼠标右键 “链接到” 或以连线拖拽的方式选择相应输入图片。接着设置需要统计灰度值的区域默认状态下是统计整个图像所有像素的灰度值信息。运行工具后可得到像素灰度值的分布直方图在结果栏中可查看各种统计信息。用直方图工具检测有胶无胶图像是常用的兔子头图像把眼睛位置涂抹当做胶来检测。工具搭建由于是RGB彩图需要用到 CogImageConvertTool 格式转换工具把彩图转换为灰度图用灰度图给到 Block 使用Block里面添加了模板工具、定位工具和灰度检测工具。输入像源给到格式转换工具不需要设置什么参数直接运行工具即可。点击运行后可以看到输入的是RGB彩图输出的是8位灰度图用输出的灰度图给到后续的工具。灰度检测用两个灰度工具分别检测左右眼睛位置判断查看结果可以看到平均值差异很大就用平均值来做判断平均值小于50为无胶大于50为有胶。判断后的显示效果脚本显示脚本分析及文字显示这里用了简写。脚本中 CogHistogramTool 结果对应参数用直方图工具完成自动增减亮度功能目的在工业检测中常会遇到同款产品不同颜色的情况需要设置不同的亮度来突出检测部分特征使用 CogHistogramTool 直方图工具搭配 CogIPOneImageTool 图像处理工具完成自动增减亮度的功能当切换不同颜色产品时自动设置合适的系数。工具搭建输入图像给到格式转换图像转换为灰度图灰度图给到blockblock计算出系数给到图像处理工具添加终端右击图像处理工具点击添加终端点击浏览选择所有未过滤找到所需的参数点击添加输入不是输出block工具使用block工具是为了方便写脚本和添加自定义参数block工具内包含了两个直方图工具选取不同位置来计算一个平均值来决定系数。自定义输出参数脚本编写把得到的系数赋值给到输出参数把输出参数链接到图像处理工具的灰度乘数参数上当系数变化时自动修改灰度乘数的值得到不同亮度的图像。脚本源码有胶无胶判断#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; #endregion public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase { #region Private Member Variables private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock; private 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 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 // Run each tool using the RunTool function foreach(ICogTool tool in mToolBlock.Tools) mToolBlock.RunTool(tool, ref message, ref result); gc.Clear(); CogHistogramTool his1 mToolBlock.Tools[CogHistogramTool1] as CogHistogramTool; CogHistogramTool his2 mToolBlock.Tools[CogHistogramTool2] as CogHistogramTool; double newMean (his1.Result.Mean his1.Result.Mean) / 2; if (his1.Result.Mean 50) AddLabel(100, 50, 左眼有胶, CogColorConstants.Green); else AddLabel(100, 50, 左眼无胶, CogColorConstants.Red); if (his2.Result.Mean 50) AddLabel(100, 110, 右眼有胶, CogColorConstants.Green); else AddLabel(100, 110, 右眼无胶, CogColorConstants.Red); AddLabel(400, 50, 最大值: his1.Result.Maximum, CogColorConstants.Cyan); AddLabel(400, 80, 平均值: his1.Result.Mean, CogColorConstants.Cyan); AddLabel(400, 110, 中值: his1.Result.Median, CogColorConstants.Cyan); AddLabel(400, 140, 最小值: his1.Result.Minimum, CogColorConstants.Cyan); AddLabel(400, 170, 模式: his1.Result.Mode, CogColorConstants.Cyan); AddLabel(400, 200, 示例: his1.Result.NumSamples, CogColorConstants.Cyan); AddLabel(400, 230, 标准差: his1.Result.StandardDeviation, CogColorConstants.Cyan); AddLabel(400, 270, 方差: his1.Result.Variance, CogColorConstants.Cyan); return false; } // 文本方法 private void AddLabel (double x, double y, string text, CogColorConstants color) { CogGraphicLabel label new CogGraphicLabel(); label.Color color; label.Font new Font(楷体, 30); label.SetXYText(x, y, text); gc.Add(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 item in gc) { mToolBlock.AddGraphicToRunRecord(item, lastRecord, CogPMAlignTool1.InputImage, ); } } #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 }自动亮度#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; #endregion public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase { #region Private Member Variables private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock; #endregion /// 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 // Run each tool using the RunTool function foreach(ICogTool tool in mToolBlock.Tools) mToolBlock.RunTool(tool, ref message, ref result); // 映射工具 CogHistogramTool his1 mToolBlock.Tools[CogHistogramTool1] as CogHistogramTool; CogHistogramTool his2 mToolBlock.Tools[CogHistogramTool2] as CogHistogramTool; // 创建两个浮点数的变量一个接收平均值另一个设置系数 double meanValue (his1.Result.Mean his2.Result.Mean) / 2; double adaptValue 1; // 用两个直方图工具结果的平均值来给系数赋值 if (meanValue 120) adaptValue 1.2; if (meanValue 120 meanValue 100) adaptValue 1.5; if (meanValue 100) adaptValue 2; // 把平均值和系数赋值给到输出参数 mToolBlock.Outputs[meanValue].Value meanValue; mToolBlock.Outputs[kValue].Value adaptValue; 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 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) { } #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 }

本月热点