机器视觉9 —— CogHistogramTool 直方图工具
2026/7/24 22:41:40 网站建设 项目流程

CogHistogramTool 是康耐视(Cognex)VisionPro 中的一个用于计算和分析图像直方图的工具。以下是其详细介绍:

  • 功能
    • 直方图计算:能计算图像的灰度直方图,统计图像中每个灰度值的像素数量,也能计算颜色直方图,统计图像中每种颜色值的像素数量。
    • 区域选择:支持对整个图像或指定区域计算直方图,方便用户根据具体需求分析图像不同部分的像素分布。
    • 统计分析:提供直方图的统计数据,如均值、方差、标准差、像素总数、中值、出现概率最大的像素值等,帮助用户更全面地了解图像像素的分布特征。
  • 应用场景
    • 图像增强:通过分析直方图来调整图像的对比度和亮度,增强图像质量,例如可以根据直方图的分布情况,拉伸或压缩图像的灰度范围,使图像的细节更加清晰。
    • 质量检测:在工业自动化领域,通过直方图分析产品图像的灰度分布,检测质量缺陷。如产品表面的划痕、污渍等可能会导致图像灰度分布的异常,通过分析直方图可以发现这些异常,从而实现质量检测。
  • 使用方法:在 VisionPro 中,通常先打开工具栏,通过双击或点击鼠标拖拽添加 CogHistogramTool 工具。然后添加输入图像,可以通过点击鼠标右键 “链接到” 或以连线拖拽的方式选择相应输入图片。接着设置需要统计灰度值的区域,默认状态下是统计整个图像所有像素的灰度值信息。运行工具后,可得到像素灰度值的分布直方图,在结果栏中可查看各种统计信息。

用直方图工具检测有胶无胶

图像是常用的兔子头图像,把眼睛位置涂抹当做胶来检测。

工具搭建

由于是RGB彩图,需要用到 CogImageConvertTool 格式转换工具,把彩图转换为灰度图,用灰度图给到 Block 使用,Block里面添加了模板工具、定位工具和灰度检测工具。

输入像源给到格式转换工具,不需要设置什么参数,直接运行工具即可。

点击运行后可以看到输入的是RGB彩图,输出的是8位灰度图,用输出的灰度图给到后续的工具。

灰度检测

用两个灰度工具分别检测左右眼睛位置

判断

查看结果可以看到平均值差异很大,就用平均值来做判断,平均值小于50为无胶,大于50为有胶。

判断后的显示效果

脚本显示

脚本分析及文字显示,这里用了简写。

脚本中 CogHistogramTool 结果对应参数

用直方图工具完成自动增减亮度功能

目的

在工业检测中常会遇到同款产品不同颜色的情况,需要设置不同的亮度来突出检测部分特征,使用 CogHistogramTool 直方图工具搭配 CogIPOneImageTool 图像处理工具完成自动增减亮度的功能,当切换不同颜色产品时自动设置合适的系数。

工具搭建

  • 输入图像给到格式转换图像转换为灰度图
  • 灰度图给到block
  • block计算出系数给到图像处理工具

添加终端

  1. 右击图像处理工具
  2. 点击添加终端
  3. 点击浏览
  4. 选择所有(未过滤)
  5. 找到所需的参数
  6. 点击添加输入(不是输出!!!)

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 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(); 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 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, "CogPMAlignTool1.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 }

自动亮度

#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 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); // 映射工具 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 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) { } #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 }

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询