Halcon示例:bottlet.hdev 光学字符识别(创建OCR)

时间:2023-03-09 16:35:41
Halcon示例:bottlet.hdev   光学字符识别(创建OCR)

*
* Training of the OCR
* The font is used in "bottle.hdev"
*
*
* Step 0: Preparations
FontName := 'bottle'
*
* Step 1: Segmentation
dev_update_window ('off')
read_image (Bottle, 'bottle2')
get_image_size (Bottle, Width, Height)
dev_close_window ()
dev_open_window (0, 0, 2 * Width, 2 * Height, 'black', WindowID)
dev_display (Bottle)
dev_set_draw ('fill')
set_display_font (WindowID, 27, 'mono', 'true', 'false')
threshold (Bottle, RawSegmentation, 0, 95)
fill_up_shape (RawSegmentation, RemovedNoise, 'area', 1, 5)
*圆形结构开运算 可以平滑区域的边缘和消除小的区域 形态学————Region
opening_circle (RemovedNoise, ThickStructures, 2.5)
fill_up (ThickStructures, Solid)
* dilation_rectangle1 (Solid, RegionDilation, 1, 7)
* erosion_rectangle1 (RegionDilation, RegionErosion, 2, 14)
*矩形结构开运算 形态学————Region
opening_rectangle1 (Solid, Cut, 1, 7)
connection (Cut, ConnectedPatterns)
*区域的交集 Regions区域——————区域设置Sets
intersection (ConnectedPatterns, ThickStructures, NumberCandidates)
select_shape (NumberCandidates, Numbers, 'area', 'and', 300, 9999)
*sort_region(Regions : SortedRegions : SortMode, Order, RowOrCol : ) Regions区域——————区域转换Transformations
*SortMode:排序的类型 Order:true递增 false递减 RowOrCol:行优先或列优先
sort_region (Numbers, FinalNumbers, 'first_point', 'true', 'column')
dev_display (Bottle)
dev_set_color ('green')
dev_set_line_width (2)
*设置区域的输出模式 original(最初的) convex(凸) outer_circle(外接圆) inner_circle(内接圆)
*rectangle1(平行外接矩形) rectangle2(角度矩形) ellipse(椭圆) icon(图标)
dev_set_shape ('rectangle1')
dev_set_draw ('margin')
dev_display (FinalNumbers)
*
* Step2: Training file generation
dev_set_shape ('original')
TrainingNames := ['0','1','0','8','9','4']
TrainingFileName := FontName + '.trf'
sort_region (FinalNumbers, SortedRegions, 'first_point', 'true', 'column')
*转换区域 Regions区域——————区域转换Transformations
shape_trans (SortedRegions, RegionTrans, 'rectangle1')
area_center (RegionTrans, Area, Row, Column)
*等同于tuple_mean (Row, Mean) 平均数
MeanRow := mean(Row)
*设置错误处理 ~give_error忽略错误
dev_set_check ('~give_error')
*前面设置忽略错误,如果TrainingFileName不存在,那么在删除时就不会报错
delete_file (TrainingFileName)
*再设置为默认状态,不忽略错误
dev_set_check ('give_error')
for i := 0 to |TrainingNames| - 1 by 1
select_obj (SortedRegions, CharaterRegions, i + 1)
*字符识别(OCR)——————训练文件(Training Files)
*向训练文件中添加字符
*append_ocr_trainf(Character, Image : : Class, TrainingFile : )
*Character:要添加的训练字符 Image:灰度图像 Class:类名,制定字符的分类 TrainingFile:训练文件,扩展名(.trf .otr)
append_ocr_trainf (CharaterRegions, Bottle, TrainingNames[i], TrainingFileName)
disp_message (WindowID, TrainingNames[i], 'image', MeanRow - 40, Column[i] - 6, 'yellow', 'false')
endfor
*
* Step3: Training
*Tuple_uniq源自(元组Tuple:选择Selection)
*剔除Tuple变量中连续且相同的元素,只保存一个,剩下的元素构成新的Tuple变量返回
CharNames := uniq(sort(TrainingNames))
*归类字符识别OCR——————神经网络Neural Nets
*创建一个用于多层感知器(MLP)的OCR分类器
create_ocr_class_mlp (8, 10, 'constant', 'default', CharNames, 5, 'none', 10, 42, OCRHandle)
*用OCR训练文件中存储的训练字符来训练基于MLP的OCR分类器
trainf_ocr_class_mlp (OCRHandle, TrainingFileName, 200, 1, 0.01, Error, ErrorLog)
*将OCR分类器写入到文件中, 扩展名.omc
write_ocr_class_mlp (OCRHandle, FontName)
*清除由句柄参数OCRHandle指定的由算子create_ocr_class_mlp创建的OCR分类器
clear_ocr_class_mlp (OCRHandle)