QQ登录

只需一步,快速开始

电路板金手指间距测量工业视觉项目源代码

[ 复制链接 ]

电路板金手指间距测量工业视觉项目源代码

电路板金手指间距测量工业视觉项目源代码


最近结束了一项目,项目要实现的功能是计算上图中两金手指的间距,
将间距传递给执行机构,让上下两块板对齐。
实现的思路也是简单,如上图,
1.先是提取上边金手指,拟合成矩形,获取矩形相关几何参数。
然后是获取下边金手指区域,拟合成矩形,获取矩形相关几何参数。

2.计算下矩形中心线起点与上矩形中心线的垂直点坐标点,此点与下矩形中心线距离就为要求得的间距。

3.计算所得的间距 单位是像素单位,所以最终还得进行转换。
我们事先已知上边两金手指间距为100毫米,(厂家产品规格确定)。计算出其间隔像素单位,
这样像素毫米关系就确定,也就是间距的像素单位就可以进行转换为毫米单位。

下面为步骤2,步骤3实现的halcon代码,同时附件为一张产品图片供大家下载测试学习
请点击此处下载

查看状态:已购买或有权限

您的用户组是:游客

文件名称:2.rar 
文件大小:1.02 MB 
下载权限: 不限 以上或 VIP会员   [购买捐助会员]   [充值积分]   有问题联系我


  

如果您认可,可联系功能定制!

  

如果您着急,充值会员可直接联系发您资料!

  

QQ联系我

