OCR(Optical Character Recognition),光学字符识别,是指使用扫描仪或数码相机等电子设备检查纸上的字符,通过检测暗、亮的方法确定字符的形状,并使用字符识别方法把字符转化为计算机数据的过程;即对文本资料进行扫描,然后对图像文件进行图像处理和分析,最终获取文字的过程。

一、OCR主要步骤:

获取图像、选取感兴趣区域(ROI,Region ofInterest)、图像校正、图像预处理(滤波)、提取分割参数、分割图像等步骤主要运用图像处理技术。训练OCR、读取特征、显示结果、摧毁分类器等步骤主要运用人工神经网络技术。


       可能有些人在图像处理技术里面会有做着做着就不知道下一步该干什么了的问题,其实,我们要知道我们前面应用图像处理技术这一步的目的是什么,无非就是把需要识别的字符分割出来。那么大方向有了,细节其实就没有那么死板了,是很灵活的,只要记住字体有倾斜要校正,后面一系列处理都是为分割服务的,无非就是用二值化、形态学、特征提取、然后分割(点状字体要intersection求一下交集,将点连接起来形成一个连通域)这些套路,至于其中一些具体细节、方法需要大家实战积累,最后交给后面的识别就妥了。

二、模式识别技术

1、训练OCR分为离线处理和在线识别:

离线处理:包括训练字体,将区域代表的字母或数字提取出来并以相应的字符名储存在训练文件中,用来训练字体的训练文件中的内容可以有选择的进行再次修改。
离线处理过程:
1)生成训练文件.trf:每一个字符的大量训练样本存在训练文件.trf 中
注意:图像上每个字体都要形成一个连通域
2)创建训练器:对如颜色、纹理等特征进行分类。Halcon中的分类器主要有多层神经网络(MLP)、支持向量机(SVM) 、K-最邻近 (K-NN)、高斯混合类型(GMM)
3)训练分类器:训练文件.trf+新ocr分类器
4)保存分类器
5)识别字符:do_ocr_multi_class_mlp(一起识别)或 do_ocr_single_class_mlp(逐个识别)
在线识别部分:读取已经训练好的字体文件(.ocm)或Halcon自带的训练文件(.ocm),通过图像预处理将图像中的字符区域提取并分割来,再使用分类器对提取出来的区域进行识别。

三、实战 1:以食品包装袋生产日期为例

dev_close_window ()
dev_open_window (0, 0, 680, 300, 'black', WindowHandle)
*定位
read_image (Image2, 'C:/Users/Administrator/Desktop/2.bmp')
disp_message (WindowHandle, '请绘制ROI区域,我的宝贝', 'window', 12, 12, 'blue', 'true')
draw_rectangle2 (WindowHandle, Row, Column, Phi, Length1, Length2)
gen_rectangle2 (Rectangle, Row, Column, Phi, Length1, Length2)
*校正
area_center (Rectangle, Area, Row1, Column1)
text_line_orientation (Rectangle, Image2, 75, -Phi, Phi, OrientationAngle)
vector_angle_to_rigid (Row1, Column1, OrientationAngle, Row1, Column1, 0, HomMat2D)
affine_trans_image (Image2, ImageAffinTrans, HomMat2D, 'constant', 'false')
affine_trans_region (Rectangle, RegionAffineTrans, HomMat2D, 'nearest_neighbor')
*字符分割
reduce_domain (ImageAffinTrans, RegionAffineTrans, ImageReduced)
dots_image (ImageReduced, DotImage, 15, 'dark', 0)
binary_threshold (DotImage, Region, 'max_separability', 'light', UsedThreshold)
dilation_rectangle1 (Region, RegionClosing, 10, 10)
connection (RegionClosing, ConnectedRegions)
shape_trans (ConnectedRegions, RegionTrans, 'rectangle1')
select_shape (RegionTrans, SelectedRegions, ['height','width'], 'and', [69.184,27.55], [200,200])
partition_rectangle (SelectedRegions, Partitioned, 45, 85)
intersection (Partitioned, Region, RegionIntersection)
*字符识别
sort_region (RegionIntersection, SortedRegions, 'character', 'true', 'row')
read_ocr_class_mlp ('C://Users//Administrator//Desktop//halcon程序//包装袋生产日期识别//DotPrint_0-9A-Z.omc', OCRHandle)
do_ocr_multi_class_mlp (SortedRegions, ImageReduced, OCRHandle, Class, Confidence)
*字符显示
dev_display (ImageAffinTrans)
count_obj (SortedRegions, Number)
smallest_rectangle1 (SortedRegions, Row11, Column1, Row2, Column21)
for i := 1 to  Number by 1disp_message (WindowHandle, Class[i - 1], 'image', Row2[i - 1]+120, Column1[i - 1], 'blue', 'false')
endfor

