OCR识别-字符和背景很小差别的字符识别

OCR识别中,某些图像中的字符和背景之间的亮暗差别很小,需要进行处理,将字符区域从图像中扣出来进行识别。

* 
* This example describes one step from the semiconductor product chain.
* In the front-end-of-line step, the ICs are printed on the wafer. To
* tag a single wafer from the production life line, each wafer receives
* an ID number, printed with the SEMI font. This ID number is read here.
* 
* 读图像
dev_update_off ()
dev_close_window ()
read_image (Image, 'ocr/wafer_semi_font_01')
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
dev_set_draw ('margin')
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_line_width (2)
dev_set_colored (12)
* 
* 读指定分类器
read_ocr_class_mlp ('SEMI', OCRHandle)
NumImages := 10
for Index := 1 to NumImages by 1
    * 
    * Segment characters
    * 读入一张图像
    read_image (Image, 'ocr/wafer_semi_font_' + Index$'02')
    * Characters must be black-on-white, i.e., dark characters on a light background
    * 翻转图像,目前是黑底白字,而字符识别,需要的是白底黑字
    * 此翻转将图像变为白底黑字
    invert_image (Image, ImageInvert)
    * 下面两个算子一般一块使用
    * 对于mean_image的参数mask的大小,一般取需要的目标的大小的2倍;如目标是个圆形,直径为10,则mask的大小一般取20
    * 对于一些目标与背景的阈值对比不明显的图像,需要用到动态阈值分割,使用局部阈值来分割图像
    * 均值滤波
    * 对于背景和前景之间差别不大的图像,可以使用如下两个算子进行处理
    mean_image (Image, ImageMean, 31, 31)
    * 阈值处理,ImageMean一般为通过均值滤波处理之后的区域
    dyn_threshold (Image, ImageMean, RegionDynThresh, 7, 'light')
    * Characters are often dotted. Therefore, we first merge close dots
    * that belong to the same character just before calling the operator connection
    *闭运算,让一个字符能成为一个连通域,然后再进行connection计算,得到单个字符的连通域
    closing_circle (RegionDynThresh, RegionClosing, 2.0)
    connection (RegionClosing, ConnectedRegions)
    * Filter out characters based on two facts:
    * 1. Characters are printed in SEMI-12. Therefore we can make strong assumptions
    *    on the dimensions of the characters
    * 2. Characters are printed along a straight line
    
    * 通过特征直方图,将字符的区域提取出来,
    * 第一步提取使用的特征是宽和高
    * 第二步提取使用的特征是行坐标
    select_shape (ConnectedRegions, SelectedRegions1, ['height','width'], 'and', [29,15], [60,40])
    area_center (SelectedRegions1, Area, RowCh, ColumnCh)
    MedianRow := median(RowCh)
    select_shape (SelectedRegions1, Chars, 'row', 'and', MedianRow - 30, MedianRow + 30)
    * 
    * Read out segmented characters
    * 字符区域排序,同时获得各个字符的方形区域
    sort_region (Chars, CharsSorted, 'character', 'true', 'column')
    * 将字符区域转成矩形区域
    shape_trans (CharsSorted, Characters, 'rectangle1')
    * 字符识别
    do_ocr_multi_class_mlp (Characters, ImageInvert, OCRHandle, Class, Confidence)
    * 
    * 显示识别出来的字符
    dev_display (ImageInvert)
    dilation_rectangle1 (Characters, RegionDilation, 7, 7)
    dev_display (RegionDilation)
    area_center (CharsSorted, Area1, Row, Column)
    MeanRow := mean(Row)
    for IndexL := 0 to |Class| - 1 by 1
        disp_message (WindowHandle, Class[IndexL], 'image', MeanRow + 40, Column[IndexL] - 20, 'black', 'true')
    endfor
    if (Index != NumImages)
        disp_continue_message (WindowHandle, 'black', 'true')
        stop ()
    endif
endfor

* 释放分类器资源
clear_ocr_class_mlp (OCRHandle)

进行OCR识别的图像,如下图所示:

可以看到,这种图像的字符和背景之间的差别很小,我们需要将字符区域从这样的图像中提取出来。

在对图像进行预处理和Blob分析中,针对此类图像,有如下两个算子是比较关键的:

* 对于背景和前景之间差别不大的图像,可以使用如下两个算子进行处理
mean_image (Image, ImageMean, 31, 31)
* 阈值处理,ImageMean一般为通过均值滤波处理之后的区域
dyn_threshold (Image, ImageMean, RegionDynThresh, 7, 'light')

通过预处理和Blob分析,可以得到如下字符区域:

将以上字符区域放入字符识别算子,进行识别,得到的结果如下:

 

更多halcon代码分析,见如下公众号:

 

 

 

 

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值