CogOCRMaxTool_车牌训练

CogOCRMaxTool 讲解

CogOCRMaxTool 是字符读取工具,能够根据已训练的字符样本读取灰度图像中的字符,并返回读取结果

在使用 CogOCRMaxTool 工具读取字符的时候,需要设置字符区域,每个字符的最大最小宽度等参数。在字符读取之前需要首先进行字符分割和字符训练,下面分别进行介绍:

字符分割:

  • 字符分割的过程就是将字符像素从背景像素中分离出来,然后将这些分离后的字符像素分割成独立 的符号,CogOCRMaxTool 工具支持一套参数,这些参数指示如何把字符和背景以及字符和字符分割开,这些参数的设置需要考虑很多因素,如字符之间的距离、字符的种类、图像的质量等,通常情况下,默认的分割参数不能将字符充分分割。需要不断的尝试着修改分割参数、直到字符能够充分分割

字符分类

  • 字符分类的过程就是为每一个分割的字符训练一个最佳匹配字符 。新添加 CogOCRMaxTool 工具是没有训练字符的。可以从文件中调用已经存在的字符文件或临时添加字符集、添加字符集是一个不断重复的过程,需要添加所有需要读取的字符,只有在训练字符集中存在的字符才能够被成功读取。对于为训练的字符则不能成功读取

案例

效果

工具

得分

区段

Save & Load

脚本

创建label

cs 复制代码
private CogGraphicLabel createlabel(string text, float size, double x, double y, CogColorConstants color)
  {
    CogGraphicLabel label = new CogGraphicLabel();
    label.Font = new Font("Arial", size, FontStyle.Bold, GraphicsUnit.Pixel);
    label.Alignment = CogGraphicLabelAlignmentConstants.TopLeft;
    label.Color = color;
    label.BackgroundColor = CogColorConstants.White;
    label.SetXYText(x, y, text);
    return label;
  }

工具声明&创建集合

cs 复制代码
private CogGraphicCollection dt = new CogGraphicCollection();
dt.Clear();
    mToolBlock.Outputs["ResultContent"].Value = "";
    CogPMAlignTool pma = mToolBlock.Tools["CogPMAlignTool1"]as CogPMAlignTool;
    CogOCRMaxTool ocr = mToolBlock.Tools["CogOCRMaxTool1"]as CogOCRMaxTool;
    CogFixtureTool fix = mToolBlock.Tools["CogFixtureTool1"]as CogFixtureTool;

创建转灰度工具

cs 复制代码
 CogImageConvertTool Imageconvert = new CogImageConvertTool();
    Imageconvert.InputImage = mToolBlock.Inputs["OutputImage"].Value as ICogImage;
    Imageconvert.RunParams.RunMode = CogImageConvertRunModeConstants.Intensity;
    Imageconvert.Run();

图片赋值

cs 复制代码
 pma.InputImage = Imageconvert.OutputImage;
    fix.InputImage=Imageconvert.OutputImage;
    pma.Run();
    fix.Run();

声明显示label

cs 复制代码
 dt.Add(createlabel("车牌号是:" + ocr.LineResult.ResultString, 25, 0, 0, CogColorConstants.Blue));
    dt.Add(createlabel(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), 25, 0, 60, CogColorConstants.Green));
    mToolBlock.Outputs["ResultContent"].Value = ocr.LineResult.ResultString;

遍历显示

cs 复制代码
foreach(ICogGraphic s in dt)
    {
      mToolBlock.AddGraphicToRunRecord(s, lastRecord, "CogPMAlignTool1.InputImage", "script");
    }

ALL 脚本

