当前位置: 首页 > news >正文

机器视觉13-1

案例1: 齿轮缺角

1.预留图形预处理工具

1.边缘提取工具

利用copyRegin工具 实现 齿轮环 分割效果 为blob分析 齿轮缺失做准备

1和2 把 blob中的灰度图添加到 copyRegin的俩个图像输入参数中

3.把blob匹配结果中心坐标给copyRegin 匹配区域坐标

4.设置填充数值 128 目的 为了 使填充的区域灰度值和 目标图像背景灰度值 一致

5.填充区域设置

6.区域外 调整像素 调整为 不写入像素

1.勾选使用图像配对

1.运行

2.运行后最终效果 (此 图效果 可以清晰的匹配 每个齿轮)

1.使用blob工具

2.设置参数

3.匹配效果图

根据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.ImageProcessing;
using Cognex.VisionPro.PMAlign;
using Cognex.VisionPro.Blob;
#endregion

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

//定义图形集合
CogGraphicLabel label = new CogGraphicLabel();

public override bool GroupRun(ref string message, ref CogToolResultConstants result)
{



int count = (int)mToolBlock.Inputs["Count"].Value;
CogBlobTool blob = mToolBlock.Tools["CogBlobTool2"] as CogBlobTool;


// Run each tool using the RunTool function
foreach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool, ref message, ref result);

//获取blob结果个数
int currentCount = blob.Results.GetBlobs().Count;
//获取齿轮差值
int diff = count - currentCount;


string res = "";
label.Color = CogColorConstants.Green;
label.Font = new Font("宋体", 20);
//根据差值判断是否有缺失
if (diff > 0)
{
res = "缺失:" + diff;
label.Color = CogColorConstants.Red;
}
else
{
res = "良品";

}
label.SetXYText(100, 250, res);
return false;
}

public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
{
mToolBlock.AddGraphicToRunRecord(label, lastRecord, "CogIPOneImageTool1.InputImage", "Script");
}

案例2:引脚缺失

1使用cogIPoneImage去掉背景图中的白色噪点

2.添加灰阶形态NxM 内核高度与宽度 设置 3 x3

1.使用blob 匹配出图像中的引脚

1.设置过滤参数

1.使用卡尺工具 测量每个引脚距离 目的:检测是否有缺失的引脚

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

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
#endregion
//声明图形集合
private CogGraphicCollection col = new CogGraphicCollection();
//声明BlobTool成员变量
CogBlobTool blob;
// 声明卡尺工具成员变量
CogCaliperTool cal1;
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


//映射blob
blob = mToolBlock.Tools["CogBlobTool1"] as CogBlobTool;
//映射卡尺工具
cal1 = mToolBlock.Tools["CogCaliperTool1"] as CogCaliperTool;

//判断是否有图形缺陷 true 为无缺陷 false反之
bool b = true;


//清空图形集合
col.Clear();
// Run each tool using the RunTool function
foreach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool, ref message, ref result);


//判断引脚是否偏移

//根据blob个数结果
for (int i = 0; i < blob.Results.GetBlobs().Count; i++)
{
//获取角度
double angle = Math.Abs((blob.Results.GetBlobs()[i].Angle) * 180 / Math.PI);

//通过角度大小判断是否NG
if (Math.Abs(angle- 90) > 5)
{
//初始化多边线图形
CogPolygon polygon = new CogPolygon();
polygon = blob.Results.GetBlobs()[i].GetBoundary();
polygon.Color = CogColorConstants.Red;

col.Add(polygon);
b = false;
}


//获取blob检测的高度
double h = blob.Results.GetBlobs()[i].GetMeasure(CogBlobMeasureConstants.BoundingBoxExtremaAngleHeight);
//通过Y坐标判断 斑点在上方还是下方
//上方
if (blob.Results.GetBlobs()[i].CenterOfMassY < 220)
{
//判断高度 大于110或者小于90 上方特征有缺陷
if (h > 110 || h < 90)
{
//
CogPolygon polygon = new CogPolygon();
polygon = blob.Results.GetBlobs()[i].GetBoundary();
polygon.Color = CogColorConstants.Red;
col.Add(polygon);
b = false;
}
}
//下方
else if(blob.Results.GetBlobs()[i].CenterOfMassY > 220)
{
//判断高度 大于65或者小于50 下方特征有缺陷
if (h > 65 || h < 50)
{

//初始化边界显示线
CogPolygon polygon = new CogPolygon();
polygon = blob.Results.GetBlobs()[i].GetBoundary();
polygon.Color = CogColorConstants.Red;
//添加到图形集合工具
col.Add(polygon);
b = false;
}
}
}



