当前位置:   article > 正文

Openmv实现图像中最大色块查找_openmv怎么导入库

openmv怎么导入库

目录

前言

一、前期准备

二、使用步骤

1.引入库

2.初始化

 3.元件初始化

4.色块分类


前言

本例程实现对图像中红 黄 绿三种不同颜色色块的分类


一、前期准备

openmv IDE安装及熟悉,详细可以参考

https://book.openmv.cc/

二、使用步骤

1.引入库

  1. import sensor, image, time, pyb, json
  2. from pyb import UART
  3. from pyb import LED

2.初始化

  1. red = LED(1) # 红led
  2. green=LED(2)
  3. uart = UART(3, 115200)
  4. uart.init(115200, bits=8, parity=None, stop=1) #8位数据位,无校验位,1位停止位、
  5. #红色阈值
  6. pink_threshold =(44, 75, 8, 77, -44, 21)
  7. # 黄色阈值
  8. yellow_threshold = (36, 75, -20, 11, 23, 48)
  9. # 蓝色阈值
  10. blue_threshold = (61, 95, -23, -10, -30, -10)
  11. # 绿色阈值
  12. green_threshold =(50, 60, -48, -30, 15, 38)
  13. # Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
  14. # The below thresholds track in general red/green things. You may wish to tune them...
  15. thresholds = [pink_threshold, # generic_red_thresholds -> index is 0 so code == (1 << 0)
  16. yellow_threshold, # generic_green_thresholds -> index is 1 so code == (1 << 1)
  17. green_threshold] # generic_blue_thresholds -> index is 2 so code == (1 << 2)
  18. # Codes are or'ed together when "merge=True" for "find_blobs".

设置颜色分类阈值,初始化openmv的串口以及三色的led灯光,识别到上述三种颜色则亮对应的  

 3.元件初始化

  1. sensor.reset()
  2. sensor.set_pixformat(sensor.RGB565)
  3. sensor.set_framesize(sensor.QVGA)
  4. sensor.skip_frames(time = 2000)
  5. sensor.set_auto_gain(False) # must be turned off for color tracking
  6. sensor.set_auto_whitebal(False) # must be turned off for color tracking
  7. clock = time.clock()

4.色块分类

 

  1. while(True):
  2. #clock.tick()
  3. time.sleep_ms(10)
  4. img = sensor.snapshot()
  5. for blob in img.find_blobs(thresholds, pixels_threshold=100, area_threshold=100, merge=True):
  6. s1=0
  7. if(blob.area()>s1):
  8. s1=blob.area()
  9. aimblob=blob
  10. if aimblob.code() == 1:
  11. img.draw_rectangle(blob.rect())
  12. img.draw_cross(blob.cx(), blob.cy())
  13. img.draw_string(blob.x() + 2, blob.y() + 2, "r/g")
  14. red.on()
  15. uart.write("red")
  16. time.sleep_ms(300)
  17. print("识别到红色!")
  18. red.off()
  19. if aimblob.code() == 2:
  20. img.draw_rectangle(blob.rect())
  21. img.draw_cross(blob.cx(), blob.cy())
  22. img.draw_string(blob.x() + 2, blob.y() + 2, "r/b")
  23. red.on()
  24. green.on()
  25. uart.write("yellow")
  26. time.sleep_ms(300)
  27. print("识别到黄色!")
  28. red.off()
  29. green.off()
  30. if aimblob.code() == 4: # g/b code
  31. img.draw_rectangle(blob.rect())
  32. img.draw_cross(blob.cx(), blob.cy())
  33. img.draw_string(blob.x() + 2, blob.y() + 2, "g/b")
  34. green.on()
  35. uart.write("green")
  36. time.sleep_ms(300)
  37. print("识别到绿色!")
  38. green.off()
  39. #print(clock.fps())

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号