例程讲解26-April-Tags->find_apriltags 寻找小的Apriltag

# 寻找小的Apriltag
#
# 此脚本展示如何使用blob颜色跟踪作为预过滤器,使用blob颜色跟踪查找图像中的Apriltags,
# 以查找标记首先出现的区域,然后在该blob上调用find_apriltags。

# 请注意,假设图像的大多数部分未通过阈值测试,则此脚本运行良好...
# 否则,您无法获得距离优势。


import sensor, image, time, math, omv

# 设置阈值以查找白色对象(即标记边框)
thresholds = (150, 255)

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
if omv.board_type() == "H7": sensor.set_framesize(sensor.VGA)
elif omv.board_type() == "M7": sensor.set_framesize(sensor.QVGA)
else: raise Exception("You need a more powerful OpenMV Cam to run this script")
sensor.skip_frames(time = 200) # 增加此值以使auto方法运行更长时间
sensor.set_auto_gain(False) # 必须关闭才能进行颜色跟踪
sensor.set_auto_whitebal(False) # 必须关闭才能进行颜色跟踪
clock = time.clock()

# apriltag代码最多支持可以同时处理6种tag家族。
# 返回的tag标记对象,将有其tag标记家族及其在tag标记家族内的id。


tag_families = 0
tag_families |= image.TAG16H5 # 注释掉,禁用这个家族
tag_families |= image.TAG25H7 # 注释掉,禁用这个家族
tag_families |= image.TAG25H9 # 注释掉,禁用这个家族
tag_families |= image.TAG36H10 # 注释掉,禁用这个家族
tag_families |= image.TAG36H11 # 注释掉以禁用这个家族(默认家族)
tag_families |= image.ARTOOLKIT # 注释掉,禁用这个家族

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

    # 首先,我们发现可能是标签的候选blob。
    box_list = []

    # 由于没有足够的RAM,apriltag标签可能会失败。
    tag_list = []

    for blob in img.find_blobs([thresholds], pixels_threshold=100, area_threshold=100, merge=True):
        # 接下来,我们在ROI中寻找比blob更大的tag。
        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不应该耗尽ram。
        # 但是,如果耗尽ram,我们进行以下处理
        try:
            tag_list.extend(img.find_apriltags(roi=(x,y,w,h), families=tag_families))
        except (MemoryError): # 不要捕获所有异常,否则你无法停止脚本。
            pass

    for b in box_list:
        img.draw_rectangle(b)
    # 现在打印出找到的tag
    for tag in tag_list:
        img.draw_rectangle(tag.rect())
        img.draw_cross(tag.cx(), tag.cy())
        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 ""