Embedded_game/002_B_Car/颜色识别.py
2025-01-02 12:48:11 +08:00

52 lines
1.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'''
实验名称:颜色识别
版本: v1.0
实验目的:实现单个颜色识别
'''
import sensor,lcd,time
lcd.init(freq=15000000) #初始化LCD
sensor.reset() #复位和初始化摄像头
sensor.set_vflip(1) #将摄像头设置成后置方式(所见即所得)
sensor.set_pixformat(sensor.RGB565) # 设置像素格式为彩色 RGB565
#sensor.set_pixformat(sensor.GRAYSCALE) # 设置像素格式为灰色
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) # 等待设置生效
clock = time.clock() # 创建一个时钟来追踪 FPS每秒拍摄帧数
#lcd初始化
lcd.init()
clock=time.clock()
# 颜色识别阈值 (L Min, L Max, A Min, A Max, B Min, B Max) LAB模型
# 下列阈值元组用来识别 红、绿、蓝三种颜色,可通过调整数据阈值完成更多颜色的识别。
# thresholds = [(59, 100, 40, 127, 5, 127), # 红色阈值
# (87, 100, -59, 127, -10, 127), # 绿色阈值
# (0, 30, 0, 64, -128, -20)] # 蓝色阈值
thresholds = [(35, 100, 6, 127, 0, 127), # 红色阈值
(25, 100, -6, 127, 5, 43), # 黄色阈值
(87, 100, -59, 127, -10, 127)] # 绿色阈值
while True:
clock.tick()
img=sensor.snapshot()
img = img.resize(224, 224)
blobs = img.find_blobs([thresholds[1]]) # 0,1,2分别表示红绿色。
if blobs:
print(blobs)
for b in blobs:
print(b)
tmp=img.draw_rectangle(b[0:4])
tmp=img.draw_cross(b[5], b[6])
lcd.display(img) #LCD显示图片
print(clock.fps()) #打印FPS