机器视觉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 }

相关新闻

BetterNCM Installer终极指南:3分钟搞定网易云插件安装的完整方案

BetterNCM Installer终极指南:3分钟搞定网易云插件安装的完整方案

BetterNCM Installer终极指南:3分钟搞定网易云插件安装的完整方案 【免费下载链接】BetterNCM-Installer 一键安装 Better 系软件 项目地址: https://gitcode.com/gh_mirrors/be/BetterNCM-Installer 还在为网易云音乐插件安装的复杂步骤而烦恼吗&#xff1f…

2026/10/30 8:16:14 阅读更多 →
nginx---cors跨域风险修复和防止Host头攻击

nginx---cors跨域风险修复和防止Host头攻击

经过两天的折腾,试了很多方法,最后总结下有效的设置:location /test/abc/ {#防止修改Host头攻击if ($http_Host ! "***.***.com:15689"){return 403;}#不允许任意跨域访问set $andflag 0;set $cors ;#后台已设置Access-Control-All…

2026/10/29 5:34:49 阅读更多 →
AI驱动的GEO智能体:数字营销的实时优化与多维度数据融合

AI驱动的GEO智能体:数字营销的实时优化与多维度数据融合

1. 项目背景与核心价值在数字营销领域,GEO(Geo-Enabled Optimization)优化一直是品牌提升本地化营销效果的关键手段。传统GEO优化主要依赖人工分析地理位置数据、用户画像和消费行为,不仅效率低下,还难以应对瞬息万变的…

2026/10/24 21:38:16 阅读更多 →

最新新闻

Electron桌面应用开发实战:从环境搭建到打包发布全流程

Electron桌面应用开发实战:从环境搭建到打包发布全流程

做前端这些年,我大部分时间都在跟浏览器标签页打交道,直到第一次把桌面应用跑起来,才发现原来自己一直被限制在地址栏和刷新按钮围起来的框框里。Electron这个词,最开始是在某段时间频繁出现在同事的讨论里,一个用HTML…

2026/10/30 0:01:36 阅读更多 →
滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

简介:亚太天能T109滑盖智能锁产品说明书是一份PDF格式的官方技术文档,主要面向智能锁安装人员、终端用户及售后维护者,帮助快速掌握产品安装、配置与常见故障处理。全包仅1个PDF文件,大小4.37MB,内容结构清晰&#xff…

2026/10/29 8:45:01 阅读更多 →
PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

上个月我在调试一台高速AGV的驱动系统,整车要求空载跑到8m/s,电机转速已经拉到额定点,驱动器的输出电压也顶到了母线电压极限附近。再往上踩速度指令,转速纹丝不动,三相电流反而开始震荡。那会儿我的第一反应就是&…

2026/10/29 9:52:56 阅读更多 →
C++核心知识点:从内存管理到STL的实战指南

C++核心知识点:从内存管理到STL的实战指南

C 这门语言我前前后后折腾了十几年,用它写过嵌入式驱动、做过游戏服务端,也搭过基础组件库。每次看到新人一头扎进"C 核心知识点"这个搜索词,我其实挺感慨的——这门语言的难点从来不是语法多,而是知识点之间的耦合太深…

2026/10/30 0:01:37 阅读更多 →
T109滑盖智能锁怎么装?安装调试与故障排查全攻略

T109滑盖智能锁怎么装?安装调试与故障排查全攻略

简介:亚太天能T109滑盖智能锁产品说明书以PDF格式发布,适合智能锁用户、安装人员与售后维修人员阅读。内容围绕T109系列指纹密码刷卡锁展开,先介绍前面板、后面板、锁体、机械钥匙、电池盒盖等部件名称与功能,再给出完整安装步骤&…

2026/10/29 9:51:34 阅读更多 →
LLM知识降熵:从信息垄断到理解权下放

LLM知识降熵:从信息垄断到理解权下放

你有没有过这种经历:翻开源码注释、论文摘要或者一份专业报告,每个字都认识,连在一起却像天书。想找人聊聊,身边要么没人懂,要么懂的懒得跟你解释。后来我试着把同一段内容丢给一个大语言模型,几秒钟之后&a…

2026/10/29 8:44:28 阅读更多 →

日新闻

周新闻

月新闻

我发现了一个新思路:用 Remotion + Claude Code 像写代码一样自动化生成短视频

我发现了一个新思路:用 Remotion + Claude Code 像写代码一样自动化生成短视频

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/29 17:01:10 阅读更多 →
Windows下 Codex 中 Chrome 和 Computer Use 插件不可用问题排查及解决参考方式:TaoToken 统一 Key 配置与验证

Windows下 Codex 中 Chrome 和 Computer Use 插件不可用问题排查及解决参考方式:TaoToken 统一 Key 配置与验证

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/29 9:42:17 阅读更多 →
黑夜航拍船只数据集训练YOLOV5模型全流程解析

黑夜航拍船只数据集训练YOLOV5模型全流程解析

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/30 0:01:33 阅读更多 →