opencv视频采集和虚拟机上的摄像头访问

opencv video capture and camera access on virtual machine

本文关键字:摄像头 访问 虚拟机 视频 opencv      更新时间:2023-10-16

我正试图使用电脑上的网络摄像头实时捕捉图像。我正在使用虚拟盒子运行Ubuntu,我知道我需要设置USB设置才能使用网络摄像头,但我还需要安装网络摄像头驱动程序吗?如果是的话,我该怎么做!!

我安装了virtualbox 5.0.6ubuntu 14.04.3

我正在运行一个windows 10机器

这是我正在运行的代码,我得到"错误:无法访问相机!"。。

你能帮忙吗!

// Get access to the webcam.
void initWebcam(VideoCapture &videoCapture, int cameraNumber)
{
    // Get access to the default camera.
    try {   
        videoCapture.open(cameraNumber);
    } catch (Exception &e) {}
    if ( !videoCapture.isOpened() ) {
        cerr << "ERROR: Could not access the camera!" << endl;
        exit(1);
    }
    cout << "Loaded camera " << cameraNumber << "." << endl;
}
int main(int argc, char** argv)
{
    const int DESIRED_CAMERA_WIDTH = 640;
    const int DESIRED_CAMERA_HEIGHT = 480;    
    int cameraNumber = 0;  
    // Get access to the camera.
    VideoCapture camera;
    initWebcam(camera, cameraNumber);
    camera.set(CV_CAP_PROP_FRAME_WIDTH, DESIRED_CAMERA_WIDTH);
    camera.set(CV_CAP_PROP_FRAME_HEIGHT, DESIRED_CAMERA_HEIGHT);
    while (true) {
        // Grab the next camera frame. Note that you can't modify camera frames.
        Mat cameraFrame;
        camera >> cameraFrame;
        if( cameraFrame.empty() ) {
            cerr << "ERROR: Couldn't grab the next camera frame." << endl;
            exit(1);
        }
        Mat displayedFrame = Mat(cameraFrame.size(), CV_8UC3);
        // DO SOME PROCESSING
return 0;
}

我得到了同样的错误。发生这种情况是因为我的虚拟机没有检测到我的网络摄像头。我已经安装了cheese(一个使用网络摄像头的程序)并确认了这一点。所以,我已经打开了虚拟机配置上的USB控制器。然后,在运行虚拟机时,我在"设备"菜单中查看了我的网络摄像头,一切都开始工作了!。希望能有所帮助。

这似乎是一个授权问题。尝试从命令行使用sudo运行。另一个问题可能是你选择了错误的摄像头(例如,你有一台笔记本电脑,上面也有集成摄像头),你可以使用"dmesg|grep-usb"在连接到vm的设备上得到一些提示