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(tops is top.Results[0].Width.ToString(F2), 12, 30, 100, CogColorConstants.Blue)); dt.Add(Createlabel(bottoms is bottom.Results[0].Width.ToString(F2), 12, 30, 140, CogColorConstants.Blue)); dt.Add(Createlabel(middles 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 (纯工具)工具CogPMAlignToolCogResultsAnalysisToolCogCreateGraphicLabelToolCogPMAlignTool1. 抓取训练图像2.制作掩膜凸显轮廓特征3.运行参数4.训练区域5.添加终端CogResultsAnalysisTool1.添加名称变量算法运算符2.添加终端CogCreateGraphicLabelTool1.选择器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 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 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 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 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 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(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].NumFoundprivate 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 namepma.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 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 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 namepma.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 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 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 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 }效果