HALCON从入门到入门-环形一维码识别,极坐标转换

测试效果

处理步骤

读取图片

dev_update_off ()

get_system ('clip_region', Information)

set_system ('clip_region', 'true')

read_image (Image, 'circular_barcode')

get_image_size (Image, Width, Height)

dev_close_window ()

dev_open_window (0, 0, Width / 2, Height / 2, 'black', WindowHandle)

dev_set_colored (12)

dev_display (Image)

set_display_font (WindowHandle, 14, 'mono', 'true', 'false')

stop ()

一系列区域处理 得到中间黑色圆环区域

threshold (Image, Region, 0, 100)

closing_circle (Region, Region, 3.5)

connection (Region, ConnectedRegions)

select_shape (ConnectedRegions, Ring, ['width','height'], 'and', [550,550], [750,750])

shape_trans (Ring, OuterCircle, 'outer_circle')

complement (Ring, RegionComplement)

connection (RegionComplement, ConnectedRegions)

select_shape (ConnectedRegions, InnerCircle, ['width','height'], 'and', [450,450], [650,650])

smallest_circle (Ring, Row, Column, OuterRadius)

smallest_circle (InnerCircle, InnerRow, InnerColumn, InnerRadius)

dev_set_color ('green')

dev_set_draw ('margin')

dev_set_line_width (3)

dev_display (Image)

dev_display (OuterCircle)

dev_display (InnerCircle)

stop ()

对这个区域的图像进行极坐标转换

WidthPolar := 1440

HeightPolar := round(OuterRadius - InnerRadius - 10)

polar_trans_image_ext (Image, PolarTransImage, Row, Column, rad(360), 0, OuterRadius - 5, InnerRadius + 5, WidthPolar, HeightPolar, 'bilinear')

invert_image (PolarTransImage, ImageInvert)

创建一维码识别模型,对图像中的一维码进行识别

zoom_image_factor (ImageInvert, ImageZoomed, 1, 2, 'weighted')

create_bar_code_model ([], [], BarCodeHandle)

set_bar_code_param (BarCodeHandle, 'element_size_min', 1.5)

set_bar_code_param (BarCodeHandle, 'meas_thresh', 0.3)

find_bar_code (ImageZoomed, SymbolRegions, BarCodeHandle, 'Code 128', DecodedDataStrings)

dev_set_window_extents (-1, -1, WidthPolar / 2, HeightPolar)

dev_display (ImageZoomed)

dev_display (SymbolRegions)

set_system ('clip_region', Information)

disp_message (WindowHandle, DecodedDataStrings, 'image', 10, 180, 'black', 'true')

stop ()

测试代码

* Read circularly printed bar codes.
* 
dev_update_off ()
get_system ('clip_region', Information)
set_system ('clip_region', 'true')
read_image (Image, 'circular_barcode')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width / 2, Height / 2, 'black', WindowHandle)
dev_set_colored (12)
dev_display (Image)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
stop ()
* 
* Segment the ring on the CD that contains the bar code.
threshold (Image, Region, 0, 100)
closing_circle (Region, Region, 3.5)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, Ring, ['width','height'], 'and', [550,550], [750,750])
shape_trans (Ring, OuterCircle, 'outer_circle')
complement (Ring, RegionComplement)
connection (RegionComplement, ConnectedRegions)
select_shape (ConnectedRegions, InnerCircle, ['width','height'], 'and', [450,450], [650,650])
* 
* Determine the parameters of the ring that contains the bar code.
smallest_circle (Ring, Row, Column, OuterRadius)
smallest_circle (InnerCircle, InnerRow, InnerColumn, InnerRadius)
dev_set_color ('green')
dev_set_draw ('margin')
dev_set_line_width (3)
dev_display (Image)
dev_display (OuterCircle)
dev_display (InnerCircle)
stop ()
* 
* Now read the bar code. This is done by computing the polar transformation
* of the ring in the image that contains the bar code.
WidthPolar := 1440
HeightPolar := round(OuterRadius - InnerRadius - 10)
polar_trans_image_ext (Image, PolarTransImage, Row, Column, rad(360), 0, OuterRadius - 5, InnerRadius + 5, WidthPolar, HeightPolar, 'bilinear')
invert_image (PolarTransImage, ImageInvert)
* 
* Since the bar code region is quite flat the image height is doubled.
zoom_image_factor (ImageInvert, ImageZoomed, 1, 2, 'weighted')
create_bar_code_model ([], [], BarCodeHandle)
* 
* Bars are small and the contrast is low; therefore the threshold is raised from 0.05 to 0.1.
set_bar_code_param (BarCodeHandle, 'element_size_min', 1.5)
set_bar_code_param (BarCodeHandle, 'meas_thresh', 0.3)
find_bar_code (ImageZoomed, SymbolRegions, BarCodeHandle, 'Code 128', DecodedDataStrings)
dev_set_window_extents (-1, -1, WidthPolar / 2, HeightPolar)
dev_display (ImageZoomed)
dev_display (SymbolRegions)
set_system ('clip_region', Information)
disp_message (WindowHandle, DecodedDataStrings, 'image', 10, 180, 'black', 'true')
stop ()
* 
* Transform the code region back to the original image and display it.
zoom_region (SymbolRegions, SymbolRegions, 1, 0.5)
polar_trans_region_inv (SymbolRegions, CodeRegionCircular, Row, Column, rad(360), 0, OuterRadius - 5, InnerRadius + 5, WidthPolar, HeightPolar, Width, Height, 'nearest_neighbor')
dev_set_window_extents (-1, -1, Width / 2, Height / 2)
dev_display (Image)
dev_display (CodeRegionCircular)
disp_message (WindowHandle, DecodedDataStrings, 'window', 12, 12, 'black', 'true')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄晓魚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值