今天突然想起来玩一下Halcon,在Halcon论坛上看到有一个帖子写的是字符识别的方法,当时看了看有些没明白就查了点资料,现在来分享一下:
1、Halcon的图像处理过程一般分为以下几个步骤
(1)图像读取
(2)图像分割
(3)形态学处理
(4)特征提取
(5)输出结果
2、其实字符的识别和别的光学图像处理的步骤大体类似,主要用到的算子是 append_ocr_trainf ()、read_ocr_class_mlp()和do_ocr_multi_class_mlp()
下面将我今天做得这个贴一下,有问题再回顾和改进
FontName := 'Image'
dev_update_window ('off')
read_image (Image, 'C:/Users/Administrator/Desktop/9_542_620ebbb9212f968.jpg')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
set_display_font (WindowHandle, 27, 'mono', 'true', 'false')
threshold (Image, Region, 150, 400)
fill_up_shape (Region, RegionFillUp, 'area', 1, 5)
opening_circle (RegionFillUp, RegionOpening, 2.5)
fill_up (RegionOpening, RegionFillUp1)
opening_rectangle1 (RegionFillUp1, RegionOpening1, 1, 7)
connection (RegionOpening1, ConnectedRegions)
intersection (ConnectedRegions, RegionOpening, RegionIntersection)
select_shape (RegionIntersection, SelectedRegions, 'area', 'and', 300, 99999)
sort_region (SelectedRegions, SortedRegions, 'first_point', 'true', 'column')
dev_display (Image)
dev_set_color ('green')
dev_set_line_width (2)
dev_set_shape ('rectangle2')
dev_set_draw ('margin')
dev_display (SortedRegions)
TrainingNames := ['1','0','7','5','3']
TrainingFileName := FontName+'.trf'
sort_region (SortedRegions, SortedRegions1, 'first_point', 'true', 'column')
shape_trans (SortedRegions1, RegionTrans, 'rectangle1')
area_center (RegionTrans, Area, Row, Column)
MeanRow :=mean(Row)
dev_set_check ('~give_error')
delete_file (TrainingFileName)
dev_set_check ('give_error')
for i := 0 to |TrainingNames|-1 by 1
select_obj (SortedRegions, CharaterRegions, i+1) //从一个目标元组中选择目标
append_ocr_trainf (CharaterRegions, Image, TrainingNames[i], TrainingFileName)
disp_message (WindowHandle, TrainingNames[i], 'window', MeanRow-120, Column[i], 'yellow', 'false')
endfor
read_ocr_trainf_names (TrainingFileName, CharacterNames, CharacterCount)
NumHidden:=20
create_ocr_class_mlp (8, 10, 'constant', 'default', CharacterNames, 80, 'none', 10, 42, OCRHandle)
trainf_ocr_class_mlp (OCRHandle, TrainingFileName, 200, 1, 0.01, Error, ErrorLog)
write_ocr_class_mlp (OCRHandle, TrainingFileName)
上面就是训练的过程
然后是识别
dev_close_window ()
read_image (Image1, 'C:/Users/Administrator/Desktop/9_542_620ebbb9212f968.jpg')
get_image_size (Image1, Width1, Height1)
dev_open_window (0, 0, Width1, Height1, 'black', WindowHandle1)
dev_display (Image1)
text_line_orientation (CharaterRegions, Image1, 25, -0.523599, 0.523599, OrientationAngle)
rotate_image (Image1, ImageRotate, OrientationAngle, 'constant')
threshold (ImageRotate, Region1, 150, 400)
connection (Region1, ConnectedRegions1)
select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 300, 99999)
sort_region (SortedRegions1, SortedRegions2, 'upper_left', 'true', 'column')
read_ocr_class_mlp (TrainingFileName, OCRHandle1)
do_ocr_multi_class_mlp (SortedRegions2, ImageRotate, OCRHandle1, Class, Confidence)
for i := 0 to |TrainingNames|-1 by 1
select_obj (SortedRegions2, ObjectSelected, i+1)
disp_message (WindowHandle1, Class[i], 'window', 120, Column[i], 'green', 'true')
endfor
用到的图片会贴在最后。