步骤:
1.图片的采集,通过halcon连接相机进行采集
2.图片中文字区域的获取
3.旋转区域,对旋转之前的区域和旋转之前的image进行仿射变换。
4.缩减图片,只要识别文字的图像定义域。
5.将每个文字进行分割,分割成独立的连通域。
6.形成trf文件,让字符图像和字符(中文)相关联。
7.创建神经网络分类器
8.训练分类器,并形成omc文件,用来识别中文。
9.识别中文,并显示。
代码如下:
read_image (Image, 'C:/Users/jayyy/Desktop/Image.hobj')
* 降低维度,rgb彩色图片转化成灰度图像
rgb1_to_gray (Image, GrayImage)
********* 2.预处理
scale_image (GrayImage, ImageScaled, 1.25, -62)
threshold (GrayImage, Regions, 103, 254)
closing_circle (Regions, RegionOpening, 3.5)
connection (Regions, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, ['area','column','column1','column2','phi'], 'and', [0,481.75,473.81,521.43,-1.1889], [500000,2000,2000,2000,2])
union1 (SelectedRegions, RegionUnion)
connection (RegionUnion, ConnectedRegions1)
select_shape (ConnectedRegions1, SelectedRegions1, ['row','column','column'], 'and', [0,0,0], [336.9,927.78,927.78])
union1 (SelectedRegions1, RegionUnion1)
******** 区域旋转,摆正
* 将区域矩形化
shape_trans (RegionUnion1, RegionTrans, 'rectangle2')
* 获取角度
orientation_region (RegionTrans, Phi)
* 获取中心点
area_center (RegionTrans, Area, Row, Column)
* 基于中心点偏移
vector_angle_to_rigid (Row, Column, Phi, Row, Column, rad(180), HomMat2D)
* 仿射变换--->>>>
affine_trans_region (RegionTrans, RegionAffineTrans, HomMat2D, 'nearest_neighbor')
affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')
* 缩减图像定义域,获取想要的区域
reduce_domain (ImageAffineTrans, RegionAffineTrans, ImageReduced)
rgb1_to_gray (ImageReduced, GrayImage1)
* 增加亮度
scale_image_max (GrayImage1, ImageScaleMax)
threshold (ImageScaleMax, Regions1, 121, 255)
* 膨胀
dilation_rectangle1 (Regions1, RegionDilation, 5, 8)
* 形成特定的连通域
connection (RegionDilation, ConnectedRegions2)
* 两个区域取交集
intersection (ConnectedRegions2, Regions1, RegionIntersection)
sort_region (RegionIntersection, SortedRegions, 'first_point', 'true', 'column')
count_obj (SortedRegions, Number)
*************** 3. 形成trf文件, 字符图像和字符关联
words:=['无', '止', '境', '追', '求', '一', '流', '品', '质']
Trainfile:='train_ocr'
* 遍历各个字符图像
for i:=1 to Number by 1
select_obj (SortedRegions, SingleWord, i)
append_ocr_trainf (SingleWord, ImageAffineTrans, words[i-1], 'train_ocr')
endfor
* 读取trf文件
read_ocr_trainf_names (Trainfile, CharacterNames, CharacterCount)
****************** 4.神经网络
* 创建分类器
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)
* 形成omc文件
write_ocr_class_mlp (OCRHandle, 'word0')
* 清除句柄
clear_ocr_class_mlp (OCRHandle)
* 识别文字
read_ocr_class_mlp ('word0', OCRHandle1)
do_ocr_multi_class_mlp (SortedRegions, ImageReduced, OCRHandle1, Class, Confidence)