Example: 02-Image-Processing/02-Color-Tracking/automatic_grayscale_color_tracking.py

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

import sensor
import time

print("Letting auto algorithms run. Don't put anything in front of the camera!")

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)
sensor.set_auto_gain(False)  # 必须关闭以进行颜色跟踪
sensor.set_auto_whitebal(False)  # 必须关闭以进行颜色跟踪
clock = time.clock()

# 捕捉图像中心任何内容的颜色阈值。
r = [(320 // 2) - (50 // 2), (240 // 2) - (50 // 2), 50, 50]  # QVGA 的 50x50 中心。

print(
    "Auto algorithms done. Hold the object you want to track in front of the camera in the box."
)
print(
    "MAKE SURE THE COLOR OF THE OBJECT YOU WANT TO TRACK IS FULLY ENCLOSED BY THE BOX!"
)
for i in range(60):
    img = sensor.snapshot()
    img.draw_rectangle(r)

print("Learning thresholds...")
threshold = [128, 128]  # 中间灰度值。
for i in range(60):
    img = sensor.snapshot()
    hist = img.get_histogram(roi=r)
    lo = hist.get_percentile(
        0.01
    )  # 获取直方图在 1% 范围内的 CDF(根据需要调整)!
    hi = hist.get_percentile(
        0.99
    )  # 获取直方图在 99% 范围内的 CDF(根据需要调整)!
    # 百分位值的平均值。
    threshold[0] = (threshold[0] + lo.value()) // 2
    threshold[1] = (threshold[1] + hi.value()) // 2
    for blob in img.find_blobs(
        [threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10
    ):
        img.draw_rectangle(blob.rect())
        img.draw_cross(blob.cx(), blob.cy())
        img.draw_rectangle(r)

print("Thresholds learned...")
print("Tracking colors...")

while True:
    clock.tick()
    img = sensor.snapshot()
    for blob in img.find_blobs(
        [threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10
    ):
        img.draw_rectangle(blob.rect())
        img.draw_cross(blob.cx(), blob.cy())
    print(clock.fps())

results matching ""

    No results matching ""