开发者

OpenCV2.2 cvCaptureFromCAM problems...only returning a green screen. (Logitech C260 camera)

I'm writing a program that needs to capture from a webcam using opencv. I am using a Logitech C260 Camera and OpenCV2.2, Windows XP, QT Creator. Here is an example piece of my code:

int i = 0;

int arg = 0;
CvCapture *pCapturedImage = cvCaptureFromCAM(arg);

cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
IplImage* img1= cvQueryFrame(pCapturedImage);
IplImage* img2;
IplImage* merged;
int MAX_FRAMES = 10;
    for (int i = 0; i < MAX_FRAMES; i++){
    pCapturedImage = cvCaptureFromCAM(arg);
    if (!pCapturedImage){
        printf("Uh oh");
    }
    img1 = cvQueryFrame(pCapturedImage);

    printf("Query");
    cvShowImage( "Example1", img1 );
    cvWaitKey(0);
}
for (int i = 0; i < MAX_FRAMES; i++){
    printf("Enter");
    img2 = img1;
    pCapturedImage = cvCaptureFromCAM(arg);
    if (!pCapturedImage){
        printf("Uh oh");
    }else
    printf("Capture");
    img1 = cvQueryFrame(pCapturedImage);
    printf("Query");
    cvShowImage( "Example1", img1 );
    cvWaitKey(0);
    img1 = mergePhotos(img2, img1, i*25, i*25);
    printf("Merge");
    cvSho开发者_如何学编程wImage( "Example1", img1 );
    cvWaitKey(0);

}

It's not working though. I'm getting only a for around the first 10 captures, then after that, the capture becomes null (I'd post a picture but I'm not allowed to).

I'm stumped. I've tried cvCaptureFromCAM and cvCreateCameraCapture. I've tried changing the args to that function. I've tried all my USB ports.

Does anybody know what the problem might be? I greatly appreciate all help.


You need to call cvCaptureFromCAM (or cvCreateCameraCapture) only once, outside the loops, to initialize and open the camera. Then you can get new frames by simply calling cvQueryFrame each time. (Think of the cvCapture as a frame source, not as the frame itself.)

There is probably some OS restriction that you can't open a camera resource more than a few times... that could be the explanation for the "just 10 frames" you described.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