第四-八讲 OCR实战练习
在基于之前的例程分析之上,这里做具体应用,比如,食品包装袋上倾斜的日期识别,温度计上倒着的字符识别等。
倾斜日期识别
首先,对于这样一幅图片,怎样实现对日期的提取?
法一:矫正—分割—识别
第一步:矫正
在上一篇博客(OCR识别字符排列圆形或字体倾斜的处理办法)中我们分析了如何矫正倾斜的字符,这里直接上代码。这里推荐了两种方法实现,最终实现的识别效果都是一样的。
第一种方法,直接将其看做倾斜字符,利用单位矩阵和倾斜角度,保持y
轴固定不动进行仿射得到的图像如下,这种方法得到的图形y
轴是竖直的,但x
轴不是水平的,如果想要x
轴水平,将hom_mat2d_slant
的参数``x改为
’y’`即可。
* 加载图片,注意更改文件名
read_image (Image, 'ImageName')
text_line_slant (Image, Image, 60, rad(-45), rad(45), SlantAngle)
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_slant (HomMat2DIdentity, -SlantAngle, 'x', 0, 0, HomMat2DSlant)
affine_trans_image (Image, ImageAffineTrans, HomMat2DSlant, 'constant', 'false')
第二种方法,利用区域定位gen_rectangle1
和强制转换vector_angle_to_rigid
在几何定位+仿射+车牌识别中有说明,和第一种方法不同的是,这里采用的是旋转变换,固定位置不变的点是(Height/2, Width/2)
,这样得到的仿射变换后的图形x
, y
都是水平或竖直的。代码如下。
get_image_size (Image, Width, Height)
gen_rectangle1 (ROI_0, 258.724, 388.308, 645.626, 1209.03)
text_line_orientation (ROI_0, Image, 75, -0.4, 0.4, OrientationAngle)
vector_angle_to_rigid (Height/2, Width/2, OrientationAngle, Height/2, Width/2, 0, HomMat2D)
affine_trans_image (Image, ImageAffineTrans1, HomMat2D, 'constant', 'false')
第二步:分割
同样分析两种方法,第一种方法是常规操作,由于背景简单,我们直接借助直方图工具做阈值化,然后通过选择区域、腐蚀、膨胀、打散、求交集等一系列操作,由于这一部分在之前的笔记中都多次提到,且对于每个图片的操作都要“因图制宜”,基本思路一样,即把感兴趣的区域想方设法拎出来,并且单个字符属于一个连通域,参数或者步骤不尽相同,这里给出参考程序。
threshold (ImageAffineTrans, Regions, 0, 215)
connection (Regions, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 0, 35498.5)
erosion_circle (SelectedRegions, RegionErosion, 3.5)
dilation_circle (RegionErosion, RegionDilation, 9)
union1 (RegionDilation, RegionUnion)
connection (RegionUnion, ConnectedRegions1)
intersection (ConnectedRegions1, ConnectedRegions, RegionIntersection)
select_shape (RegionIntersection, SelectedRegions1, 'area', 'and', 333.08, 5000)
partition_dynamic (SelectedRegions1, Partitioned, 50, 20)
sort_region (Partitioned, SortedRegions, 'character', 'true', 'row')
第二种方法是考虑到数字是点状的,因此我们调用dots_image
算子直接实现对点状图形的提取。
dots_image (ImageAffineTrans, DotImage, 15, 'dark', 0)
得到