基于关键点的物体追踪示例

# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# 基于关键点的物体追踪示例
# 向摄像头展示物体后运行脚本。系统将提取一组关键点
# 并在后续帧中进行追踪。如需重新提取关键点,请再次运行
# 脚本。注意:请查阅文档了解如何调整find_keypoints和match_keypoints的参数
import csi
import time
import image

csi0 = csi.CSI()
csi0.reset()
csi0.contrast(3)
csi0.gainceiling(16)
csi0.framesize(csi.VGA)
csi0.window((320, 240))
csi0.pixformat(csi.GRAYSCALE)

csi0.snapshot(time=2000)
csi0.auto_gain(False, gain_db=100)


def draw_keypoints(img, kpts):
    if kpts:
        print(kpts)
        img.draw_keypoints(kpts)
        img = csi0.snapshot()
        time.sleep_ms(1000)


kpts1 = None
# 注意:取消注释以从文件加载关键点描述符
# kpts1 = image.load_descriptor("desc.orb")
# img = csi0.snapshot()
# draw_keypoints(img, kpts1)

clock = time.clock()
while True:
    clock.tick()
    img = csi0.snapshot()
    if kpts1 is None:
        # 注意:默认情况下find_keypoints会返回从图像金字塔中提取的多尺度关键点
        kpts1 = img.find_keypoints(max_keypoints=150, threshold=10, scale_factor=1.2)
        draw_keypoints(img, kpts1)
    else:
        # 注意:当提取关键点以匹配第一个描述符时,我们使用normalized=True仅从第一层尺度提取
        # 关键点,这将与第一个描述符中的某个尺度相匹配
        kpts2 = img.find_keypoints(max_keypoints=150, threshold=10, normalized=True)
        if kpts2:
            match = image.match_descriptor(kpts1, kpts2, threshold=85)
            if match.count > 10:
                # 如果我们至少有 n 个“良好匹配”
                # 绘制边界矩形和十字。
                img.draw_detection(match, size=10)

            print(kpts2, "matched:%d dt:%d" % (match.count, match.theta))
            # 注意:如果想绘制特征点,请取消注释
            # img.draw_keypoints(kpts2, size=KEYPOINTS_SIZE, matched=True)

    # 绘制帧率(FPS)
    img.draw_string((0, 0), "FPS:%.2f" % (clock.fps()))

results matching ""

    No results matching ""