Textures from IplImage

Started by
0 comments, last by navyenzo 12 years, 11 months ago
I''m trying to create a dynamic texture for the purposes of displaying captured frames from a web-cam using OpenGL, instead of the premade functions of OpenCV. Moreover, if I can get this to work, I will be able to draw on the captured image using GL functions, which (as I understand it) should be much faster, and have many more options then using the OpenCV drawing primatives. One of the basic requirements of this program is that it NOT use GLUT in any way. It is required to be a "Win32 Application", and I am copiling/linking/running using MSVisual C++. The problem I am encountering is getting the data from the IplImage structure to become a texture. The texture I get output is pure white (I assume this, since the clear-color for the windw is black). I''m including incomplete code-snipets to show my efforts. Please note that my code is VERY dirty and unorganized at this point, but I will make it prettier when I get things to work. BEGINNING OF CODE-SNIPETS: //-------------------------------------------------------------- //My initializations; run at beginning of ''WinMain'' program. //-------------------------------------------------------------- /*Neccessary to show this line to show that the IplImage variable ''camTexture'' is pre-made to a size which conforms to the requirements for a texture... that its width and height are powers of 2 (shown here as 128x128). IPL_DEPTH_8U means that the image''s bit-depths are unsigned 8-bit integers. The final number is the number of channels in the image (from 1 - 4)*/ IplImage *camTexture = cvCreateImage(cvSize(128,128), IPL_DEPTH_8U, 3); glBindTexture(GL_TEXTURE_2D, 1); //I''m not clear on if this line is necessary, or even if I''m using it right. glPixelStorei(GL_UNPACK_ALIGNMENT, 1); //GL Texture Parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //Can I get an explanation of this line as well? Documentaion is not good to get things in layman''s terms. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); //GL Initializations, run just before my main message-processing loop. glShadeModel(GL_SMOOTH); //glClearDepth(1.0f); //glEnable(GL_DEPTH_TEST); //glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glBindTexture(GL_TEXTURE_2D, 1); //-------------------------------------------------------------- //Everything from here on is in my Message-Processing loop for my open window; assume everything is simply run repeatedly. //-------------------------------------------------------------- //Setting textures for this drawing cycle. glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 1); /*OpenCV functions; I''ll explain each for clarity.*/ //Grabs a frame from the input camera and stores internally. cvGrabFrame( capture ); //Moves the internally stored image to an ''IplImage'' object. frame = cvRetrieveFrame( capture ); //Fills ''camTexture'' with data from ''frame'', formatted to EXACTLY fit into camTexture. cvResize(frame, camTexture); //Corrects x-axis flipping caused by copy from previous line. cvFlip(camTexture, NULL, 0); /*END of OpenCV functions*/ //For debugging. Insert "test1 = glGetError(); where needed. GLenum test1; glBindTexture(GL_TEXTURE_2D, 1); /* "camTexture->(width or height)" returns (int) indicating size of IplImage. "camTexture->imageDate" returns a pointer to a (char*), which (from the Ipl specs) is a "pointer to aligned image data".*/ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, camTexture->width, camTexture->height, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, camTexture->imageData); glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); glClear( GL_COLOR_BUFFER_BIT ); //Attempts to draw texture to the opened window. glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f); glEnd(); //Diables texturing at the end of each loop. glDisable(GL_TEXTURE_2D); END OF CODE-SNIPETS. Sorry, I know that''s a lot to digest, and a lot of the potential problems don''t even have anything to do with OpenGL. But there are no forums out there (that I can find) called "Using OpenGL and OpenCV Together". One more thing before I post this and begin to wade hip-deep through the "That''s not an OpenGL question!!" flames; the ONLY source of potential problems I''ve been able to find with my own efforts: I took the "IplImage camText" object, and displayed it to my screen using CV functions. It looked fine, but to satisify my own curiosity, I begin lowering the size of the image from 128x128, to 64x64, all the way down to 2x2. At 2x2, I should see (so you would think), four pixels output. Instead, there is only one pixel, and it only takes up the lower-right of the display. Put another way, the first row and first column of the image are consistently NULL. I have no idea what could cause this, and I doubt it''s even the problem, but it''s the only lead I''ve been able to find. Anyways, if you need more detail, I''ll be happy to provide it. Any insights would be very appreciated at this point. Thanks, CP
Advertisement
With my super light opencv library "[url="http://www.barbato.us/2010/10/14/image-data-structure-based-shared_ptr-iplimage/"]blImageAPI[/url]", you can easily do that with a couple of lines of code. Check it out, it also wraps IplImage with shared_ptr giving you automatic memory deallocation.
There's an example at the post [url="http://www.barbato.us/2010/11/11/load-an-iplimage-into-an-opengl-texture-and-create-webcam-and-video-textures-using-the-blimageapi/"]blTexture[/url] where I show how I connect to the webcam and feed the video into an opengl texture.

There's also the ability to do that in a parallel thread so that the main opengl thread is not slowed down by the video grabbing thread.

Enjoy,
Enzo





This topic is closed to new replies.

Advertisement