欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > Halcon OCR字符识别(极坐标转换,字符识别)

Halcon OCR字符识别(极坐标转换,字符识别)

2025/2/23 7:36:07 来源:https://blog.csdn.net/weixin_45875105/article/details/140147271  浏览:    关键词:Halcon OCR字符识别(极坐标转换,字符识别)

Halcon OCR字符识别(极坐标转换,字符识别)

请添加图片描述

代码

* 1.加载图片 ***************************************************
dev_close_window ()
read_image (Image, './img')
get_image_size (Image, Width, Height)
dev_get_window (WindowHandle)
dev_set_colored (12)
dev_set_draw ('margin')
dev_display (Image)* 2.获取字符区域 ***************************************************
rgb1_to_gray (Image, GrayImage)
get_image_size (GrayImage, Width, Height)
threshold (GrayImage, Region, 40, 140)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 13000, 99999)
fill_up (SelectedRegions, RegionFillUp)
opening_circle (RegionFillUp, RegionOpening, 5.5)* 3.字符环形区域 ***************************************************
gen_contour_region_xld (RegionOpening, Contours, 'border')
segment_contours_xld (Contours, ContoursSplit, 'lines_circles', 5, 4, 2)
length_xld (ContoursSplit, Lengths)
LengthIndex := sort_index(Lengths)[|Lengths| - 1]
select_obj(ContoursSplit, ObjectSelected, LengthIndex + 1)fit_circle_contour_xld (ObjectSelected, 'algebraic', -1, 0, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
gen_circle (Circle, Row, Column, Radius)dilation_circle (Circle, RegionDilation, 5.0)
erosion_circle (Circle, RegionErosion, 30)difference (RegionDilation, RegionErosion, RegionDifference)
reduce_domain (GrayImage, RegionDifference, ImageReduced)
stop()* 4.极坐标转换与旋转 ***************************************************
polar_trans_image_ext (ImageReduced, PolarTransImage, Row, Column, \rad(85), rad(280), Radius-100, Radius, \750, 250, 'nearest_neighbor')
rotate_image (PolarTransImage, ImageRotate, 90, 'constant')* 5.获取文本区域 ***************************************************
threshold (ImageRotate, RegionText, 160, 255)
threshold (ImageRotate, RegionBack, 35, 80)
fill_up (RegionBack, RegionBack)
connection (RegionBack, ConnectedRegions1)
select_shape_std (ConnectedRegions1, SelectedBack, 'max_area', 70)intersection (RegionText, SelectedBack, RegionIntersection)
connection (RegionIntersection, ConnectedRegions2)
select_shape (ConnectedRegions2, SelectedTexts, 'area', 'and', 10, 99999)* 6. 获取文本外接矩形***************************************************
opening_circle (SelectedTexts, SelectedTexts, 1.5)
union1 (SelectedTexts, RegionUnion)closing_rectangle1 (RegionUnion, RegionClosing, 80, 3)
fill_up (RegionClosing, RegionFillUp1)
connection (RegionFillUp1, ConnectedRegions3)
shape_trans (ConnectedRegions3, RegionTrans, 'rectangle1')
dilation_rectangle1 (RegionTrans, RegionDilation1, 15, 5)
sort_region (RegionDilation1, SortedRegions, 'first_point', 'true', 'row')invert_image (ImageRotate, ImageInvert)* 7.循环识别字符 ***************************************************
* 读取OCR
read_ocr_class_mlp ('Industrial_0-9A-Z_NoRej.omc', OCRHandle)* 存放结果字典
create_dict (DictHandle)count_obj (SortedRegions, Number)
for Index := 1 to Number by 1* 获取区域内文本select_obj (SortedRegions, SubRect, Index)intersection (SubRect, SelectedTexts, SubRegion)connection (SubRegion, ConnectedRegions4)sort_region (ConnectedRegions4, SortedRegions1, 'first_point', 'true', 'column')* 字符识别do_ocr_multi_class_mlp (SortedRegions1, ImageInvert, OCRHandle, Class, Confidence)area_center (SubRect, Area, RowRect, ColumnRect)tuple_mean (Confidence, MeanConfidence)* 拼接结果Concat := ''if(|Class| == 1)Concat := Classelsefor Index1 := 1 to |Class|by 1Concat := Concat + Class[Index1-1] endforendif* 结果写入字典set_dict_tuple (DictHandle, Index, [RowRect, ColumnRect, Concat, MeanConfidence])endfor* 8.显示结果 ***************************************************
dev_clear_window ()
dev_display (ImageRotate)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
* 循环解析字典
get_dict_param (DictHandle, 'keys', [], AllKeys)
for Index := 0 to |AllKeys| - 1 by 1NextKey := AllKeys[Index]* 获取数据get_dict_param (DictHandle, 'key_data_type', NextKey, NextKeyDataType)get_dict_tuple (DictHandle, NextKey, ResultTuple)* 显示区域RowText := ResultTuple[0] - 15CloumnText := ResultTuple[1] + 20* 显示结果result_msg := ResultTuple[2] + ' ' +   ResultTuple[3] $ '.3f'disp_message (WindowHandle, result_msg, 'image', RowText, CloumnText, 'blue', 'true')endfor

结果显示

请添加图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词