Halcon汉字OCR训练识别
2018-10-12 本文已影响29人
晚晴风_
起初想到做这个是因为项目里有几个发光字要识别,而Halcon自带的OCR里面没有找到汉字的分类器,所以想着自己训练一下,这样可以更有针对性。
做为一个肤浅的初学者,学halcon也是个逐步摸索的过程,感谢老乡Z提供的入门资料,这里记录下实验过程。
步骤:
-
创建训练文件。
-
训练OCR分类器
-
识别测试图像。
创建训练文件
这一步主要是把训练图像中的汉字部分图像和文字符号关联起来。比如图像中找到了几个汉字区域,我们把这几个字的区域存储下来,然后建立个数组,里面放进去跟存储区中一一对应的汉字字符,这样就建立了关联。然后把这个关联关系存储在一个.trf文件中。
训练OCR分类器
这部分由两个选择,可以用svm,也可以用mlp。这里我用了mlp作例子。训练这个过程比较简单,主要是三个函数:
create_ocr_class_mlp
trainf_ocr_class_mlp
write_ocr_class_mlp
create_ocr_class_mlp (8, 10, 'constant', 'default', CharacterNames, 80, 'none', 10, 42, OCRHandle)
trainf_ocr_class_mlp (OCRHandle, 'G:/2.trf', 200, 1, 0.01, Error, ErrorLog)
write_ocr_class_mlp (OCRHandle, 'G:/2.omc')
识别汉字字符
接下来就可以导入测试图象检验下分类器的检测效果了,首先对图像做一个预处理,提取出需要检测的文字区域,然后,用read_ocr_class_mlp读取分类器,使用do_ocr_multi_class_mlp进行文字识别。
详细过程如下:
WindowHandle:=3600
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
gen_empty_obj (EmptyObject)
read_image (Image, 'G:/Yq/Code/test1.jpg')
rgb1_to_gray (Image, GrayImage)
for Index := 1 to 4 by 1
disp_message (WindowHandle, '请框选单个汉字区域,右键确认:','window', 12, 12, 'yellow', 'false')
**画个矩形
draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)
**根据画的矩形生成对应的矩形
gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)
*裁出来
reduce_domain (GrayImage, Rectangle, ImageReduced1)
*阈值
threshold (ImageReduced1, Region1, 128, 255)
*开运算,我看这步省了也行
opening_circle (Region1, RegionOpening, 1.5)
*准备接收所有提取的字符区域
concat_obj (EmptyObject, RegionOpening, EmptyObject)
endfor
words:=['测','试','文','字']
*排个序
sort_region (EmptyObject, SortedRegions1, 'character', 'true', 'row')
for Index1:=1 to 4 by 1
select_obj (SortedRegions1, ObjectSelected1, Index1)
append_ocr_trainf (ObjectSelected1, Image, words[Index1-1], 'G:/2.trf')
endfor
read_ocr_trainf_names ('G:/2.trf', CharacterNames, CharacterCount)
create_ocr_class_mlp (8, 10, 'constant', 'default', CharacterNames, 80, 'none', 10, 42, OCRHandle)
trainf_ocr_class_mlp (OCRHandle, 'G:/2.trf', 200, 1, 0.01, Error, ErrorLog)
write_ocr_class_mlp (OCRHandle, 'G:/2.omc')
*导入另一张做测试的图
read_image (Image1, 'G:/Yq/Code/test2.jpg')
*二值化
threshold (Image1, testwordregion, 125, 255)
*将像素相连的区域合并成一个Element
connection (testwordregion, ConnectedwordRegions)
*筛选符合条件的区域
select_shape (ConnectedwordRegions, SelectedwordRegions, 'height', 'and', 50, 250)
*从左到右,排个序
sort_region (SelectedwordRegions, SortedRegions2, 'upper_left', 'true', 'column')
*数数有几个字
count_obj(SortedRegions2, Number)
*开始识别
read_ocr_class_mlp ('G:/2.omc', OCRHandle1)
do_ocr_multi_class_mlp (SortedRegions2, Image1, OCRHandle1, Class, Confidence)
*显示结果
disp_message(WindowHandle, '识别结果:', 'image', 30, 50, 'white', 'false')
for i:=1 to 4 by 1
disp_message(WindowHandle, Class[i-1], 'image', 30, 120+40*i, 'yellow', 'false')
endfor
实验结果
首先准备训练图像和测试图象,我做了两张图
-
训练图:
test1.jpg -
测试图:
test2.jpg
以上程序运行正常的话,会在第一个环节要求标注要训练的字符区域
标注完成后,会自动识别与分割区域:
p4.jpg
导入测试图象后,后面会得到识别结果:
p5.jpg接下来可能会考虑更复杂的文字识别情况,有空再更新。