目录
目标:检测工具器件圆圈处是否缺失,False:NG, TRUE:PASS
目标:检测工具器件圆圈处是否缺失,False:NG, TRUE:PASS
运用工具:1.CogPMAlignTool目标定位,2.CogFixtureTool中心点坐标,3.CogBlobTool斑点工具,4.CogResultAnalysisTool运算函数 5.文字显示/高级脚本编辑
1.CogPMAlignTool目标定位
抓取图像,训练角度调整可允许随意反转360度,训练
2.CogFixtureTool中心点坐标
获取坐标位置与GetPose进行对接,获取输入图像
3.CogBlobTool斑点工具
区域选择方形区域
运行查看结果显示(小孔测量)
其中ID2的面积不是我们所需要的地方所以需要进行过滤操作
下一步复制操作 (大孔操作)
极性为白底黑点,也可以理解为亮底黑面,运行查看结果
过滤掉没用的信息,ID2,4,3可以看到是没用的信息
根据性能比填充最小面积为200像素时最优,这里的小孔填充和大孔填充与Opencv很像,小孔填充是利用面积筛选的方式留下需要的信息,大孔填充则是利用类似泛洪填充,或者是闭运算(先膨胀后腐蚀)。
可以看到四个空位已经找到
接下来进行个数计算
4.CogResultAnalysisTool
紧接着需要添加终端,找到可以传输的double类型的数值,具体实现参考上一个VisionPro博客
接着是显示文字,因为需要判断,所以选择高级脚本编写
5.高级脚本判断
#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;
using Cognex.VisionPro.CalibFix;
using Cognex.VisionPro.Blob;
using Cognex.VisionPro.ResultsAnalysis;
#endregion
public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
//1
CogGraphicLabel myLabel = new CogGraphicLabel();
#endregion
/// <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);
//2
CogResultsAnalysisTool c = new CogResultsAnalysisTool();
c = mToolBlock.Tools["CogResultsAnalysisTool1"] as CogResultsAnalysisTool;
if(c.Result.EvaluatedExpressions["Sum"].Value.ToString() == 4.ToString())
{
myLabel.SetXYText(0, 0, "PASS" + c.Result.EvaluatedExpressions["Sum"].Value.ToString());
}
else
{
myLabel.SetXYText(0, 0, "PASS" + c.Result.EvaluatedExpressions["Sum"].Value.ToString());
}
myLabel.Color = CogColorConstants.Red;
myLabel.Font = new Font("宋体", 15);
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,
"CogFixtureTool1.OutputImage",
""
);
}
#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
}
代码中有一处判断:
if(c.Result.EvaluatedExpressions["Sum"].Value.ToString() == 4.ToString())
这里进行了强转,string类型是不可以与int类型进行赋值比较
6.VS进行C#的界面要求展示
点击VisionPro Application模块
文件中添加刚刚保存好的 程序
一路next
在界面中添加输入字段
路径为Tools下CogToolBlock中output输出的内容的value值
选用VS执行C#
点击持续运行,qt界面中显示的胃实际检测孔洞数量