其他分享
首页 > 其他分享> > halcon学习笔记-ocr识别

halcon学习笔记-ocr识别

作者:互联网

ocr识别

关键:字符分割、字符定位

识别分类器

1.mlp神经网络 2.支持向量机SVM 3.KNN 4.混合高斯模型,将识别目标的特征进行分类、训练,下一次识别的时候符合哪个特征,从而得出识别结果。
特征种类:颜色、形状、概率特征、描述算子特征等等
mlp神经网络:输入层、中间层、输出层
套路:创建分类器,训练分类器
创建分类器之前要进行训练:其中 .trf格式文件包含着用于训练的样本数据和样本名称,trf格式文件在halcon里面是一个训练文件,将图像(区域)和字符进行关联。

示例1:使用mlp神经网络做ocr识别。

* This example program shows how to use a simple MLP OCR classifier
* 
dev_update_off ()
read_image (Image, 'letters')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
dev_set_colored (12)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
get_tmp_dir (TmpDir)
TrainFile := TmpDir + '/letters.trf'
dev_display (Image)
gen_rectangle1 (Rectangle, 0, 0, Height - 1, 400)
reduce_domain (Image, Rectangle, Image)

* Segment the image图像分割
binary_threshold (Image, Region, 'max_separability', 'dark', UsedThreshold)

* Connect the i's and j's with their dots 膨胀扩大区域
dilation_circle (Region, RegionDilation, 3.5)
* Compute the correct connected components 计算出正确的连通域
connection (RegionDilation, ConnectedRegions)
* Reduce each connected component (character) to its original shape
intersection (ConnectedRegions, Region, RegionIntersection)
* Sort the characters line-by-line
sort_region (RegionIntersection, Characters, 'character', 'true', 'row')
dev_display (Characters)
disp_message (WindowHandle, 'Training characters', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* Compute the true class of each character
count_obj (Characters, Number)
Length := Number / 27
Classes := []
for J := 0 to 25 by 1
    Classes := [Classes,gen_tuple_const(Length,chr(ord('a') + J))]
endfor
Classes := [Classes,gen_tuple_const(Length,'.')]
* Construct the necessary training file from the segmented characters
write_ocr_trainf (Characters, Image, Classes, TrainFile)
* Create the classifier.  We read out the classes from the train file.
* Therefore, the training part of the program is generic and can be
* used to train any OCR classifier.
read_ocr_trainf_names (TrainFile, CharacterNames, CharacterCount)
create_ocr_class_mlp (8, 10, 'constant', 'default', CharacterNames, 20, 'canonical_variates', 26, 42, OCRHandle)
* Train the classifier
trainf_ocr_class_mlp (OCRHandle, TrainFile, 100, 0.01, 0.01, Error, ErrorLog)
* 
* Now test the classifier on the whole training image
full_domain (Image, Image)
* Segment characters the same way as before
binary_threshold (Image, Region, 'max_separability', 'dark', UsedThreshold1)
dilation_circle (Region, RegionDilation, 3.5)
connection (RegionDilation, ConnectedRegions)
intersection (ConnectedRegions, Region, RegionIntersection)
sort_region (RegionIntersection, Characters, 'character', 'true', 'row')
* Classification分类
do_ocr_multi_class_mlp (Characters, Image, OCRHandle, Class, Confidence)
* 
* Display results显示结果
area_center (Characters, Area, Row, Column)
dev_display (Image)
set_display_font (WindowHandle, 16, 'sans', 'true', 'false')
for J := 0 to |Row| - 1 by 1
    disp_message (WindowHandle, Class[J], 'image', Row[J] - 16, Column[J] + 8, 'blue', 'false')
endfor
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
disp_message (WindowHandle, 'Classification result', 'window', 12, 12, 'black', 'true')
clear_ocr_class_mlp (OCRHandle)
dev_set_check ('~give_error')
delete_file (TrainFile)
dev_set_check ('give_error')

标签:Image,dev,halcon,笔记,Characters,ocr,true,WindowHandle
来源: https://blog.csdn.net/weixin_51244852/article/details/117406697