//卡尺判断间距 判断引脚是否有缺失

//初始化泛型集合
List<double> listX = new List<double>();

//循环 卡尺工具结果个数
for (int j = 0; j < cal1.Results.Count; j++)
{

//添加每个结果X坐标点到泛型集合中
listX.Add(cal1.Results[j].Edge0.PositionX);
}
listX.Sort();//升序 目的:把做升序排列 方便后续 进行差值计算



for (int i = 0; i < listX.Count - 1; i++)
{

//相邻坐标点求 差值
double width = listX[i + 1] - listX[i];
//差值大于80 证明引脚有缺失
if (width > 80)
{

//初始化图形限定框
CogRectangleAffine rec = new CogRectangleAffine();
//限定款位置
rec.CenterX = (listX[i + 1] + listX[i]) / 2;
rec.CenterY = cal1.Results[i].Edge0.PositionY;
rec.SideXLength = width;
rec.SideYLength = 100;
rec.Color = CogColorConstants.Red;
//添加到图形集合工具
col.Add(rec);
b = false;
}
}

//初始化label 显示NG 或者OK信息
CogGraphicLabel label = new CogGraphicLabel();
label.Font = new Font("微软雅黑", 20);
if (b)
{
label.SetXYText(100, 80, "OK");
label.Color = CogColorConstants.Green;
}
else
{
label.SetXYText(100, 80, "NG");
label.Color = CogColorConstants.Red;
}
//添加到图形集合工具
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, "CogIPOneImageTool1.InputImage", "Script");
}
}

案例3:液位高度检测

1.预处理工具 处理图像对比度

1.边缘提取工具 突出图像边缘效果

1.模板匹配

2.中心原点位置 调整到正常液位坐标 用于后续对比

1.卡尺工具 目的:测量瓶口端基准线位置

1.利用卡尺测量出瓶口端基准线位置

1.创建线工具

2.根据卡尺工具线的坐标 创建线

1.卡尺工具 目的 找到液位高点的基准线

2.模板匹配中心点位置确定卡尺工具的位置

1.点到线距离工具

2. x y是卡尺工具上端线的点信息

3.Line 创建线工具的结果

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

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
#endregion
//创建图像集合
CogGraphicCollection col = new CogGraphicCollection();
CogPMAlignTool pma;
//
CogCaliperTool cal;
//
CogCreateLineTool line;
//
CogDistancePointLineTool dis;
public override bool GroupRun(ref string message, ref CogToolResultConstants result)
{

//工具映射
pma = mToolBlock.Tools["CogPMAlignTool1"] as CogPMAlignTool;
cal = mToolBlock.Tools["CogCaliperTool1"] as CogCaliperTool;
line = mToolBlock.Tools["CogCreateLineTool1"] as CogCreateLineTool;
dis = mToolBlock.Tools["CogDistancePointLineTool1"] as CogDistancePointLineTool;


//清空图形集合
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++)
{
//液位上端卡尺工具的位置
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;
dis.Y = cal.Results[0].Edge0.PositionY;
dis.Line = line.GetOutputLine();
//运行工具
dis.Run();


//创建液位上端线段
double x0 = cal.Results[0].Edge0.PositionX+20;
double y0 = cal.Results[0].Edge0.PositionY;
double x1 = cal.Results[0].Edge0.PositionX-20;
double y1 = cal.Results[0].Edge0.PositionY;

CogLineSegment segment = new CogLineSegment();
segment.SetStartEnd(x0, y0, x1, y1);
segment.Color = CogColorConstants.Red;

//创建文本图形
CogGraphicLabel label = new CogGraphicLabel();
string res = "";

//判断液位误差范围
if (dis.Distance > 130 || dis.Distance < 110)
{
res = "NG: " + dis.Distance.ToString("0.00");
label.Color = CogColorConstants.Red;
}
else
{
res = "OK: " + dis.Distance.ToString("0.00");
label.Color = CogColorConstants.Green;
}
label.SetXYText(dis.X, dis.Y, res);
//添加label到集合
col.Add(label);
//添加线段到集合
col.Add(segment);
}
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, "CogIPOneImageTool1.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);


