Click here to Skip to main content
Click here to Skip to main content

Video Texture in OpenGL

By , 25 Mar 2009
 

Introduction

This short program shows how a live video stream from a web cam (or from a video file) can be rendered in OpenGL as a texture. The live video stream is captured using the Open Source Computer Vision library (OpenCV). The program also shows how an OpenCV image can be converted into OpenGL texture. This code can be used as the first step in the development of an Augmented Reality application using OpenCV and OpenGL.

Understanding the Code

The program renders an OpenGL textured quad which shows a live video stream. The code does not contain any additional functionality, and is kept very simple for easy understanding.

The OpenGL texture is continuously created in the OnIdle callback function. The next available frame in the video stream is captured first:

IplImage *image = cvQueryFrame(g_Capture);

The image is stored in the OpenCV data structure IplImage. Please see the OpenCV documentation for details. The image captured by OpenCV is stored as a BGR. It is first converted to RGB using the OpenCV function cvCvtColor.

cvCvtColor(image, image, CV_BGR2RGB);

Then, the following magic call creates a 2D OpenGL texture from the OpenCV image:

gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image->width, image->height, 
                  GL_RGB, GL_UNSIGNED_BYTE, image->imageData);

The texture is loaded into memory and is available for rendering.

Compiling and Running

The code is compiled and tested using Microsoft Visual Studio 2008. However, it can be compiled using any C++ compiler on any platform. The program uses the OpenGL, GLUT, and OpenCV libraries. Please make sure that you have installed them and paths to the include and lib directories are set. OpenCV can be downloaded from here.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

About the Author

Arsalan Malik
Software Developer (Senior)
Pakistan Pakistan
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionhow to play a videomemberSweety Khan13 Oct '11 - 21:25 
you seems good in opengl. can u plz tell me how to play a video in opengl c++ and map it on a 3d surface.
GeneralVideo textures in openglmembernavyenzo23 Nov '10 - 15:58 
I have created a very simple set of APIs that, among many things, can also show video in opengl textures, build and render dynamic height maps from the video feeds or from images in general and much more.
 
It is based on the OpenCV library, opengl and uses boost::shared_ptr and ublas::matrix from the boost libraries.
 
You can find the APIs here: http://www.barbato.us/category/programming-labs/
 
Good luck
GeneralMy vote of 1memberBig D21 Mar '10 - 21:11 
Nothing is useful...Codeproject is a very useful and repository of some serious stuffs...Try to make a helpful article
GeneralMy vote of 1memberJim Crafton14 Jan '10 - 9:58 
Sorry but this really isn't an article. You don't explain how anything works. Plus the usage of the gpl means it's pretty worthless even if the code is good. Bummer, because it's a really interesting topic.
GeneralThank you very much [modified]memberMember 443275517 Jun '09 - 1:07 
Your code is very useful for me. Nehe has a good texture example actually but it is complecated to understand all code.
Your code is simple and understandable. It is perfect to whom want to apply OpenCV and OpenGL together. Nehe didn't explan anything about OpenCV.
 
Anyway, I have one commend. The output from webcam is invert 180 degree. It would be better if you a little bit fix this problem.
 
Cheer up for more codes
 
modified on Wednesday, June 17, 2009 7:18 AM

GeneralSome efficiency notesmemberm.e.fathy30 Apr '09 - 23:34 
You do not have to build mipmaps (which doubles the texture loading time). So, you do not have to use gluBuild2DMipmaps(). Instead, consider using
 
glTexImage2D(
GL_TEXTURE_2D, //target
0, //mip-map level 0
GL_RGB, //internal format: the format OpenGL will use to store the image
image->width,
image->height,
0, //no border
GL_RGB, //src format is RGB. You can save more time by removing cvCvtColor and set the src format here to GL_BGR
GL_UNSIGNED_BYTE, //type
image->imageData);
 
I think we could even speed the loading process up by using fast asynchronous PBO (Pixel Buffer Object) DMA transfers as described in http://www.songho.ca/opengl/gl_pbo.html[^].
 
One more possible issue, I think you need to flip the image vertically before loading it onto OpenGL, or otherwise adjust the rendering code so that the vertical texture coordinates t become 1 - t.
GeneralRe: Some efficiency notesmemberaquawicket25 Apr '12 - 11:31 
I agree that this method is slow.. I'm interested in finding the absolute fastest way to take the capture and display it to my openGL context. So far the fastest I've done is this.
 
void DrawIplImage1(IplImage *image, int x, int y, GLfloat xZoom, GLfloat yZoom)
{
    GLenum format;
        switch(image->nChannels) {
            case 1:
                format = GL_LUMINANCE;
                break;
            case 2:
                format = GL_LUMINANCE_ALPHA;
                break;
            case 3:
                format = GL_BGR;
                break;
            default:
                return;
        }
 
    yZoom =- yZoom;
    glRasterPos2i(x, y);
    glPixelZoom(xZoom, yZoom);
    glDrawPixels(image->width, image->height, format, GL_UNSIGNED_BYTE, image->imageData);
}
 
Although glPixelZoom seems to slow things down a ton. I've even tried blImage API but can't figure out how to do blTexture::LoadImageToTexture() as NOT mipmap. Mipmap is very slow. What is absolute fastest OpenCV 2 OpenGL ?
General[My vote of 1] Check Nehe SDKmembersrsr25 Mar '09 - 21:06 
Nehe SDK, and mainly Lesson35 gives an excellent overview and (re)useable code for Video Texture. Current article has no real interest.
SRSR
GeneralRe: [My vote of 1] Check Nehe SDKmemberArsalan Malik26 Mar '09 - 4:12 
Thanks for the NeHe link, its very useful lesson on this topic. However, the purpose of this article was to present the simplest program for those who are working with OpenCV and have hard time to convert IplImage into OpenGL texture.
 

 ARSALAN MALIK  

GeneralRe: [My vote of 1] Check Nehe SDKmembersrsr26 Mar '09 - 4:24 
According to your answer change the title of your submission to "IPLimage to OpenGL texture with OpenCV". And even in this case you can with very little change have it also working with Direct3D ( DirectX9) as at this level of generality it's same policy (buffer image to texture). I maintain my 1 : your contribution is not realy interesting for Video texture with OpenGL as announced.
SRSR

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 25 Mar 2009
Article Copyright 2009 by Arsalan Malik
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid