当前位置:   article > 正文

基于Halcon学习的一维码识别【二十八】circular_barcode.hdev_环形布置条形码识别

环形布置条形码识别

阅读环形打印的条形码。


总代码:

  1. *更新状态
  2. dev_update_off ()
  3. *获取HALCON系统参数的当前值。
  4. get_system ('clip_region', Information)
  5. *设置HALCON系统参数。
  6. set_system ('clip_region', 'true')
  7. read_image (Image, 'circular_barcode')
  8. get_image_size (Image, Width, Height)
  9. dev_close_window ()
  10. dev_open_window (0, 0, Width / 2, Height / 2, 'black', WindowHandle)
  11. dev_set_colored (12)
  12. dev_display (Image)
  13. set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
  14. stop ()
  15. *
  16. * Segment the ring on the CD that contains the bar code.
  17. *分割CD上包含条形码的圆环
  18. *二值化--阈值分割
  19. threshold (Image, Region, 0, 100)
  20. *使用圆形结构元素闭合区域--闭运算
  21. closing_circle (Region, Region, 3.5)
  22. *形成不同的连通域
  23. connection (Region, ConnectedRegions)
  24. *通过高度宽度选择
  25. select_shape (ConnectedRegions, Ring, ['width','height'], 'and', [550,550], [750,750])
  26. *获得圆环的外圆
  27. shape_trans (Ring, OuterCircle, 'outer_circle')
  28. *返回一个区域的补码
  29. complement (Ring, RegionComplement)
  30. connection (RegionComplement, ConnectedRegions)
  31. select_shape (ConnectedRegions, InnerCircle, ['width','height'], 'and', [450,450], [650,650])
  32. *
  33. * Determine the parameters of the ring that contains the bar code.
  34. *确定包含条形码的环的参数。
  35. *求外圆半径
  36. smallest_circle (Ring, Row, Column, OuterRadius)
  37. *求内圆半径
  38. smallest_circle (InnerCircle, InnerRow, InnerColumn, InnerRadius)
  39. dev_set_color ('green')
  40. dev_set_draw ('margin')
  41. dev_set_line_width (3)
  42. dev_display (Image)
  43. dev_display (OuterCircle)
  44. dev_display (InnerCircle)
  45. stop ()
  46. *
  47. * Now read the bar code. This is done by computing the polar transformation
  48. * of the ring in the image that contains the bar code.
  49. *现在读一下条形码。这是通过计算极坐标变换来实现的
  50. *包含条形码的图像中的戒指
  51. *极坐标宽度
  52. WidthPolar := 1440
  53. *极坐标高度
  54. HeightPolar := round(OuterRadius - InnerRadius - 10)
  55. *进行极坐标变换
  56. polar_trans_image_ext (Image, PolarTransImage, Row, Column, rad(360), 0, OuterRadius - 5, InnerRadius + 5, WidthPolar, HeightPolar, 'bilinear')
  57. *图像反转
  58. invert_image (PolarTransImage, ImageInvert)
  59. *
  60. * Since the bar code region is quite flat the image height is doubled.
  61. *由于条形码区域非常平坦,图像高度增加了一倍。
  62. *放大图像
  63. zoom_image_factor (ImageInvert, ImageZoomed, 1, 2, 'weighted')
  64. *创建模型句柄
  65. create_bar_code_model ([], [], BarCodeHandle)
  66. *
  67. * Bars are small and the contrast is low; therefore the threshold is raised from 0.05 to 0.1.
  68. *条很小,对比度很低;因此,阈值从0.05提高到0.1
  69. *设置黑白条形码的最小像素为1.5
  70. set_bar_code_param (BarCodeHandle, 'element_size_min', 1.5)
  71. *设置阈值为0.3
  72. set_bar_code_param (BarCodeHandle, 'meas_thresh', 0.3)
  73. *寻找条形码
  74. find_bar_code (ImageZoomed, SymbolRegions, BarCodeHandle, 'Code 128', DecodedDataStrings)
  75. *清楚模型句柄
  76. clear_bar_code_model (BarCodeHandle)
  77. *设置窗口的尺寸
  78. dev_set_window_extents (-1, -1, WidthPolar / 2, HeightPolar)
  79. *显示条形码的放大区域
  80. dev_display (ImageZoomed)
  81. dev_display (SymbolRegions)
  82. set_system ('clip_region', Information)
  83. disp_message (WindowHandle, DecodedDataStrings, 'image', 10, 180, 'black', 'true')
  84. stop ()
  85. *
  86. * Transform the code region back to the original image and display it.
  87. *将代码区域转换回原始图像并显示。
  88. *缩小图像到原来大小
  89. zoom_region (SymbolRegions, SymbolRegions, 1, 0.5)
  90. *变换到环形条码区域
  91. polar_trans_region_inv (SymbolRegions, CodeRegionCircular, Row, Column, rad(360), 0, OuterRadius - 5, InnerRadius + 5, WidthPolar, HeightPolar, Width, Height, 'nearest_neighbor')
  92. dev_set_window_extents (-1, -1, Width / 2, Height / 2)
  93. dev_display (Image)
  94. dev_display (CodeRegionCircular)
  95. *显示信息
  96. disp_message (WindowHandle, DecodedDataStrings, 'window', 12, 12, 'black', 'true')

逐段分析:

  1. *更新状态
  2. dev_update_off ()
  3. *获取HALCON系统参数的当前值。
  4. get_system ('clip_region', Information)
  5. *设置HALCON系统参数。
  6. set_system ('clip_region', 'true')
  7. *读取图片
  8. read_image (Image, 'circular_barcode')
  9. get_image_size (Image, Width, Height)
  10. dev_close_window ()
  11. dev_open_window (0, 0, Width / 2, Height / 2, 'black', WindowHandle)
  12. *设置一些显示参数
  13. dev_set_colored (12)
  14. dev_display (Image)
  15. set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
  16. stop ()

  1. *分割CD上包含条形码的圆环
  2. *二值化--阈值分割
  3. threshold (Image, Region, 0, 100)

  1. *使用圆形结构元素闭合区域--闭运算
  2. closing_circle (Region, Region, 3.5)
  3. *形成不同的连通域
  4. connection (Region, ConnectedRegions)

  1. *通过高度宽度选择
  2. select_shape (ConnectedRegions, Ring, ['width','height'], 'and', [550,550], [750,750])

  1. *获得圆环的外圆
  2. shape_trans (Ring, OuterCircle, 'outer_circle')

  1. *返回一个区域的补集
  2. complement (Ring, RegionComplement)

  1. *二值化--阈值分割
  2. connection (RegionComplement, ConnectedRegions)

  1. *通过高度宽度选择
  2. select_shape (ConnectedRegions, InnerCircle, ['width','height'], 'and', [450,450], [650,650])

  1. *确定包含条形码的环的参数。
  2. *求外圆半径
  3. smallest_circle (Ring, Row, Column, OuterRadius)
  4. *求内圆半径
  5. smallest_circle (InnerCircle, InnerRow, InnerColumn, InnerRadius)
  6. *设置一些参数
  7. dev_set_color ('green')
  8. *设置描绘方式为边缘
  9. dev_set_draw ('margin')
  10. dev_set_line_width (3)
  11. *显示图片
  12. dev_display (Image)
  13. *显示外圆
  14. dev_display (OuterCircle)
  15. *显示内圆
  16. dev_display (InnerCircle)
  17. stop ()
  18. *

  1. *现在读一下条形码。这是通过计算极坐标变换来实现的
  2. *包含条形码的图像中的戒指
  3. *极坐标宽度
  4. WidthPolar := 1440
  5. *极坐标高度
  6. HeightPolar := round(OuterRadius - InnerRadius - 10)
  7. *进行极坐标变换
  8. polar_trans_image_ext (Image, PolarTransImage, Row, Column, rad(360), 0, OuterRadius - 5, InnerRadius + 5, WidthPolar, HeightPolar, 'bilinear')

  1. *图像反转
  2. invert_image (PolarTransImage, ImageInvert)

  1. *由于条形码区域非常平坦,图像高度增加了一倍。
  2. *放大图像
  3. zoom_image_factor (ImageInvert, ImageZoomed, 1, 2, 'weighted')
  4. *创建模型句柄
  5. create_bar_code_model ([], [], BarCodeHandle)
  6. *条很小,对比度很低;因此,阈值从0.05提高到0.1
  7. *设置黑白条形码的最小像素为1.5
  8. set_bar_code_param (BarCodeHandle, 'element_size_min', 1.5)
  9. *设置阈值为0.3
  10. set_bar_code_param (BarCodeHandle, 'meas_thresh', 0.3)
  11. *寻找条形码
  12. find_bar_code (ImageZoomed, SymbolRegions, BarCodeHandle, 'Code 128', DecodedDataStrings)
  13. *清楚模型句柄
  14. clear_bar_code_model (BarCodeHandle)

  1. *设置窗口的尺寸
  2. dev_set_window_extents (-1, -1, WidthPolar / 2, HeightPolar)

  1. *显示条形码的放大区域
  2. dev_display (ImageZoomed)
  3. dev_display (SymbolRegions)
  4. *设置系统参数
  5. set_system ('clip_region', Information)
  6. *显示信息
  7. disp_message (WindowHandle, DecodedDataStrings, 'image', 10, 180, 'black', 'true')
  8. stop ()

  1. *将代码区域转换回原始图像并显示。
  2. *缩小图像到原来大小
  3. zoom_region (SymbolRegions, SymbolRegions, 1, 0.5)
  4. *变换到环形条码区域
  5. polar_trans_region_inv (SymbolRegions, CodeRegionCircular, Row, Column, rad(360), 0, OuterRadius - 5, InnerRadius + 5, WidthPolar, HeightPolar, Width, Height, 'nearest_neighbor')
  6. dev_set_window_extents (-1, -1, Width / 2, Height / 2)
  7. dev_display (Image)
  8. dev_display (CodeRegionCircular)
  9. *显示信息
  10. disp_message (WindowHandle, DecodedDataStrings, 'window', 12, 12, 'black', 'true')

 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号