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

H.K*_*H.K 5 c++ ubuntu opencv video-capture virtual-machine

我正在尝试使用计算机上的网络摄像头实时捕获图像。我正在使用 Virtual Box 运行 Ubuntu,并且我知道我需要设置 USB 设置才能使用网络摄像头,但我还需要安装网络摄像头驱动程序吗?如果是的话我该怎么做!

我安装了virtualbox 5.0.6 ubuntu 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;
}
Run Code Online (Sandbox Code Playgroud)

Gab*_*tyn 0

我遇到了同样的错误。这发生在我身上,因为我的虚拟机没有检测到我的网络摄像头。我已经安装了奶酪(一个使用网络摄像头的程序)并确认了这一点。因此,我在虚拟机配置上打开了 USB 控制器。然后,运行虚拟机,我在“设备”菜单上检查了我的网络摄像头,一切都开始工作了!希望能帮助到你。