Powered by Blogger.

Tuesday, October 21, 2008

Tag: , ,

OpenCV & OpenGL

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

About David Millán Escrivá

David completed his studies in Universidad Politecnica de Valencia in IT with a Master's degree in artificial intelligence, computer graphics, and pattern recognition, focusing on pattern recognition and Computer Vision. David has more than 15 years of experience in IT, with more than ten years of experience in Computer Vision, computer graphics, and pattern recognition, working on different projects and start-ups, applying his knowledge of Computer Vision, optical character recognition, and augmented reality. Co-Author of two OpenCV books and reviewer of few more.


  1. 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.

  2. 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.

  3. Mr. Dinesh B. VaghelaJanuary 30, 2009 at 1:42 PM

    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

  4. 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.

  5. 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.

  6. 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.

  7. 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.


  8. Hello

    Can you tell me how to create avatar in OpenGL and use them in OpenCV to compute gesture recognition

  9. Wow, great comment, thanks for your code.

  10. 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.

  11. Would go read the rest of your blog posts now.

  12. Would go read the rest of your blog posts now.

  13. This was very helpfull for me : D thanks for posting

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

  15. Thanks for advice, i solve it.

  16. Hi Damiles,

    I am just about to start working with an iOS project using OpenCV.
    To get a better performance I want to use OpenGL shaders.

    Any experience or pointers using OpenGL and OpenCV for iOS?

    Thank you for sharing


  17. 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);


  18. [...] OpenCV & OpenGL by damiles http://blog.damiles.com/2008/10/opencv-opengl/ [...]

  19. Sorry, you can download now the code from Github: https://github.com/damiles/OpenCV_OpenGL_Tutorial

    I repair the link.

  20. hello sir,
    i gotta integrate opengl n opencv code in which i need to move robotarm in opengl depending on the shape of the object read by opencv i have got both the codes but how do i integrate them??

  21. Bluehost's overly simple, one option plan does offer some benefits.
    Today I am going to reveal to you the truth behind acme people search scam and it's business opportunity.

    For example, if you are hosting your site on a shared server,
    someone abusing the server may cause the server to go down temporarily.

  22. Its such as you learn my mind! You appear to understnd so much
    about this, like you wrote the guide inn it or something.

    I believe thzt you simply can doo wijth some p.c.
    to powewr the message home a littke bit, however other than that,
    that is magnificent blog. A great read. I'll definitely
    be back.

    My web blog certificate in project management [Chelsey]