36 lines
1.2 KiB
Python
36 lines
1.2 KiB
Python
|
import sensor, lcd, time
|
||
|
|
||
|
lcd.init(freq=15000000) # 初始化LCD
|
||
|
sensor.reset() # 复位和初始化摄像头
|
||
|
sensor.set_vflip(1) # 将摄像头设置成后置方式(所见即所得)
|
||
|
sensor.set_pixformat(sensor.RGB565) # 设置像素格式为彩色 RGB565
|
||
|
sensor.set_framesize(sensor.QVGA) # 设置帧大小为 QVGA (320x240)
|
||
|
sensor.set_auto_gain(False)
|
||
|
sensor.set_auto_whitebal(False)
|
||
|
sensor.set_auto_gain(0,0)
|
||
|
sensor.skip_frames(time = 200) # 等待设置生效
|
||
|
|
||
|
# 阈值设置
|
||
|
thresholds = [(35, 100, 6, 127, 0, 127), # 红色阈值
|
||
|
(25, 100, -6, 127, 5, 43), # 黄色阈值
|
||
|
(87, 100, -59, 127, -10, 127)] # 绿色阈值
|
||
|
|
||
|
# while True:
|
||
|
img = sensor.snapshot()
|
||
|
max_blobs = []
|
||
|
|
||
|
for threshold in thresholds:
|
||
|
# 查找每种颜色的色块
|
||
|
blobs = img.find_blobs([threshold], pixels_threshold=200, area_threshold=200, merge=True)
|
||
|
if blobs:
|
||
|
# 找到最大的色块
|
||
|
max_blob = max(blobs, key=lambda b: b.pixels())
|
||
|
max_blobs.append(max_blob)
|
||
|
|
||
|
# 绘制每种颜色最大色块的矩形和中心十字
|
||
|
for b in max_blobs:
|
||
|
img.draw_rectangle(b[0:4])
|
||
|
img.draw_cross(b[5], b[6])
|
||
|
|
||
|
lcd.display(img)
|