import sensor, image, time, math
thresholds = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds
(30, 100, -64, -8, -32, 32), # generic_green_thresholds
(0, 15, 0, 40, -80, -20)] # generic_blue_thresholds
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) #自动增益开启(True)或者关闭(False)。在使用颜色追踪时,需要关闭自动增益。
sensor.set_auto_whitebal(False) # 自动白平衡开启(True)或者关闭(False)。在使用颜色追踪时,需要关闭自动白平衡。
clock = time.clock()
sensor.set_vflip(True)#垂直方向反转
int max_area=0;
while(True):
clock.tick()
img = sensor.snapshot()
for blob in img.find_blobs([thresholds[0]], pixels_threshold=200, area_threshold=200):
if (blob.area()>max_area)
max_area = blob.area()
if (max_area>blob.area())
img.draw_rectangle(blob.rect())
我主要想通过这个代码来识别出距离摄像头最近的红球
https://book.openmv.cc/project/zhui-xiao-qiu-de-xiao-8f665d28-project-pan-tilt-md.html#%E8%B0%83%E6%95%B4%E5%8F%82%E6%95%B0%EF%BC%8C%E5%AE%9E%E7%8E%B0%E8%B7%9F%E9%9A%8F
看里面的find_max函数。