实战 2:表盘数字

dev_close_window ()
dev_open_window (0, 0, 680, 320, 'black', WindowHandle)
set_display_font (WindowHandle, 40, 'mono', 'true', 'false')
read_image (Image2, 'C:/Users/Administrator/Desktop/halcon程序/温度计表盘识别/字符.bmp')
mirror_image (Image2, ImageMirror, 'row')
mirror_image (ImageMirror, ImageMirror1, 'column')
*1、预处理
scale_image (ImageMirror1, ImageScaled, 4.39655, -853)
*2、Blob分析定位
threshold (ImageScaled, Regions, 0, 42)
connection (Regions, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, ['area','width','height'], 'and', [0,621.9,408.06], [459711,2000,542.36])
reduce_domain (ImageMirror1, SelectedRegions, ImageReduced)
*3、字符分割
threshold (ImageReduced, Regions1, 67, 129)
connection (Regions1, ConnectedRegions1)
select_shape (ConnectedRegions1, SelectedRegions1, ['roundness','height','ra','rb'], 'and', [-0.8673,17.86,34.18,0.663], [0.6755,125,435.2,34.541])
union1 (SelectedRegions1, RegionUnion)
*法一:
*gen_rectangle2 (Rectangle, 16, 16, rad(145), 7, 7)
*closing (RegionUnion, Rectangle, RegionClosing)
*connection (RegionClosing, ConnectedRegions2)
*intersection ( ConnectedRegions2, Regions1, RegionIntersection)
*法二
dilation_rectangle1 (RegionUnion, RegionDilation, 10, 10)
connection (RegionDilation, ConnectedRegions2)
intersection ( ConnectedRegions2, Regions1, RegionIntersection)
*4、字符识别
sort_region (RegionIntersection, SortedRegions, 'character', 'true', 'row')read_ocr_class_mlp ('Industrial_0-9A-Z_NoRej.omc', OCRHandle)
do_ocr_multi_class_mlp (SortedRegions, ImageMirror1, OCRHandle, Class, Confidence)
*5、显示
dev_display (ImageMirror1)
count_obj (SortedRegions, Number)
smallest_rectangle1 (SortedRegions, Row11, Column1, Row2, Column21)
for i := 1 to  Number by 1disp_message (WindowHandle, Class[i - 1], 'image', Row2[i - 1], Column1[i - 1], 'blue', 'false')
endfor

实战 3:环形字符


