介绍
本篇博客介绍了如何使用v4l-utils
工具来查看所购买的USB摄像头支持的分辨率等信息,并使用一个短小的 opencv
脚本将图像读取出来。
测试 USB 摄像头支持的分辨率和压缩格式
- 需要的工具:
apt install v4l-utils
- 查找已经连接的USB设备名称:
ls /dev/video*
- 查看对应相机支持的型号与格式:命令行输入
v4l2-ctl -d /dev/video2 --list-formats-ext
得到如下输出结果:
ioctl: VIDIOC_ENUM_FMT
Type: Video Capture
[0]: 'MJPG' (Motion-JPEG, compressed)
Size: Discrete 1920x1080
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 2048x1536
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 1600x1200
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 1280x1024
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 1280x720
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 800x600
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 640x480
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)
[1]: 'YUYV' (YUYV 4:2:2)
Size: Discrete 1920x1080
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 2048x1536
Interval: Discrete 0.333s (3.000 fps)
Size: Discrete 1600x1200
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 1280x1024
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 1280x720
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 800x600
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 640x480
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Interval: Discrete 0.200s (5.000 fps)
- 输出结果的解读:示例相机支持
MJPG
和YUYV
两种传输格式,每种格式支持多种不同的分辨率,每种分辨率对应相应的连续读取帧率。
使用 OpenCV 按正确的格式读取图像(以python3
为例)
由于通常MJPG
格式才能满足实时性的要求,且图像损失也并不严重,此处介绍MJPG
的配置方法。
- 需要的工具:
apt install python3-opencv
- 需要对
cv2.VideoCapture
实例进行额外的参数配置,示例代码如下:
# In file `cv_viewer.py`
import cv2
import sys
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("device", type=str, nargs='?', default='/dev/video0')
parser.add_argument("width", type=int, nargs='?', default=1920)
parser.add_argument("height", type=int, nargs='?', default=1080)
parser.add_argument("fps", type=int, nargs='?', default=30)
args = parser.parse_args()
print(args)
cap = cv2.VideoCapture(f"v4l2src device={args.device} do-timestamp=true ! image/jpeg, width={args.width}, height={args.height}, framerate={args.fps}/1 ! jpegdec ! videoconvert ! appsink")
if not cap.isOpened():
cap = cv2.VideoCapture(args.device)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, args.width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, args.height)
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter__fourcc(*"MJPG"))
cap.set(cv2.CAP_PROP_FPS, args.fps)
ok, img = cap.read()
assert ok, "Camera Open Failed"
print("Actual img shape is ", img.shape)
while ok:
cv2.imshow("img", img)
if 27 == cv2.waitKey(1): break # Press `Esc` to exit.
ok, img = cap.read()
- 调用时按顺序传入设备路径、宽、长、帧率即可,注意这些数值都应与
v4l2-ctl
提示的备选值一致。例:python3 cv_viewer.py /dev/video2 1920 1080 30
。注意请使用设备名而不是数字作为相机代号。