# Multi Color Blob Tracking Example
#
# This example shows off multi color blob tracking using the OpenMV Cam.
import sensor, image, time, math
red = (22, 67, 19, 84, -12, 53)# generic_red_thresholds
black= (0, 31, -34, 41, 24, -21)# generic_black_thresholds
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
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()
# Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are
# returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the
# camera resolution. Don't set "merge=True" becuase that will merge blobs which we don't want here.
def find_max(blobs): #寻找最大色块函数定义
max_pixels=0 #通过像素比较
for blob in blobs: #blob[0,1,2,3,4]=x,y,w,h,pixels (int)
if blob[4]>max_pixels:
max_blob=blob
max_pixels=blob[4]
return max_blob
while(True):
clock.tick()
img = sensor.snapshot()
red_blobs= img.find_blobs([red])
black_blobs= img.find_blobs([black])
if red_blobs:
max_blob_red=find_max(red_blobs)
img.draw_rectangle(max_blob_red.rect())
img.draw_cross(max_blob_red.cx(),max_blob_red.cy())
red_x_error = int(max_blob_red.cx()-img.width()/2)
red_y_error = int(max_blob_red.cy()-img.height()/2)
if black_blobs:
max_blob_black=find_max(black_blobs)
img.draw_rectangle(max_blob_black.rect())
img.draw_cross(max_blob_black.cx(),max_blob_black.cy())
black_x_error = int(max_blob_black.cx()-img.width()/2)
black_y_error = int(max_blob_black.cy()-img.height()/2)
#print(blob.code())
print(clock.fps())缩进不对,导致逻辑不对。