*1、读取图像
read_image (Image11, 'C:/Users/Administrator/Desktop/halcon程序/环形字符识别/1.1.bmp')
dev_close_window ()
dev_open_window (0, 0, 480, 480, 'black', WindowHandle)
dev_display (Image11)
get_image_size (Image11, Width, Height)
*2、环形字符校正
rgb1_to_gray (Image11, GrayImage)
scale_image (GrayImage, ImageScaled, 1, 0)
*求环形外圆区域
threshold (ImageScaled, Regions, 0, 106)
shape_trans (Regions, RegionTrans, 'outer_circle')
*求环形内圆区域
complement (Regions, RegionComplement)
opening_circle (RegionComplement, RegionOpening, 8)
connection (RegionOpening, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 1.37755e+006, 2.29592e+006)
*求内外圆尺寸(圆心坐标和半径)
*smallest_circle——一个区域周围的最小外接圆
*内圆
smallest_circle (SelectedRegions, Row, Column, Radius)
*外圆
smallest_circle (RegionTrans, Row1, Column1, Radius1)
*极坐标转换
polar_trans_image_ext (GrayImage, PolarTransImage, Row, Column, rad(360), 0, Radius1-100,  Radius+40,1440,100, 'nearest_neighbor')
*3、预处理+字符分割
invert_image (PolarTransImage, ImageInvert)
scale_image (ImageInvert, ImageScaled1, 5.54348, -1147)
binary_threshold (ImageScaled1, Region, 'max_separability', 'light', UsedThreshold)
connection (Region, ConnectedRegions1)select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 67.15, 5000)
union1 (SelectedRegions1, RegionUnion)
dilation_rectangle1 (RegionUnion, RegionDilation, 5, 5)connection (RegionDilation, ConnectedRegions2)
intersection (ConnectedRegions2, Region, RegionIntersection)
sort_region (RegionIntersection, SortedRegions, 'character', 'true', 'column')
*4、字符识别
read_ocr_class_mlp ('Industrial_0-9A-Z_NoRej.omc', OCRHandle)
do_ocr_multi_class_mlp (SortedRegions, PolarTransImage, OCRHandle, Class, Confidence)
*5、显示
dev_display (PolarTransImage)
set_display_font (WindowHandle, 40, 'mono', 'true', 'false')
count_obj (SortedRegions, Number)
smallest_rectangle1 (SortedRegions, Row11, Column1, Row2, Column21)
for i := 1 to  Number by 1disp_message (WindowHandle, Class[i - 1], 'image', Row2[i - 1]+10, Column1[i - 1], 'blue', 'false')
endfor

核心思想:先进行极坐标转换,把环形区域拉直,后面套路一样,字符分割、提取、识别
关键代码:将图像直角坐标系转换成极坐标系函数
polar_trans_image_ext(Image :输入图像
                                              PolarTransImage :输出图像
                                              Row, 环形区域中心点y坐标
                                              Column, 环形区域中心点x坐标
                                              AngleStart, 要拉直的环形区域起始角度(与x轴正方向)
                                              AngleEnd, 要拉直的环形区域终点角度
                                              RadiusStart,要拉直的环形区域内圆与原点的距离
                                              RadiusEnd, 要拉直的环形区域外圆与原点的距离
                                              Width, Height, 输出图像的宽(一般1440)、高(一般字符高度)
                                              Interpolation : 变换的差值算法)

注:因为直接用的halcon训练好的文件,里面并没有训练&,所以会显示8

总结:一般我们拿到图像后,一般都需要对目标区域进行校正处理,也就是几何变换,几何变换种类有:

极坐标转换:针对环形字符,将原图像直角坐标转换成极坐标,拉直polar_trans_image_ext()
zoom_image_factor()
仿射变换:针对倾斜字体进行旋转(位移、缩放)等校正处理
投射变换:针对图像可能不是垂直拍摄的,发生了深度变化、垂直于图像方向发生倾斜时使用

————————————————
版权声明:本文为CSDN博主「四夕文_」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43197380/article/details/90932775

