案例1:测量出零件的真实宽度 并且标识出来
1.标定工具
2.模板匹配
3.定位工具
4.卡尺工具
卡尺工具参数设置
#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.CalibFix;
using Cognex.VisionPro.PMAlign;
using Cognex.VisionPro.Caliper;
//List<T> 命名空间
using System.Collections.Generic;
#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
#endregion
//CogGraphicLabel 集合
List<CogGraphicLabel> mlist = new List<CogGraphicLabel>();
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
//映射卡尺工具
CogCaliperTool cpt = mToolBlock.Tools["CogCaliperTool1"] as CogCaliperTool;
//清空集合
mlist.Clear();
// Run each tool using the RunTool function
foreach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool, ref message, ref result);
//循环创建 CogGraphicLabel
for(int i = 0; i < cpt.Results.Count;i++)
{
CogGraphicLabel mLabel = new CogGraphicLabel();
//保留小数点两位数
double widthNumber = Math.Round(cpt.Results[i].Width, 2);
//设置卡尺坐标点 和卡尺宽度
mLabel.SetXYText(cpt.Results[i].PositionX, cpt.Results[i].PositionY, widthNumber.ToString());
//添加图形
mlist.Add(mLabel);
}
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 < mlist.Count ;i++){
mToolBlock.AddGraphicToRunRecord(mlist[i], lastRecord, "CogCalibCheckerboardTool1.OutputImage", "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}
案例2:充电器镭雕字符缺陷检测 并显示NG/OK结果
模板匹配 用于缺陷检测定位
缺陷检测 提供blob检测图像
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.PMAlign;
using Cognex.VisionPro.PatInspect;
using Cognex.VisionPro.Blob;
#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
//声明blob
private CogBlobTool mBlob;
//声明文本
private CogGraphicLabel mLabel;
#endregion//GroupRun 方法
public override bool GroupRun(ref string message, ref CogToolResultConstants result)
{
//映射blob
mBlob = mToolBlock.Tools["CogBlobTool1"]as CogBlobTool;
//遍历block工具
foreach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool, ref message, ref result);
//判断blob结果个数 创建对应文本内容
if(mBlob.Results.GetBlobs().Count == 0)
{//创建label
mLabel = new CogGraphicLabel();
mLabel.Color = CogColorConstants.Green;
mLabel.SetXYText(200, 200, "Result:OK");
}
else
{//创建label
mLabel = new CogGraphicLabel();
mLabel.Color = CogColorConstants.Red;
mLabel.SetXYText(200, 200, "Result:NG");
}return false;
}//ModifyLastRunRecord 方法
public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
{
//显示label
mToolBlock.AddGraphicToRunRecord(mLabel, lastRecord, "CogPMAlignTool1.InputImage", "script");
}
案例3:图标缺陷检测
1.缺陷用红色画圈标注出来(只标注面积65以上的缺陷);
2.在视图内显示OK/NG
模板匹配 用于缺陷检测定位
缺陷检测工具
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.PMAlign;
using Cognex.VisionPro.CalibFix;
using Cognex.VisionPro.PatInspect;
using Cognex.VisionPro.Blob;
using Cognex.VisionPro.Caliper;
using Cognex.VisionPro.Dimensioning;
#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
//声明blob
private CogBlobTool mBlob;
//声明文本
private CogGraphicLabel mLabel;
//声明创建圆
private CogCreateCircleTool mCircle ;
#endregion
public override bool GroupRun(ref string message, ref CogToolResultConstants result)
{
//映射blob
mBlob = mToolBlock.Tools["CogBlobTool1"]as CogBlobTool;
//映射圆
mCircle = mToolBlock.Tools["CogCreateCircleTool1"]as CogCreateCircleTool;// Run each tool using the RunTool function
foreach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool, ref message, ref result);
//判断blob结果大于0 创建文本 并且 运行 圆工具
if(mBlob.Results.GetBlobs().Count > 0)
{
mLabel = new CogGraphicLabel();
mLabel.Color = CogColorConstants.Red;
mLabel.SetXYText(200, 100, "NG");
//运行圆工具 设置中心点位置 和半径
mCircle.InputCircle.Radius = 25;
mCircle.InputCircle.CenterX = mBlob.Results.GetBlobs()[0].CenterOfMassX;
mCircle.InputCircle.CenterY = mBlob.Results.GetBlobs()[0].CenterOfMassY;//颜色
mCircle.OutputColor=CogColorConstants.Red;
//设置圆线条 的像素宽度
mCircle.LineWidthInScreenPixels = 2;//主动执行某个工具 run() 执行工具
mCircle.Run();
}
else
{
mLabel = new CogGraphicLabel();
mLabel.Color = CogColorConstants.Green;
mLabel.SetXYText(200, 100, "OK");
}return false;
}public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
{
mToolBlock.AddGraphicToRunRecord(mLabel, lastRecord, "CogPMAlignTool1.InputImage", "script");
}
案例4:正确检出图中3M字符出现的多墨、少墨现象 用圆标识出来 且显示NG/PK结果
#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.PMAlign;
using Cognex.VisionPro.PatInspect;
using Cognex.VisionPro.Blob;
#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
#endregion
//声明图形集合
CogGraphicCollection col = new CogGraphicCollection();
//声明一个圆图形
CogCircle mCircle;
//声明一个文本图形
CogGraphicLabel mLabel;
//声明blob工具
CogBlobTool mBlob;// GroupRun方法
public override bool GroupRun(ref string message, ref CogToolResultConstants result)
{
//转换并赋值
mBlob = mToolBlock.Tools["CogBlobTool1"]as CogBlobTool;
//清空集合
col.Clear();
//运行vp后遍历 block中工具
foreach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool, ref message, ref result);
//判断blob结果 等于0 ok 大于 0 ng
if(mBlob.Results.GetBlobs().Count == 0)
{
mLabel = new CogGraphicLabel();
mLabel.Color = CogColorConstants.Green;
mLabel.SetXYText(200, 200, "Result:OK");
col.Add(mLabel);
}
else
{
mLabel = new CogGraphicLabel();
mLabel.Color = CogColorConstants.Red;
mLabel.SetXYText(200, 200, "Result:NG");
col.Add(mLabel);
}
for (int i = 0; i < mBlob.Results.GetBlobs().Count; i++)
{
//初始化圆
mCircle = new CogCircle();
//设置颜色
mCircle.Color = CogColorConstants.Red;
//设置半径
mCircle.Radius = 15;
//设置圆心
mCircle.CenterX = mBlob.Results.GetBlobs()[i].CenterOfMassX;
mCircle.CenterY = mBlob.Results.GetBlobs()[i].CenterOfMassY;
//设置圆线条 的像素宽度
mCircle.LineWidthInScreenPixels = 2;//把画圆工具 放入到集合中
col.Add(mCircle);
}return false;
}// ModifyLastRunRecord 方法
public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
{
for (int i = 0; i < col.Count; i++)
{
mToolBlock.AddGraphicToRunRecord(col[i], lastRecord, "CogPMAlignTool1.InputImage", "script");
}
}
CogPolarUnwrapTool工具解析
作用:能够将环形区域转换为矩形区域 便于后续检测
转换原理:圆形输入区域转换矩形区域
1.圆形输入区域(Circular input Region)中的采样点的数量确定输出图像的大小(以像素为单位)。
2.PolarUnwrap工具自动计算矩形输出图像(Default Output lmage Size)的大小,以便最大限度地减少图像失真。
3.对于矩形输出区域(Default Output lmage Size),默认的X轴大小是内弧长度(InnerArcLength)和外弧长度(OuterArcLength)的平均值,
4.默认的Y轴大小是内半径(InnerRadius)和外半径(OuterRadius)之间的差值,如下图所示:
使用流程
圆形和椭圆形区域形状
调整展开区域
显示效果
案例:见例程
CogIPOneImageTool工具的功能解析
作用:
提供的一个用于单张图像基本处理的工具。如滤波、增强、变换等。这些操作可以帮助改善图像质量,突出图像中的重要特征,便于后续的图像分析和处理。
主要功能:
图像滤波:支持多种滤波操作,如高通滤波、低通滤波、边缘检测等。
图像增强:支持对图像进行对比度增强、亮度调整等操作。
图像变换:支持图像的旋转、缩放、平移等几何变换操作。
图像算术:支持对图像进行算术运算,如加法、减法、乘法等。
使用步骤:
1初始化工具:创建 CogIPOneImageTool 实例并进行初始化。
2设置输入图像:配置要处理的输入图像。
3 选择处理操作:根据需要选择具体的处理操作和参数。
4运行工具:调用运行方法执行图像处理。
5.获取处理后图像:从工具中获取处理后的图像。
1、加减常量
为灰度图像中每个像素的灰度值添加正值或负值,从而生成一张更亮或更暗的图像
对于彩色图像,将值添加到平面0(红色),平面1(绿色),平面2(蓝色)
封装:超出255亮度的像素会执行减去256的操作,小于0的会执行加256的操作
箝位:最高到255的亮度,最小到 0
2、3x3卷积运算
VisionPro支持卷积运算符,基于相邻像素值修改像素值。
VisionPro将输入图像中的每个像素乘以一个称为核的数值
后替换输出图像中的 相应像素。不同卷积核可以
实现不同的效果,比如平滑、模糊、去噪、锐化、 边缘提
取等,都可以通过卷积操作来完成
卷积核大小:卷积核大小定义了卷积的视图。根据卷积核的大小,常见的卷积尺寸有1×1卷积、3×3 卷积、5×5卷积、7×7卷积
利用3X3矩阵对图像执行卷积运算。
1.边缘锐化效果
卷积核效果参考网址:
https://setosa.io/ev/image-kernels/
3.扩展:
用指定的放大倍数放大整个图像或整个图像的一部分。该操作接受单独 的参数以沿x轴和y轴放大图像,因此可以使用该操作仅沿一个方向放大输 入图像
4.翻转:
对输入图像的全部或部分执行水平翻转或顺时针旋转。下图显示了输入图像 的一部分如何旋转180度。您可能需要翻转
或旋转图像,以便视觉工具在每次应 用程序执行时分析正确的功能
5.高斯采样
(1)缩小图像的像素个数比例 提高应用程序的速度时
缩小一倍二次采样调整图像大小
(2)使用平滑操作可减轻图像中纹理,信号噪声等带来的
影响。平滑:消除噪声
(3)可调整量级移动,范围为-7至7。 使用负值作为
移位因子可产生较暗的输出图像,而使用正值可产生较亮
的输出图像。图像亮度调整
6.灰度形态调整
对输入图像执行灰度形态,根据其大小和方向有选择地增强或减少图像特征。
检查每个像素及其 八个相邻像素的灰度值
腐蚀:侵蚀会降低图像的亮部,从而完全消除噪点像素或小的缺陷
膨胀:增强了图像的明亮特征,同时抑制了较暗的特征
打开(开运算):首先对输入图像进行腐蚀,然后对结果进行膨胀,以生成输出图像。打开图像会 删除少量明亮像素,然 后增强其余的明亮功能
关闭(闭运算):首先对输入图像执行膨胀,然后对结果进行腐蚀以生成输出图像。关闭图像可减少或完全消除图像的暗区
打开和关闭操作都倾向于保留大特征的尺寸和形状,同
时影响小特征的尺寸和形状
关闭-原件 (黑帽) (得到图像内部的小孔, 或者前景色中的小黑点)
原件-打开 (顶帽)(得到图像的噪声)
图像噪声是指存在于图像数据中的不必要的或多余的干扰信息。噪声的存在严重影响了遥感图像的质量。
7.灰阶形态 NxM
灰度形态调整的扩展 其中也包括 腐蚀 膨胀 等功能
8.高通滤波:
高通滤波就是保留图像中频率比较高的部分,即突出边缘;可以执行高斯均值或中值平滑操作,然后从输入图 像中减去结果图像。高频信号的输出和采样
均值滤波 Mean
中值滤波Median
高斯滤波Gauss
均值滤波 和中值滤波算法