机器视觉11 —— CogGraphicCollection 图形集合 使用图形集合CogGraphicCollection在CogBlobTool工具找到的斑点周围画图形。CogGraphicCollection 是康耐视CognexVisionPro 中用于管理图形对象集合的工具。以下是其详细介绍功能特点图形管理可用于存储和管理多个 CogGraphic 类型的图形对象如 CogGraphicLabel、CogCircle、CogLine 等。通过该工具能方便地对这些图形对象进行统一的添加、删除、遍历和操作。灵活组合可以将不同类型的图形对象组合在一起形成一个复杂的图形集合。例如在一个检测场景中可能同时需要在图像上显示标签、圆形标记和直线标注等CogGraphicCollection 就可以将这些不同的图形组合到一起进行管理和显示。与其他工具集成能与 VisionPro 中的其他工具如 CogRecordDisplay、CogToolBlock 等进行集成。例如可以将 CogGraphicCollection 中的图形对象添加到 CogRecordDisplay 中进行显示或者在 CogToolBlock 的脚本中对 CogGraphicCollection 进行操作以实现更复杂的视觉应用功能。应用场景工业检测可视化在工业检测中用于将检测结果、标注信息等以图形的形式显示在图像上方便操作人员直观地查看检测情况。例如在检测零件的缺陷时可将缺陷位置用圆形或矩形标记出来同时添加标签注明缺陷类型。视觉引导辅助在机器人视觉引导系统中可用于显示目标物体的位置、姿态等信息的图形表示为机器人的操作提供直观的参考。比如用箭头表示目标物体的方向用圆形表示目标物体的中心位置。数据展示与分析在各种视觉应用中将处理后的数据以图形化的方式展示在图像上方便用户查看和分析。例如在统计产品数量时用柱状图或饼图表示不同类型产品的数量分布。原图和代码在文末。CogRectangleAffine仿射矩形CogGraphicCollection图形集合显示效果命名空间和私有字段在运行工具之前需要先清空图形集合不然切换图片后之前画的图案会一直保留。参数 ( CogBlobAxisConstants.Principal ) 最小外接矩形遍历blob2工具找到的所有斑点添加到图形集合中。通过面积来判断矩形的颜色并把绿色矩形的旋转角度设置为0红色矩形角度没有设置。最后添加输出显示作业原图脚本代码#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.Blob; #endregion public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase { #region Private Member Variables private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock; // 声明仿射矩形和圆变量 private CogRectangleAffine rec1; private CogRectangleAffine rec2; private CogCircle cir; // 创建图形集合 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(); // 映射工具 CogBlobTool blob1 mToolBlock.Tools[CogBlobTool1] as CogBlobTool; CogBlobTool blob2 mToolBlock.Tools[CogBlobTool2] as CogBlobTool; CogBlobTool blob3 mToolBlock.Tools[CogBlobTool3] as CogBlobTool; // 在blob1工具找到的第一个斑点质心位置画出最小外接矩形 if (blob1.Results.GetBlobs().Count 0) { rec1 new CogRectangleAffine(); // 矩形rec1为blob1找到的第一个结果的(CogBlobAxisConstants.Principal)最小外接矩形 rec1 blob1.Results.GetBlobByID(0).GetBoundingBox(CogBlobAxisConstants.Principal); // 设置矩形颜色为蓝色 rec1.Color CogColorConstants.Blue; gc.Add(rec1); //把矩形添加进入图形集合 } // 在blob2工具找到的全部斑点质心位置画出最小外接矩形 foreach (CogBlobResult b1 in blob2.Results.GetBlobs()) { rec2 new CogRectangleAffine(); rec2 b1.GetBoundingBox(CogBlobAxisConstants.Principal); if (b1.Area 1000) // 用面积来判断矩形颜色 { rec2.Rotation 0; // 绿色矩形的角度 rec2.Color CogColorConstants.Green; } else { rec2.Color CogColorConstants.Red; } gc.Add(rec2); //把矩形添加进入图形集合 } // 在blob3工具找到的第一个斑点的质心位置画圆 if (blob3.Results.GetBlobs().Count 0) { cir new CogCircle(); cir.Color CogColorConstants.Magenta; // 设置圆的XY坐标位置为blob3找到的第一个结果的质心位置 cir.CenterX blob3.Results.GetBlobByID(0).CenterOfMassX; cir.CenterY blob3.Results.GetBlobByID(0).CenterOfMassY; cir.Radius 40; // 圆半径 gc.Add(cir); //把圆添加进入图形集合 } 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) { foreach (ICogGraphic item in gc) { mToolBlock.AddGraphicToRunRecord(item, lastRecord, CogBlobTool1.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分钟解锁QQ音乐加密文件,实现跨平台自由播放

