当前位置: 首页 > news >正文

CogPMAlignMultiTool 工具 脚本实写硬币及载具案例

1.载具测量

先导入图片

拉取所对应的工具

private CogGraphicCollection dt = new CogGraphicCollection(); dt.Clear(); CogCaliperTool top = mToolBlock.Tools["top"]as CogCaliperTool; CogCaliperTool bottom = mToolBlock.Tools["bottom"]as CogCaliperTool; CogCaliperTool middle = mToolBlock.Tools["middle"]as CogCaliperTool; foreach(ICogTool tool in mToolBlock.Tools) { mToolBlock.RunTool(tool, ref message, ref result); if(tool.RunStatus.Result == CogToolResultConstants.Accept) { dt.Add(Createlabel("top's is " + top.Results[0].Width.ToString("F2"), 12, 30, 100, CogColorConstants.Blue)); dt.Add(Createlabel("bottom's is " + bottom.Results[0].Width.ToString("F2"), 12, 30, 140, CogColorConstants.Blue)); dt.Add(Createlabel("middle's is " + middle.Results[0].Width.ToString("F2"), 12, 30, 180, CogColorConstants.Blue)); } } private CogGraphicLabel Createlabel(string text, float size, double x, double y, CogColorConstants color) { CogGraphicLabel label = new CogGraphicLabel(); label.BackgroundColor = CogColorConstants.White; label.SetXYText(x, y,text); label.Color = color; label.Font = new Font("Arial", size,FontStyle.Bold,GraphicsUnit.Pixel); label.Alignment = CogGraphicLabelAlignmentConstants.TopLeft; return label; } foreach(ICogGraphic s in dt) { mToolBlock.AddGraphicToRunRecord(s, lastRecord, "CogPMAlignTool1.InputImage", "script"); }

效果如下


2.硬币检测统计(三种方式)

方式1 (纯工具)

工具:

CogPMAlignTool

CogResultsAnalysisTool

CogCreateGraphicLabelTool

CogPMAlignTool

1. 抓取训练图像

2.制作掩膜,凸显轮廓特征

3.运行参数

4.训练区域

5.添加终端


CogResultsAnalysisTool

1.添加名称变量,算法运算符

2.添加终端


CogCreateGraphicLabelTool

1.选择器

2.文本类型

3.保留两位小数格式

效果


方式2(工具+脚本)

如图:

1.创建标签脚本:

private CogGraphicLabel createlabel(string text, float size, double x, double y, CogColorConstants color) { CogGraphicLabel label = new CogGraphicLabel(); label.Font = new Font("Arial", size, FontStyle.Bold, GraphicsUnit.Pixel); label.BackgroundColor = CogColorConstants.White; label.Alignment = CogGraphicLabelAlignmentConstants.TopLeft; label.Color = color; label.SetXYText(x, y, text); return label; }

2.label集合模块

private CogGraphicCollection dt = new CogGraphicCollection(); dt.Clear();

3.工具脚本声明

CogPMAlignTool yimao = mToolBlock.Tools["一角"]as CogPMAlignTool; CogPMAlignTool yiyuan = mToolBlock.Tools["一元"]as CogPMAlignTool; CogPMAlignTool wumao = mToolBlock.Tools["五角"]as CogPMAlignTool;

4.创建生明

foreach(ICogTool tool in mToolBlock.Tools) { mToolBlock.RunTool(tool, ref message, ref result); if(tool.RunStatus.Result == CogToolResultConstants.Accept) { dt.Add(createlabel("一角的数量是:" + yimao.Results.Count + "个" + " 额度是:" + (yimao.Results.Count * 0.1), 25, 20, 120, CogColorConstants.Blue)); dt.Add(createlabel("一元的数量是:" + yiyuan.Results.Count + "个" + " 额度是:" + (yiyuan.Results.Count * 1.0), 25, 20, 160, CogColorConstants.Purple)); dt.Add(createlabel("五角的数量是:" + wumao.Results.Count + "个" + " 额度是:" + (wumao.Results.Count *0.5), 25, 20, 200, CogColorConstants.Orange)); } } dt.Add(createlabel("总额是:" +((yimao.Results.Count * 0.1) + (yiyuan.Results.Count * 1.0) + (wumao.Results.Count * 0.5))+ "元", 25, 20, 250, CogColorConstants.Red));

5.label声明主页

foreach(ICogGraphic s in dt) { mToolBlock.AddGraphicToRunRecord(s, lastRecord, "一角.InputImage", "script"); }

