標題: python openmv色塊跟隨源程序 [打印本頁]

作者: zwb111    時間: 2019-8-5 19:55
標題: python openmv色塊跟隨源程序
openmv色塊跟隨,串口打印出色塊區(qū)域的中點坐標
利用openmv實現(xiàn)色塊追隨,在程序中調(diào)整閾值即可實現(xiàn)任意顏色的跟隨
,想要提升幀率可以把圖像格式改為QQVGA

源程序如下:
  1. # Blob Detection and uart transport
  2. import sensor, image, time
  3. from pyb import UART
  4. import json
  5. # For color tracking to work really well you should ideally be in a very, very,
  6. # very, controlled enviroment where the lighting is constant...
  7. yellow_threshold   = (31, 88, 40, 127, -25, 122)
  8. # You may need to tweak the above settings for tracking green things...
  9. # Select an area in the Framebuffer to copy the color settings.

  10. sensor.reset() # Initialize the camera sensor.
  11. sensor.set_pixformat(sensor.RGB565) # use RGB565.
  12. sensor.set_framesize(sensor.QVGA) # use QQVGA for speed.
  13. sensor.skip_frames(10) # Let new settings take affect.
  14. sensor.set_auto_whitebal(False) # turn this off.
  15. clock = time.clock() # Tracks FPS.

  16. uart = UART(3, 115200)
  17. def find_max(blobs):
  18.     max_size=0
  19.     for blob in blobs:
  20.         if blob.pixels() > max_size:
  21.             max_blob=blob
  22.             max_size = blob.pixels()
  23.     return max_blob

  24. while(True):
  25.     img = sensor.snapshot() # Take a picture and return the image.

  26.     blobs = img.find_blobs([yellow_threshold])
  27.     if blobs:
  28.         max_blob=find_max(blobs)
  29.         print('sum :', len(blobs))
  30.         img.draw_rectangle(max_blob.rect())
  31.         img.draw_cross(max_blob.cx(), max_blob.cy())

  32.         output_str="%.3d,%.3d" % (max_blob.cx(),max_blob.cy()) #方式1
  33.         #output_str=json.dumps([max_blob.cx(),max_blob.cy()]) #方式2
  34.         print('you send:',output_str)
  35.         uart.write(output_str+'\r\n')
  36.     else:
  37.         print('not found!')
復制代碼

以上資料51hei提供下載:
uart_red.zip (1.16 KB, 下載次數(shù): 25)







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1