新闻详情

新闻详情

首页 / 资讯中心 / 详情

VisionPro-保险丝颜色识别-高级脚本练习

发布时间:2026/9/6 3:58:55来源:尧图网络
VisionPro-保险丝颜色识别-高级脚本练习
目标显示当前页面的所有颜色数量结果一、控件设置1、CogToolBlock因为图片不只有一张并且保险丝的数量和位置也不确定所以需要编写脚本通过CogPMAlignTool定位然后将定位的XY坐标传输给CogCompositeColorMatchTool最后通过脚本遍历出所有的保险丝位置然后判断颜色外部只写一个控件就是CogToolBlock接下来进入CogToolBlock开始配置其他控件所有控件2、CogImageCovertTool因为CogPMAlignTool只能处理灰度图所以通过CogImageCovertTool转换为灰度图后再将转换后的图传入CogPMAlignTool进行位置判断。直接连线即可不需要打开自动转换3、CogPMAlignTool双击进入此控件4、CogCompositeColorMatchTool双击进入控件二、代码设置1、基础配置Region.(Cognex.VisionPro.CogRectangleAffine).CenterXRegion.(Cognex.VisionPro.CogRectangleAffine).CenterYRegion.(Cognex.VisionPro.CogRectangleAffine).Rotation上面这三个仅作演示不需要连接或添加终端使用默认的CogCompositeColorMatchTool1工具即可2、代码#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; using Cognex.VisionPro.PMAlign; using Cognex.VisionPro.CompositeColorMatch; #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 /// //全局变量:一个用于管理 VisionPro 图形对象ICogGraphic的集合可以同时存储标签、圆、矩形、多边形等各种图形 CogGraphicLabel label new CogGraphicLabel(); 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); //定义五个颜色变量 int red 0; int yellow 0; int green 0; int blue 0; int orange 0; //获取PMAlign对象 CogPMAlignTool pma mToolBlock.Tools[CogPMAlignTool1] as CogPMAlignTool; //获取CompositeColorMatch对象 CogCompositeColorMatchTool colorMatch mToolBlock.Tools[CogCompositeColorMatchTool1] as CogCompositeColorMatchTool; //获取CompositeColorMatch的Region对象 CogRectangleAffine region colorMatch.Region as CogRectangleAffine; //根据pma找到的数量进入循环 foreach(CogPMAlignResult item in pma.Results) { //移动CompositeColorMatch的中心点 region.CenterX item.GetPose().TranslationX; region.CenterY item.GetPose().TranslationY; region.Rotation item.GetPose().Rotation; //旋转角度 //完成移动后直接运行 colorMatch.Run(); //获取运行后的最高匹配度的颜色名字 string colorName colorMatch.Result.ResultOfBestMatch.Color.Name; //根据名字进行操作 if (colorName red) { red; } else if(colorName yellow) { yellow; } else if(colorName blue) { blue; } else if(colorName orange) { orange; } else if(colorName green) { green; } } //创建最终要输出的字符串 String colorString String.Format(蓝色:{0},绿色{1},橙色:{2},红色:{3},黄色:{4}, blue, green, orange, red, yellow); //指定label的输出位置和内容 label.SetXYText(240, 30, colorString); //修改颜色 label.Color CogColorConstants.DarkRed; //修改字体和大小 label.Font new Font(楷体, 20); 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) { //输出 //label输出内容 //CogImageConvertTool1.InputImage输出到那张图中 //日志 mToolBlock.AddGraphicToRunRecord(label, lastRecord, CogImageConvertTool1.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 }3、对象对照图三、结果
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

流失预警归因诊断:从「知道用户要流失」到「知道为什么流失」 2026/9/6 4:41:00

流失预警归因诊断:从「知道用户要流失」到「知道为什么流失」

一、问题的起点:预警为什么需要「归因」很多流失预警系统做到这样一步就停了:用户连续 N 天没有交易 → 标记为流失预警按风险等级分个「高 / 中 / 低」→ 推给客服去跟进但客服拿到一条预警时,心里想的是另一件事:「我知道这个客…

阅读更多 →
缓存与性能优化:Spring Boot 3 集成 Redis 与 Caffeine 实现多级缓存策略 2026/9/6 4:41:00

缓存与性能优化:Spring Boot 3 集成 Redis 与 Caffeine 实现多级缓存策略

系列导读 你现在看到的是《Spring Boot 3 企业开发实战:从零到生产级架构的十步进阶》的第 6/10 篇,当前这篇会重点解决:通过多级缓存实战,帮助读者理解缓存原理并有效提升系统并发能力。 上一篇回顾:第 5 篇《Spring Boot 3 安全实战:基于 Spring Security 6 的 JWT 认…

阅读更多 →
【自动驾驶/机器人控制】之坐标系运动学仿真代码工程分析(一) 2026/9/6 4:41:00

【自动驾驶/机器人控制】之坐标系运动学仿真代码工程分析(一)

来源:motion.cpp,仿真刚体圆周运动,模拟 IMU 姿态积分过程。 整体逻辑:每一步仿真周期 dt 内,先做位置更新,再做姿态旋转更新,最后推送数据给 Pangolin 可视化。 注意执行顺序:先更新…

阅读更多 →
从单体应用到微服务:后端系统拆分的基本方法 2026/9/6 4:41:00

从单体应用到微服务:后端系统拆分的基本方法

摘要 微服务并不是把一个项目简单拆成几个 Spring Boot 应用。真正的微服务改造,需要重新设计业务边界、数据所有权、服务通信、部署方式、故障处理和团队协作模式。 单体应用在早期通常开发效率高、部署简单、事务边界清晰,但随着业务增长,代…

阅读更多 →
买 SaaS 还是买源码?连锁门店系统的三年成本账 2026/9/6 4:41:00

买 SaaS 还是买源码?连锁门店系统的三年成本账

先给结论:门店在 5 家以内、三年内不打算大规模扩张,SaaS 更省心;门店超过 8 家、或者这套系统准备用满三年以上,源码私有化部署的总成本通常更低。分界线不在单价,在于同一套能力你重复付了多少次费。 在这里插入图片…

阅读更多 →
Spring Boot 3 应用容器化与 CI/CD:从 Docker 到 Kubernetes 的云原生部署实战 2026/9/6 4:37:59

Spring Boot 3 应用容器化与 CI/CD:从 Docker 到 Kubernetes 的云原生部署实战

系列导读 你现在看到的是《Spring Boot 3 企业开发实战:从零到生产级架构的十步进阶》的第 10/10 篇,当前这篇会重点解决:打通从开发到运维的最后一公里,让 Spring Boot 3 应用真正具备云原生能力。 上一篇回顾:第 9 篇《生产环境可观测性:Spring Boot 3 集成 Micromet…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