* 第0步,准备
FontName := ‘bottle‘
read_image (Bottle, ‘bottle2‘)
* 第1步,分割
*二值化
threshold (Bottle, RawSegmentation, 0, 95)
.
*填充形状
fill_up_shape (RawSegmentation, RemovedNoise, ‘area‘, 1, 5)
opening_circle (RemovedNoise, ThickStructures, 2.5)
fill_up (ThickStructures, Solid)
opening_rectangle1 (Solid, Cut, 1, 7)
connection (Cut, ConnectedPatterns)
intersection (ConnectedPatterns, ThickStructures, NumberCandidates)
select_shape (NumberCandidates, Numbers, ‘area‘, ‘and‘, 300, 9999) *选择后的区域仅包括6个字符区域
sort_region (Numbers, FinalNumbers, ‘first_point‘, ‘true‘, ‘column‘) *排序,注意排序方式有多种
dev_display (Bottle)
dev_set_color (‘green‘)
dev_set_line_width (2)
dev_set_shape (‘rectangle1‘)
dev_set_draw (‘margin‘)
dev_display (FinalNumbers)
*
* 第2步,生成训练文件
TrainingNames := [‘0‘,‘1‘,‘0‘,‘8‘,‘9‘,‘4‘] *训练标签名
TrainingFileName := FontName + ‘.trf‘ *训练文件名
sort_region (FinalNumbers, SortedRegions, ‘first_point‘, ‘true‘, ‘column‘) * 又排序???
shape_trans (SortedRegions, RegionTrans, ‘rectangle1‘) *转换为矩形区域
area_center (RegionTrans, Area, Row, Column) * 获得矩形区域的面积及中心位置
MeanRow := mean(Row) *求行中心值
dev_set_check (‘~give_error‘)
delete_file (TrainingFileName)
dev_set_check (‘give_error‘)
for i := 0 to |TrainingNames| - 1 by 1
select_obj (SortedRegions, CharaterRegions, i + 1) *依次选择对象并加入训练文件
append_ocr_trainf (CharaterRegions, Bottle, TrainingNames[i], TrainingFileName)
disp_message (WindowID, TrainingNames[i], ‘image‘, MeanRow - 40, Column[i] - 6, ‘yellow‘, ‘false‘)
endfor
*
* Step3: 训练
CharNames := uniq(sort(TrainingNames))
create_ocr_class_mlp (8, 10, ‘constant‘, ‘default‘, CharNames, 5, ‘none‘, 10, 42, OCRHandle)
trainf_ocr_class_mlp (OCRHandle, TrainingFileName, 200, 1, 0.01, Error, ErrorLog)
write_ocr_class_mlp (OCRHandle, FontName)
clear_ocr_class_mlp (OCRHandle)