如何在opencv ubuntu中从网络摄像头获取直播

时间:2014-01-18 08:59:50

标签: c++ opencv

类似的问题被多次询问,但没有可行的解决方案。

以下是我使用的代码:

#include "opencv2/highgui/highgui.hpp"
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <vector>
#include <stdio.h>


int main()
{

//Data Structure to store cam.
CvCapture* cap=cvCreateCameraCapture(0);
//Image variable to store frame
IplImage* frame;
//Window to show livefeed
cvNamedWindow("LiveFeed",CV_WINDOW_AUTOSIZE);

while(1)
{
    //Load the next frame
    frame=cvQueryFrame(cap);
    //If frame is not loaded break from the loop
    if(!frame)
        printf("\nno");;
    //Show the present frame
    cvShowImage("LiveFeed",frame);
    //Escape Sequence
    char c=cvWaitKey(33);
    //If the key pressed by user is Esc(ASCII is 27) then break out of the loop
    if(c==27)
       break;
}
//CleanUp
cvReleaseCapture(&cap);
cvDestroyAllWindows();
}

我的输出是'无'无限打印。即。没有摄像头。 请帮助任何人!!!

2 个答案:

答案 0 :(得分:1)

也许索引0没有摄像头?尝试其他数字。

另外,既然你似乎对此很陌生,请避免过时的c-api 使用较新的c ++ api,旧的已经过去3年不再积极维护了。


#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"

using namespace cv;

int main()
{
    VideoCapture cap(-1);     // get 'any' cam
    while( cap.isOpened() )   // check if we succeeded
    {
        Mat frame;
        if ( ! cap.read(frame) )
            break;
        imshow("lalala",frame);
        int k = waitKey(33);
        if ( k==27 )
            break;
    }
    return 0;
}

答案 1 :(得分:0)

使用“CV_CAP_ANY”代替使用“-1”获取第一个/默认摄像头。

VideoCapture cap(CV_CAP_ANY);
相关问题