Blob分析之bottlet.hdev
* OCR训练
* Training of the OCR
*字体在"bottle.hdev"项目中用过
* The font is used in "bottle.hdev"
*
* 第0步:准备工作
* Step 0: Preparations
FontName := 'bottle'
* 第1步:分割
* 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)
*设置显示字体
set_display_font (WindowID, 27, 'mono', 'true', 'false')
*阈值分割
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)
*根据区域第一个点排列区域
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)
* 第二部生成训练文件
* Step2: Training file generation
*训练字符名称
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)
*设置出现错误的处理方式~give_error忽略错误
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: Training
*定义据提字符
CharNames := uniq(sort(TrainingNames))
*创建OCR句柄
create_ocr_class_mlp (8, 10, 'constant', 'default', CharNames, 10, 'none', 10, 42, OCRHandle)
*训练OCR
trainf_ocr_class_mlp (OCRHandle, TrainingFileName, 200, 1, 0.01, Error, ErrorLog)
*保存训练好的OCR文件,下次可以直接用read_ocr就可以用
write_ocr_class_mlp (OCRHandle, FontName)
*清除OCR举殡占用的内存
clear_ocr_class_mlp (OCRHandle)

浙公网安备 33010602011771号