ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

机器视觉13-1

机器视觉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 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.ImageProcessing;using Cognex.VisionPro.PMAlign;using Cognex.VisionPro.Blob;#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase{#region Private Member Variablesprivate 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 functionforeach(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 x31.使用blob 匹配出图像中的引脚1.设置过滤参数1.使用卡尺工具 测量每个引脚距离 目的:检测是否有缺失的引脚#region namespace importsusing 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;#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase{#region Private Member Variablesprivate 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 DEBUGif (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();#endif//映射blobblob mToolBlock.Tools[CogBlobTool1] as CogBlobTool;//映射卡尺工具cal1 mToolBlock.Tools[CogCaliperTool1] as CogCaliperTool;//判断是否有图形缺陷 true 为无缺陷 false反之bool b true;//清空图形集合col.Clear();// Run each tool using the RunTool functionforeach(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);//通过角度大小判断是否NGif (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;}}}//卡尺判断间距 判断引脚是否有缺失//初始化泛型集合Listdouble listX new Listdouble();//循环 卡尺工具结果个数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 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.ImageProcessing;using Cognex.VisionPro.PMAlign;using Cognex.VisionPro.CalibFix;using Cognex.VisionPro.Dimensioning;using Cognex.VisionPro.Caliper;#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase{#region Private Member Variablesprivate 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 functionforeach(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.PositionX20;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 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 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 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}案例4图像去噪中值Nxm 内核高度和宽度 设置 7x7 消除干扰像素灰度形态调整设置腐蚀 来缩小白色区域 扩大黑色区域 加粗字体效果
返回列表