Tag Archives: OpenGL

Integrating OpenCV and OpenGL projects

I was working with a OpenCV project then I needed to show the results of some image processing using a 3D object rendering. So I made an OpenGL project that takes result data and display it using GLUT functions.

But then I was unable to figure out a way to integrate these two independent projects, because inside the main() in my OpenGL project there was this non terminating glutMainLoop() loop, and I found it difficult to figure out where to put the video frame processing loop in main() of my OpenCV project !!

OpenCV Project :

int main()
{
    VideoCapture cap(0);
    namedWindow("MyVideo",CV_WINDOW_AUTOSIZE);
    Mat frame;
    while(1)
    {
        cap.read(frame);
        imshow("MyVideo", frame);
        // Do some image processing on "frame"
        if(waitKey(30) == 27)
            break;
    }
}

OpenGL Project:

#include<gl/glut.h>
int main()
{
	glutInit(&argc, argv);
	MyInit();
	glutDisplayFunc(Display);
	glutKeyboardFunc(Keyboard);
	glutMouseFunc(Mouse);
	glutIdleFunc(Idle);
	glutMainLoop();  // non-returning loop
}

Looking up on the internet, I found that freeglut supports a function glutMainLoopEvent() that causes freeglut to process one iteration’s worth of events in its event loop. This allows the application to control its own event loop and still use the freeglut windowing system.

Although i was using glut and not freeglut,  i didn’t have to change my code (except for include files) for freeglut, because freeglut supported all the functions of glut (or atleast it supported all the glut functions used in my project).

With this, I was able to integrate the two projects easily.

OpenCV + OpenGL Project:

#include<gl/freeglut.h>        // Add freeglut.h
int main()
{
	glutInit(&argc, argv);
	MyInit();
	glutDisplayFunc(Display);
	glutKeyboardFunc(Keyboard);
	glutMouseFunc(Mouse);
	glutIdleFunc(Idle);

	VideoCapture cap(0);
	namedWindow("MyVideo",CV_WINDOW_AUTOSIZE);
	Mat frame;
	while(1)
	{
		cap.read(frame);
		imshow("MyVideo", frame);
		// Do some image processing on "frame"
		glutMainLoopEvent();        // One iteration only
		Display();      // Call the func used with glutDisplayFunc()
		if(waitKey(30) == 27)
			break;
	}
	glutLeaveMainLoop();
	return 0;
}

This approach may cause a delay in OpenGL rendering. So, a better approach could be to use threading. One thread for video frame processing and another for OpenGL rendering.