Opencv Can';t从网络摄像机抓取帧

Opencv Can';t从网络摄像机抓取帧,opencv,Opencv,我正在使用OpenCV 2.4.6,VS2010和Windows7 64位。我拿不到相机上的相框。下面的代码适用于avi文件,但不适用于从相机捕获。有人能帮我吗,我怎样才能捕捉到画面?提前感谢 实际上,这部分的问题是: bool bSuccess=上限读取(帧);//从视频中读取新帧 if (!bSuccess) //if not success, break loop { cout << "Cannot read a frame from video

我正在使用OpenCV 2.4.6,VS2010和Windows7 64位。我拿不到相机上的相框。下面的代码适用于avi文件,但不适用于从相机捕获。有人能帮我吗,我怎样才能捕捉到画面?提前感谢

实际上,这部分的问题是:

bool bSuccess=上限读取(帧);//从视频中读取新帧

    if (!bSuccess) //if not success, break loop
    {
        cout << "Cannot read a frame from video file" << endl;
        break;
    }
if(!bSuccess)//如果不成功,则中断循环
{
cout在使用while循环之前,您应该阅读第一帧,如下所示:
Mat框架;
阅读(框架);
而(1)
{
bool bSuccess=cap.read(frame);//从视频中读取新帧
if(!bsucces)//如果不成功,则中断循环
{

不能尝试
VideoCapture(-1);
检查下面的帖子,它回答了这个问题:Yeyuel,你能解释一下吗?我和Dejan有同样的问题,但在我的例子中,前两帧的读取失败。Mat frame;cap.read(frame);cap.read(frame);解决了问题,但看起来很难看。。
#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
    VideoCapture cap(0); // open the video camera no. 0

    if(!cap.isOpened())  // if not success, exit program
    {
        cout << "Cannot open the video file" << endl;
        return -1;
    }

    double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
    double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video

    cout << "Frame size : " << dWidth << " x " << dHeight << endl;

    namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"

    while(1)
    {
        Mat frame;

        bool bSuccess = cap.read(frame); // read a new frame from video

        if (!bSuccess) //if not success, break loop
        {
            cout << "Cannot read a frame from video file" << endl;
            break;
        }

        imshow("MyVideo", frame); //show the frame in "MyVideo" window

        if(waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
        {
            cout << "esc key is pressed by user" << endl;
            break; 
        }
    }
    return 0;
}
Mat frame;
cap.read(frame); 
while(1)
{
    bool bSuccess = cap.read(frame); // read a new frame from video

    if (!bSuccess) //if not success, break loop
    {
        cout << "Cannot read a frame from video file" << endl;
        break;
    }

    imshow("MyVideo", frame); //show the frame in "MyVideo" window

    if(waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
    {
        cout << "esc key is pressed by user" << endl;
        break; 
    }
}