案例:齿轮内径检测
1.使用模板匹配
2.设置匹配区域
3.使用掩膜不必要的干扰 (保留两个内径的圆形)
1.添加找圆工具
2.添加模板匹配中心坐标
3.给外圈圆添加找圆工具
RunParams.ExpectedCircularArc.CenterX
RunParams.ExpectedCircularArc.CenterY 找圆工具圆心坐标
因为我们模板匹配的是圆形 所引 匹配的圆形坐标 就是 找圆工具坐标
1.显示结果添加了 圆形图案 CogCircle circle
文本 CogGraphicLabel label
public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
#endregion
//声明图形集合
private CogGraphicCollection col = new CogGraphicCollection();
public override bool GroupRun(ref string message, ref CogToolResultConstants result)
{
//声明pma
CogPMAlignTool pma = mToolBlock.Tools["CogPMAlignTool1"] as CogPMAlignTool;
//声明找圆
CogFindCircleTool fc = mToolBlock.Tools["CogFindCircleTool1"] as CogFindCircleTool;
//声明一个圆图形变量 目的是在图形界面显示每一个找到的圆
CogCircle circle;
//声明一个label标签 目的是在图形界面显示每一个找到的圆半径
CogGraphicLabel label;
//清空
col.Clear();
// Run each tool using the RunTool function
foreach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool, ref message, ref result);
//遍历pma结果
for (int i = 0; i < pma.Results.Count; i++)
{
//为找圆工具添加圆心
fc.RunParams.ExpectedCircularArc.CenterX = pma.Results[i].GetPose().TranslationX;
fc.RunParams.ExpectedCircularArc.CenterY = pma.Results[i].GetPose().TranslationY;
//运行找圆
fc.Run();
//给变量圆赋值
circle = new CogCircle();
circle = fc.Results.GetCircle();
circle.Color = CogColorConstants.Orange;
//向集合中添加圆图形
col.Add(circle);
label = new CogGraphicLabel();
label.SetXYText(circle.CenterX, circle.CenterY, "半径:" + circle.Radius.ToString("0.00"));
label.Color = CogColorConstants.Red;
//向集合中添加label
col.Add(label);
}
return false;
}
public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
{
for (int i = 0; i < col.Count; i++)
{
mToolBlock.AddGraphicToRunRecord(col[i], lastRecord, "CogPMAlignTool1.InputImage", "Script");
}
}
案例:齿轮圆形到齿轮角距离检测
2. 模板匹配中心原点坐标赋值给 Region.centerX Region.centerY 卡尺工具的仿射矩形中心点坐标
Region.centerX Region.centerY 卡尺工具的仿射矩形中心点坐标
1.
2.Edge0.PositionX 和Edge0.PositionY 的坐标赋值给 点到点工具的 起点坐标
3.找圆工具的圆心坐标赋值给 点到点工具的 终点坐标
#region namespace imports
using System;
using System.Collections;
using System.Collections.Generic;
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.Caliper;
using Cognex.VisionPro.Dimensioning;
#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
#endregion
//声明图像集合
CogGraphicCollection col = new CogGraphicCollection();
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 = mToolBlock.Tools["CogPMAlignTool2"] as CogPMAlignTool;
//映射卡尺工具对象
CogCaliperTool cal = mToolBlock.Tools["CogCaliperTool1"] as CogCaliperTool;
//映射找圆工具对象
CogFindCircleTool fc = mToolBlock.Tools["CogFindCircleTool1"] as CogFindCircleTool;
//映射点到点距离工具对象
CogDistancePointPointTool dis = mToolBlock.Tools["CogDistancePointPointTool1"] as CogDistancePointPointTool;
//清空图像集合
col.Clear();
//声明集合 --存储距离数据
List<double> distances = new List<double>();
foreach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool, ref message, ref result);
//以模板匹配结果个数遍历
for (int i = 0; i < pma.Results.Count; i++)
{
//获取pma结果的中心点坐标
double x = pma.Results[i].GetPose().TranslationX;
double y = pma.Results[i].GetPose().TranslationY;
//角度 =弧度 *180/PI
//弧度 =角度/180*PI
//获取pma结果的弧度 转化成角度
double angle = pma.Results[i].GetPose().Rotation * 180 / Math.PI;
//角度-90度 转换成弧度 供卡尺工具使用
double rad = (angle - 90) / 180 * Math.PI;
//给卡尺工具赋值并运行卡尺工具
cal.Region.CenterX = x;
cal.Region.CenterY = y;
cal.Region.Rotation = rad;
cal.Run();
//给点到点距离工具赋值
dis.StartX = fc.Results.GetCircle().CenterX;
dis.StartY = fc.Results.GetCircle().CenterY;
dis.EndX = cal.Results[0].Edge0.PositionX;
dis.EndY = cal.Results[0].Edge0.PositionY;
dis.Run();
//保存距离到距离集合
distances.Add(dis.Distance);
//创建线段
CogLineSegment line = new CogLineSegment();
//设置线段的起点和终点
line.SetStartEnd(fc.Results.GetCircle().CenterX, fc.Results.GetCircle().CenterY, cal.Results[0].Edge0.PositionX, cal.Results[0].Edge0.PositionY);
//设置线段颜色
line.Color = CogColorConstants.Red;
//添加线段到图形集合中
col.Add(line);
//创建文本
CogGraphicLabel label = new CogGraphicLabel();
// 设置文本的位置 和文本内容
label.SetXYText(cal.Results[0].Edge0.PositionX, cal.Results[0].Edge0.PositionY, dis.Distance.ToString("0.00"));
//设置文本颜色
label.Color = CogColorConstants.Orange;
//添加文本到图形集合中
col.Add(label);
}
//筛选最大值和最小值和平均值
double max = 0;
double min = distances[0];
double mean;
double total = 0;
//遍历距离集合
for (int i = 0; i < distances.Count; i++)
{
//取最大值
if (distances[i] > max)
max = distances[i];
//取最小值
if (distances[i] < min)
min = distances[i];
//累加所有数值 计算平均数
total += distances[i];
}
//取平均数
mean = total / distances.Count;
//创建文本 显示最大值 最小值 平均值
CogGraphicLabel label2 = new CogGraphicLabel();
label2.SetXYText(200, 150, "最大值:" + max.ToString("0.00"));
label2.Color = CogColorConstants.Green;
label2.Font = new Font("宋体", 20);
CogGraphicLabel label3 = new CogGraphicLabel();
label3.SetXYText(200, 180, "最小值:" + min.ToString("0.00"));
label3.Color = CogColorConstants.Green;
label3.Font = new Font("宋体", 20);
CogGraphicLabel label4 = new CogGraphicLabel();
label4.SetXYText(200, 210, "平均值:" + max.ToString("0.00"));
label4.Color = CogColorConstants.Green;
label4.Font = new Font("宋体", 20);
//添加到图形集合
col.Add(label2);
col.Add(label3);
col.Add(label4);
//把结果给赋值给Block 输出属性
mToolBlock.Outputs["Max"].Value = max;
mToolBlock.Outputs["Min"].Value = min;
mToolBlock.Outputs["Mean"].Value = mean;
return false;
}
public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
{
//添加图形到lastRunRecord窗口
for (int i = 0; i < col.Count; i++)
{
mToolBlock.AddGraphicToRunRecord(col[i], lastRecord, "CogPMAlignTool1.InputImage", "script");
}
}