在虚拟机上使用OpenCV进行视频捕捉和相机访问

5
我正在尝试使用计算机上的网络摄像头实时捕获图像。 我正在使用VirtualBox运行Ubuntu,我知道需要设置USB才能使用网络摄像头,但我是否仍需要安装网络摄像头驱动程序?如果需要,如何安装! 我安装了: virtualbox 5.0.6 ubuntu 14.04.3 我正在运行Windows 10。 这是我的代码,我得到“ERROR:Could not access the camera!”。 请帮帮我!
// 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;
}

1
这个问题应该被回答了。我也遇到了同样的问题。 - userXktape
3个回答

0

看起来是授权问题。 尝试从命令行使用sudo运行。 另一个问题可能是您选择了错误的相机(例如,您有一台笔记本电脑,上面还有集成的相机),您可以使用“dmesg | grep usb”获取有关连接到虚拟机的设备的一些提示。


0
我遇到了同样的错误。这是因为我的虚拟机没有检测到我的网络摄像头。我安装了一个使用网络摄像头的程序cheese,并确认了这一点。所以,我在虚拟机配置中打开了USB控制器。然后,在运行虚拟机时,我在设备菜单中检查了我的网络摄像头,一切都开始正常工作了!希望能对你有所帮助。

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接