这段代码是一个使用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函数来完成这些任务。

Logo

助力广东及东莞地区开发者,代码托管、在线学习与竞赛、技术交流与分享、资源共享、职业发展,成为松山湖开发者首选的工作与学习平台

更多推荐