Halcon基础-瓶盖带角度的OCR批量识别

Halcon基础-OCR识别


1、OCR识别素材

这里我准备了7张不同角度的OCR图片,如下所示:

2、创建路径文件

按照下图所示创建全部文件夹和文件:
01 用来存放OCR识别原图
c01 用来存放每张图裁剪出来的OCR区域
OCRText 用来存放每张图识别出来的OCR结果



3、Halcon代码实现

csharp 复制代码
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 := 0
    TmpCtrl_RangeMin := -0.174533
    TmpCtrl_RangeMax := 0.174533
    text_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 := 0
    TmpCtrl_RangeMin := -0.174533
    TmpCtrl_RangeMax := 0.174533
    text_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)
   endif
   
TxtPath := '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识别

相关推荐
西瓜本瓜@1 天前
在Android开发中如何使用OCR获取当前屏幕中的文本?
android·java·开发语言·智能手机·ocr
陈煜的博客1 天前
python识别ocr 图片和pdf文件
python·pdf·ocr
思通数科大数据舆情2 天前
OCR、语音识别与信息抽取:免费开源的AI平台在医疗领域的创新应用
人工智能·目标检测·机器学习·计算机视觉·数据挖掘·ocr·语音识别
小菠萝09082 天前
Halcon OCR 字体训练
ocr
懂你如我丶2 天前
【TextIn:开源免费的AI智能文字识别产品(通用文档智能解析识别、OCR识别、文档格式转换、篡改检测、证件识别等)】
人工智能·深度学习·开源·ocr
图片转成excel表格3 天前
如何在线将驾驶证转为结构化excel?
人工智能·深度学习·ocr
思通数据4 天前
开源OCR免费助力法律文档数字化,提升文档管理效率
大数据·人工智能·深度学习·目标检测·计算机视觉·数据挖掘·ocr
思通数科x4 天前
法律文件智能识别:免费OCR平台优化数字化管理
大数据·人工智能·安全·目标检测·计算机视觉·数据挖掘·ocr
vvw&4 天前
如何在服务器端对PDF和图像进行OCR处理
linux·运维·服务器·ubuntu·pdf·ocr
翔云API4 天前
excel表格文字识别-ocr表格文字提取api接口集成-python
ocr·excel