光学字符识别

15 篇文章 0 订阅
6 篇文章 0 订阅

这段代码是一个使用HALCON软件进行OCR(光学字符识别)的示例脚本。
dev_update_off ()
dev_close_window ()

  • Initialize window handle
    dev_open_window_fit_size (0, 0, 960, 768, 700, 700, WindowHandle)
    set_display_font (WindowHandle, 16, ‘mono’, ‘true’, ‘false’)
  • Read OCR classifier and create the text model
    read_ocr_class_mlp (‘Industrial_Rej’, OCRHandle)
  • Initialize display variables

disp_message (WindowHandle, ‘This example shows how find_text can be used to \nfind and read text. Furthermore, the most \nimportant parameters are explained.’, ‘window’, 12, 12, ‘black’, ‘true’)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()
*
dev_clear_window ()
disp_message (WindowHandle, ‘Essentially, the only thing that needs to be \ndone before calling find_text is to create \na text model with create_text_model_reader \nwith mode set to ‘auto’.\n \nThe resulting segmentation can then be obtained \nby calling the operators get_text_result and \nget_text_object.’, ‘window’, 12, 12, ‘black’, ‘true’)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()

  • An OCR Classifier based on a multilayer perceptron (MLP) is required
    create_text_model_reader (‘auto’, OCRHandle, TextModel)
    clear_ocr_class_mlp (OCRHandle)

read_image (Image, ‘ocr/medication_package_02_right’)
*

  • Without setting further parameters, find_text will find and read all
  • text within the input image
    find_text (Image, TextModel, TextResult)
  • The segmented regions can be obtained by calling get_text_object
    get_text_object (TextLines, TextResult, ‘all_lines’)

dev_set_colored (12)
dev_display (Image)
dev_display (TextLines)
disp_message (WindowHandle, ‘Find_text extracts all text within the input image,\nregardless of the character size. \nPer default, light and dark text is extracted.’, ‘window’, 12, 12, ‘black’, ‘true’)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
clear_text_result (TextResult)
stop ()
*

  • It is possible to restrict the segmentation to text of a certain polarity
  • using the set_text_model_param operator
    set_text_model_param (TextModel, ‘polarity’, ‘dark_on_light’)

find_text (Image, TextModel, TextResult)
*
get_text_object (TextLines, TextResult, ‘all_lines’)
dev_set_colored (12)
dev_display (Image)
dev_display (TextLines)
disp_message (WindowHandle, ‘It is possible to restrict the text segmentation \naccording to the polarity of the text. \n \nFor example, restricting the polarity to dark text \non a light background ignores all light text.’, ‘window’, 12, 12, ‘black’, ‘true’)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()
clear_text_result (TextResult)
*

  • Furthermore, it is possible to restrict the segmentation to text attributes
  • such as the character height, width or stroke width
    set_text_model_param (TextModel, ‘polarity’, ‘dark_on_light’)
    set_text_model_param (TextModel, ‘min_char_height’, 20)

find_text (Image, TextModel, TextResult)
*
get_text_object (TextLines, TextResult, ‘all_lines’)
dev_set_colored (12)
dev_display (Image)
dev_display (TextLines)
disp_message (WindowHandle, ‘Furthermore, it is possible to restrict the segmentation \nto text attributes such as the character height, \nwidth or stroke width.\n \nFor example, setting the minimal characters height \nto 20px ignores all characters with a smaller height.’, ‘window’, 12, 12, ‘black’, ‘true’)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
dev_display (TextLines)
stop ()
clear_text_result (TextResult)
*

  • When searching for specific text structures, it is possible to
  • set the corresponding structure with set_text_model_param
    set_text_model_param (TextModel, ‘polarity’, ‘dark_on_light’)
    set_text_model_param (TextModel, ‘min_char_height’, 20)
  • The separators which are added to the ‘text_line_separators’
  • need to be valid characters within the used classifier. Otherwise
  • they are ignored.
    set_text_model_param (TextModel, ‘text_line_separators’, ‘/’)
    set_text_model_param (TextModel, ‘text_line_structure’, ‘2 4’)

