Halcon基础-OCR识别
- 1、OCR识别素材
- 2、创建路径文件
- 3、Halcon代码实现
- 4、运行效果
- 5、资源获取
1、OCR识别素材
这里我准备了7张不同角度的OCR图片,如下所示:
2、创建路径文件
按照下图所示创建全部文件夹和文件:
01用来存放OCR识别原图
c01 用来存放每张图裁剪出来的OCR区域
OCRText 用来存放每张图识别出来的OCR结果
3、Halcon代码实现
create_text_model_reader ('manual', [], TextModel)
set_text_model_param (TextModel, 'manual_char_width', 14)
set_text_model_param (TextModel, 'manual_char_height', 18)
set_text_model_param (TextModel, 'manual_stroke_width', 1.8)
set_text_model_param (TextModel, 'manual_return_punctuation', 'false')
set_text_model_param (TextModel, 'manual_uppercase_only', 'true')
set_text_model_param (TextModel, 'manual_fragment_size_min', 3)
set_text_model_param (TextModel, 'manual_eliminate_border_blobs', 'true')
set_text_model_param (TextModel, 'manual_base_line_tolerance', 0.2)
set_text_model_param (TextModel, 'manual_max_line_num', 2)
read_ocr_class_mlp ('Industrial_NoRej.omc', OcrHandle)* 设置文件夹路径
FolderPath := 'C:/Users/Administrator/Desktop/盖码测试/01'
* 使用find_files查找所有.jpg文件
list_files (FolderPath, 'files', Files)
* 获取文件的数量
NumFiles := |Files|
* 循环读取每一张图片 for Index := 0 to NumFiles-1 by 1 FileName := Files[Index] read_image(Image, FileName) rgb1_to_gray (Image, GrayImage)emphasize (GrayImage, ImageEmphasize, 10, 10, 18)threshold (ImageEmphasize, Regions, 0, 150)dilation_circle (Regions, RegionDilation, 2)erosion_circle (RegionDilation, RegionErosion, 2)connection (RegionErosion, ConnectedRegions) select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 50, 200)union1 (SelectedRegions, RegionUnion)dilation_circle (RegionUnion, RegionDilation, 22)connection (RegionDilation, ConnectedRegions)select_shape (ConnectedRegions, SelectedRegions, ['area','width','height'], 'and', [8000,30,30], [23000,300,300])*intersection (ImageEmphasize, RegionDilation, RegionIntersection)*reduce_domain (ImageEmphasize, RegionIntersection, ImageReduced)*area_center (RegionIntersection, Area, Row, Column)area_center (SelectedRegions, Area, Row, Column)*orientation_region (RegionDilation, Phi)*椭圆拟合求出旋转角度elliptic_axis_gray (SelectedRegions, ImageEmphasize, Ra, Rb, Phi)hom_mat2d_identity (HomMat2DIdentity)hom_mat2d_rotate (HomMat2DIdentity, -Phi, Row[0], Column[0], HomMat2DRotate)*旋转区域affine_trans_region (SelectedRegions, RegionAffineTrans, HomMat2DRotate, 'nearest_neighbor')*旋转图像affine_trans_image (ImageEmphasize, ImageAffineTrans, HomMat2DRotate, 'constant', 'false')reduce_domain (ImageAffineTrans, RegionAffineTrans, ImageReduced)crop_domain (ImageReduced, Image)write_image (Image, 'png', 0, 'C:/Users/Administrator/Desktop/盖码测试/c01/'+Index+'.png')get_image_size (Image, Width, Height)
gen_rectangle1 (ROI_OCR_01_0, 0, 0,Height, Width)access_channel (Image, TmpObj_Mono, 1)reduce_domain (TmpObj_Mono, ROI_OCR_01_0, TmpObj_MonoReduced_OCR_01_0)TmpCtrl_Orientation := 0TmpCtrl_RangeMin := -0.174533TmpCtrl_RangeMax := 0.174533text_line_orientation (TmpObj_MonoReduced_OCR_01_0, TmpObj_MonoReduced_OCR_01_0, 18, TmpCtrl_Orientation+TmpCtrl_RangeMin, TmpCtrl_Orientation+TmpCtrl_RangeMax, TmpCtrl_Orientation)hom_mat2d_identity (TmpCtrl_MatrixIdentity)hom_mat2d_rotate (TmpCtrl_MatrixIdentity, -TmpCtrl_Orientation, 0, 0, TmpCtrl_MatrixRotation)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, 9)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, 9)expand_domain_gray (TmpObj_ImageTransformed, TmpObj_ImageTransformedExpanded, 9)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)get_text_object (Symbols_OCR_01_0, TmpCtrl_ResultHandle_OCR_01_0, 'manual_all_lines')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)
* 获取元组的第一个元素
FirstSymbol := SymbolNames_OCR_01_0[0]
SecondSymbol := SymbolNames_OCR_01_0[1] if (FirstSymbol!='C' and SecondSymbol!='C')rotate_image (Image, Image, 180, 'constant')access_channel (Image, TmpObj_Mono, 1)reduce_domain (TmpObj_Mono, ROI_OCR_01_0, TmpObj_MonoReduced_OCR_01_0)TmpCtrl_Orientation := 0TmpCtrl_RangeMin := -0.174533TmpCtrl_RangeMax := 0.174533text_line_orientation (TmpObj_MonoReduced_OCR_01_0, TmpObj_MonoReduced_OCR_01_0, 18, TmpCtrl_Orientation+TmpCtrl_RangeMin, TmpCtrl_Orientation+TmpCtrl_RangeMax, TmpCtrl_Orientation)hom_mat2d_identity (TmpCtrl_MatrixIdentity)hom_mat2d_rotate (TmpCtrl_MatrixIdentity, -TmpCtrl_Orientation, 0, 0, TmpCtrl_MatrixRotation)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, 9)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, 9)expand_domain_gray (TmpObj_ImageTransformed, TmpObj_ImageTransformedExpanded, 9)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)get_text_object (Symbols_OCR_01_0, TmpCtrl_ResultHandle_OCR_01_0, 'manual_all_lines')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)endifTxtPath := 'C:/Users/Administrator/Desktop/盖码测试/OCRText'
file_name:=TxtPath+'/'+Index+'.txt'
* 打开文件以写入(如果文件已存在则覆盖)
open_file(file_name, 'output', FileHandle)
* 写入OCR信息
fwrite_string(FileHandle, SymbolNames_OCR_01_0)
*关闭文件
close_file(FileHandle)endfor
4、运行效果
5、资源获取
CSDN 源码下载:Halcon基础-瓶盖OCR识别