脚本

private CogGraphicCollection dt = new CogGraphicCollection(); dt.Clear(); CogPMAlignTool yimao = mToolBlock.Tools["一角"]as CogPMAlignTool; CogPMAlignTool yiyuan = mToolBlock.Tools["一元"]as CogPMAlignTool; CogPMAlignTool wumao = mToolBlock.Tools["五角"]as CogPMAlignTool; foreach(ICogTool tool in mToolBlock.Tools) { mToolBlock.RunTool(tool, ref message, ref result); if(tool.RunStatus.Result == CogToolResultConstants.Accept) { dt.Add(createlabel("一角的数量是:" + yimao.Results.Count + "个" + " 额度是:" + (yimao.Results.Count * 0.1), 25, 20, 120, CogColorConstants.Blue)); dt.Add(createlabel("一元的数量是:" + yiyuan.Results.Count + "个" + " 额度是:" + (yiyuan.Results.Count * 1.0), 25, 20, 160, CogColorConstants.Purple)); dt.Add(createlabel("五角的数量是:" + wumao.Results.Count + "个" + " 额度是:" + (wumao.Results.Count *0.5), 25, 20, 200, CogColorConstants.Orange)); } } dt.Add(createlabel("总额是:" +((yimao.Results.Count * 0.1) + (yiyuan.Results.Count * 1.0) + (wumao.Results.Count * 0.5))+ "元", 25, 20, 250, CogColorConstants.Red)); private CogGraphicLabel createlabel(string text, float size, double x, double y, CogColorConstants color) { CogGraphicLabel label = new CogGraphicLabel(); label.Font = new Font("Arial", size, FontStyle.Bold, GraphicsUnit.Pixel); label.BackgroundColor = CogColorConstants.White; label.Alignment = CogGraphicLabelAlignmentConstants.TopLeft; label.Color = color; label.SetXYText(x, y, text); return label; } foreach(ICogGraphic s in dt) { mToolBlock.AddGraphicToRunRecord(s, lastRecord, "一角.InputImage", "script"); }
全部脚本
#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.PMAlign; #endregion public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase { #region Private Member Variables private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock; #endregion private CogGraphicCollection dt = new CogGraphicCollection(); /// <summary> /// Called when the parent tool is run. /// Add code here to customize or replace the normal run behavior. /// </summary> /// <param name="message">Sets the Message in the tool's RunStatus.</param> /// <param name="result">Sets the Result in the tool's RunStatus</param> /// <returns>True 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 dt.Clear(); CogPMAlignTool yimao = mToolBlock.Tools["一角"]as CogPMAlignTool; CogPMAlignTool yiyuan = mToolBlock.Tools["一元"]as CogPMAlignTool; CogPMAlignTool wumao = mToolBlock.Tools["五角"]as CogPMAlignTool; // Run each tool using the RunTool function foreach(ICogTool tool in mToolBlock.Tools) { mToolBlock.RunTool(tool, ref message, ref result); if(tool.RunStatus.Result == CogToolResultConstants.Accept) { dt.Add(createlabel("一角的数量是:" + yimao.Results.Count + "个" + " 额度是:" + (yimao.Results.Count * 0.1), 25, 20, 120, CogColorConstants.Blue)); dt.Add(createlabel("一元的数量是:" + yiyuan.Results.Count + "个" + " 额度是:" + (yiyuan.Results.Count * 1.0), 25, 20, 160, CogColorConstants.Purple)); dt.Add(createlabel("五角的数量是:" + wumao.Results.Count + "个" + " 额度是:" + (wumao.Results.Count *0.5), 25, 20, 200, CogColorConstants.Orange)); } } dt.Add(createlabel("总额是:" +((yimao.Results.Count * 0.1) + (yiyuan.Results.Count * 1.0) + (wumao.Results.Count * 0.5))+ "元", 25, 20, 250, CogColorConstants.Red)); return false; } private CogGraphicLabel createlabel(string text, float size, double x, double y, CogColorConstants color) { CogGraphicLabel label = new CogGraphicLabel(); label.Font = new Font("Arial", size, FontStyle.Bold, GraphicsUnit.Pixel); label.BackgroundColor = CogColorConstants.White; label.Alignment = CogGraphicLabelAlignmentConstants.TopLeft; label.Color = color; label.SetXYText(x, y, text); return label; } #region When the Current Run Record is Created /// <summary> /// Called when the current record may have changed and is being reconstructed /// </summary> /// <param name="currentRecord"> /// 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 name="lastRecord"> /// The new last run record is available to be initialized or customized.</param> public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord) { foreach(ICogGraphic s in dt) { mToolBlock.AddGraphicToRunRecord(s, lastRecord, "一角.InputImage", "script"); } } #endregion #region When the Script is Initialized /// <summary> /// Perform any initialization required by your script here /// </summary> /// <param name="host">The 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(CogPMAlignMultiTool)

