案例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");
}
案例2:筛子
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");
}
案例4: