VisionPro 定位添加瞄准效果

VisionPro 定位添加瞄准效果


前言

图案定位其实就是在图像中找训练的模板图案,那VisionPro中的CogPMAlignTool工具可以很快的找图案,定位坐标并且标记出来,只是缺少了让人从视觉感受上在找的一个过程,类似于瞄准镜找寻目标,瞄准后识别定位。

在实现过程中,涉及一些VisionPro的东西,写出来分享一下,也给自己记录一下。


一、效果展示

实现的效果如下所示,无法上传高清的,看着比较模糊。

在这里插入图片描述

二、实现步骤

1.初始化

初始化CogImageFileTool加载图像源,关联运行CogPMAlignTool工具并显示,代码如下:

变量:
// Cognex 图像工具,读取图片
Cognex.VisionPro.ImageFile.CogImageFileTool imgtool;
// Cognex PMA工具,匹配定位
Cognex.VisionPro.PMAlign.CogPMAlignTool pmatool;
// 初始化
imgtool = Cognex.VisionPro.CogSerializer.LoadObjectFromFile(@".\imgfile.vpp") as Cognex.VisionPro.ImageFile.CogImageFileTool;
pmatool = Cognex.VisionPro.CogSerializer.LoadObjectFromFile(@".\pma.vpp") as Cognex.VisionPro.PMAlign.CogPMAlignTool;
imgtool.Run();
pmatool.InputImage = imgtool.OutputImage;
pmatool.Run();
cogDisplay1.Image = imgtool.OutputImage;
cogDisplay1.Fit(true);

用CogCompositeShape实现瞄准效果的复合图形,代码如下:

变量:
// 复合图形 瞄准样式
Cognex.VisionPro.CogCompositeShape cshape;
// 瞄准中心十字 复合图形
cshape = new Cognex.VisionPro.CogCompositeShape();
Cognex.VisionPro.CogLine hline = new Cognex.VisionPro.CogLine();
hline.SelectedSpaceName = "$";
hline.SetXYRotation(0, 0, 0);
cshape.Shapes.Add(hline);
Cognex.VisionPro.CogLine vline = new Cognex.VisionPro.CogLine();
vline.SelectedSpaceName = "$";
vline.SetXYRotation(0, 0, Cognex.VisionPro.CogMisc.DegToRad(90));
cshape.Shapes.Add(vline);
Cognex.VisionPro.CogCircle ccircle = new Cognex.VisionPro.CogCircle();
ccircle.SelectedSpaceName = "$";
ccircle.SetCenterRadius(0, 0, 20);
cshape.Shapes.Add(ccircle);
Cognex.VisionPro.CogGraphicLabel clbl = new Cognex.VisionPro.CogGraphicLabel();
clbl.SelectedSpaceName = "$";
clbl.SetXYText(40, -20, "瞄准");
cshape.Shapes.Add(clbl);
// 修改复合图形 style
cshape.Color = Cognex.VisionPro.CogColorConstants.Red;
cshape.LineStyle = Cognex.VisionPro.CogGraphicLineStyleConstants.DashDotDot;
// 仿射,添加显示复合图形到cogDisplay
Cognex.VisionPro.CogTransform2DLinear trans = pmatool.Results[0].GetPose();
trans.Rotation = 0;
Cognex.VisionPro.CogCompositeShape tmp = cshape.MapLinear(trans, Cognex.VisionPro.CogCopyShapeConstants.All);
cogDisplay1.InteractiveGraphics.Add(tmp, "cross", false);

用CogLineSegment初始化移动路径线性回归函数,代码如下:

变量:
// 移动路径线段
Cognex.VisionPro.CogLineSegment seg;
// 移动起始坐标x,y
double sourceX = 0;
double sourceY = 0;
// 移动终点坐标x,y
double targetX = 0;
double targetY = 0;
// 移动结束符
bool moveend = true;
// 初始化线段,计算线程回归方程
seg = new Cognex.VisionPro.CogLineSegment();
sourceX = trans.TranslationX;
sourceY = trans.TranslationY;

2.运行按钮

增加一个运行按钮,每次点击运行切换下一张图像,运行CogPMAlignTool得到结果,并通知瞄准复合图形开始移动到目标位置,按钮单击函数中实现代码如下:

imgtool.Run();
pmatool.InputImage = imgtool.OutputImage;
pmatool.Run();
cogDisplay1.Image = imgtool.OutputImage;
targetX = pmatool.Results[0].GetPose().TranslationX;
targetY = pmatool.Results[0].GetPose().TranslationY;
// 利用Cognex工具计算线程回归方程
seg.SetStartEnd(sourceX, sourceY, targetX, targetY);
moveend = false;

移动路径线段的起始坐标为上个对象的坐标,结束坐标为当前匹配对象的坐标。

3.复合图形移动显示

利用Timer实现复合图形的位置显示更新,代码如下:

// 若移动结束,则返回,不需要再次显示更新坐标
if (moveend)
	return;

// 临时保存移动路径线段的长度、起始坐标和线段角度
double len = seg.Length;
double sx = seg.StartX;
double sy = seg.StartY;
double ro = seg.Rotation;
// 根据线段长度,每隔一个像素更新一次坐标
for (int i = 1; i < len; i++)
{
	cogDisplay1.InteractiveGraphics.Clear();
	seg.SetStartLengthRotation(sx, sy, i, ro);
	Cognex.VisionPro.CogTransform2DLinear trans = new Cognex.VisionPro.CogTransform2DLinear();
	trans.TranslationX = seg.EndX;
	trans.TranslationY = seg.EndY;
	Cognex.VisionPro.CogCompositeShape tmp = cshape.MapLinear(trans, Cognex.VisionPro.CogCopyShapeConstants.All);
	cogDisplay1.InteractiveGraphics.Add(tmp, "cross", false);
	System.Threading.Thread.Sleep(10);
}
// 移动结束,显示匹配外框
cogDisplay1.InteractiveGraphics.Add(pmatool.Results[0].CreateResultGraphics(Cognex.VisionPro.PMAlign.CogPMAlignResultGraphicConstants.MatchRegion), "cross", false);
// 更新当前终点坐标作为下次移动线段的起始坐标
sourceX = seg.EndX;
sourceY = seg.EndY;
// 移动结束
moveend = true;

总结

在点击运行结束时,其实已经得到了坐标,显示只是为了一种视觉效果。
复合图形的子图形需要有下面这句参数,否则无法显示:

hline.SelectedSpaceName = "$";

复合图形的移动路径中间坐标,其实是利用下面这句函数,在固定起始和角度不变的情况下,不断增加线段长度,得到的线段的终点坐标,就是路径上的坐标点:

public void SetStartLengthRotation(double startX, double startY, double length, double rotation);

线段的这种功能,还可以利用在视觉引导求旋转中心变换后的坐标上,已经在项目上实现过的。

  • 4
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值