Halcon OCR,自定义训练与识别

一、准备图像与字符

图像:

字符:

words := ['K','F','R','-','3','5','W','/','H','3','X','X','1']

二、图像预处理

*准备训练的字符
words := ['K','F','R','-','3','5','W','/','H','3','X','X','1']
*训练文件
TrainFile := 'C:/Users/Administrator/Desktop/OCR/testword2.trf'
*字符文件
FontFile := 'C:/Users/Administrator/Desktop/OCR/testword2.omc'
*读取图像
read_image (Image, 'Image00035')
*获取图像尺寸
get_image_size (Image, Width, Height)
*打开新的窗口
dev_open_window (0, 0, Width/3, Height/3, 'black', WindowHandle)
*显示图像
dev_display (Image)
*绘制ROI
draw_rectangle2 (WindowHandle, Row1, Column1, Phi, Length1, Length2)
*创建ROI
gen_rectangle2 (ROI_0, Row1, Column1, Phi, Length1, Length2)
*裁剪图像
reduce_domain (Image, ROI_0, ImageReduced)
*本地二值化
local_threshold (ImageReduced, Region, 'adapted_std_deviation', 'dark', [], [])
*膨胀
dilation_rectangle1 (Region, RegionDilation, 5, 5)
*打断连通域
connection (RegionDilation, ConnectedRegions)
*根据形状特征选择区域
select_shape (ConnectedRegions, SelectedRegions, ['area','ratio'], 'and', [100,0.2], [10000,6])
*统计对象数量
count_obj (SelectedRegions, Number)
*对区域排序
sort_region (SelectedRegions, SortedRegions1, 'character', 'true', 'row')

三、字符分割

*创建空对象
gen_empty_obj (rectangle1s)
*遍历获取字符,分割
for Index := 0 to Number by 1
    if (Index==Number)
        
    else
        select_obj (SortedRegions1, ObjectSelected, Index+1)
        currntchar := words[Index]       
        shape_trans (ObjectSelected, RegionTrans, 'rectangle1')
        concat_obj (rectangle1s, RegionTrans, rectangle1s)
        if (false)
            *将分割的字符与训练字符一一对应添加到训练文件
            append_ocr_trainf (ObjectSelected, Image, currntchar, TrainFile) 
        endif
    endif     
endfor

四、创建与训练MLP OCR分类器

*训练ocr,训练时将条件置为true
if (false)
    *查询训练文件中存储的字符
    read_ocr_trainf_names (TrainFile, CharacterNames, CharacterCount)
    *创建MLP字符分类器
    create_ocr_class_mlp (8, 10, 'constant', 'default', CharacterNames, 80, 'none', 10, 42, OCRHandle)
    *训练分类器
    trainf_ocr_class_mlp (OCRHandle, TrainFile, 200, 1, 0.01, Error, ErrorLog)
    *将训练的字符写入到文件
    write_ocr_class_mlp (OCRHandle, FontFile) 
endif

五、OCR

*读取ocr
read_ocr_class_mlp (FontFile, OCRHandle1)
*创建ROI
gen_rectangle2 (ROI_0, Row1, Column1, Phi, Length1, Length2)
*裁剪图像
reduce_domain (Image, ROI_0, ImageReduced1)
*本地二值化
local_threshold (ImageReduced1, Region, 'adapted_std_deviation', 'dark', 'scale', 0.1)
*打断连通域
connection (Region, ConnectedRegions)
*膨胀
dilation_rectangle1 (ConnectedRegions, RegionDilation, 5, 5)
*根据形状特征选择区域
select_shape (RegionDilation, SelectedRegions, ['area','ratio'], 'and', [100,0.2], [10000,6])
*统计对象数量
count_obj (SelectedRegions, Number)
*对区域排序
sort_region (SelectedRegions, SortedRegions, 'character', 'true', 'row')
*识别
do_ocr_multi_class_mlp (SortedRegions, ImageReduced1, OCRHandle1, Class, Confidence)

六、清除MLP分类器

*清除句柄
clear_ocr_class_mlp (OCRHandle1)

七、显示检测结果

dev_display (Image)
*显示
for Index1 := 1 to |Class| by 1
    select_obj (SortedRegions, ObjectSelected1, Index1)
    area_center (ObjectSelected1, Area, Row, Column)
    gen_rectangle1 (Rectangle, Row-55, Column-12.5, Row-30, Column+12.5)
    dev_disp_text (Class[Index1-1], 'image', Row-42.5, Column, 'green', 'box', 'false')
