• halcon模板匹配捆绑测量矩形检测芯片针脚


    一,检测需求

            图片中的芯片会发送位置变换,需要检测芯片的针脚数量是否缺少,芯片的每一根针脚宽度是否达到要求。图像如下:

     二,halcon代码讲解

    1. 先读取一张图片,准备用来创建模板。
    2. 创建图像模板,选择芯片的中心图案作为模板。
    3. 绘制测量矩形,用两个测量矩形测量针脚。
    4. 开始读取待检测图像进行匹配,匹配后进行仿射变换,将绘制的两个矩形位置也经行变换。然后测量针脚,判断结果显示。详细代码如下。
      1. * 读取文件夹中的图像,模拟相机采集
      2. list_files ('D:/File/c# code/ce liang/bin/Debug/图像', ['files','follow_links'], ImageFiles)
      3. tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
      4. for Index := 0 to |ImageFiles| - 1 by 1
      5. read_image (ImageCheck, ImageFiles[Index])
      6. get_image_size (ImageCheck, Width, Height)
      7. *进行模板匹配
      8. find_shape_model (ImageCheck, ModelID, rad(0), rad(360), 0.5, 1, 0.5, 'least_squares', 0, 0.9,RowCheck, ColumnCheck, AngleCheck, Score)
      9. *仿射变换,匹配成功才进行
      10. if (|Score| > 0)
      11. *匹配
      12. hom_mat2d_identity (HomMat2DIdentity)
      13. hom_mat2d_translate (HomMat2DIdentity, RowCheck, ColumnCheck, HomMat2DTranslate)
      14. hom_mat2d_rotate (HomMat2DTranslate, AngleCheck, RowCheck, ColumnCheck, HomMat2DRotate)
      15. *变换模板轮廓
      16. affine_trans_contour_xld (ModelContours, ShapeModelTrans, HomMat2DRotate)
      17. dev_display (ShapeModelTrans)
      18. *转换绘制的两个矩形的点
      19. affine_trans_pixel (HomMat2DRotate, rectRow2, rectColumn2, Rect1RowCheck, Rect1ColCheck)
      20. affine_trans_pixel (HomMat2DRotate, rectRow3, rectColumn3, Rect2RowCheck, Rect2ColCheck)
      21. *根据转换后位置生成两个测量矩形
      22. gen_rectangle2 (Rectangle1Check, Rect1RowCheck, Rect1ColCheck, AngleCheck, rect1Width, rect1Height)
      23. gen_rectangle2 (Rectangle2Check, Rect2RowCheck, Rect2ColCheck, AngleCheck, rect2Width, rect2Height)
      24. dev_set_color ('blue')
      25. dev_set_draw ('margin')
      26. dev_set_line_width (3)
      27. dev_display (Rectangle1Check)
      28. dev_display (Rectangle2Check)
      29. count_seconds (S1)
      30. gen_measure_rectangle2 (Rect1RowCheck, Rect1ColCheck, AngleCheck, rect1Width, rect1Height, Width, Height, 'bilinear', MeasureHandle1)
      31. gen_measure_rectangle2 (Rect2RowCheck, Rect2ColCheck, AngleCheck, rect2Width, rect2Height, Width, Height, 'bilinear', MeasureHandle2)
      32. *测量,使用测量队模式,将一组边缘作为一对
      33. measure_pairs (ImageCheck, MeasureHandle1, 3, 99, 'positive', 'all', RowEdgeFirst1, ColumnEdgeFirst1, AmplitudeFirst1, RowEdgeSecond1, ColumnEdgeSecond1, AmplitudeSecond1, IntraDistance1, InterDistance1)
      34. measure_pairs (ImageCheck, MeasureHandle2, 3, 99, 'positive', 'all', RowEdgeFirst2, ColumnEdgeFirst2, AmplitudeFirst2, RowEdgeSecond2, ColumnEdgeSecond2, AmplitudeSecond2, IntraDistance2, InterDistance2)
      35. close_measure (MeasureHandle1)
      36. close_measure (MeasureHandle2)
      37. count_seconds (S2)
      38. dev_set_color ('red')
      39. *画线对,把测量到的边缘绘制显示
      40. disp_line (WindowHandle, RowEdgeFirst1 - rect1Height * cos(AngleCheck), ColumnEdgeFirst1 - rect1Height * sin(AngleCheck), RowEdgeFirst1 + rect1Height * cos(AngleCheck), ColumnEdgeFirst1 + rect1Height * sin(AngleCheck))
      41. disp_line (WindowHandle, RowEdgeSecond1 - rect1Height * cos(AngleCheck), ColumnEdgeSecond1 - rect1Height * sin(AngleCheck), RowEdgeSecond1 + rect1Height * cos(AngleCheck), ColumnEdgeSecond1 + rect1Height * sin(AngleCheck))
      42. disp_line (WindowHandle, RowEdgeFirst2 - rect2Height * cos(AngleCheck), ColumnEdgeFirst2 - rect2Height * sin(AngleCheck), RowEdgeFirst2 + rect2Height * cos(AngleCheck), ColumnEdgeFirst2 + rect2Height * sin(AngleCheck))
      43. disp_line (WindowHandle, RowEdgeSecond2 - rect2Height * cos(AngleCheck), ColumnEdgeSecond2 - rect2Height * sin(AngleCheck), RowEdgeSecond2 + rect2Height * cos(AngleCheck), ColumnEdgeSecond2 + rect2Height * sin(AngleCheck))
      44. dev_set_line_width (1)
      45. *测量时间
      46. time:=S2-S1
      47. *针脚大小判断
      48. distance_threshold:=10.5
      49. wrongdistance:=[]
      50. wrongRow:=[]
      51. wrongCol:=[]
      52. *从第一个测量矩形判断,判断每一个针脚的宽度是否符合要求,将不符合要求的针脚边缘中心的坐标保存
      53. for Index1 := 0 to |IntraDistance1|-1 by 1
      54. if(IntraDistance1[Index1]
      55. wrongRow:=[wrongRow,RowEdgeFirst1[Index1]]
      56. wrongCol:=[wrongCol,ColumnEdgeFirst1[Index1]]
      57. *将缺陷距离保存
      58. wrongdistance:=[wrongdistance,IntraDistance1[Index1]]
      59. endif
      60. endfor
      61. *判断第二个测量矩形的结果
      62. for Index2 := 0 to |IntraDistance2|-1 by 1
      63. if(IntraDistance2[Index2]
      64. wrongRow:=[wrongRow,RowEdgeFirst2[Index2]]
      65. wrongCol:=[wrongCol,ColumnEdgeFirst2[Index2]]
      66. *将缺陷距离保存
      67. wrongdistance:=[wrongdistance,IntraDistance2[Index2]]
      68. endif
      69. endfor
      70. *针脚总数计算
      71. set_display_font (WindowHandle, 10, 'mono', 'true', 'false')
      72. NumLeads := |IntraDistance1| + |IntraDistance2|
      73. *显示结果模块
      74. if (NumLeads<28)
      75. disp_message (WindowHandle, '缺少针脚', 'window', 20, 20, 'red', 'false')
      76. elseif (|wrongRow|>0)
      77. dev_set_draw ('margin')
      78. dev_set_color ('red')
      79. dev_clear_window ()
      80. dev_display (ImageCheck)
      81. *显示不合格的针脚
      82. for Index3 := 0 to |wrongRow|-1 by 1
      83. disp_circle (WindowHandle, wrongRow[Index3], wrongCol[Index3], 6.0)
      84. disp_message (WindowHandle, wrongdistance[Index3]$'.2f', 'window', wrongRow[Index3]-30, wrongCol[Index3]-50, 'black', 'true')
      85. endfor
      86. disp_message (WindowHandle, '针脚有缺陷', 'window', 20, 20, 'red', 'false')
      87. else
      88. dev_set_color ('green')
      89. disp_message (WindowHandle, '图像OK,针脚数:'+NumLeads, 'window', 20, 20,'green', 'true')
      90. endif
      91. stop()
      92. * Image Acquisition 01: Do something
      93. endif
      94. endfor

     三,检测示意图

     最后的检测效果如下,左上角显示检测结果。

    结语

    在本专栏下一篇文章中,将实现halocn代码的导出,和c#软件界面的搭建。

    这里是图像文件和halcon的源码

    链接:https://pan.baidu.com/s/1MhI64rQA2GAIIjdMZ7R8iw 
    提取码:wtu2

    链接:https://pan.baidu.com/s/119fA7JdX_Hgh-GyMIvBC5w 
    提取码:7f2q

  • 相关阅读:
    frida打印byte数组
    05※、Map集合介绍、HashMap、Hashtable、LinkedHashMap、Properties和Collections
    【快手小玩法-弹幕游戏】开发者功能测试报告提交模板
    Educational Codeforces Round 115 (Rated for Div. 2)
    动态规划求解最大子段和 (两种写法+还原最优解)
    AndroidStudio 新建工程的基本修改及事件添加
    从指定 URL 读取图像并以 OpenCV 格式返回的函数(从指定 URL 读取图像并使其可由 OpenCV 处理。)
    SpringMVC 程序开发
    WorldPop2000年至2020年的全中国的人口统计数据
    零售业:别让数据安全成为业务的绊脚石!(附文件下载)
  • 原文地址:https://blog.csdn.net/uiopmn1/article/details/126051493