基于Halcon学习的一维码识别【二十八】circular_barcode.hdev

阅读环形打印的条形码。


总代码:

*更新状态
dev_update_off ()
*获取HALCON系统参数的当前值。
get_system ('clip_region', Information)
*设置HALCON系统参数。
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.
*分割CD上包含条形码的圆环
*二值化--阈值分割
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.
*条很小,对比度很低;因此,阈值从0.05提高到0.1。
*设置黑白条形码的最小像素为1.5
set_bar_code_param (BarCodeHandle, 'element_size_min', 1.5)
*设置阈值为0.3
set_bar_code_param (BarCodeHandle, 'meas_thresh', 0.3)
*寻找条形码
find_bar_code (ImageZoomed, SymbolRegions, BarCodeHandle, 'Code 128', DecodedDataStrings)
*清楚模型句柄
clear_bar_code_model (BarCodeHandle)
*设置窗口的尺寸
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')

逐段分析:

*更新状态
dev_update_off ()

*获取HALCON系统参数的当前值。
get_system ('clip_region', Information)

*设置HALCON系统参数。
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 ()

*分割CD上包含条形码的圆环
*二值化--阈值分割
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)

*条很小,对比度很低;因此,阈值从0.05提高到0.1。
*设置黑白条形码的最小像素为1.5
set_bar_code_param (BarCodeHandle, 'element_size_min', 1.5)

*设置阈值为0.3
set_bar_code_param (BarCodeHandle, 'meas_thresh', 0.3)

*寻找条形码
find_bar_code (ImageZoomed, SymbolRegions, BarCodeHandle, 'Code 128', DecodedDataStrings)

*清楚模型句柄
clear_bar_code_model (BarCodeHandle)

*设置窗口的尺寸
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 ()

*将代码区域转换回原始图像并显示。
*缩小图像到原来大小
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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值