• Halcon知识:用分箱实现OCR分类器


    一、提要

    以下也是超级箱子supperbox在字符识别的应用;本篇给出所有相关算子。

    二、实现分类算子

    2.1 read_ocr

    从文件读取OCR分类器

    read_ocr( : : FileName : OcrHandle)

    2.2 write_ocr

    将OCR分类器写入文件

    write_ocr(:: FileName, OcrHandle
     

    2.3 close_ocr 

    重新分配 OCR 分类器的内存。

    close_ocr( : : OcrHandle : )

            运算符 close_ocr 释放编号为 OcrHandle 的分类器的内存。因此,所有相应的数据将被删除。但是,如果需要,可以使用运算符 write_ocr 提前保存它们。调用close_ocr后OcrHandle号码无效;但稍后系统可以再次将其用于新的分类器。

    2.4 create_ocr_class_box

    产生新的分类器

    create_ocr_class_box( : : WidthPattern, HeightPattern, Interpolation, Features, Character : OcrHandle)

            运算符 create_ocr_class_box 创建一个新的 OCR 分类器。有关此分类器的描述,请参见运算符 learn_class_box。然后必须在操作符 traind_ocr_class_box 或 trainf_ocr_class_box 的帮助下训练这个分类器。

            参数 WidthPattern 和 HeightPattern 表示网络输入层的大小。此尺寸用于特征“projection_horizo​​ntal”、“projection_vertical”、“pixel”、“pixel_invar”和“pixel_binary”,以将字符转换为标准尺寸。标准尺寸越大,可以区分的字符越多。因此,训练所需的时间量(以及训练随机样本的数量)和识别所需的时间也会增加。参数 Interpolation 表示与图像中的字符适应网络有关的插值模式。有关此参数的更多详细信息,另请参阅 affine_trans_image。值 0 导致与 affine_trans_image 中的“nearest_neighbor”相同的插值,即不执行插值。对于 1,获得与 affine_trans_image 中的 'constant' 相同的行为,即在相邻像素之间使用等权插值。最后,2 产生与“加权”相同的插值,即使用相邻像素之间的高斯插值。必须选择参数 Interpolation,以便在字符缩放到标准大小时不会出现锯齿。通常,这意味着应该将 Interpolation 设置为 1,除非字符被大量缩小,在这种情况下应该选择 Interpolation = 2。仅当不缩放字符时才应选择插值 = 0。

            参数 Character 决定了所有需要识别的字符。通常,传输的字符串由一个字符组成(例如字母表)。但也可以学习任何长度的字符串。可区分字符数(Character 中的字符串数)限制为 2048 个。

            参数 Features 有助于选择灰度值以外的其他特征,以识别各类的特别之处。

    • Features可以使用以下功能选择:

    'default'-- 'ratio' and 'pixel_invar' are selected.

    'ratio'-- Ratio of the character.

    'width'-- Width of the character (not invariant to scaling).

    'height'-- Height of the character (not invariant to scaling).

    'zoom_factor'-- Difference in size between the current character and the values of WidthPattern and HeightPattern (not invariant to scaling).

    'foreground'-- Relative number of pixels in the foreground.

    'foreground_grid_9' -- Relative number of foreground pixels in a 3x3 grid within the surrounding rectangle of the character.

    'foreground_grid_16' -- Relative number of foreground pixels in a 4x4 grid within the surrounding rectangle of the character.

    'anisometry' -- Form feature anisometry.

    'compactness' -- Form feature compactness.

    'convexity' -- Form feature convexity.

    'moments_region_2nd_invar' -- Normed 2nd geometric moments of the region. See also moments_region_2nd_invar.

    'moments_region_2nd_rel_invar' -- Normed 2nd relativ geometric moments of the region. See also moments_region_2nd_rel_invar.

    'moments_region_3rd_invar' -- Normed 3rd geometric moments of the region. See also moments_region_3rd_invar.

    'moments_central' -- Normed central geometric moments of the region. See also moments_region_central.

    'phi' -- Sinus and cosinus of the orientation (angle) of the character.

    'num_connect' -- Number of connecting components.

    'num_holes' -- Number of holes.

    'projection_horizontal' -- Horizontal projection of the gray values.

    'projection_horizontal_invar' -- Horizontal projection of the gray values with are automatically scaled to maximum range.

    'projection_vertical' -- Vertical projection of the gray values.

    'projection_vertical_invar' -- Vertical projection of the gray values with are automatically scaled to maximum range.

    'cooc' -- Values of the binary cooccurrence matrix.

    'moments_gray_plane' -- Normed gray value moments and the angles of the gray value level.

    'num_runs' -- Number of chords in the region normed to the height.

    'chord_histo' -- Frequency of the chords per row.

    'pixel' -- Gray value of the character.

    'pixel_invar' -- Gray values of the character with automatic maximal scaling of the gray values.

    'pixel_binary' -- Region of the character as a binary image zoomed to a size of WidthPattern x HeightPattern.

    'gradient_8dir' -- Gradients are computed on the character image. The gradient directions are discretized into 8 directions. The amplitude image is decomposed into 8 channels according to these discretized directions. 25 samples on a 5x5 grid are extracted from each channel. These samples are used as features.


    2.5 fwrite_serialized_item

    序列化多项数据。

    fwrite_serialized_item( : : FileHandle, SerializedItemHandle : )

            fwrite_serialized_item使用句柄 FileHandle 将序列化项写入输出文件。序列化项由句柄序列化项处理定义。open_file,操作员可以打开该文件。请注意,该文件必须以二进制格式打开。要在一个文件中写入多个项目,必须多次调用运算符fwrite_serialized_item。可以使用fread_serialized_item读取序列化项。

    序列化和反序列化的基础知识

            为了通过套接字连接将图标对象、数据或模型写入或发送到文件或另一个HALCON进程,首先,必须将图标对象、数据或模型转换为序列化项。为此,有许多操作员可用。E、 例如,运算符serialize\u shape\u model对形状模型执行序列化。在此之后,可以通过fwrite\u serialized\u item将序列化项写入文件,也可以通过send\u serialized\u item通过套接字连接传输到另一个HALCON进程。

            要反序列化序列化序列化项,首先从文件中读取序列化项(请参阅fread_serialized_ item),或通过套接字连接从另一个HALCON进程接收序列化项(请参阅receive_serialized_item)。还有一些运算符将序列化项转换为原始格式,即转换为图标对象、数据或模型。这些操作符反序列化图标对象、数据或模型。E、 例如,运算符反序列化形状模型为序列化形状模型反序列化项。

    2.6 deserialize_ocr

    deserialize_ocr( : : SerializedItemHandle : OcrHandle)

            反序列化ocr反序列化由serialize\u ocr序列化的ocr分类器(有关序列化的基本原理的介绍,请参阅fwrite\u serialized\u项)。序列化OCR分类器由句柄SerializedItemHandle定义。反序列化的值存储在自动创建的具有句柄OcrHandle的OCR分类器中。


    2.7 do_ocr_multi

            对字符进行分类。

    do_ocr_multi(Character, Image : : OcrHandle : Class, Confidence)

            运算符do\u ocr\u multi为每个字符(字符)分配一个类。对于灰度值特征,使用区域周围矩形的灰度值。灰度值将取自参数图像。对于每个字符,相应的类将在类中返回,置信值将在置信中返回。置信值表示输入模式和指定字符之间的相似性。


    2.8 do_ocr_single

    do_ocr_single(Character, Image : : OcrHandle : Classes, Confidences)

            运算符do\u ocr\u single为字符指定类。对于灰度值特征,将使用区域周围矩形的灰度值。灰度值将取自参数图像。对于每个角色,置信度最高的两个类将在类中返回。相应的机密将以机密形式返回。置信值表示输入模式和指定字符之间的相似性。

    参数:

    • Character (input_object)  region → object,Character to be recognized.
    • Image (input_object)  singlechannelimage → object (byte / uint2),Gray values of the characters.
    • OcrHandle (input_control)  ocr_box → (integer),ID of the OCR classifier.
    • Classes (output_control)  string-array → (string),Classes (names) of the characters.
    • Confidences (output_control)  real-array → (real),Confidence values of the characters.

    2.9 info_ocr_class_box

    获取ocr分类器的内部信息。

    info_ocr_class_box( : : OcrHandle : WidthPattern, HeightPattern, Interpolation, WidthMaxChar, HeightMaxChar, Features, Characters)

            operator info_ocr_class_box返回有关ocr分类器的一些信息。这些参数与create_ocr_class_box的参数等效。参数WidthMaxChar和HeightMaxChar表示最大训练字符的扩展。这些值可用于控制分割。

    参数:

    • OcrHandle (input_control)  ocr_box → (integer)

            ID of the OCR classifier.

    • WidthPattern (output_control)  integer → (integer)

            Width of the scaled characters.

    • HeightPattern (output_control)  integer → (integer)

            Height of the scaled characters.

    • Interpolation (output_control)  integer → (integer)

            Interpolation mode for scaling the characters.

    • WidthMaxChar (output_control)  integer → (integer)

            Width of the largest trained character.

    • HeightMaxChar (output_control)  integer → (integer)

            Height of the largest trained character.

    • Features (output_control)  string-array → (string)

            Used features.

    • Characters (output_control)  string-array → (string)

            All characters of the set.


    2.10 ocr_change_char

    为字符定义新的转换表。

    ocr_change_char( : : OcrHandle, Character : )

    运算符 ocr_change_char 为字符建立一个新的查找表。因此,Character 的字符串数必须与分类器 OcrHandle 的相同。为了放大字体,操作符 ocr_change_char 可以如下使用: 使用 (create_ocr_class_box) 创建网络时,将显示比实际需要的字符多的字符。到目前为止,最后 n 个字符不会被使用。如果后期需要更多字符,这些未使用的字符将被分配,然后在算子 ocr_change_char 的帮助下进行训练。


    2.11 ocr_get_features

    访问与字符对应的功能。

    ocr_get_features(Character : : OcrHandle : FeatureVector)

            运算符 ocr_get_features 计算给定字符的特征。特征的类型和数量由分类器 OcrHandle 确定。 FeatureVector 包含与 traind_ocr_class_box 或 trainf_ocr_class_box 等运算符内部使用的值相同的值。



    2.12 serialize_ocr


    Serialize an OCR classifier.

    序列化ORC


    2.13 testd_ocr_class_box

    测试ocr的分类能力

    testd_ocr_class_box(Character, Image : : OcrHandle, Class : Confidence)

            运算符 testd_ocr_class_box 测试字符属于给定类的置信度。可以传递图像的任意数量的区域。对于 Character 中的每个字符(区域),必须指定相应的名称(类)Class。灰度值在 Image 中传递。当操作员完成时,参数 Confidence 提供有关字符属于(任意选择的)类的确定程度的信息。


    2.14 traind_ocr_class_box

            通过输入区域来训练 OCR 分类器。

    traind_ocr_class_box(Character, Image : : OcrHandle, Class : AvgConfidence)

            操作符 traind_ocr_class_box 通过输入图像中的区域直接训练分类器。可以传递图像的任意数量的区域。对于 Character 中的每个字符(区域),必须指定相应的名称(类)Class。灰度值在 Image 中传递。当程序完成时,参数 AvgConfidence 提供有关训练成功的信息:它包含通过重新分类测量的训练字符的平均置信度。不匹配字符的置信度设置为0(因此,平均置信度将显着降低)。


    2.15 trainf_ocr_class_box

            借助训练文件训练 OCR 分类器。

    trainf_ocr_class_box( : : OcrHandle, TrainingFile : AvgConfidence)

            算子 trainf_ocr_class_box 通过指定的训练文件训练分类器 OcrHandle。可以指示任意数量的文件。参数 AvgConfidence 提供有关训练成功的信息:它包含通过重新分类测量的训练字符的平均置信度。不匹配字符的置信度设置为0(因此,平均置信度将显着降低)。请注意,在分类器 OcrHandle 中没有对应类别的训练字符将被丢弃。

    参考地址:

    https://www.mvtec.com/doc/halcon/13/en/create_ocr_class_box.html

    其它分类器

    Hyperboxes
    K-Nearest Neighbors
    Lexica
    Neural Nets
    Segmentation
    Support Vector Machines
    Training Files

  • 相关阅读:
    HttpClient 在vivo内销浏览器的高并发实践优化
    如何获取一个会议的 transcripts
    x265参数-early-skip
    Servlet
    分享一个基于微信小程序的医院口腔助手小程序 牙科诊所预约小程序 源码 lw 调试
    pdd.order.basic.list.get订单基础信息列表查询接口(拼多多店铺订单列表查询接口)代码对接教程
    go test, go bench & go examples
    P1554 梦中的统计--------洛谷 / 题目列表 / 题目详情
    SpringBoot + Vue实现博文上传+展示+博文列表
    Spring Cloud LoadBalancer--指定负载均衡策略--方法/实例
  • 原文地址:https://blog.csdn.net/gongdiwudu/article/details/125901684