OpenCV & OpenGL

Oct 21, 2008 by     22 Comments    Posted under: OpenCV, OpenGL, Tutorials

In this tutorial we see how we can acces to OpenCV functions and use the images we process with it to use in our OpenGL program.

To use an OpenCV image (IplImage) and use it as an OpenGL textures we mus use this function.

int loadTexture_Ipl(IplImage *image, GLuint *text) {
if (image==NULL) return -1;
glGenTextures(1, text);
glBindTexture( GL_TEXTURE_2D, *text ); //bind the texture to it's array
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image->width, image->height,0, GL_BGR, GL_UNSIGNED_BYTE, image->imageData);
return 0;

This function only get data image and store it as an image Texture.

We can get a camera image and use it  to texture aplane, to do it we need a function to update the texture data, this we can do in OpenGL main loop or in glutiddleFunc as show

//En la funcion donde se actualizen los datos
frame = cvQueryFrame( capture );
if( frame ){
loadTexture_Ipl(frame, &texture);

Download source

22 Comments + Add Comment

  • Hi there, Damiles. Thanks a lot for sharing the code. I am working with OpenCV and OpenGL too, and this was of great help. Or would be if it worked :) the textures are showing up completely white, but I can’t figure out what’s wrong. Is there a possibility that there’s a bug in the code? Or am I overlooking something? Has this happened to you? Note that textures loaded with a BMP parser do show up correctly in the same program, so it seems the error is in the transfer of the data from IplImage to GL texture.

  • Hi Waldir, This code is too old, it get image from webcam and process it, let me some time to review it and respond with solution.

  • Waldir, i test the demo (in osx) and run well, there area a box in middle of scene rotating, and a plane with image captured from webcam.

    Instead, i go to create a more basically and clear code of this thread.

    Regards David.

  • Waldir, i post new code and new basic download demo, sure you forget set this opengl code:


    The new code is more clear and perhaps more useful.

  • Respected Sir,

    I am student of Master of Engg. in S.P. University, Gujarat. And my dissertation is in computer graphics, and hence for the well programming I may need the OpenCV support.

    So please send me the software for OpenCV. Or the basic requirements to learn the OpenCV.

    Hope for your possitive response.

    Thanking you.

    Yours truely,

    Dinesh Vaghela

  • Hi Mr. Dinesh B. Vaghela,

    First, respected sir??? please with a simply Hi is sufficient, hahaha.

    Well, the OpenCV you can download directly in soruceforge ( ) or if you work in linux you can install with apt-get install opencv or better with cvs, the info for download with cvs is in sourceforge.

    In the download you can find the documentation to learn opencv, it’s very clear and simply, it have more samples in sample directory.

    There are online wiki with the same doc here:

    Thank you for comment. Regards David.

  • Hi Damiles,
    thanx for the source code. It helped me a lot. I have a question though. I cant use any image as a texture. I found out that the sides of the image have to have their sizes of power of two. Lena.jpg works fine but other images dont (at least on my computer). Could you help me how to overcome this problem? Thanx for your help man.

  • Your source code seems really okay.

    I just detected a problem with GL_BGR that seems undeclared. I just replaced by GL_LUMINANCE and gotta work. By the way the images it’s always gray, that’s a problem to me, but i’m working on it.

    Thanks for share your knowledge and the code to the community.

  • Okay, I just checked in documentation about the gray issue and then I just go to another site that I’m referencing here:

    Of course, this only works if your Image is color, if your Image is grayscale your going to want to change GL_BGR to GL_LUMINANCE


  • Hi, thank you for sharing your work.

    First, to help Luis, just need to change GL_BGR to GL_BGR_EXT, in order to work properly.

    Secondly, i am trying to use this approach in order to show the webcam captures of opencv into an opengl window, however this is very slow. Do you have any ideas on how to make it refresh the textures faster?

  • hi

    I am student of Engg. i hav a mini project on openGL i need the simple source code for car race.
    So please send me the source code or tell me whrer i will get it.

    Hope for your possitive response.

  • Hi Ankur, there are a lot of tuts about car race over internet, search in google, this post explain how integrate opencv and opengl. I don’t have any tutorial or example about this.


  • Hi,

    nice post! I have another solution to combine opencv and opengl if you want to try.

    I use Qt 4.7 GUI Framework as HighGUI for openCV. You can build opencv with Qt Support and use QtOpenGL as well as QThreads and other benefits. The Idea is the same:

    You just convert the CvArr* to a QFrame Object. I use Mat instead of IplImage. (OpenCV v 2.0+ is better to work with Mat instead of cvMAt or IplImage because it’s old C code. Mat ist C++ style opencv Array and compatlible to all new functions).

    The QFrame can be used as a texture for QtOpenGL Object. Here’s some of the code.

    The OpenGL Widget to view frames (GLWidget.h):

    class GLWidget : public QGLWidget {

    void sendImage(Mat *img, QImage::Format);

    void initializeGL();
    void paintGL();
    void resizeGL(int width, int height);
    QImage qframe;

    Then the Class implementation (GLWidget.cpp):

    GLWidget::GLWidget() : QGLWidget(QGLFormat(QGL::SampleBuffers)) {

    void GLWidget::initializeGL() {
    void GLWidget::paintGL() {
    glClear (GL_COLOR_BUFFER_BIT);
    glClearColor (0.0,0.0,0.0,1.0);
    if (!qframe.isNull()) {
    qframe = qframe.scaled(this->size(), Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
    glDrawPixels(qframe.width(),qframe.height(), GL_RGBA, GL_UNSIGNED_BYTE, qframe.bits());

    void GLWidget::resizeGL(int w, int h) {
    glViewport (0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
    glMatrixMode (GL_MODELVIEW);
    // qDebug() <data), img->cols, img->rows, img->step, format).rgbSwapped();
    qframe = QGLWidget::convertToGLFormat(qframe);

    That’s it: You can now create GLWidgets and use them as standard QTWidgets for fancy GUI.
    When you process your frames you just have to do like this (for RGB):

    myCoolNewGLWidget->sendImage(&frame, QImage::Format_RGB888)

    or example for Grayscale:

    glWidget_canny->sendImage(&edges, QImage::Format_Indexed8);

    If you would send the canny edge frame from example in RGB888 Format you will get a black or grey screen as well. Perhaps other people had a similar problem when they talk about grey screen…

  • Wow, great comment, thanks for your code.

  • As an addition:

    Now that we can use OpenGL and Qt with cv I found this to be very interesting:

    It uses openGL for visualizing Data Arrays and interfaces Qt as well. If I had more time I would really like to start another project with this right now.

    Perhaps you guys have use of this. This should be awesome for analyzing cv data while being able to rotate and zoom the graphs you plotted.

  • This was very helpfull for me : D thanks for posting

  • Links to source files are not available. Pls have a look. Cheers!

  • Thanks for advice, i solve it.

  • Great code! Just what I needed! I was initially getting some strange output but a quick change seemed to fix it.

    Instead of:

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    I used:

    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);


  • [...] OpenCV & OpenGL by damiles [...]

  • Hi damiles, I’m currently painting Mat images in a QGLWidget translating each one to a QImage. I found your post looking for better ways for doing this, and I would like to take a look at your code to see how you render the image using textures.

    The thing is that the code’s link is not available any more so I can’t download it. Could you please restore the link so I can download it?

    Thank you very much in advance.

  • Sorry, you can download now the code from Github:

    I repair the link.

Got anything to say? Go ahead and leave a comment!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">



How Is My Site?

View Results

Loading ... Loading ...

Twitter: damiles3D

  • Could not connect to Twitter