find_text (Image, TextModel, TextResult)
*
get_text_object (TextLines, TextResult, ‘all_lines’)
dev_set_colored (12)
dev_display (Image)
dev_display (TextLines)
disp_message (WindowHandle, ‘When searching for specific text structures, it can \nbe helpful to set the corresponding text_line_structure \nwith set_text_model_param. \n \nFor example, when searching for a date of the form MM/YYYY,\nit is possible to add ‘/’ to the text_line_separators and \nset the text_line_structure to ‘2 4’.’, ‘window’, 12, 12, ‘black’, ‘true’)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()
*

  • It is possible to directly retain the classification results for
  • each of the segmented characters with the get_text_result operator

dev_set_colored (12)
dev_display (Image)
dev_display (TextLines)
*

  • Display the single characters
    smallest_rectangle1 (TextLines, Row1, Column1, Row2, Column2)
    get_text_result (TextResult, ‘class’, SingleCharacters)
    tuple_sum (SingleCharacters, TextLineCharacters)
    dev_set_color (‘dark green’)
    for CharacterIndex := 0 to |SingleCharacters| - 1 by 1
    set_tposition (WindowHandle, Row2[CharacterIndex] + 10, Column1[CharacterIndex])
    write_string (WindowHandle, SingleCharacters[CharacterIndex])
    endfor
    disp_message (WindowHandle, 'It is possible to directly retain the classification \nresults for each of the segmented characters \nwith the get_text_result operator. \n \nExtracted Text: ’ + TextLineCharacters, ‘window’, 12, 12, ‘black’, ‘true’)
    stop ()
    clear_text_result (TextResult)
    clear_text_model (TextModel)

medication_package_02_right.png
在这里插入图片描述
以下是程序运行结果:

在这里插入图片描述
HALCON是一个用于机器视觉和图像分析的高级软件包。以下是对脚本中各个部分的解释:

dev_update_off() 和 dev_close_window():关闭图像更新和关闭窗口。

dev_open_window_fit_size():打开一个新的窗口,设置窗口的大小和位置。

set_display_font():设置窗口中显示文本的字体。

read_ocr_class_mlp():读取OCR分类器,这里使用的是基于多层感知器(MLP)的分类器。

disp_message() 和 disp_continue_message():在窗口中显示消息。

stop():停止执行。

create_text_model_reader():创建文本模型读取器,用于读取文本。

clear_ocr_class_mlp():清除OCR分类器。

read_image():读取图像文件。

find_text():在图像中查找文本。

get_text_object():获取文本对象,例如文本行。

dev_set_colored() 和 dev_display():设置显示颜色并显示图像或文本。

set_text_model_param():设置文本模型的参数,例如文本的极性、最小字符高度等。

smallest_rectangle1():获取文本行的最小矩形。

get_text_result():获取文本结果,例如分类结果。

tuple_sum():将两个元组(tuple)相加。

set_tposition() 和 write_string():设置文本位置并写入字符串。

clear_text_result() 和 clear_text_model():清除文本结果和文本模型。

这个脚本演示了如何使用HALCON软件进行文本识别,包括如何设置参数以查找特定类型的文本,如何获取和显示识别结果。脚本中使用了多个HALCON函数来完成这些任务。

  • 19
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
光学字符识别算法的实现是通过将图像中的字符转换为计算机字符的过程。它的核心思想是检测图像中字符的形状,并通过字符识别模型将形状翻译为计算机可以理解的文字。 具体实现光学字符识别算法的方法有很多,以下是其中一种常见的方法: 1. 预处理:对图像进行预处理是光学字符识别的第一步。这包括图像的灰度化、二值化、去噪等操作。通过将彩色图像转为灰度图像,可以简化后续的处理步骤。然后,使用适当的二值化方法将图像转换为黑白图像,以便更好地检测字符的形状。最后,通过去噪操作去除图像中的干扰信息,以提高字符识别的准确性。 2. 字符检测与定位:在图像中准确定位和检测字符是光学字符识别的关键步骤。常用的方法包括边缘检测、轮廓提取和连通区域分析。通过这些方法,可以确定字符的位置和边界框,以便后续的字符识别。 3. 字符识别字符识别光学字符识别算法的核心。常用的方法包括模板匹配、基于特征的方法和机器学习方法。模板匹配是一种简单但有限的方法,它将图像中的字符与预定义的模板进行匹配。基于特征的方法通过提取字符的特征,如边缘、线段和角点等,来进行识别。机器学习方法则通过训练一个字符识别模型,将字符的图像特征映射到字符的类别或标签。 通过以上步骤,光学字符识别算法可以实现将图像中的字符转换为计算机字符的过程。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Happy Monkey

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值