keypoints.py
# Esta obra está licenciada bajo la licencia MIT.
# Copyright (c) 2013-2023 OpenMV LLC. Todos los derechos reservados.
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# Ejemplo de seguimiento de objetos con puntos clave.
# Muestre un objeto a la cámara y luego ejecute el script. Se extraerá un conjunto de puntos clave
# una vez y luego se rastreará en los siguientes fotogramas. Si desea un nuevo conjunto de puntos clave, vuelva a ejecutar
# el script. NOTA: consulte la documentación para conocer los argumentos que permiten ajustar find_keypoints y 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
# NOTA: descomente para cargar un descriptor de puntos clave desde un archivo
# 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:
# NOTA: De forma predeterminada, find_keypoints devuelve puntos clave multiescala extraídos de una pirámide de imágenes.
kpts1 = img.find_keypoints(max_keypoints=150, threshold=10, scale_factor=1.2)
draw_keypoints(img, kpts1)
else:
# NOTA: Al extraer puntos clave para hacer coincidir con el primer descriptor, usamos normalized=True para extraer
# puntos clave únicamente de la primera escala, que coincidirá con una de las escalas del primer descriptor.
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:
# Si tenemos al menos n "buenas coincidencias"
# Dibujar rectángulo delimitador y cruz.
img.draw_detection(match, size=10)
print(kpts2, "matched:%d dt:%d" % (match.count, match.theta))
# NOTA: descomente si desea dibujar los puntos clave
# img.draw_keypoints(kpts2, size=KEYPOINTS_SIZE, matched=True)
# Dibujar FPS
img.draw_string((0, 0), "FPS:%.2f" % (clock.fps()))