尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

机器视觉9

机器视觉9 案例1识别环形字符并显示1.添加圆形展开工具2.调整内外径圆环 注意转换方向 以3闭合圆形的地方开始 沿4箭头的方向分割圆环1.添加ocr工具提取字符2.提取字符操作3.选框范围设置要包括提取的字符注意不要超过图形边缘区段设置如果有需要引用 ovr工具的命名空间#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.OCRMax;#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase{#region Private Member Variablesprivate Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;#endregion//声明label字段CogGraphicLabel label;public override bool GroupRun(ref string message, ref CogToolResultConstants result){CogOCRMaxTool ocr mToolBlock.Tools[CogOCRMaxTool1] as CogOCRMaxTool;label new CogGraphicLabel();// Run each tool using the RunTool functionforeach(ICogTool tool in mToolBlock.Tools)mToolBlock.RunTool(tool, ref message, ref result);//ocr 提取字符串string text ocr.LineResult.ResultString;//设置label的位置和内容label.SetXYText(200, 800, 编号: text);、//label的颜色label.Color CogColorConstants.Green;//label的字体样式Font font new Font(楷体, 20);label.Font font;return false;}public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord){mToolBlock.AddGraphicToRunRecord(label, lastRecord, CogPolarUnwrapTool1.InputImage, script);}案例2展开图像并读取字符 分别读取左右两边字符1.使用fixture空间坐标2和3 不使用#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.OCRMax;#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase{#region Private Member Variablesprivate Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;#endregionCogGraphicLabel label new CogGraphicLabel();CogGraphicLabel label1 new CogGraphicLabel();/// summary/// Called when the parent tool is run./// Add code here to customize or replace the normal run behavior./// /summary/// param namemessageSets the Message in the tools RunStatus./param/// param nameresultSets the Result in the tools RunStatus/param/// returnsTrue if the tool should run normally,/// False if GroupRun customizes run behavior/returnspublic 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();// #endifCogOCRMaxTool ocr mToolBlock.Tools[CogOCRMaxTool1] as CogOCRMaxTool;CogOCRMaxTool ocr2 mToolBlock.Tools[CogOCRMaxTool2] as CogOCRMaxTool;// Run each tool using the RunTool functionforeach(ICogTool tool in mToolBlock.Tools)mToolBlock.RunTool(tool, ref message, ref result);//左边字符提取内容string text ocr.LineResult.ResultString;//左边label设置label.SetXYText(120, 200, 左边: text);label.Color CogColorConstants.Green;Font font new Font(楷体, 20);label.Font font;//右边字符提取内容string text1 ocr2.LineResult.ResultString;//右边label设置label1.SetXYText(120, 230, 右边: text1);label1.Color CogColorConstants.Red;Font font1 new Font(楷体, 30);label1.Font font1;return false;}public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord){左边label添加mToolBlock.AddGraphicToRunRecord(label, lastRecord, CogPMAlignTool1.InputImage, script);右边label添加mToolBlock.AddGraphicToRunRecord(label1, lastRecord, CogPMAlignTool1.InputImage, script);}案例31.把彩图转换为灰度图1.blob工具 设置相关属性1.blob筛选结果结果图案例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.PMAlign;using Cognex.VisionPro.PatInspect;using Cognex.VisionPro.Blob;#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase{#region Private Member Variablesprivate Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;//声明blobprivate CogBlobTool mBlobRight;private CogBlobTool mBlobLeft;private CogBlobTool mBlobWuRan;//声明文本private CogGraphicLabel mLabel new CogGraphicLabel();#endregionpublic 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();// #endifmBlobLeft mToolBlock.Tools[CogBlobTool左眼]as CogBlobTool;mBlobRight mToolBlock.Tools[CogBlobTool右眼]as CogBlobTool;mBlobWuRan mToolBlock.Tools[CogBlobTool污染]as CogBlobTool;// Run each tool using the RunTool functionforeach(ICogTool tool in mToolBlock.Tools)mToolBlock.RunTool(tool, ref message, ref result);//判断缺陷是否存在if(mBlobLeft.Results.GetBlobs().Count 0mBlobRight.Results.GetBlobs().Count 0mBlobWuRan.Results.GetBlobs().Count 0){//无缺陷mLabel.Color CogColorConstants.Green;mLabel.SetXYText(200, 200, Result:OK);}else{//有缺陷//分别对缺陷结果进行筛选string a mBlobLeft.Results.GetBlobs().Count 0 ? : 右眼缺失;string b mBlobRight.Results.GetBlobs().Count 0 ? : 左眼缺失;string c mBlobWuRan.Results.GetBlobs().Count 0 ? : 有污染;mLabel.Color CogColorConstants.Red;mLabel.SetXYText(200, 200, 缺陷结果:a\nb\nc\n);}return false;}public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord){mToolBlock.AddGraphicToRunRecord(mLabel, lastRecord, CogPMAlignTool1.InputImage, script);}案例5展开零件的圆形区域 进行缺陷检测 如果显示NG 否则显示OK1.使用模板匹配1.模板匹配 出现缺陷的区域1.使用fixtureTool定位1.设置找圆工具2.0 找内径的圆形1.添加环形展开工具2.把找到的圆形 中心点信息 赋值给 centerX centerY3.调整双圆环 内外径位置1.添加blob工具 对齐缺陷部位进行分析CogAffineTransformTool 工具解析仿射变换工具作用通过仿射变换旋转、缩放、平移、剪切消除目标区域的几何畸变输出无旋转、无倾斜的标准矩形图像。作用1缩放调整‌按比例缩放图像像素点放到之前图像比例放大矩形区域内的特征放大后 图像比例作用2剪切处理作用3旋转校正如图输入仿射矩形图像以及仿射变换工具生成的输出图像 消除旋转和倾斜案例CogPMAlignTool1工具和CogFixtureTool工具用来定位。CogAffineTransformTool接收定位后图像进行仿射变换消除旋转和倾斜总结流程定位用 CogPMAlignToolPatMax找到工件输出姿态位置 角度。定义仿射 ROI根据定位结果生成 CogRectangleAffine带旋转角的平行四边形。仿射变换CogAffineTransformTool 把该 ROI 转正、缩放输出正矩形图像。后续处理对转正图像做 Blob、测量、OCR 等。
返回列表