案例-文字识别

案例三:文字识别

汉字与字符的识别区别:无法使用halcon内置的分类结果,需要自己训练然后分类。
程序流程:采集图片->预处理(缩减灰度值、滤波、形态学处理)->矫正位置->膨胀、区域划分、求交集->筛选文字->添加训练字符->训练->保存->读取->识别字符

程序代码:

*1.读取图片
dev_close_window()
dev_open_window(0, 0, -1, -1, 'black', WindowHandle)
dev_open_file_dialog('read_image', 'default', 'default', Selection)
read_image (Image,Selection)
mean_image (Image,ImageMean, 5, 5)
rgb1_to_gray(ImageMean, GrayImage)
invert_image(GrayImage, ImageInvert)
*形态增强
scale_image(ImageInvert, ImageScaled, 5.20408, -364)
*2.二值化
threshold(ImageScaled, Regions, 249, 255)
*3.形态画
closing_circle(Regions, RegionClosing,8)
closing_rectangle1(RegionClosing, RegionClosing1, 2,25)
*4.邻域划分
connection(RegionClosing1, ConnectedRegions)
*5.求交集
intersection(ConnectedRegions, Regions, RegionIntersection)
*6.筛选文字
select_shape(RegionIntersection, SelectedRegions, 'area', 'and', 1176.22, 9115.56)
text:=['本','公','司','提','示','吸','烟','有','害','健','康','请','勿','在','禁','场','所']
sort_region(SelectedRegions, SortedRegions, 'character', 'true', 'row')
for Index := 1 to 15 by 1
    select_obj (SortedRegions, ObjectSelected,
Index)
    if(Index==1)
	write_ocr_trainf (ObjectSelected,ImageScaled, text[Index-1], 'train_ocr')
    else
	 append_ocr_trainf (ObjectSelected,ImageScaled, text[Index-1], 'train_ocr')
    endif
endfor
select_obj (SortedRegions, ObjectSelected,17)
append_ocr_trainf (ObjectSelected,ImageScaled, text[16-1], 'train_ocr')
select_obj (SortedRegions, ObjectSelected,18)
append_ocr_trainf (ObjectSelected,ImageScaled, text[17-1], 'train_ocr')
*创建样本
create_ocr_class_svm(8, 10, 'constant', 'default', text, 'rbf', 0.02, 0.05, 'one-versus-one',
'normalization', 10, OCRHandle)
*训练样本
trainf_ocr_class_svm(OCRHandle, 'C:/Users/robot/Desktop/train_ocr.trf', 0.001, 'default')
*保存样本
write_ocr_class_svm(OCRHandle, 'C:/Users/robot/Desktop/1.osc')
*读取训练
read_ocr_class_svm('C:/Users/robot/Desktop/1.osc', OCRHandle1)
*识别字符
do_ocr_multi_class_svm(SortedRegions, ImageScaled, OCRHandle1, Class)
disp_message(WindowHandle, Class, 'window', 0, 0, 'black', 'true')

实验结果:

案例-文字识别_第1张图片图3

你可能感兴趣的:(halcon)