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

相关新闻

最新新闻

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现 【免费下载链接】serenity The Serenity Operating System 🐞 项目地址: https://gitcode.com/GitHub_Trending/se/serenity 导读 本文以 getopt(3) 手册 为核心&a…

2026/10/1 19:32:24
轻量服务器还是ECS?大促云服务器选购与避坑实战指南

轻量服务器还是ECS?大促云服务器选购与避坑实战指南

每年大促节点,群里永远有人在问同一个问题:“38元的轻量服务器到底怎么抢?为什么我每次点进去都是已售罄?68元直购和99元的ECS我到底选哪个?”作为一个常年帮团队和自己采购云服务器的老用户,我太清楚这种纠…

2026/9/30 21:32:07
为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南 【免费下载链接】agents Multi-harness agentic plugin marketplace for Claude Code, Codex, Cursor, OpenCode, GitHub Copilot, and Google Antigravity 项目地址:…

2026/10/2 15:29:32
PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署 【免费下载链接】PaddleOCR Turn any PDF or image document into structured data for your AI. A powerful, lightweight OCR toolkit that bridges the gap between i…

2026/10/1 19:32:23
Spring源码解析:构造器注入的类型转换与候选匹配机制

Spring源码解析:构造器注入的类型转换与候选匹配机制

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

2026/10/1 19:32:35
openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由 【免费下载链接】openai-agents-python A lightweight, powerful framework for multi-agent workflows 项目地址: https://gitcode.com/GitHub_Trending/op/openai-agents-pyth…

2026/9/30 21:32:11

日新闻

周新闻

月新闻