halcon第十八讲:环形字符识别

对于水平的字符很好识别,通过分割字符,创建训练文件,创建分类器,读入训练文件,训练分类器,识别字符,或者直接使用助手可快速实现ocr识别。对于环形字符,最关键的一步就是先将环拉直,即将直角坐标转换为极坐标,比如在直角坐标中的一个圆,在极坐标中就被拉直成了一条直线。

要识别的环形字符图片如下:

read_image (Image, '1.png')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width/2, Height/2, 'black', WindowHandle)
dev_display (Image)
*均值滤波
mean_image (Image, ImageMean, 3, 3)
*灰度范围,结果为掩码矩形内灰度最大值减最小值
gray_range_rect (ImageMean, ImageResult, 8, 8)
threshold (ImageResult, Regions, 40, 138)
connection (Regions, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 31134.7, 50000)
*生成区域的轮廓
gen_contour_region_xld (SelectedRegions, Contours, 'border')
fit_circle_contour_xld (Contours, 'algebraic', -1, 0, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
gen_circle_contour_xld (ContCircle, Row, Column, Radius, 0, 6.28318, 'positive', 1)
gen_circle (Circle, Row, Column, Radius-60)
gen_circle (Circle1, Row, Column, Radius-5)
difference (Circle1, Circle, RegionDifference)

dev_open_window (0, 0, 900, 60, 'black', WindowHandle1)
*直角坐标转极坐标
polar_trans_image_ext (Image, PolarTransImage, Row, Column, 0, 6.28319, Radius-60, Radius-5, 900, 60, 'bilinear')
rotate_image (PolarTransImage, ImageRotate, 180, 'constant')

*以下是通过助手实现光学字符识别并自动生成的代码
create_text_model_reader ('manual', [], TextModel)
set_text_model_param (TextModel, 'char_width', 17)
set_text_model_param (TextModel, 'char_height', 26)
set_text_model_param (TextModel, 'stroke_width', 5.9)
set_text_model_param (TextModel, 'return_punctuation', 'false')
set_text_model_param (TextModel, 'uppercase_only', 'true')
set_text_model_param (TextModel, 'fragment_size_min', 17)
set_text_model_param (TextModel, 'eliminate_border_blobs', 'true')
set_text_model_param (TextModel, 'base_line_tolerance', 0.2)
set_text_model_param (TextModel, 'max_line_num', 1)

read_ocr_class_mlp ('Industrial_0-9A-Z_NoRej.omc', OcrHandle)

gen_rectangle1 (ROI_OCR_01_0, 2.77273, 86.9906, 31.1364, 290.457)
* OCR 01: 
* OCR 01: Extract symbol regions (segmentation step)
* OCR 01: Only consider first channel for color images
access_channel (ImageRotate, TmpObj_Mono, 1)
reduce_domain (TmpObj_Mono, ROI_OCR_01_0, TmpObj_MonoReduced_OCR_01_0)

TmpCtrl_Orientation := 0
* OCR 01: Corrected for text line orientation
TmpCtrl_RangeMin := -0.174533
TmpCtrl_RangeMax := 0.174533
text_line_orientation (TmpObj_MonoReduced_OCR_01_0, TmpObj_MonoReduced_OCR_01_0, 26, TmpCtrl_Orientation+TmpCtrl_RangeMin, TmpCtrl_Orientation+TmpCtrl_RangeMax, TmpCtrl_Orientation)
* OCR 01: Build rotation matrix
hom_mat2d_identity (TmpCtrl_MatrixIdentity)
hom_mat2d_rotate (TmpCtrl_MatrixIdentity, -TmpCtrl_Orientation, 0, 0, TmpCtrl_MatrixRotation)
* OCR 01: Apply transformation to image and domain
get_domain (TmpObj_MonoReduced_OCR_01_0, TmpObj_Domain)
get_system ('clip_region', TmpCtrl_ClipRegion)
set_system ('clip_region', 'false')
dilation_circle (TmpObj_Domain, TmpObj_DomainExpanded, 29)
affine_trans_region (TmpObj_DomainExpanded, TmpObj_DomainTransformedRaw, TmpCtrl_MatrixRotation, 'true')
smallest_rectangle1 (TmpObj_DomainTransformedRaw, TmpCtrl_Row1, TmpCtrl_Col1, TmpCtrl_Row2, TmpCtrl_Col2)
hom_mat2d_translate (TmpCtrl_MatrixIdentity, -TmpCtrl_Row1, -TmpCtrl_Col1, TmpCtrl_MatrixTranslation)
hom_mat2d_compose (TmpCtrl_MatrixTranslation, TmpCtrl_MatrixRotation, TmpCtrl_MatrixComposite)
affine_trans_region (TmpObj_Domain, TmpObj_DomainTransformed, TmpCtrl_MatrixComposite, 'true')
affine_trans_image (TmpObj_MonoReduced_OCR_01_0, TmpObj_ImageTransformed, TmpCtrl_MatrixComposite, 'constant', 'true')
dilation_circle (TmpObj_Domain, TmpObj_DomainExpanded, 29)
expand_domain_gray (TmpObj_ImageTransformed, TmpObj_ImageTransformedExpanded, 29)
reduce_domain (TmpObj_ImageTransformed, TmpObj_DomainTransformed, TmpObj_ImageTransformedReduced)
crop_part (TmpObj_ImageTransformedReduced, TmpObj_MonoReduced_OCR_01_0, 0, 0, TmpCtrl_Col2-TmpCtrl_Col1+1, TmpCtrl_Row2-TmpCtrl_Row1+1)
set_system ('clip_region', TmpCtrl_ClipRegion)
find_text (TmpObj_MonoReduced_OCR_01_0, TextModel, TmpCtrl_ResultHandle_OCR_01_0)

* OCR 01: Read text (classification step)
get_text_object (Symbols_OCR_01_0, TmpCtrl_ResultHandle_OCR_01_0, 'all_lines')
clear_text_result (TmpCtrl_ResultHandle_OCR_01_0)
dev_display (TmpObj_MonoReduced_OCR_01_0)
dev_set_draw ('fill')
dev_set_colored (3)
dev_display (Symbols_OCR_01_0)
do_ocr_multi_class_mlp (Symbols_OCR_01_0, TmpObj_MonoReduced_OCR_01_0, OcrHandle, SymbolNames_OCR_01_0, Confidences_OCR_01_0)

dev_display (ImageRotate)
set_display_font (WindowHandle1, 16, 'mono', 'true', 'false')
for i:=0 to |SymbolNames_OCR_01_0|-1 by 1
    disp_message (WindowHandle1, SymbolNames_OCR_01_0[i], 'image', 30, 10*i+88, 'white', 'false')
endfor
clear_text_model (TextModel)
clear_ocr_class_mlp (OcrHandle)

 运行结果如下:

猜你喜欢

转载自blog.csdn.net/qq_24946843/article/details/82254103