Halcon OCR字符识别相关推荐

  1. halcon光学字符识别(训练后识别),验证码识别

    识别效果 halcon光学字符识别 使用的halcon版本21.05 先训练模型,标记图片对应的字符,训练生成分类器 * * Training of the OCR * The font is use ...

  2. labview 如何实现ocr字符识别?

    labview自带vision算法库包含了ocr字符识别,与halcon的区别是需要自己建立字库,今天我们来看看如何利用labview视觉助手建立字库. ocr工具建立字库 设置参考坐标系 建立新的字 ...

  3. 通过labview vision视觉模块写的带学习功能的OCR字符识别程序

    labview vision视觉模块写的带学习功能的OCR字符识别程序 为解决在用Labview做OCR字符识别视觉开发中每次都要用开发工具自带的学习模块,自己用VDM函数自己写了个子程序,方便嵌入带 ...

  4. ocr字符识别_Google的光学字符识别(OCR)软件适用于248种以上的语言

    ocr字符识别 Google的光学字符识别 (OCR)软件现在可用于248多种世界语言(包括所有主要的南亚语言). 它非常简单易用,并且可以检测大多数语言,且准确性超过90%. 该技术从图像中提取文本 ...

  5. 应用OpenCV进行OCR字符识别

    opencv自带一个字符识别的例子,它的重点不是OCR字符识别,而主要是演示机器学习的应用.它应用的是UCI提供的字符数据(特征数据). DAMILES在网上发布了一个应用OpenCV进行OCR的例子 ...

  6. OCR字符识别,支持手写字体识别,中英文识别

    OCR字符识别,支持手写字体识别,中英文识别,基于深度学习,轻量级网络,检测进度高,支持python开发 编号:4199652712767280盐市口会玩的川乌

  7. 【深度学习】使用transformer实现OCR字符识别

    目录 1.数据集相关操作 1.1标签最长字符个数统计 1.2char和id的映射字典构建 1.3数据集图像尺寸分析 2.将transformer引入OCR 2.1准备工作 2.2数据集创建 3.模型构 ...

  8. 基于python的OCR字符识别

    官方网站:https://github.com/tesseract-ocr/tesseract 官方文档:https://github.com/tesseract-ocr/tessdoc 语言包地址: ...

  9. [Halcon识别] OCR字符识别

    OCR(Optical Character Recognition),光学字符识别,是指使用扫描仪或数码相机等电子设备检查纸上的字符,通过检测暗.亮的方法确定字符的形状,并使用字符识别方法把字符转化为 ...

最新文章

  1. Linux shell 学习笔记(8)— 使用结构化命令(if-then 语句、数值比较、字符串比较、文件比较、case 语句)
  2. 1月23日服务器例行维护更新公告,1月24日服务器例行维护公告(已完成)
  3. 第十一天-《企业应用架构模式》-对象-关系行为模式
  4. IB component change - CL_IBCOMPONENT_IL~CHANGE_COMPONENT
  5. 常用公有云接入——谷歌
  6. 小票上为啥指甲能划出印_指甲上出现竖纹,除遗传问题,或是身体在向你拉警报了,别忽视...
  7. 简单的一个月之设计实现内容1
  8. 算法总结之 生成窗口的最大值数组
  9. vueRouter使用心得
  10. 【考古向翻译】Pwn2Own 2010 Windows 7 Internet Explorer 8 exploit
  11. java.lang.Object android.content.Context.getSystemService(java.lang.String)‘ on a null object
  12. tp5 ThinkPHP5 自定义异常处理类
  13. 深入理解Core Data
  14. udp广播收到重复包
  15. .net导出Excel设置颜色font.ColorIndex 各颜色编号
  16. Canvas坐标轴中的Y轴距离是X轴的两倍
  17. C语言小知识——uthash使用
  18. win7设置html,Windows 7自动备份设置图解 设置Win7系统备份方式
  19. 看完这套书才发现,以前的四大名著都白看了!
  20. 【陈工笔记】# 微信小程序实现的基础步骤速记,持续更新关键词 #

热门文章

  1. (基于Packet Tracer 的校园网络设计方案(计算机网络与课程实验)(三)——Run Time 观察报文传输过程
  2. 谷歌浏览器插件-jsonView插件
  3. C语言100以内的素数
  4. 小白用户也能看懂的卷积神经网络5
  5. 2019河南对口升学高考试卷计算机专业课,2019年河南省对口升学:考试和录取
  6. 如何赢得数学游戏(博弈问题)
  7. platform总线(Linux驱动开发篇)
  8. HEVC的软解码和硬解码
  9. ***常用工具下载大全
  10. 基于SSH开发网上零食销售系统的设计与实现