keypoints.py
# この作品はMITライセンスの下で提供されています。
# Copyright (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()))