import cv2 # 创建跟踪器对象 tracker = cv2.TrackerCSRT_create() # 打开视频文件 video = cv2.VideoCapture('plane.mp4') # 读取第一帧 ok, frame = video.read() # 选择要跟踪的目标区域 bbox = cv2.selectROI(frame, False) # 初始化跟踪器 ok = tracker.init(frame, bbox) while True: # 读取当前帧 ok, frame = video.read() if not ok: break # 跟踪目标 ok, bbox = tracker.update(frame) # 如果跟踪成功,绘制跟踪框和中心点 if ok: # 获取跟踪框坐标 x, y, w, h = [int(i) for i in bbox] # 绘制跟踪框 cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) # 计算中心点坐标 center_x = x + w/2 center_y = y + h/2 # 绘制中心点 cv2.circle(frame, (int(center_x), int(center_y)), 5, (0, 0, 255), -1) # 显示中心点坐标 text = 'Center: ({:.1f}, {:.1f})'.format(center_x, center_y) cv2.putText(frame, text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2) # 特征点检测 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) detector = cv2.ORB_create() keypoints = detector.detect(gray, None) for kp in keypoints: x, y = kp.pt cv2.circle(frame, (int(x), int(y)), 3, (255, 0, 0), -1) cv2.putText(frame, 'Head', (int(x)-10, int(y)-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2) cv2.putText(frame, '({:.1f}, {:.1f})'.format(x, y), (int(x)-50, int(y)-30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2) # 显示视频帧 cv2.imshow('Airplane Tracking', frame) # 等待按下 ESC 键退出 if cv2.waitKey(1) == 27: break # 释放资源 video.release() cv2.destroyAllWindows()将这段代码的特征点改为只显示其中一个特征点
时间: 2024-04-28 12:19:47 浏览: 73
可以将for循环中的绘制特征点的部分改为只绘制第一个检测到的特征点,代码如下:
```
if keypoints:
kp = keypoints[0]
x, y = kp.pt
cv2.circle(frame, (int(x), int(y)), 3, (255, 0, 0), -1)
cv2.putText(frame, 'Head', (int(x)-10, int(y)-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
cv2.putText(frame, '({:.1f}, {:.1f})'.format(x, y), (int(x)-50, int(y)-30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
```
阅读全文