Hey guys, I'm using OpenCV to get an image from my webcam and then run it through code. It's supposed to be live feed, and I got this code from an online tutorial (somewhat revised):

#include <stdio.h>
#include <stdlib.h>

#include <opencv/cvaux.h>
#include <opencv/highgui.h>          //opencv libraries
#include <opencv/cxcore.h>

CvCapture* camCapture;      //opencv class for retrieving image from external camera
IplImage* origImage;        //initialization of binary image

int main()
{
    camCapture = cvCaptureFromCAM(0);   //automatically syncs with first installed camera;
                                        //for a second camera, argument would be '1'.

    printf("Your Error Messages Work.");       //make sure print function works

    if(camCapture == NULL)        //if there is no image coming from the synced camera, print error message
    {
        printf("Dude, your camera's not working.");
        getchar();
    }
    cvNamedWindow("OK", 3);          //create a window box named "OK", with 3 changeable values (R, G, B);
                                     //second param would be 2 for grayscale, 1 for black/white
    while(1)           //loop forever
    {
        origImage = cvQueryFrame(camCapture);        //the binary image is the current frame from the camera

        if(origImage==NULL)          //if the image is blank, print error message
        {
            printf("Dude, I'm not getting an image.");
            getchar();
            break;
        }
        cvShowImage("OK", origImage);         //show the image in the window named "OK"
    }
}

Here's what happens:
1). Regular dialog box comes up and prints: "Your Error Messages Work."
2). The light on my webcam turns on like its recording.
3). An alternate window appears named "OK".
4). The entire "OK" window turns slate-gray and is forever "loading". No image appears.
5). No error messages show up for code or image being null.

Has anyone worked with opencv before? Is it a problem with my webcam or with my code?
Thanks in advance!

--Ben

Recommended Answers

All 2 Replies

Add cvWaitKey(30); after line 35 and see if that makes a difference.

Moschops:
I did what you said. The "loading" symbol goes away, but the image is still grey. If i try to close the "OK" window, it pops back up, still grey.

Got anything else?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.