halcon bottle上的数字识别实例注释

时间:2022-08-23 19:48:47
* 关键掌握数字的分割技术
* bottle2.hdev: Segmenting and reading of numbers on a beer bottle
* Program without visualization: "bottle.hdev"

* Step 0: Preparations
* Specify the name of the font to use for reading the date on the bottle.
* It is easiest to use the pre-trained font Industrial_0-9.  If you have run the
* program bottlet.hdev in this directory, you can activate the second line
* to use the font trained with this program.
FontName := 'Industrial_0-9'
* FontName := 'bottle'

* Step 1: Segmentation
dev_update_window ('off')
read_image (Bottle, 'bottle2')
get_image_size (Bottle, Width, Height)
dev_close_window ()
dev_open_window (0, 0, 2*Width, 2*Height, 'black', WindowID)
set_display_font (WindowID, 20, 'mono', 'true', 'false')
dev_display (Bottle)
disp_continue_message (WindowID, 'black', 'true')
stop ()
*二值化,选取比较暗的部分
threshold (Bottle, RawSegmentation, 0, 95)
*把中间一些小洞洞补起来
fill_up_shape (RawSegmentation, RemovedNoise, 'area', 1, 5)
*开运算,把小杂质图像去掉

opening_circle (RemovedNoise, ThickStructures, 2.5)

halcon bottle上的数字识别实例注释

dev_display (Bottle)
dev_set_color ('green')
dev_display (ThickStructures)
disp_continue_message (WindowID, 'black', 'true')
stop ()
dev_set_draw ('margin')
dev_set_line_width (3)
dev_display (Bottle)
dev_display (ThickStructures)
disp_continue_message (WindowID, 'black', 'true')
stop ()
*填充区域,里面孔都封起来,处理边缘
fill_up (ThickStructures, Solid)
dev_display (Bottle)
dev_clear_window()
dev_display (Solid)
disp_continue_message (WindowID, 'black', 'true')

stop ()

halcon bottle上的数字识别实例注释

*矩形开运算,把连通的小区域分开
opening_rectangle1 (Solid, Cut, 1, 7)
dev_display (Bottle)
dev_display (Cut)
disp_continue_message (WindowID, 'black', 'true')

stop ()

halcon bottle上的数字识别实例注释

*计算连通区域
connection (Cut, ConnectedPatterns)
*交集
dev_clear_window()
dev_display(ConnectedPatterns)
dev_clear_window()
dev_display(ThickStructures)
intersection (ConnectedPatterns, ThickStructures, NumberCandidates)
dev_clear_window()
dev_display(NumberCandidates)
halcon bottle上的数字识别实例注释

dev_set_colored (12)
dev_display (Bottle)
dev_display (NumberCandidates)
disp_continue_message (WindowID, 'black', 'true')

stop ()

*选择区域

select_shape (NumberCandidates, Numbers, 'area', 'and', 300, 9999)

*排队排队

sort_region (Numbers, FinalNumbers, 'first_point', 'true', 'column')
dev_set_color ('red')
dev_display (Bottle)
dev_display (Numbers)
dev_set_shape ('rectangle1')
dev_set_color ('green')
dev_display (FinalNumbers)
dev_set_shape ('original')
dev_set_line_width (1)
dev_set_draw ('fill')

* Step2: Reading
* 读取训练好的分类器.这应该是神经网络之类的东东.
read_ocr_class_mlp (FontName, OCRHandle)
*识别
do_ocr_multi_class_mlp (FinalNumbers, Bottle, OCRHandle, RecNum, Confidence)
*计算中心点
area_center (FinalNumbers, Area, Row, ColNum)
set_display_font (WindowID, 27, 'mono', 'true', 'false')
for i := 0 to |RecNum|-1 by 1
    disp_message (WindowID, RecNum[i], 'image', 80, ColNum[i]-3, 'green', 'false')
endfor

*显示效果

halcon bottle上的数字识别实例注释

clear_ocr_class_mlp (OCRHandle)
dev_update_window ('on')