微信扫扫联系我

  

  1. dev_close_window ()
  2. read_image (Image1, '1.bmp')
  3. dev_open_window_fit_image (Image1, 0, 0, -1, -1, WindowHandle)
  4. dev_display (Image1)
  5. get_image_size (Image1, ImageWidth, ImageHeight)
  6. rgb1_to_gray (Image1, Image)
  7. binary_threshold (Image, IconTemp, 'max_separability', 'light', UsedThreshold)
  8. connection (IconTemp, Icon)
  9. *筛选全部白色区域
  10. select_shape (Icon, IconTemp, ['area','ratio','column'], 'and', [2000,3,0], [999999,20,ImageWidth*0.95])
  11. select_shape(IconTemp, Icon, 'row1', 'and', 0, 100)
  12. union1 (Icon, IconTemp)
  13. smallest_rectangle1 (IconTemp, Row, Column, Row1, Column1)
  14. gen_rectangle1 (Rectangle, Row, Column, Row1/2, ImageWidth)
  15. reduce_domain(Image, Rectangle, IconTemp)
  16. binary_threshold (IconTemp, Icon, 'smooth_histo', 'dark', UsedThreshold)

  17. connection (Icon, IconTemp)
  18. select_shape (IconTemp, Icon, ['area','ratio','column'], 'and', [500,3,0], [999999,20,ImageWidth*0.95])
  19. sort_region (Icon, IconTemp2, 'upper_right', 'false', 'column')
  20. select_obj(IconTemp2, Icon, 1)
  21. gen_contour_region_xld (Icon, IconTemp, 'border')
  22. fit_rectangle2_contour_xld (IconTemp, 'regression', -1, 0, 0, 3, 2, Row, Column, Phi, Length1, Length2, PointOrder)
  23. *提取矩形中心线为上参考线
  24. get_rectangle2_points (Row, Column, Phi, Length1, Length2, CornerY, CornerX, LineCenterY, LineCenterX)
  25. if( Phi<=0)
  26.     XValue := LineCenterX[3]
  27.     YValue := LineCenterY[3]
  28.     XValue1:= LineCenterX[1]
  29.     YValue1:= LineCenterY[1]
  30. else
  31.     XValue := LineCenterX[1]
  32.     YValue := LineCenterY[1]  
  33.     XValue1:= LineCenterX[3]
  34.     YValue1:= LineCenterY[3]
  35. endif
  36. *
  37. select_obj(IconTemp2, Icon, 2)
  38. gen_contour_region_xld (Icon, IconTemp, 'border')
  39. fit_rectangle2_contour_xld (IconTemp, 'regression', -1, 0, 0, 3, 2, Row, Column, Phi, Length1, Length2, PointOrder)
  40. *提取矩形中心线为上参考线
  41. get_rectangle2_points (Row, Column, Phi, Length1, Length2, CornerY, CornerX, LineCenterY, LineCenterX)
  42. if( Phi<=0)
  43.     XValue2 := LineCenterX[3]
  44.     YValue2 := LineCenterY[3]
  45.     XValue3:= LineCenterX[1]
  46.     YValue3:= LineCenterY[1]
  47. else
  48.     XValue2 := LineCenterX[1]
  49.     YValue2 := LineCenterY[1]  
  50.     XValue3:= LineCenterX[3]
  51.     YValue3:= LineCenterY[3]
  52. endif
  53. distance_pp(YValue1, XValue1,YValue3, XValue3,Distance)
  54. *显示距离
  55. gen_contour_polygon_xld(LineXLD,[YValue,YValue1],[XValue,XValue1])
  56. gen_contour_polygon_xld(LineXLD1,[YValue2,YValue3],[XValue2,XValue3])
  57. gen_arrow_contour_xld(Arrow, YValue1, XValue1, YValue3, XValue3, 10, 10)
  58. gen_arrow_contour_xld(Arrow1, YValue3, XValue3, YValue1, XValue1,  10, 10)
  59. dev_display(Image)
  60. dev_set_color('red')
  61. dev_display(LineXLD)
  62. dev_display(LineXLD1)
  63. dev_set_color('green')
  64. dev_display(Arrow1)
  65. dev_display(Arrow)
  66. message := Distance[code]dev_close_window ()
  67. read_image (Image1, '1.bmp')
  68. dev_open_window_fit_image (Image1, 0, 0, -1, -1, WindowHandle)
  69. dev_display (Image1)
  70. get_image_size (Image1, ImageWidth, ImageHeight)
  71. rgb1_to_gray (Image1, Image)
  72. binary_threshold (Image, IconTemp, 'max_separability', 'light', UsedThreshold)
  73. connection (IconTemp, Icon)
  74. *筛选全部白色区域
  75. select_shape (Icon, IconTemp, ['area','ratio','column'], 'and', [2000,3,0], [999999,20,ImageWidth*0.95])
  76. *筛选上,下白色区
  77. select_shape(IconTemp, Icon, 'row1', 'and', 0, 100)
  78. difference (IconTemp, Icon, IconTemp2)
  79. *处理上白色
  80. union1 (Icon, IconTemp)
  81. smallest_rectangle1 (IconTemp, Row, Column, Row1, Column1)
  82. gen_rectangle1 (Rectangle, Row, Column, Row1/2, ImageWidth)
  83. reduce_domain(Image, Rectangle, IconTemp)
  84. binary_threshold (IconTemp, Icon, 'smooth_histo', 'dark', UsedThreshold)

  85. connection (Icon, IconTemp)
  86. select_shape (IconTemp, Icon, ['area','ratio','column','rectangularity'], 'and', [500,3,0,0.8], [999999,20,ImageWidth*0.95,1])
  87. sort_region (Icon, IconTemp, 'upper_right', 'false', 'column')
  88. select_obj(IconTemp, Icon, 1)
  89. gen_contour_region_xld (Icon, IconTemp, 'border')
  90. fit_rectangle2_contour_xld (IconTemp, 'regression', -1, 0, 0, 3, 2, Row, Column, Phi, Length1, Length2, PointOrder)
  91. gen_rectangle2_contour_xld(Rect, Row, Column, Phi, Length1, Length2)
  92. *提取矩形中心线为上参考线
  93. get_rectangle2_points (Row, Column, Phi, Length1, Length2, CornerY, CornerX, LineCenterY, LineCenterX)
  94. if( Phi<=0)
  95.     XValue := LineCenterX[3]
  96.     YValue := LineCenterY[3]
  97.     XValue1:= LineCenterX[1]
  98.     YValue1:= LineCenterY[1]
  99. else
  100.     XValue := LineCenterX[1]
  101.     YValue := LineCenterY[1]  
  102.     XValue1:= LineCenterX[3]
  103.     YValue1:= LineCenterY[3]
  104. endif

  105. *处理下白色区
  106. union1 (IconTemp2, IconTemp)
  107. smallest_rectangle1 (IconTemp, Row, Column, Row1, Column1)
  108. *gen_rectangle1 (Rectangle, Row-(Row1-Row)*0.1, Column, Row1+(Row1-Row)*0.1, ImageWidth)
  109. gen_rectangle1 (Rectangle, Row, Column, Row1, ImageWidth)
  110. reduce_domain(Image, Rectangle, IconTemp)
  111. auto_threshold (IconTemp, Icon, 5)
  112. * binary_threshold (IconTemp, Icon, 'smooth_histo', 'light', UsedThreshold)
  113. connection (Icon, IconTemp)
  114. fill_up(IconTemp, IconTemp)
  115. select_shape (IconTemp, Icon, ['area','ratio','column','rectangularity'], 'and', [500,3,0,0.8], [999999,20,ImageWidth*0.95,1])
  116. sort_region (Icon, IconTemp, 'upper_right', 'false', 'column')
  117. select_obj(IconTemp, Icon, 1)
  118. gen_contour_region_xld (Icon, IconTemp, 'border')
  119. fit_rectangle2_contour_xld (IconTemp, 'regression', -1, 0, 0, 3, 2, Row, Column, Phi, Length1, Length2, PointOrder)
  120. gen_rectangle2_contour_xld(Rect1, Row, Column, Phi, Length1, Length2)
  121. get_rectangle2_points (Row, Column, Phi, Length1, Length2, CornerY, CornerX, LineCenterY, LineCenterX)
  122. if( Phi<=0)
  123.     XValue2 := LineCenterX[3]
  124.     YValue2 := LineCenterY[3]
  125.     XValue3 := LineCenterX[1]
  126.     YValue3 := LineCenterY[1]  
  127. else
  128.     XValue2 := LineCenterX[1]
  129.     YValue2 := LineCenterY[1]  
  130.     XValue3 := LineCenterX[3]
  131.     YValue3 := LineCenterY[3]   
  132. endif
  133. *下金手指中心线顶点到上金手指中心线的投影。
  134. projection_pl(YValue2,XValue2,YValue,XValue,YValue1,XValue1,YValue1,XValue1)
  135. *投影点到下金手指中心线距离
  136. distance_pl(YValue1,XValue1,YValue2,XValue2,YValue3,XValue3,Distance)
  137. if(|Distance|==0)
  138.     return()
  139. endif
  140. *投影点在下金手指中心线右侧,表示上板要往左移,距离为负,反之则反
  141. if(XValue1>XValue2)
  142.     Distance := -Distance
  143. endif
  144. *显示结果
  145. gen_contour_polygon_xld(LineXLD,[YValue,YValue1],[XValue,XValue1])
  146. gen_contour_polygon_xld(LineXLD1,[YValue2,YValue3],[XValue2,XValue3])
  147. gen_arrow_contour_xld(Arrow, YValue1, XValue1, YValue2, XValue2, 10, 10)
  148. gen_arrow_contour_xld(Arrow1, YValue2, XValue2, YValue1, XValue1, 10, 10)
  149. dev_display(Image1)
  150. dev_set_line_width (2)
  151. dev_set_draw ('margin')
  152. dev_set_color('red')
  153. dev_display(Rect)
  154. dev_display(Rect1)
  155. dev_set_color('magenta')
  156. dev_display(LineXLD)
  157. dev_display(LineXLD1)
  158. dev_set_color('green')
  159. dev_display(Arrow)
  160. dev_display(Arrow1)
  161. message := Distance


  162. .3f'+'Pix'
  163. get_string_extents(WindowHandle, message, Ascent, Descent, Width, Height)
  164. dev_disp_text(message, 'image', YValue1-Height*2, XValue1, 'green', ['box'], ['false'])