// Store a local copy of the script host
this.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host));
}
#endregion

}

案例4:图像去噪

中值Nxm: 内核高度和宽度 设置 7x7 消除干扰像素

灰度形态调整:设置腐蚀 来缩小白色区域 扩大黑色区域 加粗字体效果

http://www.cnnetsun.cn/news/4352030.html

相关文章:

  • 闭源大模型API避坑指南:幽灵扣费、移动靶心与参数迷雾
  • STC89C52抢答器设计与仿真全解析:从原理到Proteus调试
  • 逻辑芯片采购怎么选:先看供货与核验能力
  • Hister 标签系统实战:5 种方式给你的知识库贴上自定义标签
  • OpenVoice 语音克隆实战指南:三步在本地克隆任意声音,支持跨语言与多情感控制
  • VoxCPM ZipEnhancer语音增强:带噪录音一次洗干净,克隆音色更真实
  • 2026华为春招开发岗机试备考复盘:真题、项目与面试全记录
  • Langchain-Chatchat RAG 问答完整实战
  • 基于SpringBoot的高校宿舍用电系统设计实现(程序+文档+讲解)
  • 服务器架构设计:从“单间小屋“到“智慧城市“的进化之路
  • Apache Ossie核心规范深度解析:语义模型的5层结构与版本策略
  • OpenCode 2026版:AI原生代码编辑器从安装到实战全指南
  • 96%正确率背后:gemini-skills如何让AI编码智能体真正掌握Gemini API
  • 技术博客选题指南:为何体育新闻不适合,AI工具部署才是正道
  • Frigate NVR实测:3步搭好本地实时对象检测监控系统
  • 视频文字丢失排查:用OCR+ffmpeg定位与批量识别
  • FT232R USB UART驱动安装指南:从解压到排错全搞定
  • 国赛一等奖智慧医疗小车:STM32+ROS+OpenCV低成本机器人开发全解析
  • QGIS 3.6.1二次开发实战:PyQGIS环境搭建、瓦片接入与批量脚本
  • 技术债重构还是重写?遗留系统治理的决策框架与实战指南
  • image-blaster如何为3D场景自动生成循环环境音与碰撞音效?
  • 基于SpringBoot的高校二手书籍共享推荐系统(程序+文档+讲解)
  • 基于微信小程序的毕业生就业信息管理系统(程序+文档+讲解)
  • UE5 FPS游戏开发实战:从基础框架到手感打磨的完整指南
  • NX二次开发非模态对话框实现:C# WinForms源码与避坑指南
  • Windows 上使用 Hashcat 进行 GPU 加速密码破解
  • 百度Java秋招笔试复盘:高频考点、算法套路与避坑指南
  • 路由不止静态配置:从网络层到前端与AI工作流的完整认知
  • 第一章 第一节 windows系统Java 环境安装(含JDK多版本切换方案)
  • Codex CLI接入DeepSeek:18分钟跑通低成本AI编程