终极指南:如何3分钟解锁QQ音乐加密文件,实现跨平台自由播放

终极指南:如何3分钟解锁QQ音乐加密文件,实现跨平台自由播放 【免费下载链接】qmc-decoder Fastest & best convert qmc 2 mp3 | flac tools 项目地址: https://gitcode.com/gh_mirrors/qm/qmc-decoder 你是否曾经下载了心爱的QQ音乐&#xff…

2026/7/24 21:28:24
AI Agent 面试题 600:文档分块(Chunking)策略有哪些?如何选择最优方案?

AI Agent 面试题 600:文档分块(Chunking)策略有哪些?如何选择最优方案?

🔥 AI Agent 面试题 600:文档分块(Chunking)策略有哪些?如何选择最优方案?摘要:本文深入解析了「文档分块(Chunking)策略有哪些?如何选择最优方案&#xff1f…

2026/7/24 21:28:24
零基础AI换脸神器roop-unleashed:5分钟实现专业级面部替换,无需训练直接上手

零基础AI换脸神器roop-unleashed:5分钟实现专业级面部替换,无需训练直接上手

零基础AI换脸神器roop-unleashed:5分钟实现专业级面部替换,无需训练直接上手 【免费下载链接】roop-unleashed Evolved Fork of roop with Web Server and lots of additions 项目地址: https://gitcode.com/gh_mirrors/ro/roop-unleashed 想要制…

2026/7/24 21:28:24
AI Agent 面试题 604:分块大小(Chunk Size)对RAG效果的影响分析

AI Agent 面试题 604:分块大小(Chunk Size)对RAG效果的影响分析

🔥 AI Agent 面试题 604:分块大小(Chunk Size)对RAG效果的影响分析摘要:本文深入解析了「分块大小(Chunk Size)对RAG效果的影响分析」这一 AI Agent 领域的核心面试题。文章从 文档分块策略 的基…

2026/7/24 21:28:24
Python 3.9 已停止维护!从 3.9 到 3.14 全版本深度对比,生产环境该选哪个?

Python 3.9 已停止维护!从 3.9 到 3.14 全版本深度对比,生产环境该选哪个?

Python 3.9 已停止维护!从 3.9 到 3.14 全版本深度对比,生产环境该选哪个?本文约 5000 字,阅读约 12 分钟。 你将看到:Python 3.9→3.14 六个版本的完整特性对比、性能基准数据、库兼容性矩阵、迁移风险清单&#xff0…

2026/7/24 21:28:24
3分钟上手TMSpeech:免费高效的Windows实时语音识别终极指南

3分钟上手TMSpeech:免费高效的Windows实时语音识别终极指南

3分钟上手TMSpeech:免费高效的Windows实时语音识别终极指南 【免费下载链接】TMSpeech 腾讯会议摸鱼工具 项目地址: https://gitcode.com/gh_mirrors/tm/TMSpeech 还在为会议记录手忙脚乱吗?想要将语音实时转文字却找不到合适的工具?T…

2026/7/24 21:23:24

月新闻