v4l2是Video4linux2的简称,是linux中关于视频设备的内核驱动,在Linux中,视频设备是设备文件,可以像访问普通文件一样对其进行读写,摄像头设备文件位置是/dev/video0。V4L2在设计时,是要支持很多广泛的设备的,它们之中只有一部分在本质上是真正的视频设备。
因为我们这篇文章不涉及内核部分摄像头驱动的实现,大致可以简单说下,内核部分的实现分两部分:
1.我们要根据摄像头的种类,实现具体的摄像头传感器的驱动,这里可能有一些数据和控制的通信总线的协议。
2.然后这个具体的驱动需要适配这个v4l2这个框架,然后向用户层映射成一个字符设备文件。
而我们的主题就是对这个设备文件进行操作,这就是应用层该做的事。
目录
1 v4l2-ctl 常用命令介绍:
2 查看系统识别的USB 相机设备:
3 显示测试----需要桌面终端的配合
4 ubuntu下使用v4l2-ctl查看USB摄像头参数
4.1 查看可被调用的设备
4.2 查看摄像头所支持的指定格式的分辨率
4.3查看usb摄像头所有可用的分辨率
4.4 相机的曝光增益查询
4.5 修改增益参数
4.6 相机参数说明
#List all video devices:
v4l2-ctl --list-devices
#List supported video formats and resolutions of default video device
v4l2-ctl --list-formats-ext
#List supported video formats and resolutions of a specific video device:
v4l2-ctl --list-formats-ext --device path/to/video_device
#eg:
v4l2-ctl --list-formats-ext --device /dev/video0
#Get all details of a video device:
v4l2-ctl --all --device path/to/video_device
#eg:
v4l2-ctl --all --device /dev/video0
#Capture a JPEG photo with a specific resolution from video device:
v4l2-ctl --device path/to/video_device --set-fmt-video=width=width,height=height,pixelformat=MJPG --stream-mmap --stream-to=path/to/output.jpg --stream-count=1
#eg:
v4l2-ctl --device /dev/video0 --set-fmt-video=width=1280,height=720,pixelformat=MJPG --stream-mmap --stream-to=/home/nvidia/Pictures/video0-output.jpg --stream-count=1
#Capture a raw video stream from video device:
v4l2-ctl --device path/to/video_device --set-fmt-video=width=width,height=height,pixelformat=format --stream-mmap --stream-to=path/to/output --stream-count=number_of_frames_to_capture
#eg:
v4l2-ctl --device /dev/video0 --set-fmt-video=width=1280,height=720,pixelformat=MJPG --stream-mmap --stream-to=/home/nvidia/Pictures --stream-count=10
#List all video device's controls and their values:
v4l2-ctl --list-ctrls --device /path/to/video_device
#eg:
v4l2-ctl --list-ctrls --device /dev/video0
ls /dev/video*
ls /dev/v*
cheese -d /dev/video0
v4l2-ctl -d /dev/video0 --all
如果没有相关命令,请安装
v4l2-ctl --all -d /dev/video0查看相机参数时报错:
v4l2-ctl :command not found
手动安装:
sudo apt-get install v4l-utils
相机详细信息(我自己的):
v4l2-ctl -d /dev/video0 --all
v4l2-ctl --list-ctrls --device /dev/video0
brightness 0x00980900 (int) : min=-64 max=64 step=1 default=0 value=0
contrast 0x00980901 (int) : min=0 max=64 step=1 default=32 value=32
saturation 0x00980902 (int) : min=0 max=128 step=1 default=64 value=64
hue 0x00980903 (int) : min=-40 max=40 step=1 default=0 value=0
white_balance_temperature_auto 0x0098090c (bool) : default=1 value=1
gamma 0x00980910 (int) : min=72 max=500 step=1 default=100 value=100
gain 0x00980913 (int) : min=0 max=100 step=1 default=0 value=0
power_line_frequency 0x00980918 (menu) : min=0 max=2 default=1 value=1
white_balance_temperature 0x0098091a (int) : min=2800 max=6500 step=1 default=4600 value=4600 flags=inactive
sharpness 0x0098091b (int) : min=0 max=16 step=1 default=8 value=8
backlight_compensation 0x0098091c (int) : min=0 max=121 step=121 default=0 value=121
exposure_auto 0x009a0901 (menu) : min=0 max=3 default=3 value=3
exposure_absolute 0x009a0902 (int) : min=1 max=5000 step=1 default=157 value=157 flags=inactive
exposure_auto_priority 0x009a0903 (bool) : default=0 value=1
nvidia@nvidia-desktop:~/Pictures$ v4l2-ctl -d /dev/video0 --all
Driver Info (not using libv4l2):
Driver name : uvcvideo
Card type : LRCP USB2.0
Bus info : usb-70090000.xusb-3.4
Driver version: 4.9.253
Capabilities : 0x84200001
Video Capture
Streaming
Extended Pix Format
Device Capabilities
Device Caps : 0x04200001
Video Capture
Streaming
Extended Pix Format
Priority: 2
Video input : 0 (Camera 1: ok)
Format Video Capture:
Width/Height : 1280/720
Pixel Format : 'MJPG'
Field : None
Bytes per Line : 0
Size Image : 1843789
Colorspace : Default
Transfer Function : Default (maps to Rec. 709)
YCbCr/HSV Encoding: Default (maps to ITU-R 601)
Quantization : Default (maps to Full Range)
Flags :
Crop Capability Video Capture:
Bounds : Left 0, Top 0, Width 1280, Height 720
Default : Left 0, Top 0, Width 1280, Height 720
Pixel Aspect: 1/1
Selection: crop_default, Left 0, Top 0, Width 1280, Height 720
Selection: crop_bounds, Left 0, Top 0, Width 1280, Height 720
Streaming Parameters Video Capture:
Capabilities : timeperframe
Frames per second: 30.000 (30/1)
Read buffers : 0
brightness 0x00980900 (int) : min=-64 max=64 step=1 default=0 value=0
contrast 0x00980901 (int) : min=0 max=64 step=1 default=32 value=32
saturation 0x00980902 (int) : min=0 max=128 step=1 default=64 value=64
hue 0x00980903 (int) : min=-40 max=40 step=1 default=0 value=0
white_balance_temperature_auto 0x0098090c (bool) : default=1 value=1
gamma 0x00980910 (int) : min=72 max=500 step=1 default=100 value=100
gain 0x00980913 (int) : min=0 max=100 step=1 default=0 value=0
power_line_frequency 0x00980918 (menu) : min=0 max=2 default=1 value=1
white_balance_temperature 0x0098091a (int) : min=2800 max=6500 step=1 default=4600 value=4600 flags=inactive
sharpness 0x0098091b (int) : min=0 max=16 step=1 default=8 value=8
backlight_compensation 0x0098091c (int) : min=0 max=121 step=121 default=0 value=121
exposure_auto 0x009a0901 (menu) : min=0 max=3 default=3 value=3
exposure_absolute 0x009a0902 (int) : min=1 max=5000 step=1 default=157 value=157 flags=inactive
exposure_auto_priority 0x009a0903 (bool) : default=0 value=1
nvidia@nvidia-desktop:~/Workspace/test/06-GStreamer/01-rtsp_ipc_play$ v4l2-ctl --list-devices
LRCP USB2.0 (usb-70090000.xusb-3):
/dev/video0
nvidia@nvidia-desktop:~$ sudo v4l2-ctl --list-framesizes=MJPG -d /dev/video0
[sudo] password for nvidia:
ioctl: VIDIOC_ENUM_FRAMESIZES
Size: Discrete 1280x720
Size: Discrete 800x600
Size: Discrete 640x480
Size: Discrete 640x360
Size: Discrete 352x288
Size: Discrete 320x240
nvidia@nvidia-desktop:~/Pictures$ v4l2-ctl --list-formats-ext --device /dev/video0
ioctl: VIDIOC_ENUM_FMT
Index : 0
Type : Video Capture
Pixel Format: 'MJPG' (compressed)
Name : Motion-JPEG
Size: Discrete 1280x720
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 800x600
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 640x480
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 640x360
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 352x288
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 320x240
Interval: Discrete 0.033s (30.000 fps)
Index : 1
Type : Video Capture
Pixel Format: 'YUYV'
Name : YUYV 4:2:2
Size: Discrete 1280x720
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 800x600
Interval: Discrete 0.050s (20.000 fps)
Size: Discrete 640x480
Interval: Discrete 0.040s (25.000 fps)
Size: Discrete 640x360
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 352x288
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 320x240
Interval: Discrete 0.033s (30.000 fps)
sudo v4l2-ctl -d /dev/video0 --list-formats//查看当前摄像头支持的视频压缩格式
sudo v4l2-ctl -d /dev/video0 --list-formats-ext//查看当前摄像头支持的分辨率和帧速率
sudo v4l2-ctl -d /dev/video0 --set-parm=30//设置帧率
sudo v4l2-ctl --list-framesizes=MJPG -d /dev/video0//查看摄像头所支持的分辨率
sudo v4l2-ctl --set-fmt-video=width=320,height=240,pixelformat=YUYV
sudo v4l2-ctl --set-fmt-video=width=640,height=480,pixelformat=YUYV
sudo v4l2-ctl -d /dev/video0 --set-parm=30//设置帧率
sudo v4l2-ctl --set-fmt-video=width=1280,height=720,pixelformat=MJPG
nvidia@nvidia-desktop:~/Workspace/test/captu$ v4l2-ctl -d /dev/video0 --list-ctrls
brightness 0x00980900 (int) : min=-64 max=64 step=1 default=0 value=0
contrast 0x00980901 (int) : min=0 max=64 step=1 default=32 value=32
saturation 0x00980902 (int) : min=0 max=128 step=1 default=64 value=64
hue 0x00980903 (int) : min=-40 max=40 step=1 default=0 value=0
white_balance_temperature_auto 0x0098090c (bool) : default=1 value=1
gamma 0x00980910 (int) : min=72 max=500 step=1 default=100 value=100
gain 0x00980913 (int) : min=0 max=100 step=1 default=0 value=0
power_line_frequency 0x00980918 (menu) : min=0 max=2 default=1 value=1
white_balance_temperature 0x0098091a (int) : min=2800 max=6500 step=1 default=4600 value=4600 flags=inactive
sharpness 0x0098091b (int) : min=0 max=16 step=1 default=8 value=8
backlight_compensation 0x0098091c (int) : min=0 max=121 step=121 default=0 value=121
exposure_auto 0x009a0901 (menu) : min=0 max=3 default=3 value=3
exposure_absolute 0x009a0902 (int) : min=1 max=5000 step=1 default=157 value=157 flags=inactive
exposure_auto_priority 0x009a0903 (bool) : default=0 value=1
v4l2-ctl --device=/dev/video0 --set-ctrl=exposure_auto=1
v4l2-ctl --device=/dev/video0 --set-ctrl=exposure_absolute=200
brightness 0x00980900 (int) : min=-64 max=64 step=1 default=-40 value=20
contrast 0x00980901 (int) : min=0 max=100 step=1 default=50 value=100//对比度
saturation 0x00980902 (int) : min=0 max=100 step=1 default=50 value=50//饱和度
hue 0x00980903 (int) : min=-180 max=180 step=1 default=0 value=0//色彩
white_balance_temperature_auto 0x0098090c (bool) : default=1 value=1//自动白平衡
gamma 0x00980910 (int) : min=100 max=500 step=1 default=300 value=300//伽马,数字越大,图片的亮度也就越高
power_line_frequency 0x00980918 (menu) : min=0 max=2 default=2 value=2
white_balance_temperature 0x0098091a (int) : min=2800 max=6500 step=10 default=4600 value=4600 flags=inactive//白平衡
sharpness 0x0098091b (int) : min=0 max=100 step=1 default=50 value=50//锐度
backlight_compensation 0x0098091c (int) : min=0 max=2 step=1 default=0 value=0//背光补偿
exposure_auto_priority 0x009a0903 (bool) : default=0 value=0
focus_auto 0x009a090c (bool) : default=1 value=0
v4l2-ctl --device=/dev/video0 --set-fmt-video=width=1280,height=720,pixelformat=0