1.工具

2.添加pma的方案

3.PMA的训练

4.多层训练

5.Multi Params 设置

6.PM Run Params 设置

7.得分

8.脚本

声明

CogPMAlignMultiTool pma = mToolBlock.Tools["CogPMAlignMultiTool1"]as CogPMAlignMultiTool;

匹配种类数量

pma.Results.ResultItemCollection.Coun

匹配种类名字

Results.ResultItemCollection[0].Name

种类名字的数量

pma.Results.ResultItemCollection[0].NumFound

private CogGraphicCollection dt = new CogGraphicCollection(); dt.Clear(); CogPMAlignMultiTool pma = mToolBlock.Tools["CogPMAlignMultiTool1"]as CogPMAlignMultiTool; double x = 0; double y = 60; CogColorConstants[] colors = new CogColorConstants[] { CogColorConstants.Blue, CogColorConstants.Purple, CogColorConstants.Green }; double total = 0; //string name=pma.Results for(int i = 0;i < pma.Results.ResultItemCollection.Count;i++) { switch(pma.Results.ResultItemCollection[i].Name) { case"一元": x = 1.0; break; case"一毛": x = 0.1; break; case"五毛": x = 0.5; break; default: message = "Error"; break; } if(message != "Error") { total += (pma.Results.ResultItemCollection[i].NumFound) * x; dt.Add(createlabel((pma.Results.ResultItemCollection[i].Name + "的数量是" + pma.Results.ResultItemCollection[i].NumFound + "\t 金额数是:" + ((pma.Results.ResultItemCollection[i].NumFound) * x)), 25, 20, (120 + i * y), colors[i])); } } dt.Add(createlabel("总金额是:" + total, 30, 20, 300, CogColorConstants.Red)); private CogGraphicLabel createlabel(string text, float size, double x, double y, CogColorConstants color) { CogGraphicLabel label = new CogGraphicLabel(); label.Font = new Font("Arial", size, FontStyle.Bold, GraphicsUnit.Pixel); label.Alignment = CogGraphicLabelAlignmentConstants.TopLeft; label.Color = color; label.BackgroundColor = CogColorConstants.White; label.SetXYText(x, y, text); return label; } foreach(ICogGraphic s in dt) { mToolBlock.AddGraphicToRunRecord(s, lastRecord, "CogPMAlignMultiTool1.InputImage", "script"); }
9.全部脚本
#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.PMAlign; #endregion public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase { #region Private Member Variables private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock; #endregion private CogGraphicCollection dt = new CogGraphicCollection(); /// <summary> /// Called when the parent tool is run. /// Add code here to customize or replace the normal run behavior. /// </summary> /// <param name="message">Sets the Message in the tool's RunStatus.</param> /// <param name="result">Sets the Result in the tool's RunStatus</param> /// <returns>True 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 dt.Clear(); CogPMAlignMultiTool pma = mToolBlock.Tools["CogPMAlignMultiTool1"]as CogPMAlignMultiTool; // Run each tool using the RunTool function foreach(ICogTool tool in mToolBlock.Tools) mToolBlock.RunTool(tool, ref message, ref result); double x = 0; double y = 60; CogColorConstants[] colors = new CogColorConstants[] { CogColorConstants.Blue, CogColorConstants.Purple, CogColorConstants.Green }; double total = 0; //string name=pma.Results for(int i = 0;i < pma.Results.ResultItemCollection.Count;i++) { switch(pma.Results.ResultItemCollection[i].Name) { case"一元": x = 1.0; break; case"一毛": x = 0.1; break; case"五毛": x = 0.5; break; default: message = "Error"; break; } if(message != "Error") { total += (pma.Results.ResultItemCollection[i].NumFound) * x; dt.Add(createlabel((pma.Results.ResultItemCollection[i].Name + "的数量是" + pma.Results.ResultItemCollection[i].NumFound + "\t 金额数是:" + ((pma.Results.ResultItemCollection[i].NumFound) * x)), 25, 20, (120 + i * y), colors[i])); } } dt.Add(createlabel("总金额是:" + total, 30, 20, 300, CogColorConstants.Red)); //;/dt.Add(createlabel((pma.Results.ResultItemCollection[1].Name + "的数量是" + pma.Results.ResultItemCollection[1].NumFound + "\t 金额数是:" + ((pma.Results.ResultItemCollection[1].NumFound) * 0.1)), 15, 20, 160, CogColorConstants.Purple)); //dt.Add(createlabel((pma.Results.ResultItemCollection[2].Name + "的数量是" + pma.Results.ResultItemCollection[2].NumFound + "\t 金额数是:" + ((pma.Results.ResultItemCollection[2].NumFound) * 0.1)),15, 20,200, CogColorConstants.Green)); return false; } private CogGraphicLabel createlabel(string text, float size, double x, double y, CogColorConstants color) { CogGraphicLabel label = new CogGraphicLabel(); label.Font = new Font("Arial", size, FontStyle.Bold, GraphicsUnit.Pixel); label.Alignment = CogGraphicLabelAlignmentConstants.TopLeft; label.Color = color; label.BackgroundColor = CogColorConstants.White; label.SetXYText(x, y, text); return label; } #region When the Current Run Record is Created /// <summary> /// Called when the current record may have changed and is being reconstructed /// </summary> /// <param name="currentRecord"> /// 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 name="lastRecord"> /// The new last run record is available to be initialized or customized.</param> public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord) { foreach(ICogGraphic s in dt) { mToolBlock.AddGraphicToRunRecord(s, lastRecord, "CogPMAlignMultiTool1.InputImage", "script"); } } #endregion #region When the Script is Initialized /// <summary> /// Perform any initialization required by your script here /// </summary> /// <param name="host">The 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 }