cs 复制代码
#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.ImageProcessing;
using Cognex.VisionPro.OCRMax;
using Cognex.VisionPro.CalibFix;
#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  #endregion
  private CogGraphicCollection dt = new CogGraphicCollection();

  /// <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
    dt.Clear();
    mToolBlock.Outputs["ResultContent"].Value = "";
    CogPMAlignTool pma = mToolBlock.Tools["CogPMAlignTool1"]as CogPMAlignTool;
    CogOCRMaxTool ocr = mToolBlock.Tools["CogOCRMaxTool1"]as CogOCRMaxTool;
    CogFixtureTool fix = mToolBlock.Tools["CogFixtureTool1"]as CogFixtureTool;
    CogImageConvertTool Imageconvert = new CogImageConvertTool();
    Imageconvert.InputImage = mToolBlock.Inputs["OutputImage"].Value as ICogImage;
    Imageconvert.RunParams.RunMode = CogImageConvertRunModeConstants.Intensity;
    Imageconvert.Run();
    pma.InputImage = Imageconvert.OutputImage;
    fix.InputImage=Imageconvert.OutputImage;
    pma.Run();
    fix.Run();


    // Run each tool using the RunTool function
    //ocr.InputImage = Imageconvert.OutputImage;
    foreach(ICogTool tool in mToolBlock.Tools)
      mToolBlock.RunTool(tool, ref message, ref result);
    dt.Add(createlabel("车牌号是:" + ocr.LineResult.ResultString, 25, 0, 0, CogColorConstants.Blue));
    dt.Add(createlabel(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), 25, 0, 60, CogColorConstants.Green));
    mToolBlock.Outputs["ResultContent"].Value = ocr.LineResult.ResultString;

    return false;
  }
  private CogGraphicLabel createlabel(string text, float size, double x, double y, CogColorConstants color)
  {
    CogGraphicLabel label = new CogGraphicLabel();
    label.Font = new Font("Arial", size, FontStyle.Bold, GraphicsUnit.Pixel);
    label.Alignment = CogGraphicLabelAlignmentConstants.TopLeft;
    label.Color = color;
    label.BackgroundColor = CogColorConstants.White;
    label.SetXYText(x, y, text);
    return label;
  }

  #region When the Current Run Record is Created
  /// <summary>
  /// Called when the current record may have changed and is being reconstructed
  /// </summary>
  /// <param name="currentRecord">
  /// The new currentRecord is available to be initialized or customized.</param>
  public 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 name="lastRecord">
  /// The new last run record is available to be initialized or customized.</param>
  public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
  {
    foreach(ICogGraphic s in dt)
    {
      mToolBlock.AddGraphicToRunRecord(s, lastRecord, "CogPMAlignTool1.InputImage", "script");
    }
  }
  #endregion

  #region When the Script is Initialized
  /// <summary>
  /// Perform any initialization required by your script here
  /// </summary>
  /// <param name="host">The host tool</param>
  public override void Initialize(Cognex.VisionPro.ToolGroup.CogToolGroup host)
  {
    // DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
    base.Initialize(host);


    // Store a local copy of the script host
    this.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host));
  }
  #endregion

}
相关推荐
MobotStone19 分钟前
让AI干活,为什么验收标准要在开工前写
人工智能
AI算力7 小时前
让安全驾驶有“AI”相伴|腾视科技DMS视频监控一体机,守护每一次出行
人工智能·科技·ai·车载终端·ai算力·车载智能终端·车载dvr
泯泷8 小时前
超越基准:Claude Opus 4.5 的 4 个惊人洞察
人工智能
果果软件8 小时前
2026 后台管理系统选型:若依、JeecgBoot、芋道、NocoBase 和 GewuMind Boot 怎么选?
java·人工智能·后端·ai·开源
Csvn8 小时前
刚开始用 WorkBuddy,第一步该做什么?
人工智能
一水鉴天8 小时前
同一与唯一:AppBus 外壳与 DevOps 内核 之1 20261121(元宝)
人工智能·机器学习
Wellijohn9 小时前
一个 Android 老兵的十年开源路:从 167 star 的滑动控件,到本地优先的 AI Agent
android·人工智能·开源
ZONGYUAN-ROOT9 小时前
【API配额】API 配额监控告警不生效?用 Python 监控读数变化量,抓住静态阈值漏掉的「假重置」
人工智能
Wellijohn9 小时前
我把公司内部的 12 个 CRUD 接口交给了 AI Agent,然后省出了两周
人工智能·python·mcp