multi_color_code_tracking.py
# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# 多色编码追踪示例
#
# 本示例展示了使用OpenMV Cam进行多色编码追踪的功能。
#
# 色码是由两种或以上颜色组成的色块。以下示例将仅追踪
# 包含两种或更多下述颜色的彩色物体。
import csi
import time
# 颜色跟踪阈值(L 最小值,L 最大值,A 最小值,A 最大值,B 最小值,B 最大值)
# 以下阈值通常跟踪红色/绿色物体。您可能需要调整它们...
thresholds = [
# generic_red_thresholds -> 索引为0,因此代码 == (1 << 0)
(30, 100, 15, 127, 15, 127,),
# generic_green_thresholds -> 索引为1,因此代码 == (1 << 1)
(30, 100, -64, -8, -32, 32,),
# generic_blue_thresholds -> 索引为2,因此代码 == (1 << 2)
(0, 15, 0, 40, -80, -20),
]
# 当“merge=True”用于“find_blobs”时,代码会进行或运算合并。
csi0 = csi.CSI()
csi0.reset()
csi0.pixformat(csi.RGB565)
csi0.framesize(csi.QVGA)
csi0.snapshot(time=2000)
csi0.auto_gain(False) # 必须关闭以进行颜色跟踪
csi0.auto_whitebal(False) # 必须关闭以进行颜色跟踪
clock = time.clock()
# 只有像素数超过“pixel_threshold”且面积超过“area_threshold”的blob才会被
# “find_blobs”返回。如果更改了“pixels_threshold”和“area_threshold”,请进行相应调整。
# 相机分辨率。必须设置“merge=True”以合并重叠的颜色块用于颜色代码。
while True:
clock.tick()
img = csi0.snapshot()
for blob in img.find_blobs(
thresholds, pixels_threshold=100, area_threshold=100, merge=True
):
if blob.code == 3: # 红/绿代码
img.draw_detection(blob, label="r/g", label_offset=(2, 2))
if blob.code == 5: # 红/蓝代码
img.draw_detection(blob, label="r/b", label_offset=(2, 2))
if blob.code == 6: # 绿/蓝代码
img.draw_detection(blob, label="g/b", label_offset=(2, 2))
if blob.code == 7: # 红/绿/蓝代码
img.draw_detection(blob, label="r/g/b", label_offset=(2, 2))
print(clock.fps())