光学字符识别是机器视觉领域非常重要的一个应用。机器视觉是集光学、机电和计算机三个领域的一门不算新的技术。此例子用的CNN神经网络算子识别,调用的训练库是系统HDevelop自带的
识别的图片为电表的屏幕截图,用于提取电表中间的一大串数字
要识别的图片:
识别过程
1、阈值处理
mean_image(Image,Mean,15,15)
dyn_threshold(Image,Mean,Region,5,‘dark’)
dev_display (Region)
图片效果:
2、执行代码:
*连接字符
closing_rectangle1 (Region, RegionClosing1, 1, 5)
3、裁剪不要的,将不要的其他区域剪小
opening_circle (RegionClosing1, GuoRegion,2.2)
4、 *筛选区域
在调用select_shape 之前必须要调用连通区域方法connection 才有效果,切记
*计算区域的连通组件
connection (GuoRegion, ConnPatterns)
5、筛选区域
select_shape (ConnPatterns, shapeRegion, ‘area’, ‘and’, 260, 99999)
6、去掉头顶的红杠
*去掉图片头顶的红色横杠
select_shape (shapeRegion, shapeRegion2, ‘width’, ‘and’, 10, 100)
7、求交集,与第一步处理后的区域求交集
*求交集 以区分的颜色模块在前,原字符再后,才能单独著色交集后的单体字符
intersection (shapeRegion2, Region, RegionIntersection)
dev_display(RegionIntersection)
8、最后用cnn卷积神经网络识别
*排序
sort_region (RegionIntersection, SortedRegions, ‘character’, ‘true’, ‘row’)
dev_set_draw (‘margin’)
dev_set_shape (‘rectangle1’)
dev_display (SortedRegions)
shape_trans (SortedRegions, RegionTrans, ‘rectangle1’)
area_center (RegionTrans, Area, Row, Column)
MeanRow := mean(Row)
FontName := ‘Universal_Rej’
count_obj (SortedRegions, Number)
read_ocr_class_cnn (FontName, OCRHandle)
for I := 1 to Number by 1
select_obj (SortedRegions, ObjectSelected, I)
do_ocr_single_class_cnn (ObjectSelected, Image, OCRHandle, 1, Class, Confidence)
dev_disp_text (Class, ‘image’, MeanRow+30, Column[I - 1]-5, ‘green’, ‘box’, ‘false’)
endfor
识别结果:
Halcon HDevelop完整代码:
dev_update_off()
read_image(Image,'d:/Desktop/vision/DBIAO01.png')
get_image_size(Image,Width,Height)
dev_close_window ()
dev_open_window (0, 0, Width*2, Height*2, 'black', WindowHandle)
set_display_font (WindowHandle, 30, 'mono', 'true', 'false')
dev_display (Image)
dev_set_line_width (2)
mean_image(Image,Mean,15,15)
dyn_threshold(Image,Mean,Region,5,'dark')
dev_display (Region)
*clip_region (Region, RawSegmentation, 74.8873, 58.6812, 132.943, 312.194)
*select_shape (Region, SelectedRegions, 'height', 'and',170,99999)
*gen_rectangle1 (ROI_0, 73.2924, 55.8125, 135.814, 312.938)
*连接字符,以9度闭合画圆圈
*closing_circle (CharCandidates, RegionClosing1, 1.5)
closing_rectangle1 (Region, RegionClosing1, 1, 5)
*去除不要的
opening_circle (RegionClosing1, GuoRegion,2.2)
* fill_up (RegionClosing1, Solid)
* opening_rectangle1 (Solid, Cut, 5, 2)
*计算区域的连通组件
connection (GuoRegion, ConnPatterns)
*筛选区域
select_shape (ConnPatterns, shapeRegion, 'area', 'and', 260, 99999)
*去掉图片头顶的红色横杠
select_shape (shapeRegion, shapeRegion2, 'width', 'and', 10, 100)
*求交集 以区分的颜色模块在前,原字符再后,才能单独著色交集后的单体字符
intersection (shapeRegion2, Region, RegionIntersection)
dev_display(RegionIntersection)
*排序
sort_region (RegionIntersection, SortedRegions, 'character', 'true', 'row')
dev_set_draw ('margin')
dev_set_shape ('rectangle1')
dev_display (SortedRegions)
shape_trans (SortedRegions, RegionTrans, 'rectangle1')
area_center (RegionTrans, Area, Row, Column)
MeanRow := mean(Row)
FontName := 'Universal_Rej'
count_obj (SortedRegions, Number)
read_ocr_class_cnn (FontName, OCRHandle)
for I := 1 to Number by 1
select_obj (SortedRegions, ObjectSelected, I)
do_ocr_single_class_cnn (ObjectSelected, Image, OCRHandle, 1, Class, Confidence)
dev_disp_text (Class, 'image', MeanRow+30, Column[I - 1]-5, 'green', 'box', 'false')
endfor