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

}

效果

相关推荐
庄周迷蝴蝶3 小时前
Extended Kalman Filter
线性代数·机器学习·概率论
大江东去浪淘尽千古风流人物3 小时前
【Basalt】Basalt void SqrtKeypointVioEstimator<Scalar_>::optimize() VIO优化流程
数据库·人工智能·python·机器学习·oracle
轮到我狗叫了3 小时前
Few-shot Novel Category Discovery-少样本新类发现
人工智能·机器学习·支持向量机
量化炼金 (CodeAlchemy)3 小时前
【交易策略】基于随机森林的市场结构预测:机器学习在量化交易中的实战应用
算法·随机森林·机器学习
xu_wenming3 小时前
ESP32 运行TinyML模型准确性影响因素
人工智能·深度学习·机器学习
DeepModel3 小时前
机器学习非线性降维:Isomap 等距映射
人工智能·机器学习
weixin_446934034 小时前
多分类暴露变量的亚组分析森林图功能上线了,R语言搞不了风暴统计平台一键搞定
人工智能·机器学习·分类·数据挖掘·r语言
Zero4 小时前
机器学习概率论与统计学--(10)统计学:参数估计②
机器学习·概率论·参数估计·统计学
哥布林学者5 小时前
深度学习进阶(二)多头自注意力机制(Multi-Head Attention)
机器学习·ai