《zw版·Halcon-delphi系列原创教程》 只有2行代码的超市收款单ocr脚本
只有2行代码的超市收款单ocr脚本
发了这么多教程,有网友问,为什么没有ocr的。
的确,在opencv教程当中,秀ocr的、与车牌识别、人脸识别,差不多是三大主流demo。
不过,Halcon内置的ocr模块非常彪悍,基本上,常规的工业、商业领域ocr,对于Halcon而言,已经成了标配,就象word的排版功能,实在太简单。
下面示例的脚本,不到100行,主要是图像分割太繁琐
真正用于ocr的,就两行
92 read_ocr_class_mlp ('DotPrint', OCRHandle)
93 do_ocr_multi_class_mlp (FinalCharacters, Rotated, OCRHandle, Class, Confidence)
图1,是原图
图2,字符切割
图3,最终识别结果,方框下面的字符是识别结果
ps,有兴趣的网友,可以自己改为delphi、vc、vb版
* dotprt.hdev: Segmentation of a dot printing
*
dev_update_window ('off')
read_image (Needle, 'needle1')
dev_close_window ()
get_image_size (Needle, Width, Height)
dev_open_window (, , * Width, * Height, 'black', WindowID)
set_display_font (WindowID, , 'mono', 'true', 'false')
dev_display (Needle)
disp_continue_message (WindowID, 'black', 'true')
stop ()
* *
Row1 :=
Column1 :=
Row2 :=
Column2 :=
Px := Column1 + (Column2 - Column1) /
Py := Row1 + (Row2 - Row1) /
gen_rectangle1 (Rectangle1, Row1, Column1, Row2, Column2)
* orientation correction
text_line_orientation (Needle, Needle, , -0.523599, 0.523599, OrientationAngle)
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_rotate (HomMat2DIdentity, -OrientationAngle, Px, Py, HomMat2DRotate)
affine_trans_image (Needle, Rotated, HomMat2DRotate, 'constant', 'false')
dev_display (Rotated)
disp_continue_message (WindowID, 'black', 'true')
stop ()
threshold (Rotated, RawSegmentation, , )
connection (RawSegmentation, ConnectedRegions)
select_shape (ConnectedRegions, MinSizeRegions, 'area', 'and', , )
union1 (MinSizeRegions, RemovedNoise)
dev_display (Rotated)
dev_set_color ('green')
dev_set_draw ('fill')
dev_display (RemovedNoise)
disp_continue_message (WindowID, 'black', 'true')
stop ()
clip_region (RemovedNoise, RawSegmentation, , , , )
dev_display (Rotated)
dev_display (RawSegmentation)
disp_continue_message (WindowID, 'black', 'true')
stop ()
closing_circle (RawSegmentation, ClosedPatterns, )
opening_rectangle1 (ClosedPatterns, SplitPatterns, , )
connection (SplitPatterns, ConnPatterns)
select_shape (ConnPatterns, CharCandidates, 'area', 'and', , )
shape_trans (CharCandidates, CharBlocks, 'rectangle1')
dev_set_draw ('margin')
dev_set_line_width ()
dev_display (Rotated)
dev_display (CharBlocks)
disp_continue_message (WindowID, 'black', 'true')
stop ()
partition_rectangle (CharBlocks, CharCandidates, , )
intersection (CharCandidates, RawSegmentation, Characters)
closing_circle (Characters, IntermedCharacters, 2.5)
dev_set_colored ()
dev_display (Rotated)
dev_set_draw ('fill')
dev_display (IntermedCharacters)
disp_continue_message (WindowID, 'black', 'true')
stop ()
gen_empty_obj (Characters)
count_obj (IntermedCharacters, NumIntermediate)
dev_display (Rotated)
for i := to NumIntermediate by
dev_set_color ('red')
select_obj (IntermedCharacters, Char, i)
dev_display (Char)
connection (Char, CharParts)
select_shape (CharParts, CharCandidates, 'area', 'and', , )
union1 (CharCandidates, Char)
dev_set_color ('green')
dev_display (Char)
* disp_continue_message (WindowID, 'black', 'true')
* stop ()
concat_obj (Characters, Char, Characters)
endfor
select_shape (Characters, Heigh, 'height', 'and', , )
sort_region (Heigh, FinalCharacters, 'character', 'true', 'row')
dev_set_color ('red')
dev_set_draw ('margin')
dev_display (Rotated)
dev_display (FinalCharacters)
dev_set_color ('green')
dev_set_line_width ()
dev_set_shape ('rectangle1')
dev_display (FinalCharacters)
dev_set_shape ('original')
dev_set_line_width ()
dev_set_draw ('fill')
read_ocr_class_mlp ('DotPrint', OCRHandle)
do_ocr_multi_class_mlp (FinalCharacters, Rotated, OCRHandle, Class, Confidence)
smallest_rectangle1 (FinalCharacters, Row11, Column1, Row2, Column21)
count_obj (FinalCharacters, NumberFinal)
for i := to NumberFinal by
disp_message (WindowID, Class[i - ], 'image', Row2[i - ], Column1[i - ], 'green', 'false')
endfor
clear_ocr_class_mlp (OCRHandle)
dev_update_window ('on')
【《zw版·Halcon-delphi系列原创教程》,网址,cnblogs.com/ziwang/】