# 多颜色跟踪示例
#
# 这个例子显示了使用OpenMV的多色跟踪。
import sensor, image, time
# 颜色跟踪阈值(L Min, L Max, A Min, A Max, B Min, B Max)
# 下面的阈值跟踪一般红色/绿色的东西。你不妨调整他们...
thresholds = [(47, 68, 55, 103, 25, 63), # red_thresholds
(60, 75, -80, -40, 30, 50), # green_thresholds
(29, 49, -5, 25, -63, -35)] # blue_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
sensor.set_vflip(True)
sensor.set_hmirror(True)
clock = time.clock()
# 只有比“pixel_threshold”多的像素和多于“area_threshold”的区域才被
# 下面的“find_blobs”返回。 如果更改相机分辨率,
# 请更改“pixels_threshold”和“area_threshold”。 “merge = True”合并图像中所有重叠的色块。
while(True):
clock.tick()
img = sensor.snapshot().lens_corr(1.8)
for blob in img.find_blobs(thresholds, pixels_threshold=200, area_threshold=200):
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
print(clock.fps())
测距
import sensor, image, time
yellow_threshold = ( 56, 83, 5, 57, 63, 80)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(10)
sensor.set_auto_whitebal(False)
sensor.set_vflip(True)
sensor.set_hmirror(True)
clock = time.clock()
K=5000
while(True):
clock.tick()
img = sensor.snapshot().lens_corr(1.8)
blobs = img.find_blobs([yellow_threshold])
if len(blobs) == 1:
b = blobs[0]
img.draw_rectangle(b[0:4]) # rect
img.draw_cross(b[5], b[6]) # cx, cy
Lm = (b[2]+b[3])/2
length = K/Lm
print(length)
11点30分,2018年12月21日
import sensor, image
import time, math
from servo import Servos
from machine import I2C, Pin
i2c = I2C(sda=Pin('P5'), scl=Pin('P4'))
servo = Servos(i2c, address=0x40, freq=50, min_us=650, max_us=2800, degrees=180)
red_threshold = (47, 68, 55, 103, 25, 63)
green_threshold = (45, 70, -60, -40, 20, 60)
blue_threshold = (29, 49, -5, 25, -63,