find_small_apriltags.py

# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# 小尺寸AprilTags检测
#
# 此脚本展示了如何将色块追踪用作预筛选,
# 先用色块追踪找到标签所在的
# 区域,然后再对该色块调用
# find_apriltags。

# 注意,此脚本在图像大部分区域无法通过阈值测试时
# 效果良好...否则,您不会获得距离上的
# 优势。

# 请为此脚本使用TAG36H11标签家族——这是推荐使用的标签家族。

import csi
import time

# 设置阈值以寻找白色物体(即标签边框)
thresholds = (150, 255)

csi0 = csi.CSI()
csi0.reset()
csi0.pixformat(csi.GRAYSCALE)
csi0.snapshot(time=200)  # 增大此值可让自动方法运行更长时间
csi0.auto_gain(False)  # 必须关闭以进行颜色跟踪
csi0.auto_whitebal(False)  # 必须关闭以进行颜色跟踪

clock = time.clock()

while True:
    clock.tick()
    img = csi0.snapshot()

    # 首先,我们寻找可能是标签候选的色块。
    box_list = []

    # AprilTags可能会因传入的图像太大、内存不足而失败。
    tag_list = []

    for blob in img.find_blobs(
        [thresholds], pixels_threshold=100, area_threshold=100, merge=True
    ):
        # 接下来我们在比色块更大的ROI中寻找标签。
        w = min(max(int(blob.w * 1.2), 10), 160)  # 不要太小,也不要太大。
        h = min(max(int(blob.h * 1.2), 10), 160)  # 不要太小,也不要太大。
        x = min(max(int(blob.x + (blob.w / 4) - (w * 0.1)), 0), img.width() - 1)
        y = min(max(int(blob.y + (blob.h / 4) - (h * 0.1)), 0), img.height() - 1)

        box_list.append((x, y, w, h))  # 我们稍后再绘制这些。

        # 由于我们限制了ROI大小,AprilTags应该不会耗尽内存。
        # 但如果内存不足,我们也会处理...
        try:
            tag_list.extend(img.find_apriltags(roi=(x, y, w, h)))
        except (
            MemoryError
        ):  # 不要捕获所有异常,否则您将无法停止脚本。
            pass

    for b in box_list:
        img.draw_rectangle(b)
    # 现在打印出找到的标签
    for tag in tag_list:
        img.draw_detection(tag)
        for c in tag.corners:
            img.draw_circle((c[0], c[1], 5))
        print("Tag:", tag.cx, tag.cy, tag.rotation, tag.id)

results matching ""

    No results matching ""