一、基本流程
1.采集图像、提取字符区域
2.读取字库句柄
read_ocr_class_mlp( : : FileName : OCRHandle)
3.识别时注意Halcon提供的字库为白底黑字
读取多个:do_ocr_multi_class_mlp(Character, Image : : OCRHandle : Class, Confidence)
读取单个:do_ocr_single_class_mlp(Character, Image : : OCRHandle, Num : Class, Confidence)
4.清除句柄
clear_ocr_class_mlp (OCRHandle)
二、环形字符识别
需先将环形字符通过极坐标转换到直角坐标系下,
由直角转极坐标:
polar_trans_image_ext (Image, PolarTransImage, Row, Column, 0, 6.28319, OutRadius, InRadius, 23.14OutRadius, 50, ‘nearest_neighbor’)
由极坐标转直角
polar_trans_region_inv (RegionMirror, XYTransRegion, Row, Column, 0, 6.28319, OutRadius, InRadius, 23.14OutRadius, 50, Width, Height, ‘nearest_neighbor’)
三、自训练字符库
1.提取字符区域
2.写入训练文件
write_ocr_trainf(Character, Image : : Class, TrainingFile : )
3.读取训练文件
read_ocr_trainf_names( : : TrainingFile : CharacterNames, CharacterCount)
4.创建分类器
create_ocr_class_mlp( : : WidthCharacter, HeightCharacter, Interpolation, Features, Characters, NumHidden, Preprocessing, NumComponents, RandSeed : OCRHandle)
获取句柄参数
get_params_ocr_class_mlp( : : OCRHandle : WidthCharacter, HeightCharacter, Interpolation, Features, Characters, NumHidden, Preprocessing, NumComponents)
获取句柄特征
get_features_ocr_class_mlp(Character : : OCRHandle, Transform : Features)
5.开始训练
trainf_ocr_class_mlp( : : OCRHandle, TrainingFile, MaxIterations, WeightTolerance, ErrorTolerance : Error, ErrorLog)
6.保存分类器
write_ocr_class_mlp( : : OCRHandle, FileName : )
追加的方式:
append_ocr_trainf (SortedRegions, ImageReduced, names, ‘train_ocr’)
四、Text方式
read_image (Image, ‘./bottle2.png’)
*1.创建模板
create_text_model_reader (‘auto’, ‘Universal_Rej.occ’, TextModel)
*2.设置模板参数
set_text_model_param (TextModel, ‘min_char_width’, 10)
set_text_model_param (TextModel, ‘min_char_height’, 30)
set_text_model_param (TextModel, ‘text_line_structure’, ‘2 2 2’)
set_text_model_param (TextModel, ‘min_stroke_width’, 6)
*3.查找
find_text (Image, TextModel, TextResultID)
*4.获取结果
get_text_result (TextResultID, ‘class’, ResultValue)
dev_get_window (WindowHandle)
set_display_font (WindowHandle, 25, ‘mono’, ‘true’, ‘false’)
disp_message (WindowHandle, sum(ResultValue), ‘window’, 12, 12, ‘black’, ‘true’)
clear_text_model (TextModel)
效果图如下:
五、字符检测OCV
根据图像水平、垂直的灰度投影来检测;
1.创建投影模型
create_ocv_proj (‘A’, OCVHandle)
2 .训练标准
traind_ocv_proj (ImageReduced, OCVHandle, ‘A’, ‘single’)
3.检测
do_ocv_simple (ImageReduced1, OCVHandle, ‘A’, ‘true’, ‘true’, ‘true’, ‘true’, 5, Quality)
4.清除
close_ocv(OCVHandle);