halcon之ocr识别(个人总结)

标题halcon解决ocr问题的总体思路

一:读取图片,并且对图片进行矫正
二:找到想要进行ocr识别的字符,并且把他们区域话(重点也是难点)
三:进行ocr识别

一:读取图片,并且对图片进行矫正

1.读取图片相对简单,总体思路是读取图片并且设计合适的窗口大小,代码如下:

dev_update_off ()//关闭窗口更新read_image (Image, 'letters')//读取图片,letters为图片名称dev_close_window ()//关闭窗口get_image_size (Image, Width, Height)//获取图片宽高dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)//打开适合图片大小的窗口dev_set_draw ('fill')//设计区域填充方式dev_set_colored(12)//区域颜色set_display_font (WindowHandle, 16, 'mono', 'true', 'false')//设置窗口显示字体dev_display (Image)//在窗口上显示图片

2.对图片进行矫正,总体有两种图片需要校正,以下分别讲解

(1)倾斜的图片

校正思路:
1.先求一个区域

gen_rectangle1 (ROI_0, 267.5, 381.5, 607.5, 1129.5)

2。根据刚刚所求区域获得倾斜角度

text_line_orientation (ROI_0, Image, 25, -0.523599, 0.523599, OrientationAngle)

3.仿射变换
4.仿射变换作用到图像上

affine_trans_image (Image, ImageAffinTrans, HomMat2D, 'constant', 'false')

对待第3步仿射变换通常有两种方法:
(1)运用旋转矩阵调用hom_mat2d_identity()和hom_mat2d_rotate()两个函数。

//求单位矩阵hom_mat2d_identity (HomMat2DIdentity)//根据单位矩阵求旋转矩阵hom_mat2d_rotate (HomMat2DIdentity, -OrientationAngle, Px,Py, HomMat2DRotate)

(2)直接进行校正,调用vector_angle_to_rigid()函数。

vector_angle_to_rigid (Width/4, Height/4, OrientationAngle, Width/4, Height/4, 0, HomMat2D)

(2)一个圆环上的给它拉直

校正思路:
1.找到目标区域并且求出它的内外接圆的半径

threshold (GrayImage, Regions, 9, 71)//找到目标区域shape_trans (Regions, OuterCircle, 'outer_circle')//求出外接圆complement (Regions, RegionComplement)connection (RegionComplement, ConnectedRegions)select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 1.26527e+006, 2.26003e+006)//内接圆区域smallest_circle (Regions, Row, Column, Radius)//外接圆半径和原点坐标smallest_circle (SelectedRegions, InRow, InColumn, InRadius)//内接圆半径和原点坐标

2.拉直图像

polar_trans_image_ext (GrayImage, PolarTransImage, Row, Column, rad(60), rad(-300), Radius-5, InRadius+5, 1440, 100, 'nearest_neighbor')

三:找到目标区域

找寻目标区域的方法很灵活,没有办法通用,只能以目标(找到想要进行ocr识别的字符)为导向去进行操作。下面是halcon的ocr实例教程的解法,大家可以参考一下

binary_threshold (Image, Region, 'max_separability', 'dark', UsedThreshold)//阈值化* Connect the i's and j's with their dotsdilation_circle (Region, RegionDilation, 3.5)//膨胀* Compute the correct connected componentsconnection (RegionDilation, ConnectedRegions)//区域化* Reduce each connected component (character) to its original shapeintersection (ConnectedRegions, Region, RegionIntersection)* Sort the characters line-by-linesort_region (RegionIntersection, Characters, 'character', 'true', 'row')dev_display (Characters)disp_message (WindowHandle, 'Training characters', 'window', 12, 12, 'black', 'true')disp_continue_message (WindowHandle, 'black', 'true')stop ()

说明:这个很灵活,大家可以多参考halcon的实例教程,作为一名halcon新手,我还没有能力来写(主要见的也不多)

四:进行ocr识别

识别步骤:
1.读取分类器。
2.进行ocr识别

1.读取分类器有两种方式,一种是读取自己生成分类器,另外一种是直接读取系统中的分类器。下边分别对其讲解。

(1)自己生成分类器

*(1)生成trf文件*初始化准备写入trf文件的内容count_obj (Characters, Number)length:=Number/27Classes:=[]for Index := 0 to 25 by 1    Classes:=[Classes,gen_tuple_const(length,chr(ord('a')+Index))]endforClasses:=[Classes,gen_tuple_const(length,'.')]*(2)将内容导入到trf文件夹中,trf文件夹目录已经提前初始化write_ocr_trainf (Characters, ImageReduced, Classes, TrainFile)*(3)读取文件内容,为了创建分类器的时候提供要分类的字符read_ocr_trainf_names (TrainFile, CharacterNames, CharacterCount)*(4)创建分类器create_ocr_class_mlp (8, 10, 'constant', 'default', CharacterNames, 20, 'canonical_variates', 26, 42, OCRHandle)*(5)训练分类器 trainf_ocr_class_mlp (OCRHandle, TrainFile, 100, 0.01, 0.01, Error, ErrorLog)*找完整的目标区域full_domain (ImageReduced, ImageFull)binary_threshold (ImageFull, Region1, 'max_separability', 'dark', UsedThreshold1)dilation_circle (Region1, RegionDilation1, 3.5)connection (RegionDilation1, ConnectedRegions1)intersection (ConnectedRegions1, Region1, RegionIntersection1)sort_region (RegionIntersection1, Characters, 'character', 'true', 'row')

(2)调用系统的分类器

read_ocr_class_mlp ('Industrial_0-9A-Z_Rej.omc', OCRHandle)

1.进行ocr识别有两种方式。

(1)多个识别

do_ocr_multi_class_mlp (SortedRegions, ImageAffinTrans, OCRHandle, Class, Confidence)

(2)单个识别

for Index := 1 to Number by 1    select_obj (SortedRegions, ObjectSelected, Index)    do_ocr_single_class_mlp (ObjectSelected, ImageInvert, OCRHandle, 1, Class, Confidence)    disp_message (WindowHandle, Class, 'image', MeanRow, Column[Index-1], 'yellow', 'false')endfor
(0)

相关推荐