效果

http://www.cnnetsun.cn/news/1637849.html

相关文章:

  • GPT-SoVITS语音克隆技术全解析:从原理到实践的完整指南
  • 等保.三级要求下Redis 安全测评应该怎么做?
  • 【Java结构化并发终极指南】:20年专家亲授3大优化范式与5个避坑红线
  • VS Code 代码 AI 补全冲突排查与解决指南(AI总结版)
  • 阿里人在Github分享的Spring Cloud全栈笔记,你想象不到有多全
  • 若依管理系统实战:基于Vuex的用户角色权限与动态菜单路由解析
  • c++编程:多组数据求和
  • AIAgent产业化加速落地,国泰计算机ETF(512720)逆势走强
  • 规则执行器设计与实现:优化复杂条件判断
  • 用AI重新定义中文字体设计:从3000个字符到完整字库的智能飞跃
  • 4 文件系统概述
  • Web自动化测试:selenium(环境部署和元素定位)
  • 别再用time.sleep模拟流式了!FastAPI 2.0原生async generator流式实践(含LangChain集成、RAG流式分块、错误恢复兜底机制)
  • 如何构建专业领域的大语言模型:中医AI诊疗系统的技术实现方案
  • seo优化机构怎样选择才合适_什么是seo优化机构
  • 3分钟搞定Windows软件安装难题:winget-install终极解决方案
  • FNET嵌入式TCP/IP协议栈:轻量、双栈、无OS的工业级网络方案
  • 嵌入式轻量级三自由度逆运动学库Leg
  • Qwen3.5-9B多场景应用:短视频脚本生成+分镜图描述+配音文案一体化
  • 个人知识库构建:OpenClaw+千问3.5-27B自动整理碎片化笔记
  • M5Battery嵌入式电池电量可视化库详解
  • SDMatte镜像CI/CD流程:GitHub Actions自动构建+镜像扫描+部署验证
  • 从比赛冠军到开源项目:手把手教你复刻我那台26秒跑完的STM32F103循迹小车
  • 【deepseek讲解】咖啡小白进阶指南:从豆子到杯子,手冲咖啡的避坑实战手册
  • 嵌入式技术社区运营与内容创作实践
  • 【复现】考虑双重低碳需求响应的电力系统优化调度研究(Matlab代码实现)
  • 基于 CVaR 的风光负荷不确定性鲁棒调度优化研究(Matlab代码实现)
  • 2026年4月如何集成OpenClaw?华为云保姆级10分钟安装及百炼APIKey配置方法
  • 海外多语言USDT理财项目充电宝影视基金金融货币投资4种语言
  • 国产芯片LT8619C:HDMI转LVDS/RGB技术解析,4K超高清画质如何实现