news 2026/9/1 13:18:12

机器视觉13-2

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
机器视觉13-2

案例1:蓝色工件破损检测 并且标绘出缺陷轮廓

1.颜色提取工具 目的:获取输出一个灰度图像用于blob检测

1.提取多的特征颜色 目的: 是灰度图的特征更明显

1.提取效果图

1.blob工具

#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.ColorExtractor;
using Cognex.VisionPro.ImageProcessing;
using Cognex.VisionPro.PMAlign;
using Cognex.VisionPro.CalibFix;
using Cognex.VisionPro.Caliper;
using Cognex.VisionPro.Blob;
#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
#endregion
CogGraphicCollection col;

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

col = new CogGraphicCollection();
CogBlobTool blob = mToolBlock.Tools["CogBlobTool1"] as CogBlobTool;
// Run each tool using the RunTool function
foreach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool, ref message, ref result);

for (int i = 0; i < blob.Results.GetBlobs().Count; i++)
{

// CogPolygon边界显示图形
CogPolygon p = new CogPolygon();

// 获取blob结果的Boundary边界
p = blob.Results.GetBlobs()[i].GetBoundary();
p.Color = CogColorConstants.Red;


col.Add(p);
}

return false;
}


public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
{
for (int i = 0; i < col.Count; i++)
{
mToolBlock.AddGraphicToRunRecord(col[i], lastRecord, "CogIPOneImageTool1.InputImage", "script");
}
}
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

}

案例2:齿轮内径检测

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");
}
}

案例3:齿轮圆形到齿轮角距离检测 求出最大值最小值 平均值

1.模板匹配齿轮 目的:匹配整个齿轮特征

1.FixTure齿轮定位 后续提供定位参考

1.模板匹配单个齿轮角

2.设置查找概数 等其他参数 目的:匹配所有齿轮角特征

1.模板匹配单个齿轮角

1.卡尺工具 查找每个齿轮角的边线

2. 模板匹配每个卡尺坐标赋值给 卡尺工具的仿射矩形中心点坐标 Region.centerX Region.centerY 目的:为了给卡尺工具 提供坐标点参考

2.不要需要使用fixTure 原因:模板匹配2 提供坐标

1.找圆 目的:找到圆心坐标 用于后续点到点测量

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;
#endregion

public 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);

//创建线段t图形
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");
}
}

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/1 13:17:31

ZYNQ7020嵌入式平台LVGL 9.5.0移植与优化实战指南

在 ZYNQ7020 这颗经典的 FPGAARM 双核芯片上跑 LVGL 9.5.0&#xff0c;核心要解决的是 如何在资源受限的嵌入式平台上&#xff0c;流畅运行一个现代化的图形界面库 。这不仅仅是“能不能跑起来”的问题&#xff0c;更是关于如何平衡性能、内存和开发效率。如果你手头有 ZYNQ7…

作者头像 李华
网站建设 2026/9/1 13:15:13

基于SpringBoot的非物质文化遗产管理系统(源码+lw+部署文档+讲解等)

温馨提示&#xff1a;本人主页置顶文章(点我)开头有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 温馨提示&#xff1a;本人主页置顶文章(点我)开头有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 温馨提示&#xff1a;本人主页置顶文章(点我)开头有 CSDN 平台…

作者头像 李华
网站建设 2026/9/1 13:12:56

5G NR LDPC编译码误码率仿真:MATLAB完整实现与调试指南

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华
网站建设 2026/9/1 13:10:06

OpenCV车道线识别详解:从灰度化到Hough变换的完整图像处理流程

简介&#xff1a;这是一份基于Python与OpenCV实现的车道线识别项目资料&#xff0c;面向自动驾驶、智能交通领域的开发者和图像处理初学者&#xff0c;可帮助理解从图像输入到车道线输出的完整处理链路。资源包含完整的检测流程&#xff1a;图像灰度化与高斯滤波、Canny边缘检测…

作者头像 李华
网站建设 2026/9/1 13:06:48

maxkb4j前端源码解析:Java智能体平台的Vue3实现

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华