当前位置:   article > 正文

2021年送药小车openMV巡线代码

openmv巡线代码
  • 用openMV进行巡线所需代码如下 
  1. # Hello World Example
  2. #
  3. # Welcome to the OpenMV IDE! Click on the green run arrow button below to run the script!
  4. import pyb, sensor, image, math, time
  5. from pyb import UART
  6. import json
  7. uart = UART(3,115200)
  8. red_threshold = [(71, 25, 98, 19, 101, -14)]
  9. black_threshold = [(0, 42, -84, 19, -104, 32)]
  10. #QVGA 320*240
  11. roi_1 = [(40, 0, 240, 40), # 北
  12. (40, 200, 160, 40), # 南
  13. (0, 0, 40, 240), # 西
  14. (280, 0, 40, 240), # 东
  15. (30,80,300,80)] # 中
  16. sensor.reset() # 初始化摄像头
  17. sensor.set_pixformat(sensor.RGB565) # 格式为 RGB565.
  18. sensor.set_framesize(sensor.QVGA)
  19. sensor.skip_frames(20) # 跳过10帧,使新设置生效
  20. sensor.set_auto_whitebal(False) # turn this off.
  21. sensor.set_auto_gain(False) # must be turned off for color tracking
  22. clock = time.clock()
  23. buf =[0 for i in range(5)]
  24. m = -1
  25. zhong_x = 0
  26. zhong_y = 0
  27. bei_x = 0
  28. bei_y = 0
  29. nan_x = 0
  30. nan_y = 0
  31. dong_x = 0
  32. dong_y = 0
  33. xi_x = 0
  34. xi_y = 0
  35. black_num = 0
  36. threshold_pixel = 280
  37. uart.init(115200, bits=8, parity=None, stop=1)
  38. def pack_dot_data():
  39. pack_data=bytearray([0xAA,0xFF,zhong_x>>8,zhong_x,dong_y>>8,dong_y,xi_y>>8,xi_y,
  40. black_num,0x00,0x00])
  41. lens = len(pack_data) #数据包大小
  42. #pack_data[3] = lens-6; #有效数据个数
  43. sc = 0
  44. ac = 0
  45. i = 0
  46. while i<(lens-1):
  47. sc = sc + pack_data[i]
  48. ac = ac + sc
  49. i=i+1
  50. pack_data[lens-2] = sc
  51. pack_data[lens-1] = ac;
  52. return pack_data
  53. while(True):
  54. clock.tick()
  55. m = -1
  56. isten = 0
  57. black_num = 0
  58. img = sensor.snapshot().lens_corr(strength = 1.8, zoom = 1.0)
  59. for r in roi_1:
  60. m += 1
  61. blobs = img.find_blobs(red_threshold, roi=r[0:4],pixels_threshold=100, area_threshold=100, merge=True)
  62. #img.draw_rectangle(r[0:4], color=(255,0,0))
  63. if blobs:
  64. most_pixels = 0
  65. largest_blob = 0
  66. for i in range(len(blobs)):
  67. #目标区域找到的颜色块(线段块)可能不止一个,找到最大的一个,作为本区域内的目标直线
  68. if blobs[i].pixels() > most_pixels:
  69. most_pixels = blobs[i].pixels()
  70. #merged_blobs[i][4]是这个颜色块的像素总数,如果此颜色块像素总数大于
  71. largest_blob = i
  72. #print(blobs[largest_blob].cx(),blobs[largest_blob].cy())
  73. if m == 0:
  74. bei_x = blobs[largest_blob].cx()
  75. bei_y = blobs[largest_blob].cy()
  76. elif m == 1:
  77. nan_x = blobs[largest_blob].cx()
  78. nan_y = blobs[largest_blob].cy()
  79. elif m == 2:
  80. dong_x = blobs[largest_blob].cx()
  81. dong_y = blobs[largest_blob].cy()
  82. elif m == 3:
  83. xi_x = blobs[largest_blob].cx()
  84. xi_y = blobs[largest_blob].cy()
  85. elif m == 4:
  86. zhong_x = blobs[largest_blob].cx()
  87. zhong_y = blobs[largest_blob].cy()
  88. # Draw a rect around the blob.
  89. img.draw_rectangle(blobs[largest_blob].rect())
  90. #将此区域的像素数最大的颜色块画矩形和十字形标记出来
  91. img.draw_cross(blobs[largest_blob].cx(),
  92. blobs[largest_blob].cy())
  93. buf[m] = 1
  94. else:
  95. buf[m] = 0
  96. if m == 0:
  97. bei_x = 0
  98. bei_y = 0
  99. elif m == 1:
  100. nan_x = 0
  101. nan_y = 0
  102. elif m == 2:
  103. dong_x = 0
  104. dong_y = 0
  105. elif m == 3:
  106. xi_x = 0
  107. xi_y = 0
  108. elif m == 4:
  109. zhong_x = 0
  110. zhong_y = 0
  111. blobs = img.find_blobs(black_threshold, roi=r[0:4],pixels_threshold=100, area_threshold=100, merge=False)
  112. for b in blobs:
  113. #记录大于threshold_pixel值的黑色色块的数量
  114. if b.pixels() > threshold_pixel:
  115. img.draw_rectangle(b.rect())
  116. print(b.pixels())
  117. black_num = black_num + 1;
  118. print(zhong_x,zhong_y)
  119. uart.write(pack_dot_data())
  120. print(black_num)
  121. print(clock.fps())

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/854014
推荐阅读
相关标签
  

闽ICP备14008679号