使用 OpenCV 打开 USB 摄像头

Open USB Camera using OpenCV

我想在 Linux Mint 18.3 上使用 C++ 中的 OpenCV 打开 USB 摄像头。

相机已插入,并可与 Common Vision Blocks 的 SoftwareSuite 一起正常工作。

从命令 lsusb 我得到以下输出:

Bus 002 Device 005: ID 1ab2:0001  
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 002: ID 0cf3:e300 Atheros Communications, Inc. 
Bus 001 Device 003: ID 1bcf:2b95 Sunplus Innovation Technology Inc. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

第一个条目似乎是摄像头,因为如果拔下摄像头,该条目将不存在。但是我不明白为什么没有显示名字。

然后在网上查到USB摄像头在这个路径下: /dev/v4l/by-id/ or /dev/v4l/by-path//dev/v4l/by-id/ 的条目是:

usb-CNFEH73N3462520017B2_Integrated_Webcam_HD-video-index0

/dev/v4l/by-path/ 的条目是:

pci-0000:00:14.0-usb-0:12:1.0-video-index0

所以我想用 OpenCV 打开它:

VideoCapture 
cap("/dev/v4l/by-id/usb-CNFEH73N3462520017B2_Integrated_Webcam_HD-video-index0");

我通常将 Clion 作为 IDE 和 运行 使用 root,但在这两种情况下我都会收到以下错误:

GStreamer Plugin: Embedded video playback halted; module source reported: Could not read from resource.
OpenCV Error: Unspecified error (GStreamer: unable to start pipeline) in cvCaptureFromCAM_GStreamer
/opencv-3.0.0/modules/videoio/src/cap_gstreamer.cpp, line 773 terminate called after throwing an instance of 'cv::Exception'

如何打开 USB 摄像头以及如何使用 dev/v4l/ 正确打开它?

您应该通过int id 打开一个设备。尝试:

VideoCapture cap(0);

事实上,传递字符串 OpenCV 期望打开播放文件,例如 MPEG 文件,如 documentation.

中所述

这是可行的,但问题是打开的摄像头是集成摄像头,lsusb 报告为:

Bus 001 Device 003: ID 1bcf:2b95 Sunplus Innovation Technology Inc. 

实际上是 /dev/video0,链接为:

/dev/v4l/by-path/pci-0000:00:14.0-usb-0:12:1.0-video-index0

而是 lsusb 条目列为:

Bus 002 Device 005: ID 1ab2:0001  

是外接摄像头,好像不能识别为V4L设备。我不知道 "SoftwareSuite by Common Vision Blocks",所以我不知道这个软件是否可以作为 RAW 设备使用。