Draw IplImage

OpenCV stores images in a data structure called IplImage. They provide methods for rendering it to the screen, but if you want to use OpenGL instead (which should be faster and gives you more flexibility), I wrote the following code. It should be a little faster than loading the IplImage into a texture first, and then drawing it (if you have to do this every frame).

void DrawIplImage(IplImage *image, int x = 0, int y = 0, GLfloat xZoom = 1.0f, GLfloat yZoom = 1.0f)
    GLenum format;
        switch(image->nChannels) {
            case 1:
                format = GL_LUMINANCE;
            case 2:
                format = GL_LUMINANCE_ALPHA;
            case 3:
                format = GL_BGR;
        glRasterPos2i(x, y);
        glPixelZoom(xZoom, yZoom);
    glDrawPixels(image->width, image->height, format, GL_UNSIGNED_BYTE, image->imageData);

It should be pretty straight forward to use. Handles IplImages with 1 or 3 layers. Should work with 2 layers too, but I haven’t needed or found an example of this.

2 thoughts on “Draw IplImage

Leave a Reply

Your email address will not be published. Required fields are marked *