AI智能
改变未来

OCR之 gen_training_file.hdev

*创建OCR训练文件

  • gen_training_file.hdev: create a training file for OCR
  • 关闭窗体更新
    dev_update_off ()
    *关闭窗体
    dev_close_window ()
  • ****第一步,获取图像
  • step1: acquire image

*读取图像
read_image (Image, ‘numbers_scale’)
*获取图像指针
get_image_pointer1 (Image, Pointer, Type, Width, Height)
*打开窗体
dev_open_window (0, 0, Width, Height, ‘black’, WindowID)
*使图像适应窗体大小
dev_set_part (0, 0, Height – 1, Width – 1)
*设置线宽
dev_set_line_width (2)
*设置显示颜色
dev_set_color (‘yellow’)
*设置填充方式
dev_set_draw (‘margin’)
*显示图像
dev_display (Image)
*右下角显示’Press F5 to continue’
disp_continue_message (WindowID, ‘black’, ‘true’)
stop ()

*第二步 定义要识别的字符

  • step2: specify character names

*字符为0-9
Chars := [‘0’,‘1’,‘2’,‘3’,‘4’,‘5’,‘6’,‘7’,‘8’,‘9’]
*总共有5行文字可以训练

  • index of the line used for the training (1…5)
    *定义训练的行数为第三行
    TrainingLine := 3
    *右下角显示’Press F5 to continue’
    disp_continue_message (WindowID, ‘black’, ‘true’)
    stop ()

*分割图像

  • step3: segment image

*与之分割
threshold (Image, Region, 0, 125)
*以矩形掩码进行闭运算
closing_rectangle1 (Region, RegionClosing, 70, 10)
*联通区域
connection (RegionClosing, Lines)
*选择第三行为训练目标
select_obj (Lines, Training, TrainingLine)
*求出要训练的字符区域
intersection (Training, Region, TrainingChars)
*把分割出来的字符联通分割开
connection (TrainingChars, ConnectedRegions)
*根据字符位置排序
sort_region (ConnectedRegions, SortedRegions, ‘first_point’, ‘true’, ‘column’)

  • visualize result
    *显示排序后的区域
    dev_display (SortedRegions)
    *显示训练图像
    dev_display (Training)
    *求训练目标行的最小外接矩形
    smallest_rectangle1 (TrainingChars, Row1, Column1, Row2, Column2)
    RowROI1 := Row1 – 5
    ColROI1 := Column1 – 5
    RowROI2 := Row2 + 5
    ColROI2 := Column2 + 5
    ZoomFactor := 2
    *打开新窗体
    dev_open_window (20, 0, (ColROI2 – ColROI1) * ZoomFactor, (RowROI2 – RowROI1) * ZoomFactor, ‘black’, WindowHandleZoom)
    *设置新窗体为选择行大小
    dev_set_part (round(RowROI1), round(ColROI1), round(RowROI2), round(ColROI2))
    *显示训练行
    dev_display (Image)
    *显示排序后的区域
    dev_display (SortedRegions)
    disp_continue_message (WindowID, ‘black’, ‘true’)
    stop ()

*第四步,把区域写入训练文件

  • step4: write regions into training file
  • ****训练文件名称
    TrainFile := ‘numbers.trf’
  • clear file first
    *不显示是错误
    dev_set_check (’~give_error’)
    *删除文件先
    delete_file (TrainFile)
    *显示错误
    dev_set_check (‘give_error’)
    *一个一个字符和区域的添加到训练文件
    for i := 1 to 10 by 1
    select_obj (SortedRegions, TrainSingle, i)
    append_ocr_trainf (TrainSingle, Image, Chars[i – 1], TrainFile)
    endfor
    *右下角显示’Press F5 to continue’
    disp_continue_message (WindowID, ‘black’, ‘true’)
    stop ()
    dev_close_window ()
    *有了这个文件后,就可以直接拿来用了
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » OCR之 gen_training_file.hdev