OpenCV function call within OpenGL program


I have an OpenGL program that uses an OpenCV function call to calculate the coordinates relative to some markers. Once that is done OpenGL should then open a window and display some graphic (e.g. point (5px)) at the coordinates calculated by the OpenCV function. The problem I have is that the cvCaptureFromCAM(CV_CAP_ANY); function causes the program to terminate with the following error:

GLUT Fatal Error: redisplay needed for window 1, but no display callback.

If I comment it out, then it runs (but no webcam frames ofcorse).

The OpenGL and OpenCV functions run OK as separate programs. I’m not sure what the problem could be. Would appreciate any pointers you may have.

Here’s the code (excluding header files etc)

void setup(void)
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);

void display(void)
glColor3f(0.0, 0.0, 1.0);

    glVertex2f(mid, 50);



void resize(int width, int height)
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glOrtho(0.0, 100.0, 0.0, 100.0, -1.0, 1.0);

void capture(void)  //OpenCV function
    CvCapture* capture = 0;
    IplImage* frame = 0;

    capture = cvCaptureFromCAM(CV_CAP_ANY);
        printf("Could not initialise capturing...

    frame = cvQueryFrame(capture);


int main(int argc, char **argv)
// Initialize GLUT.
glutInit(&argc, argv);

// Set display mode as single-buffered and RGB color.
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

// Set OpenGL window size.
glutInitWindowSize(500, 500);

// Set position of OpenGL window upper-left corner.
glutInitWindowPosition(100, 100);

// Create OpenGL window with title.
glutCreateWindow("Lumbar Tracking");

// Initialize.


// Register display routine.

// Register reshape routine.

// Begin processing.

return 0;  


Please use [noparse]

[/noparse] around source code.
The error message contains the necessary hint: when calling capture() no display function is registered with GLUT, because you only register it in main() after the call to capture. Register your GLUT callbacks first, then call capture().

Thank you Carsten. This has definitely helped. T

The problem I have now is that the OpenGL program runs through only once. If I comment out capture = cvCaptureFromCAM(CV_CAP_ANY) it runs continually (I’ve checked by initialising a counter and printing it to the screen using writeBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, str) after converting the number into a string).

Your feedback would be gratefully appreciated.



Hmm, does it perhaps crash in capture? Have you run it under a debugger? Does your code check for errors from OpenCV calls (sorry, not familiar with how OpenCV error reporting/handling works)? I don’t have any more concrete suggestions, you could try posting all of the capture function.

Hi Carsten, thanks again for getting back. You are right, it does crash in capture. I’ll keep working on it and will get back when I have the solution. Your feedback has been most helpful. Thanks again

I’ve got the code working. For some reason the OpenCV function would run once (function call within main() ). By moving the function call (capture()) to within the openGL display() function, this forces the tracker to recompute with each frame - now it works (a little clunky though). Not sure why it was called once from within main() though (unless capture() will not be affected by glutMainLoop())?