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

1.载具测量

先导入图片

拉取所对应的工具

cs 复制代码
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.创建标签脚本:

cs 复制代码
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集合模块

cs 复制代码
private CogGraphicCollection dt = new CogGraphicCollection();
 dt.Clear();

3.工具脚本声明

cs 复制代码
CogPMAlignTool yimao = mToolBlock.Tools["一角"]as CogPMAlignTool;
    CogPMAlignTool yiyuan = mToolBlock.Tools["一元"]as CogPMAlignTool;
    CogPMAlignTool wumao = mToolBlock.Tools["五角"]as CogPMAlignTool;

4.创建生明

cs 复制代码
 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声明主页

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

脚本

cs 复制代码
 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");
    }
全部脚本
cs 复制代码
#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

cs 复制代码
 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.全部脚本
cs 复制代码
           #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

}

效果

相关推荐
xiaotao13114 小时前
03-深度学习基础:循环神经网络(RNN)
人工智能·深度学习·机器学习
小糖学代码16 小时前
LLM系列:2.pytorch入门:3.基本优化思想与最小二乘法
人工智能·python·算法·机器学习·ai·数据挖掘·最小二乘法
叶子丶苏16 小时前
第二节_机器学习基本知识点
人工智能·python·机器学习·数据科学
DeepModel17 小时前
特征缩放(标准化/归一化)超通俗讲解
机器学习
AI科技星18 小时前
基于螺旋元逻辑的宇宙统一场论底层公理构建(乖乖数学)
算法·机器学习·数学建模·数据挖掘·量子计算
wayz1118 小时前
Day 7:第一周复习与模型综合比较
人工智能·算法·机器学习·量化交易
wayz1119 小时前
Day 9 :随机森林调参与时间序列交叉验证
算法·随机森林·机器学习
昆曲之源_娄江河畔20 小时前
婴儿版训练GPT
python·gpt·机器学习·大模型训练
zhengyquan20 小时前
特斯拉无方向盘Cybercab落地,自动驾驶商业化再提速!
人工智能·机器学习·自动驾驶
lkforce20 小时前
MiniMind学习笔记(零)--基础概念
人工智能·算法·机器学习·token·分词器·minimind·词汇表