你的代码逻辑有问题。
for max_blob in img.find_circles()应该是 for circle in img.find_circles(roi=max_blob.rect())
但是,如果判断色块是否为圆,直接判断圆度就行了。
import sensor, image, time, pyb
from pyb import UART
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()
red = 0
red_threshold = (9, 51, 29, 95, 20, 51)#red
uart = UART(3, 9600)
def find_max(blobs):
max_size=0
for blob in blobs:
if blob[2]*blob[3] > max_size:
max_blob=blob
max_size = blob[2]*blob[3]
return max_blob
while(True):
clock.tick()
img = sensor.snapshot()
blobs = img.find_blobs([red_threshold])
if blobs:
max_blob = find_max(blobs)
img.draw_rectangle(max_blob.rect())
print(max_blob.roundness())