opencv计算fps
时间: 2023-09-22 08:05:00 浏览: 53
在 OpenCV 中计算 FPS(Frames Per Second,每秒帧数)可以使用以下步骤:
1.获取当前时间戳
2.读取每一帧图像
3.获取当前时间戳
4.计算时间差
5.计算 FPS 值
下面是示例代码:
```python
import cv2
import time
# 打开摄像头
cap = cv2.VideoCapture(0)
# 获取当前时间戳
start_time = time.time()
# 读取每一帧图像
while True:
ret, frame = cap.read()
# 获取当前时间戳
end_time = time.time()
# 计算时间差
time_diff = end_time - start_time
# 计算 FPS 值
fps = 1 / time_diff
# 在图像上标注 FPS 值
cv2.putText(frame, "FPS: {:.2f}".format(fps), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
# 显示图像
cv2.imshow("frame", frame)
# 等待按键
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 更新时间戳
start_time = end_time
# 释放摄像头
cap.release()
# 关闭窗口
cv2.destroyAllWindows()
```
在上面的代码中,我们使用 `time.time()` 函数获取当前时间戳,然后读取每一帧图像,再次获取时间戳并计算时间差,最后计算 FPS 值并在图像上标注。这样就可以实时计算摄像头的 FPS 值了。