1.案例要求
使用模板匹配工具,统计所有硬币金额,并通过写脚本的方式将结果显示出来,统计每种硬币的数量及总金额。
2.实现思路
我们要先根据明白我们要完成的任务具体是什么,要有一个思考的过程,这样能帮我们更好的梳理脉络,这样才能高速度高质量的完成任务。
首先一共有三种硬币,每种硬币都有正反面,我们可以用六个模板匹配工具分别取匹配它,但是这样效率比较低并且浪费时间。我们还可以用三个模板匹配工具,我们可以只匹配这三种硬币的外侧,这就是周长这部分。通过这里来判断是哪一种硬币。
3.使用控件
我们先创建一个CogToolBlock工具,在这里面进行操作,方面我们编写脚本,我们再CogToolBlock里面创建三个模板匹配工具,加上图片连接。
我们以1元硬币为例,先找到一元硬币,这样是匹配了它的所有。
但是我们只想要它的周长那一部分,这里就需要用到图像掩膜编辑器,点开它。然后使用椭圆工具选中我们不需要匹配的部分。选择填充当前选项,这样这部分在匹配和训练的时候就不会用到。
点击应用,然后确定,再次点击训练,我们发现图像变了。
然后5角硬币和1角硬币页如此操作。
值得再重复一遍的是,因为我们用掩膜把里面的内容盖住了,所以正面反面是没有影响的。
做完这些操作我们就得到了这样的结果。
脚本代码:
然后我们就可以编写脚本代码了,下面我附上脚本代码,带有注释
#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
//创建一个标签
CogGraphicLabel mylabel = new CogGraphicLabel();
/// <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
// Run each tool using the RunTool function
foreach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool, ref message, ref result);
//三个对象对应三个模板匹配工具
CogPMAlignTool c1 = mToolBlock.Tools["CogPMAlignTool1"] as CogPMAlignTool;
CogPMAlignTool c2 = mToolBlock.Tools["CogPMAlignTool2"] as CogPMAlignTool;
CogPMAlignTool c3 = mToolBlock.Tools["CogPMAlignTool3"] as CogPMAlignTool;
//硬币数量
int count1=c1.Results.Count+c2.Results.Count+c3.Results.Count;
//硬币金额
double num=c1.Results.Count+c2.Results.Count*0.5+c3.Results.Count*0.1;
//SetXYText是往里面添加数据,我们现实的数据
mylabel.SetXYText(500, 70, "硬币总数:"+count1+" 总钱数:"+num
+" 1元硬币:"+c1.Results.Count+" 5角硬币:"+c2.Results.Count+" 1角硬币:"+c1.Results.Count);
//设置颜色
mylabel.Color = CogColorConstants.Red;
//设置字体和大小
mylabel.Font = new Font("楷书", 10);
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 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)
{
//我们最终要显示的内容和显示的页面
mToolBlock.AddGraphicToRunRecord(mylabel, lastRecord, "CogPMAlignTool1.InputImage", "");
}
#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
}
4.最终实现效果
5.知识点总结
-
模板匹配(CogPMAlignTool):用于硬币的识别,建立识别模板
-
脚本编写:脚本在里面编写的时候大部分都没有提示,需要扎实的基本功,要了解生效机制。