Halcon OCR字符识别(极坐标转换,字符识别)
代码
* 1.加载图片 ***************************************************
dev_close_window ()
read_image (Image,
get_image_size (Image, Width, Height)
dev_get_window (WindowHandle)
dev_set_colored (12)
dev_set_draw (
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,
fill_up (SelectedRegions, RegionFillUp)
opening_circle (RegionFillUp, RegionOpening, 5.5)
* 3.字符环形区域 ***************************************************
gen_contour_region_xld (RegionOpening, Contours,
segment_contours_xld (Contours, ContoursSplit,
length_xld (ContoursSplit, Lengths)
LengthIndex := sort_index(Lengths)[|Lengths| - 1]
select_obj(ContoursSplit, ObjectSelected, LengthIndex + 1)
fit_circle_contour_xld (ObjectSelected,
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,
rotate_image (PolarTransImage, ImageRotate, 90,
* 5.获取文本区域 ***************************************************
threshold (ImageRotate, RegionText, 160, 255)
threshold (ImageRotate, RegionBack, 35, 80)
fill_up (RegionBack, RegionBack)
connection (RegionBack, ConnectedRegions1)
select_shape_std (ConnectedRegions1, SelectedBack,
intersection (RegionText, SelectedBack, RegionIntersection)
connection (RegionIntersection, ConnectedRegions2)
select_shape (ConnectedRegions2, SelectedTexts,
* 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,
dilation_rectangle1 (RegionTrans, RegionDilation1, 15, 5)
sort_region (RegionDilation1, SortedRegions,
invert_image (ImageRotate, ImageInvert)
* 7.循环识别字符 ***************************************************
* 读取OCR
read_ocr_class_mlp (
* 存放结果字典
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,
* 字符识别
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 := Class
else
for Index1 := 1 to |Class|by 1
Concat := Concat + Class[Index1-1]
endfor
endif
* 结果写入字典
set_dict_tuple (DictHandle, Index, [RowRect, ColumnRect, Concat, MeanConfidence])
endfor
* 8.显示结果 ***************************************************
dev_clear_window ()
dev_display (ImageRotate)
set_display_font (WindowHandle, 16,
* 循环解析字典
get_dict_param (DictHandle,
for Index := 0 to |AllKeys| - 1 by 1
NextKey := AllKeys[Index]
* 获取数据
get_dict_param (DictHandle,
get_dict_tuple (DictHandle, NextKey, ResultTuple)
* 显示区域
RowText := ResultTuple[0] - 15
CloumnText := ResultTuple[1] + 20
* 显示结果
result_msg := ResultTuple[2] +
disp_message (WindowHandle, result_msg,
endfor
结果显示