Halcon OCR字符识别(极坐标转换,字符识别)

news/2024/7/8 1:34:20 标签: ocr

Halcon OCR字符识别(极坐标转换,字符识别)

请添加图片描述

代码

* 1.加载图片 ***************************************************
dev_close_window ()
read_image (Image, './img')
get_image_size (Image, Width, Height)
dev_get_window (WindowHandle)
dev_set_colored (12)
dev_set_draw ('margin')
dev_display (Image)

* 2.获取字符区域 ***************************************************
rgb1_to_gray (Image, GrayImage)
get_image_size (GrayImage, Width, Height)
threshold (GrayImage, Region, 40, 140)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 13000, 99999)
fill_up (SelectedRegions, RegionFillUp)
opening_circle (RegionFillUp, RegionOpening, 5.5)

* 3.字符环形区域 ***************************************************
gen_contour_region_xld (RegionOpening, Contours, 'border')
segment_contours_xld (Contours, ContoursSplit, 'lines_circles', 5, 4, 2)
length_xld (ContoursSplit, Lengths)
LengthIndex := sort_index(Lengths)[|Lengths| - 1]
select_obj(ContoursSplit, ObjectSelected, LengthIndex + 1)

fit_circle_contour_xld (ObjectSelected, 'algebraic', -1, 0, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
gen_circle (Circle, Row, Column, Radius)

dilation_circle (Circle, RegionDilation, 5.0)
erosion_circle (Circle, RegionErosion, 30)

difference (RegionDilation, RegionErosion, RegionDifference)
reduce_domain (GrayImage, RegionDifference, ImageReduced)
stop()

* 4.极坐标转换与旋转 ***************************************************
polar_trans_image_ext (ImageReduced, PolarTransImage, Row, Column, \
                       rad(85), rad(280), Radius-100, Radius, \
                       750, 250, 'nearest_neighbor')
rotate_image (PolarTransImage, ImageRotate, 90, 'constant')


* 5.获取文本区域 ***************************************************
threshold (ImageRotate, RegionText, 160, 255)
threshold (ImageRotate, RegionBack, 35, 80)
fill_up (RegionBack, RegionBack)
connection (RegionBack, ConnectedRegions1)
select_shape_std (ConnectedRegions1, SelectedBack, 'max_area', 70)

intersection (RegionText, SelectedBack, RegionIntersection)
connection (RegionIntersection, ConnectedRegions2)
select_shape (ConnectedRegions2, SelectedTexts, 'area', 'and', 10, 99999)

* 6. 获取文本外接矩形***************************************************
opening_circle (SelectedTexts, SelectedTexts, 1.5)
union1 (SelectedTexts, RegionUnion)

closing_rectangle1 (RegionUnion, RegionClosing, 80, 3)
fill_up (RegionClosing, RegionFillUp1)
connection (RegionFillUp1, ConnectedRegions3)
shape_trans (ConnectedRegions3, RegionTrans, 'rectangle1')
dilation_rectangle1 (RegionTrans, RegionDilation1, 15, 5)
sort_region (RegionDilation1, SortedRegions, 'first_point', 'true', 'row')

invert_image (ImageRotate, ImageInvert)

* 7.循环识别字符 ***************************************************
* 读取OCR
read_ocr_class_mlp ('Industrial_0-9A-Z_NoRej.omc', OCRHandle)

* 存放结果字典
create_dict (DictHandle)

count_obj (SortedRegions, Number)
for Index := 1 to Number by 1
    * 获取区域内文本
    select_obj (SortedRegions, SubRect, Index)
    intersection (SubRect, SelectedTexts, SubRegion)
    
    connection (SubRegion, ConnectedRegions4)
    sort_region (ConnectedRegions4, SortedRegions1, 'first_point', 'true', 'column')
    * 字符识别
    do_ocr_multi_class_mlp (SortedRegions1, ImageInvert, OCRHandle, Class, Confidence)
    
    area_center (SubRect, Area, RowRect, ColumnRect)
    tuple_mean (Confidence, MeanConfidence)
    * 拼接结果
    Concat := ''
    if(|Class| == 1)
        Concat := Class
    else
        for Index1 := 1 to |Class|by 1
            Concat := Concat + Class[Index1-1] 
        endfor
    endif
    * 结果写入字典
    set_dict_tuple (DictHandle, Index, [RowRect, ColumnRect, Concat, MeanConfidence])
    
endfor


* 8.显示结果 ***************************************************
dev_clear_window ()
dev_display (ImageRotate)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
* 循环解析字典
get_dict_param (DictHandle, 'keys', [], AllKeys)
for Index := 0 to |AllKeys| - 1 by 1
    
  NextKey := AllKeys[Index]
  * 获取数据
  get_dict_param (DictHandle, 'key_data_type', NextKey, NextKeyDataType)
  get_dict_tuple (DictHandle, NextKey, ResultTuple)
  
  * 显示区域
  RowText := ResultTuple[0] - 15
  CloumnText := ResultTuple[1] + 20
  * 显示结果
  result_msg := ResultTuple[2] + ' ' +   ResultTuple[3] $ '.3f'
  disp_message (WindowHandle, result_msg, 'image', RowText, CloumnText, 'blue', 'true')

endfor

结果显示

请添加图片描述


http://www.niftyadmin.cn/n/5535909.html

相关文章

Django权限系统如何使用?

Django的权限系统是一个强大而灵活的特性,允许你控制不同用户对应用程序中资源的访问。以下是使用Django权限系统的几个基本步骤: 1. 定义模型权限 在你的models.py文件中,你可以为每个模型定义自定义权限。这通过在模型的Meta类里设置perm…

nginx的重定向(rewrite)

nginx的重定向(rewrite) location 匹配 location匹配的就是后面的URI /wordpress 192.168.60.20/wordpress location匹配的分类和优先级 1、精确匹配 location / 对字符串进行完全匹配,必须完全符合 2、正则匹配 ^~ 前缀匹配&#x…

Grafana面试题精选和参考答案

目录 Grafana是什么以及它的主要应用场景 Grafana支持的数据源 Grafana的体系结构及主要组件 Grafana如何实现数据的可视化和监控 Grafana支持的图表类型 如何在Grafana中创建和编辑仪表盘 Grafana的查询编辑器功能 Grafana支持的认证方式 Grafana的性能调优建议 Gra…

网络爬虫(一)深度优先爬虫与广度优先爬虫

1. 深度优先爬虫:深度优先爬虫是一种以深度为优先的爬虫算法。它从一个起始点开始,先访问一个链接,然后再访问该链接下的链接,一直深入地访问直到无法再继续深入为止。然后回溯到上一个链接,再继续深入访问下一个未被访…

【笔试记录】华为 | 20230823 | cpp

获取连通的相邻节点列表 题目描述 在网元内,存在了 N 个转发节点,每个转发节点有自己唯一的标识 TB 且每个节点有 M 个端口,节点间通过端口进行报文通讯。出于业务隔离的需求,服务器内的端口被划分为多个通讯平面(用 VLAN 隔离&…

HandlerMethodArgumentResolver :深入spring mvc参数解析机制

❃博主首页 &#xff1a; <码到三十五> ☠博主专栏 &#xff1a; <mysql高手> <elasticsearch高手> <源码解读> <java核心> <面试攻关> ♝博主的话 &#xff1a; 搬的每块砖&#xff0c;皆为峰峦之基&#xff1b;公众号搜索(码到三十…

go Channel原理 (三)

Channel 设计原理 不要通过共享内存的方式进行通信&#xff0c;而是应该通过通信的方式共享内存。 在主流编程语言中&#xff0c;多个线程传递数据的方式一般都是共享内存。 Go 可以使用共享内存加互斥锁进行通信&#xff0c;同时也提供了一种不同的并发模型&#xff0c;即通…

【你真的了解double和float吗】

&#x1f308;个人主页&#xff1a;努力学编程’ ⛅个人推荐&#xff1a;基于java提供的ArrayList实现的扑克牌游戏 |C贪吃蛇详解 ⚡学好数据结构&#xff0c;刷题刻不容缓&#xff1a;点击一起刷题 &#x1f319;心灵鸡汤&#xff1a;总有人要赢&#xff0c;为什么不能是我呢 …