公司动态
机器视觉8
案例1测量出零件的真实宽度 并且标识出来1.标定工具2.模板匹配3.定位工具4.卡尺工具卡尺工具参数设置#region namespace importsusing 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;//ListT 命名空间using System.Collections.Generic;#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase{#region Private Member Variablesprivate Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;#endregion//CogGraphicLabel 集合ListCogGraphicLabel mlist new ListCogGraphicLabel();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 functionforeach(ICogTool tool in mToolBlock.Tools)mToolBlock.RunTool(tool, ref message, ref result);//循环创建 CogGraphicLabelfor(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 namecurrentRecord/// The new currentRecord is available to be initialized or customized./parampublic 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 namelastRecord/// The new last run record is available to be initialized or customized./parampublic 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 namehostThe host tool/parampublic override void Initialize(Cognex.VisionPro.ToolGroup.CogToolGroup host){// DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVEbase.Initialize(host);// Store a local copy of the script hostthis.mToolBlock ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host));}#endregion}案例2充电器镭雕字符缺陷检测 并显示NG/OK结果模板匹配 用于缺陷检测定位缺陷检测 提供blob检测图像blob检测#region namespace importsusing 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 Variablesprivate Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;//声明blobprivate CogBlobTool mBlob;//声明文本private CogGraphicLabel mLabel;#endregion//GroupRun 方法public override bool GroupRun(ref string message, ref CogToolResultConstants result){//映射blobmBlob 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){//创建labelmLabel new CogGraphicLabel();mLabel.Color CogColorConstants.Green;mLabel.SetXYText(200, 200, Result:OK);}else{//创建labelmLabel new CogGraphicLabel();mLabel.Color CogColorConstants.Red;mLabel.SetXYText(200, 200, Result:NG);}return false;}//ModifyLastRunRecord 方法public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord){//显示labelmToolBlock.AddGraphicToRunRecord(mLabel, lastRecord, CogPMAlignTool1.InputImage, script)}案例3图标缺陷检测1.缺陷用红色画圈标注出来(只标注面积65以上的缺陷);2.在视图内显示OK/NG模板匹配 用于缺陷检测定位缺陷检测工具blob工具创建圆形几何工具 用于标注缺陷#region namespace importsusing 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 Variablesprivate Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;//声明blobprivate CogBlobTool mBlob;//声明文本private CogGraphicLabel mLabel;//声明创建圆private CogCreateCircleTool mCircle ;#endregionpublic override bool GroupRun(ref string message, ref CogToolResultConstants result){//映射blobmBlob mToolBlock.Tools[CogBlobTool1]as CogBlobTool;//映射圆mCircle mToolBlock.Tools[CogCreateCircleTool1]as CogCreateCircleTool;// Run each tool using the RunTool functionforeach(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.OutputColorCogColorConstants.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 importsusing 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 Variablesprivate 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 ngif(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的亮度最小到 02、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均值滤波 和中值滤波算法9.乘以常数10.像素映射11.量化12.二次采样