logo

先颜色识别,再模板匹配,该怎样修改

2019-11-15 22:53
W
_5mvs

```
import sensor, image, time, math
from pyb import UART
import json
green_threshold = [(30, 84, -58, -21, -12, 50),(30, 91, -58, -21, 0, 50),(30, 80, -62, -17, 12, 40),(30, 100, -78, -1, 20, 46)]
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(10)
sensor.set_auto_whitebal(False)
clock = time.clock()
uart = UART(3,115200)
uart.init(115200, bits=8, parity=None, stop=1)
def find_max(blobs):
     max_size=0
     for blob in blobs:
          if blob.pixels() > max_size:
               max_blob=blob
               max_size = blob.pixels()
     return max_blob
while(True):
     clock.tick()
     img = sensor.snapshot()
     blobs = img.find_blobs((green_threshold),area_threshold=250)
     if blobs:
          max_blob=find_max(blobs)
          for b in blobs:
                    img.draw_rectangle(b[0:4])
                    img.draw_rectangle(max_blob.rect())
                    img.draw_cross(max_blob.cx(), max_blob.cy())
                    green_ball="[%d,%d]" % (max_blob.cx(),max_blob.cy())
                    a = max_blob.cx()
                    if a<75:
                       date = 1
                    elif 75<a<85:
                       date = 2
                    elif a>85:
                       date = 3
                    D = bytearray(str(date))
                    uart.write(D+'\r\n')
                    print(green_ball)
                    print(date)