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识别

相关推荐
明湖起风了3 天前
springBoot整合 Tess4J实现OCR识别文字(图片+PDF)
spring boot·pdf·ocr
lrlianmengba4 天前
推荐一款功能强大的光学识别OCR软件:Readiris Dyslexic
ocr
FreeLikeTheWind.4 天前
OCRSpace申请free api流程
ocr
慕容复之巅4 天前
基于一种基于OCR图像识别技术的发票采集管理系统及方法
图像处理·matlab·ocr
紫郢剑侠5 天前
小试银河麒麟系统OCR软件
linux·windows·ocr·银河麒麟系统·文字提取
机器白学5 天前
【论文精读】GOT-OCR2.0源码论文——打破传统OCR流程的多模态视觉-语言大模型架构:预训练VitDet 视觉模型+ 阿里通义千问Qwen语言模型
ocr·论文精读
机器白学5 天前
从零开始使用GOT-OCR2.0——多模态通用型OCR(非常具有潜力的开源OCR项目):项目环境安装配置 + 测试使用
ocr·transformer·多模态·视觉语言大模型
小菠萝09087 天前
Halcon lines_gauss
图像处理·人工智能·计算机视觉·halcon
李楷杰7 天前
PaddlePaddle 开源产业级文档印章识别PaddleX-Pipeline “seal_recognition”模型 开箱即用篇(一)
人工智能·python·开源·ocr·paddlepaddle·印章识别
OCR_wintone4217 天前
易泊车牌识别相机:4S 店的智能之选
人工智能·数码相机·ocr