目录
前言
visionpro中可以结合C#脚本进行检测,这可以更简洁全面的去实现复杂的检测功能,当然结果C#软件二次开发更加全面,不过有时外面还是直接在工具块里面添加脚本更方便些。
一、检测要求
检测齿数及齿端到中心的距离。
二、步骤
1.在CogToolBlock工具里添加检测工具
1.CogFindCircleTool是一个找圆工具,主要是找到齿轮外径及圆心。
2.CogBlobTool是一般二值化分析工具,主要是找到每个齿的位置,角度。
3.CogResultsAnalysisTool,结果处理工具,主要是给卡尺工具算出角度。
4.CogCaliperTool,卡尺工具,用于找齿顶边。
5.CogDistancePointPointTool,测量点到点工具,用于测量距离。
2.找圆
在找圆时,卡尺可以大一点,然后卡尺计分添加一个PositionNeg的算法,找最外面位置,否则圆会小一点。
3.Bolb分析
在找圆工具中可以得到圆心,半径,把他们输出出来给到bolb工具的区域对应参数。
然后再修改工具里面的径向缩放,角度范围参数。
然后,通过面积管控排除一些毛刺,去除影响。
4.结果处理
结果处理blob得到的对应区域的角度。使卡尺角度摆正。
5.卡尺找齿顶边
通过blob给卡尺位置角度。
6.测量距离
测量圆心到齿顶距离
7.编辑C#脚本,循环检测并显示
脚本采用复杂脚本,简单脚本功能限制。
#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.Caliper;
using Cognex.VisionPro.Blob;
using Cognex.VisionPro.ResultsAnalysis;
using Cognex.VisionPro.Dimensioning;
#endregion
public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
//声明
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
private Cognex.VisionPro.Blob.CogBlobTool mCogBlob;
private Cognex.VisionPro.Caliper.CogCaliperTool mCogCaliper;
private Cognex.VisionPro.Caliper.CogFindCircleTool mCogFindCircle;
private CogGraphicCollection labels;
private CogGraphicLabel myLabel;
#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);
//初始化
mCogBlob = mToolBlock.Tools["CogBlobTool1"] as CogBlobTool;
mCogCaliper = mToolBlock.Tools["CogCaliperTool1"] as CogCaliperTool;
mCogFindCircle = mToolBlock.Tools["CogFindCircleTool1"] as CogFindCircleTool;
mCogFindCircle.Run();
mCogBlob.Run();
int x = 0;
int y = 0;
//循环
for (int i = 0; i < mCogBlob.Results.GetBlobs().Count; i++)
{
//卡尺
mCogCaliper.Region.CenterX = mCogBlob.Results.GetBlobs()[i].CenterOfMassX;
mCogCaliper.Region.CenterY = mCogBlob.Results.GetBlobs()[i].CenterOfMassY;
mCogCaliper.Region.Rotation = mCogBlob.Results.GetBlobs()[i].Angle+1.2;
mCogCaliper.Run();
//测量
CogDistancePointPointTool mCogDistancePointPoint = new CogDistancePointPointTool();
mCogDistancePointPoint.InputImage = mToolBlock.Inputs[0].Value as CogImage8Grey;
mCogDistancePointPoint.StartX = mCogCaliper.Results[0].Edge0.PositionX;
mCogDistancePointPoint.StartY = mCogCaliper.Results[0].Edge0.PositionY;
mCogDistancePointPoint.EndX = mCogFindCircle.Results.GetCircle().CenterX;
mCogDistancePointPoint.EndY = mCogFindCircle.Results.GetCircle().CenterY;
mCogDistancePointPoint.Run();
//显示保存
myLabel = new CogGraphicLabel();
x = (i % 5) * 200+100;
y = (i / 5) * 50 + 150;
myLabel.SetXYText(x, y, "距离:" + mCogDistancePointPoint.Distance.ToString("f3"));
myLabel.SelectedSpaceName = "@";
myLabel.Font = new Font("微软黑体", 10);
myLabel.Color = Cognex.VisionPro.CogColorConstants.Blue;
labels.Add(myLabel);
}
return true;
}
#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 graphic in labels)
{
mToolBlock.AddGraphicToRunRecord(graphic, lastRecord, "CogFindCircleTool1.InputImage", "script");
}
labels.Clear();
CogGraphicLabel label = new CogGraphicLabel();
label.SetXYText(160, 50, "齿数:" + mCogBlob.Results.GetBlobs().Count.ToString());
label.SelectedSpaceName = "@";
label.Font = new Font("微软黑体", 10);
label.Color = Cognex.VisionPro.CogColorConstants.Blue;
mToolBlock.AddGraphicToRunRecord(label, lastRecord, "CogFindCircleTool1.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);
//实例化
this.mCogBlob = new CogBlobTool();
this.mCogCaliper = new CogCaliperTool();
this.mCogFindCircle = new CogFindCircleTool();
this.labels = new CogGraphicCollection();
// Store a local copy of the script host
this.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host));
}
#endregion
}
三、总结
对于要编辑很多工具的测量,采取脚本循环可以节省大量的时间,主要思路是找出每个节点,以及结果处理。
运行结果: