4

halcon如何识别硬币? - 喜欢吃鱼的青年

 2 years ago
source link: https://www.cnblogs.com/2828sea/p/16905956.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client



halcon如何识别硬币?

最近一直在学习halcon,在此做了一个案例,分享给大家,效果图如下:

1400941-20221119132217895-1891856376.png

1.思路分析

通过观察,发现1元,5角,1角,它们在面值的文字描述不一样,硬币显示的花纹不一样,如果我们通过模板匹配,那我们需要考虑2个因素,正/反面完全不一样。
换个思路,找到不变的因素【硬币外圈大小】,用最小的变量作为我们判断的基准,从而降低问题的复杂度。

2.代码逻辑分析

  1. 我们首先指定3个模板【硬币的圆圈大小】
  2. 读取原图片
  3. 使用模板匹配
  4. 标注不同的硬币

3.代码实现

3.1 创建模板

点击查看代码

创建模板
*画外圈圆
draw_circle (3600, Row1, Column1, Radius)
gen_circle (Circle, Row1, Column1, Radius)
dev_display (Image)
*画内圈圆
draw_circle (3600, Row2, Column2, Radius1)
gen_circle (Circle1, Row2, Column2, Radius1)
*取区域的差
difference (Circle, Circle1, RegionDifference)
reduce_domain (ImageEmphasize, RegionDifference, ImageReduced)
*创建模板
create_shape_model (ImageReduced, 'auto', -0.39, 0.79, 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
write_shape_model (ModelID, 'D:/WorkSpace/硬币/1元.shm')

3.2 模板匹配

点击查看代码

read_image (Image, 'E:/WorkSpace/HalconDemo/硬币检测/硬币/微信图片_20220620213720.jpg')

*均值滤波器
mean_image (Image, ImageMean, 3, 3)
*图片增亮
emphasize (ImageMean, ImageEmphasize, 7, 7, 10)
*读取模板
  read_shape_model ('1元.shm', ModelID)
  
  read_shape_model ('1角.shm', ModelID1)
  
  read_shape_model ('5角.shm', ModelID2)
  
  arr:=[ModelID,ModelID1,ModelID2]
  *设置字体的样式
    set_display_font (3600, 30, 'mono', 'true', 'false')
  for Index1 := 0 to 2 by 1
        *模板匹配
    find_shape_model (Image, arr[Index1], -0.39, 0.78, 0.4, 0, 0.5, 'least_squares', 2, 0.9, Row, Column, Angle, Score)

      if (|Score|>0)
          tuple_length (Row, Length1)
          for Index2 := 0 to Length1-1 by 1
              *字符串显示的位置
                set_tposition (3600, Row[Index2]-40, Column[Index2]-40)
     switch (Index1)
            case 0:
         dev_set_color ('red')
         write_string (3600, '1元')
          break
             case 1:
         dev_set_color ('green')
         write_string (3600, '1角')
          break
             case 2:
          dev_set_color ('blue')
          write_string (3600, '5角')
         break
     endswitch
          endfor
      endif
  endfor

4.效果展示

效果1:

1400941-20221119133140455-958620886.png

效果2:

1400941-20221119133148973-614400103.png

我在这里偷了懒,只是取了外圈大小,来确定不同硬币的识别,如果图片中存在不是硬币,但是大小一样,这样一定会误检,这也是我们的这个方案不严谨的地方,这是案例,我们以学习为主,如果你有更好的方式,请不吝赐教!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK