添加完图片之后只用模板匹配工具框选出需要检测的
以及使用卡尺卡出液体达到的线
使用找线工具找出容器的起始线
随后以找线工具某点的xy轴来拟合一条直线
随后使用点到线工具测量卡尺工具卡到线的中心点到拟合线的距离即可
连线如下
最终效果以及脚本
#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.Caliper;
using Cognex.VisionPro.Dimensioning;
#endregion
public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
CogGraphicCollection col = new CogGraphicCollection();//创建图形集合
#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
CogPMAlignTool pma = new CogPMAlignTool();//创建以下几个所需的工具
CogFitLineTool fit = new CogFitLineTool();
CogCaliperTool cal = new CogCaliperTool();
CogDistancePointLineTool dis = new CogDistancePointLineTool();
CogGraphicLabel mLable;//图形标签
col.Clear();//清空图形集合
// Run each tool using the RunTool function
foreach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool, ref message, ref result);
pma = (CogPMAlignTool)mToolBlock.Tools["CogPMAlignTool1"];//获取所需的工具
cal = (CogCaliperTool)mToolBlock.Tools["CogCaliperTool1"];
fit = (CogFitLineTool)mToolBlock.Tools["CogFitLineTool1"];
dis = (CogDistancePointLineTool)mToolBlock.Tools["CogDistancePointLineTool1"];
//使用for循环来多次使用卡尺工具
for (int i = 0; i < pma.Results.Count; i++)
{
cal.Region.CenterX= pma.Results[i].GetPose().TranslationX;//卡尺工具的输入坐标等于模板匹配工具输出的坐标
cal.Region.CenterY= pma.Results[i].GetPose().TranslationY;
cal.Run();//启动工具
dis.X=cal.Results[0].Edge0.PositionX;//给点到线工具传入所需要的参数 x y 一条线
dis.Y=cal.Results[0].Edge0.PositionY;
dis.Line = fit.Result.GetLine();
dis.Run();//启动工具
CogRectangle z = new CogRectangle();//创建一个矩形
//判断是否合格
if (dis.Distance<130&&dis.Distance>110)
{
mLable = new CogGraphicLabel();
mLable.Color = CogColorConstants.Green;
mLable.SetXYText(cal.Results[0].Edge0.PositionX, cal.Results[0].Edge0.PositionY, "OK:"+dis.Distance.ToString("F2"));
col.Add(mLable);//把图形标签存入图形集合
}
else
{
mLable = new CogGraphicLabel();
mLable.Color = CogColorConstants.Red;
mLable.SetXYText(cal.Results[0].Edge0.PositionX, cal.Results[0].Edge0.PositionY, "NG:" + dis.Distance.ToString("F2"));
col.Add(mLable);
}
//定义矩形的数据最终显示成为卡尺工具的线
z.Color = CogColorConstants.Red;
z.SetXYWidthHeight(cal.Results[0].Edge0.PositionX- 8,cal.Results[0].Edge0.PositionY, 25, 0.01);
col.Add(z);
}
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)
{
//遍历图形集合输出到页面
for(int i = 0;i < col.Count;i++){
mToolBlock.AddGraphicToRunRecord(col[i], 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
}