multi_color_blob_tracking.py

# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# 多色斑点跟踪示例
#
# 此示例展示了使用 OpenMV Cam 进行多色斑点跟踪。

import csi
import time
import math

# 颜色跟踪阈值(L 最小值,L 最大值,A 最小值,A 最大值,B 最小值,B 最大值)
# 以下阈值通常跟踪红色/绿色物体。您可能需要调整它们...
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
]
# 您最多可设置16个阈值,但实际上在颜色阈值开始严重重叠前,很难用16个阈值对任何
# 场景进行有效分割。

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=200, area_threshold=200):
        # 这些数值依赖于色块非圆形的特性——否则它们会变得不稳定。
        if blob.elongation > 0.5:
            img.draw_edges(blob.min_corners, color=(255, 0, 0))
            img.draw_line(image.get_major_axis_line(blob), color=(0, 255, 0))
            img.draw_line(image.get_minor_axis_line(blob), color=(0, 0, 255))
        # 这些数值始终保持稳定。
        img.draw_detection(blob)
        # 注意 - 色块旋转角度仅在0-180度范围内唯一。
        img.draw_keypoints(
            [(blob.cx, blob.cy, int(math.degrees(blob.rotation)))], size=20
        )
    print(clock.fps())

results matching ""

    No results matching ""