halcon 光学字符识别(Optical Character Recognition)

例程
光学字符识别 letter_mlp.hdev
bottle.hdev 模型
engraved.hdev
ocr_cd_print_polar_trans.hdev 环状
text_line_slant.hdev 倾斜字体
dotprt.hdev 点状倾斜字体

拉开对比度范围
scale_image()
gray_range_rect()

点状字体

text_line_orientation (ROI_0, Image, 25, -0.523599, 0.523599, OrientationAngle)
dots_image (ImageAffineTrans, DotImage, 15, ‘dark’, 0)
*快速二值化 适用于只有亮暗
binary_threshold (ImageReduced, Region, ‘max_separability’, ‘light’, UsedThreshold)
*分开联通的字母
partition_rectangle (RegionTrans, Partitioned, 60, 120)
*与二值化区域求交集(求完后断开部分也默认为同一区域)
intersection (Partitioned, Region, RegionIntersection)

* Image Acquisition 01: Code generated by Image Acquisition 01
list_files ('D:/Documents/Desktop/halcon/OCR', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
dev_close_window ()
read_image (Image1, 'D:/Documents/Desktop/halcon/OCR/1.jpg')
get_image_size (Image1, Width1, Height1)
dev_open_window (0, 0,Width1/3, Height1/3, 'black', WindowHandle)

for Index := 0 to |ImageFiles| - 1 by 1
    read_image (Image, ImageFiles[Index])
    get_image_size (Image, Width, Height)
    gen_rectangle2 (ROI_0, 510.526, 1006.37, rad(4.50491), 467.546, 238.33)
    text_line_orientation (ROI_0, Image, 25, -0.523599, 0.523599, OrientationAngle)
    vector_angle_to_rigid (Height/2, Width/2, OrientationAngle,Height/2, Width/2, 0, HomMat2D)
    affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')
    
    *提取点 第三参数点的直径 适当选大一点
    dots_image (ImageAffineTrans, DotImage, 15, 'dark', 0)
    reduce_domain (DotImage, ROI_0, ImageReduced)
    invert_image (ImageReduced, ImageInvert)
    *快速二值化 适用于只有亮暗
    binary_threshold (ImageReduced, Region, 'max_separability', 'light', UsedThreshold)
    dilation_rectangle1 (Region, RegionDilation, 15, 15)
    connection (RegionDilation, ConnectedRegions)
    shape_trans (ConnectedRegions, RegionTrans, 'rectangle1')
    *分开联通的字母
    partition_rectangle (RegionTrans, Partitioned, 60, 120)
    *与二值化区域求交集(求完后断开部分也默认为同一区域)
    intersection (Partitioned, Region, RegionIntersection)
    sort_region (RegionIntersection, SortedRegions, 'character', 'true', 'row')
    *read_ocr_class_mlp ('DotPrint_NoRej.omc', OCRHandle)
     read_ocr_class_mlp ('D:/Documents/Desktop/halcon/1.omc', OCRHandle)
    do_ocr_multi_class_mlp (SortedRegions, ImageInvert, OCRHandle, Class, Confidence)

    dev_display (ImageAffineTrans)
    count_obj (SortedRegions, Number)
    dev_display (SortedRegions)
    smallest_rectangle1 (SortedRegions, Row1, Column1, Row2, Column2)
    count_obj (SortedRegions, Number1)
    for i := 1 to Number1 by 1
        disp_message (WindowHandle, Class[i-1], 'image', Row1[i-1]+250, Column1[i-1], 'black', 'false')
    endfor
 stop ()   
endfor

在这里插入图片描述

液晶屏

sort_region (RegionIntersection, SortedRegions, ‘character’, ‘false’, ‘column’)
‘character’:所有区域将会被看作在同一行中的字符,然后按照它们在这一 行 中 的顺序排列。如果两个区域水平重叠,会根据它们的列值进行排序,否则将会按照它们的行值进行排序。
确定排列顺序是递增的还是递减的。‘true’:递增;‘false’:递减
smallest_rectangle1 (RegionIntersection, Row1, Column1, Row2, Column2)
set_display_font (WindowID, 20, ‘mono’, ‘true’, ‘false’)
mirror_image (Image1, ImageMirror, ‘row’)
scale_image (GrayImage, ImageScaled, 4.1129, -498)
gen_rectangle2 (Rectangle, 6, 6, rad(45), 1, 0)
closing (RegionUnion, Rectangle, RegionClosing)

*将所识别字体变为同一区域
read_image (Image1, 'D:/Documents/Desktop/halcon/OCR/12.jpg')
dev_close_window ()
get_image_size (Image1, Width, Height)
dev_open_window (0, 0, Width/2,  Height/2, 'black', WindowID)
set_display_font (WindowID, 20, 'mono', 'true', 'false')

mirror_image (Image1, ImageMirror, 'row')
mirror_image (ImageMirror, ImageMirror1, 'column')
rgb1_to_gray (ImageMirror1, GrayImage)
scale_image (GrayImage, ImageScaled, 4.1129, -498)
threshold (GrayImage, Regions, 43, 90)
connection (Regions, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, ['row','column'], 'and', [192.61,212.4], [278.21,286.31])
union1 (SelectedRegions, RegionUnion)
*reduce_domain (GrayImage, RegionUnion, ImageReduced)
gen_rectangle2 (Rectangle, 6, 6, rad(45), 1, 0)
closing (RegionUnion, Rectangle, RegionClosing)
gen_rectangle2 (Rectangle1, 6, 6, rad(135), 1, 0)
closing (RegionClosing, Rectangle1, RegionClosing1)
gen_rectangle2 (Rectangle2, 10, 10, rad(90), 4, 0)
closing (RegionClosing1, Rectangle2, RegionClosing2)
connection (RegionClosing2, ConnectedRegions1)
*得到字符后和原来的区域求交集
intersection (ConnectedRegions1, RegionUnion, RegionIntersection)

read_ocr_class_mlp ('Industrial_0-9A-Z_NoRej.omc', OCRHandle) 
sort_region (RegionIntersection, SortedRegions, 'character', 'false', 'column')
do_ocr_multi_class_mlp (RegionIntersection, GrayImage, OCRHandle, Class, Confidence)

smallest_rectangle1 (RegionIntersection, Row1, Column1, Row2, Column2)
count_obj (RegionIntersection, Number)
dev_display (GrayImage)
for i := 1 to Number by 1
    disp_message (WindowID, Class[i-1], 'image', Row2[i-1], Column1[i-1], 'blue', 'false')
endfor

在这里插入图片描述

环形字体

complement (SelectedRegions, RegionComplement)
smallest_circle (RegionTrans, Row, Column, Radius)
*极坐标转换
polar_trans_image_ext (GrayImage, PolarTransImage, Row, Column, rad(360), 0 , Radius-150, InnerRadius+100, Width, 150, ‘nearest_neighbor’)

*读入图像
read_image (Image11, 'D:/Documents/Desktop/halcon/OCR/11.jpg')
dev_close_window ()
get_image_size (Image11, Width, Height)
dev_open_window (0, 0, Width/5, Height/5, 'black', WindowHandle)
dev_display (Image11)
rgb1_to_gray (Image11, GrayImage)
*提取内外圆的半径
scale_image (GrayImage, ImageScaled, 7.28571, -1144)
threshold (ImageScaled, Regions, 0, 34)
connection (Regions, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 1.76414e+006, 2.30398e+006)

shape_trans (SelectedRegions, RegionTrans, 'outer_circle')
complement (SelectedRegions, RegionComplement)
connection (RegionComplement, ConnectedRegions1)
select_shape (ConnectedRegions1, SelectedRegions1, ['area','circularity'], 'and', [1.02956e+006,0.9762], [1.07584e+006,1])
smallest_circle (RegionTrans, Row, Column, Radius)
smallest_circle (SelectedRegions1, InnerRow, InnerColumn, InnerRadius)
*极坐标转换
polar_trans_image_ext (GrayImage, PolarTransImage, Row, Column, rad(360), 0 , Radius-150, InnerRadius+100, Width, 150, 'nearest_neighbor')

*进行预处理及字符分割,让每一个字符形成连通域
invert_image (PolarTransImage, ImageInvert)
*scale_image (ImageInvert, ImageScaled1, 12.1429, -2161)
*binary_threshold (ImageInvert, Region, 'max_separability', 'light', UsedThreshold)

threshold (ImageInvert, Regions1, 190, 255)
connection (Regions1, ConnectedRegions2)
select_shape (ConnectedRegions2, SelectedRegions2, ['area','row'], 'and', [32.43,28.66], [2000,200])
dilation_rectangle1 (SelectedRegions2, RegionDilation, 8, 8)
union1 (RegionDilation, RegionUnion)
connection (RegionUnion, ConnectedRegions3)
dev_clear_window ()
dev_display (ConnectedRegions3)
intersection (ConnectedRegions3, Regions1, RegionIntersection)
sort_region (RegionIntersection, SortedRegions, 'character', 'true', 'column')
*识别
read_ocr_class_mlp ('Industrial_0-9A-Z_NoRej.omc', OCRHandle)
do_ocr_multi_class_mlp (SortedRegions, ImageInvert, OCRHandle, Class, Confidence)
*显示
count_obj (SortedRegions, Number)
area_center (SortedRegions, Area, Row1, Column1)

for Index := 1 to Number by 1
    disp_message (WindowHandle, Class[Index-1], 'image', 100, Column1[Index-1]-20, 'black', 'true')
endfor

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值