目录
本博文描述了在Ubuntu系统下如何使用v4l-utils工具来查看所连接摄像头(包含USB摄像头及树莓派摄像头)信息,主要内容如下:
1. 获取摄像头列表;
2. 获取各摄像头支持的分辨率、帧率、像素格式;
3. 基于OpenCV抓取摄像头图像。
1. 安装v4l-utils
sudo apt-get install v4l-utils
如果有以下报错:
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package v4l-utils is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
E: Package 'v4l-utils' has no installation candidate
则先更新源,再安装即可。
sudo apt-get update
2. 查找连接的摄像头列表
ls /dev/video*
输出
/dev/video0 /dev/video1
或
扫描二维码关注公众号,回复:
14648868 查看本文章
v4l2-ctl --list-devices
输出
vi-output, imx219 7-0010 (platform:54080000.vi:0):
/dev/video0
USB Camera (usb-70090000.xusb-2.3):
/dev/video1
3. 查看各相机支持的分辨率、帧率、像素格式等参数
v4l2-ctl -d /dev/video0 --list-formats-ext
ioctl: VIDIOC_ENUM_FMT
Index : 0
Type : Video Capture
Pixel Format: 'RG10'
Name : 10-bit Bayer RGRG/GBGB
Size: Discrete 3264x2464
Interval: Discrete 0.048s (21.000 fps)
Size: Discrete 3264x1848
Interval: Discrete 0.036s (28.000 fps)
Size: Discrete 1920x1080
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 1640x1232
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 1280x720
Interval: Discrete 0.017s (60.000 fps)
v4l2-ctl -d /dev/video1 --list-formats-ext
ioctl: VIDIOC_ENUM_FMT
Index : 0
Type : Video Capture
Pixel Format: 'MJPG' (compressed)
Name : Motion-JPEG
Size: Discrete 1920x1080
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 1280x720
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 640x480
Interval: Discrete 0.033s (30.000 fps)
Index : 1
Type : Video Capture
Pixel Format: 'YUYV'
Name : YUYV 4:2:2
Size: Discrete 1920x1080
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 1280x720
Interval: Discrete 0.100s (10.000 fps)
Size: Discrete 640x480
Interval: Discrete 0.033s (30.000 fps)
4. 注意事项
(1)如上所示的/dev/video1 USB摄像头支持 MJPG 和 YUYV 两种像素格式,每种格式支持多种分辨率及帧率。如果需要实时抓取1080p的图像,则需要采用MJPG格式,默认的YUYV格式不能满足实时抓取需求。
(2)另外,调用摄像头时传入的摄像头设备、图像帧宽、帧高、帧率,应与v4l2-ctl 打印的参数一致。调用代码如下。
5. OpenCV(C++)调用摄像头代码
#include <iostream>
#include <opencv2/opencv.hpp>
int main()
{
cv::VideoCapture capture;
capture.open("/dev/video1", cv::CAP_V4L2);
capture.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M','J','P','G'));
capture.set(cv::CAP_PROP_FRAME_WIDTH, 1920);
capture.set(cv::CAP_PROP_FRAME_HEIGHT, 1080);
capture.set(cv::CAP_PROP_FPS, 30);
cv::Mat frame;
while (1)
{
capture >> frame;
if (!frame.empty())
{
cv::imshow("frame", frame);
}
if (cv::waitKey(1) == 27)
break;
}
return 0;
}