天天看点

halcon基于mlp神经网络分类器的OCR字符识别

OCR字符识别常用流程如下:

1.读取图像

2.预处理

3.图像分割

4.创建字符标识关联图像区域形成.trf文件

5.创建mlp神经网络分类器create_ocr_class_mlp,然后训练

6.保存.omc文件

7.识别

按照如上的流程,通过一张图实现二十六个字母的训练,在另一张图上实现字母的识别,代码部分包含详细的注释,直接贴上代码如下:

dev_close_window ()
*读图
read_image (Image, 'C:/Users/Administrator/Desktop/字母/81i58PICZ89.jpg')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)
*字符分割
rgb1_to_gray (Image, GrayImage)
threshold (GrayImage, Regions, 50, 200)
connection (Regions, ConnectedRegions)
sort_region (ConnectedRegions, SortedRegions, 'character', 'true', 'row')
count_obj (SortedRegions, Number)
*逐个显示确定顺序
for Index := 1 to Number by 1
    dev_clear_window ()
    select_obj (SortedRegions, ObjectSelected, Index)
    dev_display (ObjectSelected)
    stop ()
endfor
*字符标识
word:= ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
*创建训练文件
TrainFile:='D:\\words_A_Z.trf'
*将图像区域与字符标识关联,保存到训练文件
write_ocr_trainf (SortedRegions, Image, word, TrainFile)
*创建OMC文件
FontFlie:='D:\\FontA_Z.omc'
*读取训练文件
read_ocr_trainf_names (TrainFile, CharacterNames, CharacterCount)
*创建神经网络分类器mlp
create_ocr_class_mlp (8, 10, 'constant', 'default', CharacterNames, 80, 'none', 10, 42, OCRHandle)
*训练
trainf_ocr_class_mlp (OCRHandle, TrainFile, 200, 1, 0.01, Error, ErrorLog)
*保存训练结果
write_ocr_class_mlp (OCRHandle, FontFlie)
clear_ocr_class_mlp (OCRHandle)

*文字识别
dev_close_window ()
read_image (Image1, 'C:/Users/Administrator/Desktop/字母/3.jpg')

get_image_size (Image1, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image1)
text_line_orientation (ObjectSelected, Image1, 25, -0.523599, 0.523599, OrientationAngle)
rotate_image (Image1, ImageRotate, OrientationAngle, 'constant')
*字符分割
rgb1_to_gray (ImageRotate, GrayImage1)
threshold (GrayImage1, Regions2, 0, 55)
connection (Regions2, ConnectedRegions1)
select_shape (ConnectedRegions1, SelectedRegions, ['area','height'], 'and', [63.54,18.089], [249.07,20])
sort_region (SelectedRegions, SortedRegions1, 'character', 'true', 'row')
count_obj (SortedRegions1, Number1)
*识别
read_ocr_class_mlp (FontFlie, OCRHandle1)
do_ocr_multi_class_mlp (SortedRegions1, GrayImage1, OCRHandle1, Class, Confidence)
dev_display (Image1)
for j := 1 to Number1 by 1
    select_obj (SortedRegions1, ObjectSelected1, j)
    area_center (ObjectSelected1, Area, Row, Column)
    disp_message (WindowHandle, Class[j-1], 'window', Row+10, Column, 'black', 'true')
    
endfor
           

字符分割

halcon基于mlp神经网络分类器的OCR字符识别

识别结果

halcon基于mlp神经网络分类器的OCR字符识别

源码和图片下载地址:点击打开链接

继续阅读