机器视觉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/7/24 22:42:02 阅读更多 →
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/7/24 22:42:02 阅读更多 →
AI驱动的GEO智能体:数字营销的实时优化与多维度数据融合

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

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

2026/7/24 22:42:02 阅读更多 →

最新新闻

终极指南:如何轻松访问全球最大同人创作平台AO3

终极指南:如何轻松访问全球最大同人创作平台AO3

终极指南:如何轻松访问全球最大同人创作平台AO3 【免费下载链接】AO3-Mirror-Site 项目地址: https://gitcode.com/gh_mirrors/ao/AO3-Mirror-Site 还在为无法访问Archive of Our Own(AO3)而烦恼吗?😊 作为全球…

2026/7/24 22:49:06 阅读更多 →
DP(动态规划)入门相关书籍

DP(动态规划)入门相关书籍

1、一本通 启蒙C版 2、算法训练营:入门篇(全彩版) 3、算法竞赛实战笔记(2024.01) 4、聪明人的游戏信息学探秘.提高篇-2017年06月 5、哇,编程!——跟小明一起学算法(2020.05) 6、算法入门之西游漫记——Python语言版(20…

2026/7/24 22:49:06 阅读更多 →
ERP 专家:懂 SAP、精 Oracle EBS、通华为 MetaERP—— 同时掌握国际两大顶级 ERP,又吃透国产顶级替代方案,属于现在企业数字化转型里很稀缺的 “三栖” 顾问 / 架构师。下面

ERP 专家:懂 SAP、精 Oracle EBS、通华为 MetaERP—— 同时掌握国际两大顶级 ERP,又吃透国产顶级替代方案,属于现在企业数字化转型里很稀缺的 “三栖” 顾问 / 架构师。下面

ERP 专家:懂 SAP、精 Oracle EBS、通华为 MetaERP—— 同时掌握国际两大顶级 ERP,又吃透国产顶级替代方案,属于现在企业数字化转型里很稀缺的 “三栖” 顾问 / 架构师。下面把三者定位、能力侧重和对比讲清楚,方便你直接用在简历、…

2026/7/24 22:49:06 阅读更多 →
别人家骑手下午在刷手机,你家骑手还在跑单——诚心呈意怎么做到的?

别人家骑手下午在刷手机,你家骑手还在跑单——诚心呈意怎么做到的?

做配送的人,心里都有一本账。 每天中午11点到下午1点,订单像潮水一样涌进来,骑手不够用,商家催、顾客催,后台的红字报警一个接一个;下午2点到5点,单量断崖式下跌,骑手在站点刷手机&…

2026/7/24 22:49:06 阅读更多 →
道路车辆智能追踪、车牌识别、测速、区域入侵和实时监控的可落地方案

道路车辆智能追踪、车牌识别、测速、区域入侵和实时监控的可落地方案

文章目录一、先说推荐结论二、为什么选择YOLO26,而不是YOLO11、YOLOv12或YOLOv131. CPU场景首选YOLO26n2. 推荐的选择规则普通办公CPU、低功耗设备8核以上桌面CPU、单路1080p高性能CPU、精度优先三、完整系统架构四、车辆检测与ByteTrack方案1. 检测类别2. ByteTrac…

2026/7/24 22:49:05 阅读更多 →
贪心算法精讲(附4道题型详细解析及代码)

贪心算法精讲(附4道题型详细解析及代码)

在了解贪心算法,我们要知道贪心的使用!!! 1.贪心是什么? 贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择。也就是说,不从整体最优上加以考虑,他所做出的是某种意义上的局部最优解。 2.贪心有哪些注意点呢? 贪心算法不是对所有问题都…

2026/7/24 22:48:05 阅读更多 →

日新闻

用Highcharts 创建可拖拽三维散点立方体3D图表

用Highcharts 创建可拖拽三维散点立方体3D图表

该案例基于Highcharts scatter3d 三维散点图实现空间立方体散点可视化,核心特色:三维 X/Y/Z 三轴空间,所有散点分布在 0~10 立方体空间内;散点使用径向渐变实现立体 3D 圆球质感;支持鼠标 / 触屏拖拽画布,…

2026/7/24 0:00:29 阅读更多 →
AppCertDlls:进程创建路径上的 DLL 入口

AppCertDlls:进程创建路径上的 DLL 入口

AppCertDlls:进程创建路径上的 DLL 入口 AppCertDlls 位于 HKLM\System\CurrentControlSet\Control\Session Manager\AppCertDlls。本文的程序功能是只读列出这个键在 64 位和 32 位注册表视图中的全部值,并显示每条值的来源、名称、类型和可安全显示的数…

2026/7/24 0:00:29 阅读更多 →
我的编程之路:第一篇博客

我的编程之路:第一篇博客

大家好,我是一名编程初学者,同时这也是我编程学习之路上的第一篇博客。在这里,我想要向大家介绍我的一些想法和规划。a.自我介绍我是一个刚刚接触编程的新手,目前在学习c语言,我对编程世界充满了强烈的好奇。当然&…

2026/7/24 0:00:29 阅读更多 →

周新闻

Go语言静态资源打包方案对比与实践指南

Go语言静态资源打包方案对比与实践指南

1. 项目背景与核心需求在Go语言开发中,我们经常需要处理静态资源文件的打包问题。无论是Web应用的模板文件、前端资源,还是配置文件、证书等,都需要随程序一起分发。传统做法是将这些文件与编译后的二进制文件放在同一目录下,但这…

2026/7/24 3:59:20 阅读更多 →
Go语言实现高性能LDAP认证服务的架构与实践

Go语言实现高性能LDAP认证服务的架构与实践

1. 项目背景与核心价值LDAP(轻量级目录访问协议)作为企业级身份认证的黄金标准,已经服务了超过80%的财富500强公司。我在金融科技领域实施统一认证体系时,发现传统Java方案存在启动慢、内存占用高等痛点。而Go语言凭借其协程并发模…

2026/7/24 1:23:39 阅读更多 →
【AI面试官实战指南】:用ChatGPT模拟10类高频技术岗面试,3天提升应答精准度92%

【AI面试官实战指南】:用ChatGPT模拟10类高频技术岗面试,3天提升应答精准度92%

更多请点击: https://intelliparadigm.com 第一章:AI面试官实战指南的核心价值与适用场景 AI面试官并非替代人类HR的“黑箱工具”,而是以可解释、可审计、可迭代的方式,赋能招聘全链路的关键基础设施。其核心价值在于将主观经验沉…

2026/7/24 18:52:18 阅读更多 →

月新闻