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.

Get frame count from AVI

Here’s a little code snippet I wrote to get the number of frames in an AVI video file.

int getFrameCount(const char *filename) {
    // only works on AVIs
    int frameCount;
    char size[4];
    ifstream fin(filename, ios::in|ios::binary);
    if(!fin) {
        cerr << "Could not open " << filename << endl;
    fin.seekg(0x30, ios::beg); // number of frames is stored at this location
    fin.read(size, 4);
    frameCount = size[0] | size[1]<<8 | size[2]<<16 | size[3]<<24;
    return frameCount;

Use it however you like. I’m using it because OpenCV’s cvGetCaptureProperty doesn’t seem to work.