复制代码
  1. dev_close_window ()
  2. read_image (Image1, '1.bmp')
  3. dev_open_window_fit_image (Image1, 0, 0, -1, -1, WindowHandle)
  4. dev_display (Image1)
  5. get_image_size (Image1, ImageWidth, ImageHeight)
  6. rgb1_to_gray (Image1, Image)
  7. binary_threshold (Image, IconTemp, 'max_separability', 'light', UsedThreshold)
  8. connection (IconTemp, Icon)
  9. *筛选全部白色区域
  10. select_shape (Icon, IconTemp, ['area','ratio','column'], 'and', [2000,3,0], [999999,20,ImageWidth*0.95])
  11. *筛选上,下白色区
  12. select_shape(IconTemp, Icon, 'row1', 'and', 0, 100)
  13. difference (IconTemp, Icon, IconTemp2)
  14. *处理上白色
  15. union1 (Icon, IconTemp)
  16. smallest_rectangle1 (IconTemp, Row, Column, Row1, Column1)
  17. gen_rectangle1 (Rectangle, Row, Column, Row1/2, ImageWidth)
  18. reduce_domain(Image, Rectangle, IconTemp)
  19. binary_threshold (IconTemp, Icon, 'smooth_histo', 'dark', UsedThreshold)

  20. connection (Icon, IconTemp)
  21. select_shape (IconTemp, Icon, ['area','ratio','column','rectangularity'], 'and', [500,3,0,0.8], [999999,20,ImageWidth*0.95,1])
  22. sort_region (Icon, IconTemp, 'upper_right', 'false', 'column')
  23. select_obj(IconTemp, Icon, 1)
  24. gen_contour_region_xld (Icon, IconTemp, 'border')
  25. fit_rectangle2_contour_xld (IconTemp, 'regression', -1, 0, 0, 3, 2, Row, Column, Phi, Length1, Length2, PointOrder)
  26. gen_rectangle2_contour_xld(Rect, Row, Column, Phi, Length1, Length2)
  27. *提取矩形中心线为上参考线
  28. get_rectangle2_points (Row, Column, Phi, Length1, Length2, CornerY, CornerX, LineCenterY, LineCenterX)
  29. if( Phi&lt;=0)
  30.     XValue := LineCenterX[3]
  31.     YValue := LineCenterY[3]
  32.     XValue1:= LineCenterX[1]
  33.     YValue1:= LineCenterY[1]
  34. else
  35.     XValue := LineCenterX[1]
  36.     YValue := LineCenterY[1]  
  37.     XValue1:= LineCenterX[3]
  38.     YValue1:= LineCenterY[3]
  39. endif

  40. *处理下白色区
  41. union1 (IconTemp2, IconTemp)
  42. smallest_rectangle1 (IconTemp, Row, Column, Row1, Column1)
  43. *gen_rectangle1 (Rectangle, Row-(Row1-Row)*0.1, Column, Row1+(Row1-Row)*0.1, ImageWidth)
  44. gen_rectangle1 (Rectangle, Row, Column, Row1, ImageWidth)
  45. reduce_domain(Image, Rectangle, IconTemp)
  46. auto_threshold (IconTemp, Icon, 5)
  47. * binary_threshold (IconTemp, Icon, 'smooth_histo', 'light', UsedThreshold)
  48. connection (Icon, IconTemp)
  49. fill_up(IconTemp, IconTemp)
  50. select_shape (IconTemp, Icon, ['area','ratio','column','rectangularity'], 'and', [500,3,0,0.8], [999999,20,ImageWidth*0.95,1])
  51. sort_region (Icon, IconTemp, 'upper_right', 'false', 'column')
  52. select_obj(IconTemp, Icon, 1)
  53. gen_contour_region_xld (Icon, IconTemp, 'border')
  54. fit_rectangle2_contour_xld (IconTemp, 'regression', -1, 0, 0, 3, 2, Row, Column, Phi, Length1, Length2, PointOrder)
  55. gen_rectangle2_contour_xld(Rect1, Row, Column, Phi, Length1, Length2)
  56. get_rectangle2_points (Row, Column, Phi, Length1, Length2, CornerY, CornerX, LineCenterY, LineCenterX)
  57. if( Phi&lt;=0)
  58.     XValue2 := LineCenterX[3]
  59.     YValue2 := LineCenterY[3]
  60.     XValue3 := LineCenterX[1]
  61.     YValue3 := LineCenterY[1]  
  62. else
  63.     XValue2 := LineCenterX[1]
  64.     YValue2 := LineCenterY[1]  
  65.     XValue3 := LineCenterX[3]
  66.     YValue3 := LineCenterY[3]   
  67. endif
  68. *下金手指中心线顶点到上金手指中心线的投影。
  69. projection_pl(YValue2,XValue2,YValue,XValue,YValue1,XValue1,YValue1,XValue1)
  70. *投影点到下金手指中心线距离
  71. distance_pl(YValue1,XValue1,YValue2,XValue2,YValue3,XValue3,Distance)
  72. if(|Distance|==0)
  73.     return()
  74. endif
  75. *投影点在下金手指中心线右侧,表示上板要往左移,距离为负,反之则反
  76. if(XValue1&gt;XValue2)
  77.     Distance := -Distance
  78. endif
  79. *显示结果
  80. gen_contour_polygon_xld(LineXLD,[YValue,YValue1],[XValue,XValue1])
  81. gen_contour_polygon_xld(LineXLD1,[YValue2,YValue3],[XValue2,XValue3])
  82. gen_arrow_contour_xld(Arrow, YValue1, XValue1, YValue2, XValue2, 10, 10)
  83. gen_arrow_contour_xld(Arrow1, YValue2, XValue2, YValue1, XValue1, 10, 10)
  84. dev_display(Image1)
  85. dev_set_line_width (2)
  86. dev_set_draw ('margin')
  87. dev_set_color('red')
  88. dev_display(Rect)
  89. dev_display(Rect1)
  90. dev_set_color('magenta')
  91. dev_display(LineXLD)
  92. dev_display(LineXLD1)
  93. dev_set_color('green')
  94. dev_display(Arrow)
  95. dev_display(Arrow1)
  96. message := Distance


  97. .2f'+'pix'
  98. get_string_extents(WindowHandle, message, Ascent, Descent, Width1, Height1)
  99. dev_disp_text(message, 'image', YValue2-Height1*2, XValue2, 'green', ['box'], ['false'])
复制代码



.3f'+'Pix'
get_string_extents(WindowHandle, message, Ascent, Descent, Width, Height)
dev_disp_text(message, 'image', YValue1-Height*2, XValue1, 'green', ['box'], ['false'])

[/code][        DISCUZ_CODE_25        ]


.2f'+'pix'
get_string_extents(WindowHandle, message, Ascent, Descent, Width1, Height1)
dev_disp_text(message, 'image', YValue2-Height1*2, XValue2, 'green', ['box'], ['false'])
[/code]


.2f'+'pix'
get_string_extents(WindowHandle, message, Ascent, Descent, Width1, Height1)
dev_disp_text(message, 'image', YValue2-Height1*2, XValue2, 'green', ['box'], ['false'])
[/code]


.3f'+'Pix'
get_string_extents(WindowHandle, message, Ascent, Descent, Width, Height)
dev_disp_text(message, 'image', YValue1-Height*2, XValue1, 'green', ['box'], ['false'])

[/code][        DISCUZ_CODE_25        ]


回复

使用道具 举报

大神点评(1)

快速回复 返回列表 客服中心 搜索