endfor
stop ()
dev_display (Image)
dev_set_draw ('margin')
dev_display (rectangle1s)

八、完整代码

*准备训练的字符
words := ['K','F','R','-','3','5','W','/','H','3','X','X','1']
*训练文件
TrainFile := 'C:/Users/Administrator/Desktop/OCR/testword2.trf'
*字符文件
FontFile := 'C:/Users/Administrator/Desktop/OCR/testword2.omc'
*读取图像
read_image (Image, 'Image00035')
*获取图像尺寸
get_image_size (Image, Width, Height)
*打开新的窗口
dev_open_window (0, 0, Width/3, Height/3, 'black', WindowHandle)
*显示图像
dev_display (Image)
*绘制ROI
draw_rectangle2 (WindowHandle, Row1, Column1, Phi, Length1, Length2)
*创建ROI
gen_rectangle2 (ROI_0, Row1, Column1, Phi, Length1, Length2)
*裁剪图像
reduce_domain (Image, ROI_0, ImageReduced)
*本地二值化
local_threshold (ImageReduced, Region, 'adapted_std_deviation', 'dark', [], [])
*膨胀
dilation_rectangle1 (Region, RegionDilation, 5, 5)
*打断连通域
connection (RegionDilation, ConnectedRegions)
*根据形状特征选择区域
select_shape (ConnectedRegions, SelectedRegions, ['area','ratio'], 'and', [100,0.2], [10000,6])
*统计对象数量
count_obj (SelectedRegions, Number)
*对区域排序
sort_region (SelectedRegions, SortedRegions1, 'character', 'true', 'row')
stop()
*创建空对象
gen_empty_obj (rectangle1s)
*遍历获取字符,分割
for Index := 0 to Number by 1
    if (Index==Number)
        
    else
        select_obj (SortedRegions1, ObjectSelected, Index+1)
        currntchar := words[Index]       
        shape_trans (ObjectSelected, RegionTrans, 'rectangle1')
        concat_obj (rectangle1s, RegionTrans, rectangle1s)
        if (false)
            *将分割的字符与训练字符一一对应添加到训练文件
            append_ocr_trainf (ObjectSelected, Image, currntchar, TrainFile) 
        endif
    endif     
endfor
stop()
*训练ocr
if (false)
    *查询训练文件中存储的字符
    read_ocr_trainf_names (TrainFile, CharacterNames, CharacterCount)
    *创建MLP字符分类器
    create_ocr_class_mlp (8, 10, 'constant', 'default', CharacterNames, 80, 'none', 10, 42, OCRHandle)
    *训练分类器
    trainf_ocr_class_mlp (OCRHandle, TrainFile, 200, 1, 0.01, Error, ErrorLog)
    *将训练的字符写入到文件
    write_ocr_class_mlp (OCRHandle, FontFile) 
endif




*读取ocr
read_ocr_class_mlp (FontFile, OCRHandle1)
*创建ROI
gen_rectangle2 (ROI_0, Row1, Column1, Phi, Length1, Length2)
*裁剪图像
reduce_domain (Image, ROI_0, ImageReduced1)
*本地二值化
local_threshold (ImageReduced1, Region, 'adapted_std_deviation', 'dark', 'scale', 0.1)
*打断连通域
connection (Region, ConnectedRegions)
*膨胀
dilation_rectangle1 (ConnectedRegions, RegionDilation, 5, 5)
*根据形状特征选择区域
select_shape (RegionDilation, SelectedRegions, ['area','ratio'], 'and', [100,0.2], [10000,6])
*统计对象数量
count_obj (SelectedRegions, Number)
*对区域排序
sort_region (SelectedRegions, SortedRegions, 'character', 'true', 'row')
*识别
do_ocr_multi_class_mlp (SortedRegions, ImageReduced1, OCRHandle1, Class, Confidence)
*清除句柄
clear_ocr_class_mlp (OCRHandle1)
dev_display (Image)
*显示
for Index1 := 1 to |Class| by 1
    select_obj (SortedRegions, ObjectSelected1, Index1)
    area_center (ObjectSelected1, Area, Row, Column)
    gen_rectangle1 (Rectangle, Row-55, Column-12.5, Row-30, Column+12.5)
    dev_disp_text (Class[Index1-1], 'image', Row-42.5, Column, 'green', 'box', 'false')
endfor
stop ()
dev_display (Image)
dev_set_draw ('margin')
dev_display (rectangle1s)

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HeliosXxzh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值