Example: 05-Feature-Detection/keypoints.py

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

# 重置传感器
sensor.reset()

# 传感器设置
sensor.set_contrast(3)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.VGA)
sensor.set_windowing((320, 240))
sensor.set_pixformat(sensor.GRAYSCALE)

sensor.skip_frames(time=2000)
sensor.set_auto_gain(False, value=100)


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


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

clock = time.clock()
while True:
    clock.tick()
    img = sensor.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_rectangle(match.rect())
                img.draw_cross(match.cx(), match.cy(), size=10)

            print(kpts2, "matched:%d dt:%d" % (match.count(), match.theta()))
            # NOTE: uncomment if you want to draw the keypoints
            # img.draw_keypoints(kpts2, size=KEYPOINTS_SIZE, matched=True)

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

results matching ""

    No results matching ""