找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 4104|回復(fù): 0
收起左側(cè)

python openmv色塊跟隨源程序

[復(fù)制鏈接]
ID:255401 發(fā)表于 2019-8-5 19:55 | 顯示全部樓層 |閱讀模式
openmv色塊跟隨,串口打印出色塊區(qū)域的中點(diǎn)坐標(biāo)
利用openmv實(shí)現(xiàn)色塊追隨,在程序中調(diào)整閾值即可實(shí)現(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!')
復(fù)制代碼

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


回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表