当前位置:   article > 正文

OpenMV4 基于色块识别的图形+颜色+坐标识别代码(micropython)_openmv实现颜色识别并返回坐标值

openmv实现颜色识别并返回坐标值

Hello大家好,最近竞赛需要开始研究OpenMV4,今天和大家分享一段基于色块识别的图形+颜色+坐标识别代码,实测准确率高于90%哦,当然,需要在光线和距离都合适的情况下使用(假如你的识别结果不尽如人意,可以自行调节颜色阈值和目标与摄像头的距离),下面,话不多说,上代码!(需要搭配OpenMV IDE使用)

  1. # Untitled - By: zzy - 周五 11月 25 2022
  2. import sensor, image, time
  3. from pyb import UART
  4. import json
  5. output_str_green="[0,0]"
  6. output_str_red="[0,0]"
  7. output_str_blue="[0,0]"
  8. output_str_brown="[0,0]"
  9. output_str_yellow="[0,0]"
  10. #green_threshold = ( 0, 80, -70, -10, -0, 30)
  11. green_threshold = ( 3, 39, -29, 2, 1, 25)
  12. red_threshold = ( 28, 40, 51, 65, 22, 50)
  13. orange_threshold = ( 23, 39, 19, 42, 13, 31)
  14. blue_threshold = ( 50, 56, -14, 1, -31, -13)
  15. brown_threshold = ( 22, 30, 1, 17, 8, 25)
  16. yellow_threshold = ( 53, 58, -7, 3, 58, 63)
  17. sensor.reset()
  18. sensor.set_pixformat(sensor.RGB565)
  19. sensor.set_framesize(sensor.QVGA)
  20. sensor.set_windowing((0,20,320,200))#QVGA find Region Of Interest
  21. #sensor.set_windowing((5,10,160,95))#QQVGA find Region Of Interest
  22. sensor.skip_frames(10)
  23. sensor.set_auto_whitebal(False)
  24. clock = time.clock()
  25. uart = UART(3, 115200)
  26. def find_max(blobs):
  27. max_size=0
  28. for blob in blobs:
  29. if blob.pixels() > max_size:
  30. max_blob=blob
  31. max_size = blob.pixels()
  32. return max_blob
  33. def detect(max_blob):#输入的是寻找到色块中的最大色块
  34. #print(max_blob.solidity())
  35. shape=0
  36. if max_blob.solidity()>0.90 or max_blob.density()>0.84:
  37. img.draw_rectangle(max_blob.rect(),color=(255,255,255))
  38. shape=1
  39. elif max_blob.density()>0.6:
  40. img.draw_circle((max_blob.cx(), max_blob.cy(),int((max_blob.w()+max_blob.h())/4)))
  41. shape=2
  42. elif max_blob.density()>0.4:
  43. img.draw_rectangle(max_blob.rect(),color=(0,0,0))
  44. shape=3
  45. return shape
  46. while(True):
  47. #clock.tick()
  48. img = sensor.snapshot() # Take a picture and return the image.
  49. blobs_green = img.find_blobs([green_threshold])
  50. blobs_red = img.find_blobs([red_threshold])
  51. #blobs_orange = img.find_blobs([orange_threshold])
  52. blobs_blue = img.find_blobs([blue_threshold])
  53. blobs_brown = img.find_blobs([brown_threshold])
  54. blobs_yellow = img.find_blobs([yellow_threshold])
  55. if blobs_green:
  56. max_blob_green=find_max(blobs_green)
  57. shape_green=detect(max_blob_green)
  58. #img.draw_rectangle(max_blob_green.rect(),color=(0,255,0))#画框
  59. img.draw_cross(max_blob_green.cx(), max_blob_green.cy(),color=(0,255,0))#画十字准星
  60. output_str_green="[%d,%d,%d]" % (max_blob_green.cx(),max_blob_green.cy(),shape_green) #方式1
  61. print('green:',output_str_green)
  62. else:
  63. print('not found green!')
  64. if blobs_red:
  65. max_blob_red=find_max(blobs_red)
  66. shape_red=detect(max_blob_red)
  67. #img.draw_rectangle(max_blob_red.rect(),color=(255,0,0))
  68. img.draw_cross(max_blob_red.cx(), max_blob_red.cy(),color=(255,0,0))
  69. output_str_red="[%d,%d,%d]" % (max_blob_red.cx(),max_blob_red.cy(),shape_red) #方式1
  70. print('red:',output_str_red)
  71. else:
  72. print('not found red !')
  73. #if blobs_orange:
  74. #max_blob_orange=find_max(blobs_orange)
  75. #detect(max_blob_orange)
  76. ##img.draw_rectangle(max_blob_orange.rect(),color=(255,128,0))
  77. #img.draw_cross(max_blob_orange.cx(), max_blob_orange.cy(),color=(255,128,0))
  78. #output_str_orange="[%d,%d]" % (max_blob_orange.cx(),max_blob_orange.cy()) #方式1
  79. #print('orange:',output_str_orange)
  80. #uart.write(output_str_orange+'\r\n')
  81. #else:
  82. #print('not found orange !')
  83. if blobs_blue:
  84. max_blob_blue=find_max(blobs_blue)
  85. shape_blue=detect(max_blob_blue)
  86. #img.draw_rectangle(max_blob_blue.rect(),color=(0,0,255))
  87. img.draw_cross(max_blob_blue.cx(), max_blob_blue.cy(),color=(0,0,255))
  88. output_str_blue="[%d,%d,%d]" % (max_blob_blue.cx(),max_blob_blue.cy(),shape_blue) #方式1
  89. print('blue:',output_str_blue)
  90. else:
  91. print('not found blue !')
  92. if blobs_brown:
  93. max_blob_brown=find_max(blobs_brown)
  94. shape_brown=detect(max_blob_brown)
  95. #img.draw_rectangle(max_blob_brown.rect(),color=(205,133,63))
  96. img.draw_cross(max_blob_brown.cx(), max_blob_brown.cy(),color=(205,133,63))
  97. output_str_brown="[%d,%d,%d]" % (max_blob_brown.cx(),max_blob_brown.cy(),shape_brown) #方式1
  98. print('brown:',output_str_brown)
  99. else:
  100. print('not found brown !')
  101. if blobs_yellow:
  102. max_blob_yellow=find_max(blobs_yellow)
  103. shape_yellow=detect(max_blob_yellow)
  104. #img.draw_rectangle(max_blob_yellow.rect(),color=(255,255,0))
  105. img.draw_cross(max_blob_yellow.cx(), max_blob_yellow.cy(),color=(255,255,0))
  106. output_str_yellow="[%d,%d,%d]" % (max_blob_yellow.cx(),max_blob_yellow.cy(),shape_yellow) #方式1
  107. print('yellow:',output_str_yellow)
  108. else:
  109. print('not found yellow !')
  110. uart.write(output_str_green + output_str_red + output_str_blue + output_str_brown + output_str_yellow + '\r\n')
  111. #print(clock.fps())

再来看看程序的运行结果吧 ,在识别出多个图形,颜色及坐标之后,性能仍然不赖

实测运行结果,在颜色阈值选择正确情况下识别率还是比较高的哦(三角形识别出之后画黑色矩形框,不是没识别出来哦)
解除部分注释之后可以查看帧数以调整性能,开发者还可以根据自己的需求增加被检测颜色与图形,再将其通过串口发送到目标单片机上哦,假如之后有时间,我再出一份解释代码含义的文章,嘻嘻,就看大家的需求和小编的时间啦。

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

闽ICP备14008679号