OpenCV 2.2无法打开相机

时间:2012-05-03 01:27:29

标签: c++ qt opencv camera

我想使用相机并对输入流执行图像处理,但是当执行程序时会出现一个窗口,询问“捕获源”,之后“确定”或“应用”没有任何反应。

但如果我使用视频文件而不是相机,程序运行完全正常。

以下是代码:

int MainWindow::on_CameraOpen_triggered()
{
// programming done in Qt and using Opencv 2.2.
// all the variables are defined & declared in private of header file
// there is no compilation error
// return type of the function is 'int'
// Following program works fine if name of video file is mentioned 
// instead of '0' is the line below
VideoCapture capture(0);

if(!capture.isOpened())
    return 1;

    bool stop(false);
    double rate = capture.get(CV_CAP_PROP_FPS);
    namedWindow("Extracted Frame");
    int delayVideo = 1000/rate;

    while(!stop)
    {
        if(!capture.read(frame))
        {
            break;
        }
        imshow("Extracted frame", frame);
        if(waitKey(delayVideo)>=0)
        {
            stop = true;
        }
    }

capture.release();
}

我试图删除在以下链接中纠正的错误: https://code.ros.org/trac/opencv/changeset/4400

https://code.ros.org/trac/opencv/browser/trunk/opencv/modules/highgui/src/precomp.hpp?rev=4400

相机在gtalk和其他相机软件上运行良好。

请建议/指导可以做些什么。

非常感谢。

此致 DBS

2 个答案:

答案 0 :(得分:0)

试试这段代码:

# include <opencv2/highgui/highgui.hpp>

CvCapture *_capture;


_capture = cvCaptureFromCAM(-1);
//_capture = cvCaptureFromFile("test1.mp4");
if (!_capture)
{
    //"Unable capture video"
    return 1;
}

double rate = cvGetCaptureProperty(_capture, CV_CAP_PROP_FPS);

//...

cv::Mat = cvQueryFrame(_capture);

//...

答案 1 :(得分:0)

这是另一个可用于检查OpenCV是否一切正常的最小示例:

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>

int main () {
    cv::VideoCapture cam = cv::VideoCapture(0);
    cv::Mat frame;
    cv::namedWindow ("Demo", CV_WINDOW_AUTOSIZE);

    while (1) {
        cam >> frame;
        imshow ("Demo", frame);
    }

    cam.release();
    return 0;
}

尝试查看OpenCV或Qt程序是否存在问题。