OpenCV & OpenGL

Hi. How can I project the model created by OpenGL to the image loaded by OpenCV?

I used OpenCV to load an image (IplImage *src = cvLoadImage(argv[1]),and separately I used OpenGL to create a model. But I want to project the model I created to that image and then do some other tasks by using OpenCV. I am a very beginner for both OpenGL and OpenCV. If anybody knows how to do it,please help me. Thank you.

If you don’t need to do this task several times per frame, you could use glReadPixels. This reads your window (or a portion, whatever you ask for it) onto an off-screen buffer. I suppose you could then load this buffer into OpenCV. Remember to check the image storage type, such as byte, etc.

Thanks for your help. But I have to do that task many times per frame. Namely, I have to adjust the model -> project to the image -> do some Image processing process -> adjust the model -> project -> and so on. I have to do these process several times. Do you know how I can do it efficiently? Thank you.

Hi there!!

See, I am using OpenCV to process images captured form a webcam. But, I want to make drawings in this images with OpenGL. So, I found this functions, glDrawPixels and glReadPixels, that should help me getting my OpenCV image from and to OpenCV. But they are not working as I expected them to work.

I tried this simple code, I would like anyone to tell me what’s wrong:

glDrawPixels(320,240,GL_RGBA,GL_UNSIGNED_BYTE,dataPtr);

glClearColor (0.0, 0.0, 0.0, 0.0);
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f (0.25, 0.25, 0.0);
glVertex3f (0.75, 0.25, 0.0);
glVertex3f (0.75, 0.75, 0.0);
glVertex3f (0.25, 0.75, 0.0);
glEnd();
glFlush();

glReadPixels(0,0,320,240,GL_RGBA,GL_UNSIGNED_BYTE,dataPtr);

Are there any other ways to achive the same thing?

I’d appreciate any help. Thanks in advance.

Hi there! :slight_smile:

I finally made it, I have the OpenCV image in an OpenGL window. That’s not what I wanted, but that’s the best I can get. I didn’t use GLUT, as GLUT has its own mainloop, so I used freeGLUT instead. Here is the code.
Initializing function:

int init(void)
{
//general initialization:
	char cadena[25]="GestionMarcaAR\0";
	char **argv;
	int argc = 1;

	argv=(char **)malloc(sizeof(char *));
	*argv=(char *)malloc(sizeof(char)*10);
	strcpy(*argv, cadena);

	glutInit(&argc,argv); //we cheat it ;P

	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowPosition(800, 400);
	glutInitWindowSize(320,240);
	glutCreateWindow("Name");

	glutDisplayFunc(draw);
	glutIdleFunc(draw);

//initializating the texture mapping
	GLuint mTexture;
	glGenTextures(1, &mTexture);
	glBindTexture(GL_TEXTURE_2D, mTexture);
	glEnable(GL_TEXTURE_2D);
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);	
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

	
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

	glClearColor(1.0f, 1.0f, 1.0f, 1.0f); //color blanco de fondo
	glColor3f(0.0f,0.0f,0.0f);

	return 0;
}

Drawing function:

void draw()
{
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,320,240,0,GL_BGR,GL_UNSIGNED_BYTE, imagen->imageData);
	
	glBegin(GL_POLYGON);
		glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex2f( 1.0f, -1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex2f( 1.0f,  1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f,  1.0f);
	glEnd();
	
	glFlush();
	
	glutSwapBuffers();
}

And what is needed to be anywhere in the mainloop of openCV:

glutPostRedisplay();
glutMainLoopEvent();   //freeglut function