python实现:
import time import cv2 import numpy as np from models.matching import Matching from models.utils import (AverageTimer, VideoStreamer, make_matching_plot_fast, frame2tensor) import torch video = cv2.VideoCapture(0) fps = video.get(cv2.CAP_PROP_FPS) print("相机的FPS:{}".format(fps)) size = (int(video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))) print("相机:{}x{}".format(size[0], size[1])) r = 5 class OrbExtractor(object): def __init__(self, timer): self.timer = timer def work(self, img): gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) orb = cv2.ORB_create() keys, desps = orb.detectAndCompute(gray, None) self.timer.end() orb_frame = self.draw(img, keys, r, self.timer.start_value, self.timer.end_value) return orb_frame def draw(self, orb_frame, keypoints, r, start, end): for keypoint in keypoints: pt1 = tuple([int(value - r) for value in keypoint.pt]) pt2 = tuple([int(value + r) for value in keypoint.pt]) cv2.rectangle(orb_frame, pt1, pt2, color=(0, 255, 0)) pt = tuple(map(int, keypoint.pt)) cv2.circle(orb_frame, pt, 2, color=(0, 255, 0), thickness=-1) cv2.putText(orb_frame, "features:{}".format(len(keypoints)), (0, size[1] - 50), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, color=(255, 255, 255)) cv2.putText(orb_frame, "fps:{}".format(int(1 / (end - start))), (0, size[1] - 5), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, color=(255, 255, 255)) cv2.imshow("orb", orb_frame) return orb_frame class SuperPointsExtractor(object): def __init__(self, timer): self.timer = timer self.device = 'cuda' if torch.cuda.is_available() else 'cpu' config = { 'superpoint': { 'nms_radius': 4, 'keypoint_threshold': 0.005, 'max_keypoints': -1 }, 'superglue': { 'weights': 'indoor', 'sinkhorn_iterations': 20, 'match_threshold': 0.2, } } self._model = Matching(config).eval().to(self.device) self.keys = ['keypoints', 'scores', 'descriptors'] def work(self, frame): gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) frame_tensor = frame2tensor(gray, self.device) last_data = self._model.superpoint({'image': frame_tensor}) last_data = {k + '0': last_data[k] for k in self.keys} last_data['image0'] = frame_tensor self.timer.end() old_frame = self.draw(frame, last_data, r, self.timer.start_value, self.timer.end_value) cv2.imshow("superpoint", old_frame) return old_frame def draw(self, old_frame, keypoints, r, start, end): keypoints_list = keypoints['keypoints0'][0].tolist() for keypoint in keypoints_list: pt1 = tuple([int(value - r) for value in keypoint]) pt2 = tuple([int(value + r) for value in keypoint]) cv2.rectangle(old_frame, pt1, pt2, color=(0, 255, 0)) pt = tuple(map(int, keypoint)) cv2.circle(old_frame, pt, 2, color=(0, 255, 0), thickness=-1) cv2.putText(old_frame, "features:{}".format(len(keypoints_list)), (0, size[1] - 50), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, color=(255, 255, 255)) cv2.putText(old_frame, "fps:{}".format(int(1 / (end - start))), (0, size[1] - 5), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, color=(255, 255, 255)) return old_frame class Timer(object): def __init__(self): self.start_value = 0 self.end_value = 0 def start(self): self.start_value = time.time() return self.start def end(self): self.end_value = time.time() return self.end def run(): timer = Timer() orb = OrbExtractor(timer) super = SuperPointsExtractor(timer) cv2.namedWindow("orb", cv2.WINDOW_NORMAL) cv2.namedWindow("superpoint", cv2.WINDOW_NORMAL) while True: timer.start() ret, frame = video.read() # orb特征提取 orb_frame = frame.copy() orb_frame = orb.work(orb_frame) superpoint_frame = frame.copy() super.work(superpoint_frame) cv2.waitKey(3) if __name__ == "__main__": run()
原理参考:
[1]. (四十二)特征点检测-ORB - 知乎
[2]. 定位关键点ORB_create()函数解析_Bug刺客的博客-CSDN博客_orb_create
[3]. ORB 特征提取算法(实践篇一) - Alex777 - 博客园
[4].(1条消息) 【OpenCV】cv2.putText()函数用法_零碎@流年絮语的博客-CSDN博客_cv2.puttext
[5]. (1条消息) python+OpenCV图像处理(一)读取、复制、显示、保存_Jumping boy的博客-CSDN博客_img.copy()