案例1:识别环形字符并显示
1.添加圆形展开工具
2.调整内外径圆环 (注意:转换方向 以3闭合圆形的地方开始 沿4箭头的方向分割圆环)
1.添加ocr工具提取字符
2.提取字符操作
3.选框范围设置要包括提取的字符(注意:不要超过图形边缘)
区段设置(如果有需要)
引用 ovr工具的命名空间
#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.OCRMax;
#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private 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 function
foreach(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 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.OCRMax;
#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
#endregion
CogGraphicLabel 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 name="message">Sets the Message in the tool's RunStatus.</param>
/// <param name="result">Sets the Result in the tool's RunStatus</param>
/// <returns>True if the tool should run normally,
/// False if GroupRun customizes run behavior</returns>
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
CogOCRMaxTool ocr = mToolBlock.Tools["CogOCRMaxTool1"] as CogOCRMaxTool;
CogOCRMaxTool ocr2 = mToolBlock.Tools["CogOCRMaxTool2"] as CogOCRMaxTool;
// Run each tool using the RunTool function
foreach(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");
}
案例3
1.把彩图转换为灰度图
1.blob工具 设置相关属性
1.blob筛选结果
结果图:
案例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.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 mBlobRight;
private CogBlobTool mBlobLeft;
private CogBlobTool mBlobWuRan;
//声明文本
private CogGraphicLabel mLabel =new CogGraphicLabel();
#endregion
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
mBlobLeft = mToolBlock.Tools["CogBlobTool左眼"]as CogBlobTool;
mBlobRight = mToolBlock.Tools["CogBlobTool右眼"]as CogBlobTool;
mBlobWuRan = mToolBlock.Tools["CogBlobTool污染"]as CogBlobTool;
// Run each tool using the RunTool function
foreach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool, ref message, ref result);
//判断缺陷是否存在
if(mBlobLeft.Results.GetBlobs().Count == 0&&mBlobRight.Results.GetBlobs().Count == 0&&mBlobWuRan.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+"\n"+b+"\n"+c+"\n");
}
return false;
}
public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
{
mToolBlock.AddGraphicToRunRecord(mLabel, lastRecord, "CogPMAlignTool1.InputImage", "script");
}
案例5:展开零件的圆形区域 进行缺陷检测 如果显示NG 否则显示OK
1.使用模板匹配
1.模板匹配 出现缺陷的区域
1.使用fixtureTool定位
1.设置找圆工具
2.0 (找内径的圆形)
1.添加环形展开工具
2.把找到的圆形 中心点信息 赋值给 centerX centerY
3.调整双圆环 内外径位置
1.添加blob工具 对齐缺陷部位进行分析
CogAffineTransformTool 工具解析(仿射变换工具)
作用:
通过仿射变换(旋转、缩放、平移、剪切)消除目标区域的几何畸变,输出无旋转、无倾斜的标准矩形图像。
作用1:缩放调整
按比例缩放:图像像素点
放到之前图像比例
放大矩形区域内的特征
放大后 图像比例
作用2:剪切处理
作用3:旋转校正
如图:输入仿射矩形图像以及仿射变换工具生成的输出图像 (消除旋转和倾斜)
案例:CogPMAlignTool1工具和CogFixtureTool工具用来定位。CogAffineTransformTool接收定位后图像进行仿射变换(消除旋转和倾斜)
总结流程:
- 定位:用 CogPMAlignTool(PatMax)找到工件,输出姿态(位置 + 角度)。
- 定义仿射 ROI:根据定位结果生成 CogRectangleAffine(带旋转角的平行四边形)。
- 仿射变换:CogAffineTransformTool 把该 ROI 转正、缩放,输出正矩形图像。
- 后续处理:对转正图像做 Blob、测量、OCR 等。