[Research] A Minor Issue when using OpenCV and OpenGL TOGETHER

Today I am trying to write a few lines to read the a webCam  using OpenCV and output frames as textures using OpenGL. Start with a simple and standard structure of OpenGL program [gl_init() for initialize the GL parameters, onDraw() to draw things, which is to be called by glutDisplayFunc()] in main loop. Then read the camera input using cvQueryFrame(capture) and mapped to a texture. Finally output int real-time in a window.

The problem here is cvQueryFrame(capture) cannot be called in the onDraw(), by doing which the output stream freezes at the first frame. Here the magic function glutIdleFunc(idleFunc) comes to play. glutIdleFunc(void *func(void)) sets the global idle callback to be func so a GLUT program can perform background processing tasks or continuous animation when window system events are not being received. If enabled, the idle callback is continuously called when events are not being received. The cvQueryFrame(capture) then should be called in the idleFunc, which is handled by glutIdleFunc() in main loop.

Note: check the existence of the captured image before processing otherwise may counter seg fault.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: