Advertisement Jump to content
Sign in to follow this  

OpenGL OpenGL and OpenCV

This topic is 3868 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am writing a program which displays an openGL window using GLUT and an OpenCV window which constantly updates the image from a webcam. So far I have been able to create both, but the loop which updates the image from the camera loops until a key is pressed, meaning that the program doesn't reach the glutMainLoop() function to initialise and render the openGL window. My code is as follows:
//source file containing the main function and ending with the glutMainLoop() function

//include local source file
#include "Source4.cpp"

#include <GL/glut.h>
//#include <cv.h>
//#include <highgui.h>

void displayCB(void)						//function called whenever redisplay needed
  glClear(GL_COLOR_BUFFER_BIT);				//clear the display

  /*draw white triangle*/
  glColor3f(1.0, 1.0, 1.0);					//set current color to white
  glBegin(GL_POLYGON);						//draw filled triangle
  glVertex2i(200,125);						//specify each vertex of triangle
  glEnd();									//finish drawing the triangle

  /*draw red triangle*/
  glColor3f(1.0, 0.0, 0.0);
  glVertex2i(150, 75);
  glVertex2i(50, 325);
  glVertex2i(250, 325);
  glFlush();								//Complete any pending operations

void keyCB(unsigned char key, int x, int y)	//called on key press
  if( key == 27 ) exit(0);					//when the 'q' key is pressed, exit the program

int main(int argc, char *argv[])			//main function
  glutInit(&argc, argv);					//initialize GLUT system
  glutInitWindowPosition(700, 10);			//position the window onscreen (x, y) top left corner co-ordinates
  glutInitDisplayMode(GLUT_RGB);			//display mode set to RGB
  glutInitWindowSize(800,600);				//width = 400 pixels, height = 500 pixels
  glutCreateWindow("Triangle");				//create window

  glClearColor(0.0,0.0,0.0,0.0);			//set background to black
  gluOrtho2D(0,400,0,500);					//how object is mapped to window
  //glutDisplayFunc(cvmain);					//set openCV display callback
  glutDisplayFunc(displayCB);				//set window's display callback
  glutKeyboardFunc(keyCB);					//set window's key callback
  cvmain();									//this part of the code runs the function in Source4.cpp which initialises the openCV window

  glutMainLoop();							//start processing events...
  /* execution never reaches this point */
  return 0;

//source file containing the OpenCV function 'cvmain' which captures the image from the camera

#include <cv.h>
#include <highgui.h>
#include <stdio.h>
/*This will pop up a small box with "Welcome to OpenCV" as the text*/

int cvmain() 
	//declare for the height and width of the image
	int height = 620;
	int width = 440;
	//capture from camera
	CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);
	//IplImage* frame = cvQueryFrame(capture);
//		printf(stderr, "Error: capture is NULL \n");
		return -1;

	//specify the point to place the text
	//CvPoint pt = cvPoint(height/4 ,width/2 );
	//Create an 8 bit, 3 plane image
	//IplImage* hw = cvCreateImage(cvSize(height, width), 8, 3);

	//Query frame
	//IplImage* hw = cvQueryFrame(CvQueryFrame(CvCapture* capture));
	//Clearing the Image
//	cvSet(hw,cvScalar(0,0,0));
	//initialize the font
	//CvFont font;
	//cvInitFont( &font, CV_FONT_HERSHEY_COMPLEX,1.0, 1.0, 0, 1, CV_AA);
	//place the text on the image using the font
//	cvPutText(hw, "Welcome To OpenCV", pt, &font, CV_RGB(150, 0, 150) );
	//create the window container
	cvNamedWindow("Hello World", 0);

	//show the image captured from the camera and repeat
		//get one frame
		IplImage* frame = cvQueryFrame(capture);
//			printf(stderr, "Error: frame is NULL \n");
	//resize the window to match the 320 240 resolution
	cvResizeWindow("Hello World", 320, 240);

	//display the image in the container
	cvShowImage("Hello World", frame);

	//align the window to the left of the screen
	cvMoveWindow("Hello World", 10, 10);

	//do not release the frame

	//If ESC key is pressed remove higher bits using AND operator
	if((cvWaitKey(10)) == 27) break;

	//release the capture device housekeeping
	cvDestroyWindow("Hello World");
//	return 0;
Commenting out the function call for the OpenCV window stops the camera image been read, obviously, and displays the openGL window. I have found ways to get around this by modifying the source code for glutMainLoop() and recompiling GLUT but this seems like a lot of extra hassle. I was wondering if there was a way for a glut function to call the openCV function? This would mean the glutMainLoop() would not directly call the openCV function but would call the glut function which calls the OpenCV function. My apologies if this concept is a little hard to understand. I have tried this using glutDisplayFunc(opencvfunction); and this starts the image from the camera and begins to launch the openGL window, but the program crashes. If anybody has a solution to this problem I would be very grateful. Many thanks, Ben

Share this post

Link to post
Share on other sites
My first response is "don't use GLUT" it robs you of this obvious control.

How ever you can make simple modifications to the Glut library to make it more friendly, incase you need to retain the cross platformability.

My second response is "don't use CV's window system" expecially if you've got your own. Copy the Image into an Opengl texture and use this on a quad in your Glut application.

P.s. Don't forget to destroy "hw" :)

Also, I see in your CV_Main that you're iterating the entire video before returning. You should not be looping like this and utilize GLUT's loop to retrieve and process the next frame. In other words, CV_Main should be called from displayCB, and it should only handle 1 frame at a time.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!