在OpenCV方法中使用设备名称而不是ID VideoCapture.open() [英] Using device name instead of ID in OpenCV method VideoCapture.open()
问题描述
我的 / dev
文件夹中有很多视频设备(例如 video1
, video2
,..., video9
)和一个 / dev / video
总是指向有效的设备(当然,可以改变)。
我想用OpenCV使用 cv :: Videocapture
打开 / dev / video
只有两种方法打开它:
I have a lot of video devices in my /dev
folder (e.g. video1
, video2
, ..., video9
) and one /dev/video
which is always pointing to the valid device (which, of course, can change).
I want to open the /dev/video
device with OpenCV using cv::Videocapture
and realized that there are only two ways to open it:
VideoCapture::VideoCapture(const string& filename)
VideoCapture::VideoCapture(int device)
第一个打开一个文件,第二个打开 / dev / video [device]
。
The first one opens a file and the second one opens /dev/video[device]
.
有任何方法可以做 cap = cv :: VideoCapture(/ dev / video);
Is there any way to do something like cap = cv::VideoCapture("/dev/video");
?
推荐答案
设备ID。
#include <regex>
#include <boost/filesystem.hpp>
...
boost::filesystem::path path( "/dev/video3" );
auto target = boost::filesystem::canonical(path).string();
std::regex exp( ".*video(\\d+)" );
std::smatch match;
std::regex_search( target, match, exp );
auto id = strtod( match[1] );
auto cap = cv::VideoCapture( id );
注意使用 canonical()
使路径绝对并解析任何符号链接。这样,即使你给它v4l设备路径从 / dev / v4l / by-id
或 / dev / v4l / by-path
例如:
Note the use of canonical()
to make the path absolute and resolve any symbolic links. This way it works even if you give it v4l device paths from /dev/v4l/by-id
or /dev/v4l/by-path
like for example:
"/dev/v4l/by-id/usb-046d_0990_1188AD49-video-index0"
"/dev/v4l/by-path/pci-0000:00:13.2-usb-0:4.4.4:1.0-video-index0"
这篇关于在OpenCV方法中使用设备名称而不是ID VideoCapture.open()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!