Mirroring Video with openFrameworks - Part II

Posted on 2010-02-08 by Jan Vantomme
Tags: openframeworks, tutorial

I have been busy for the last few weeks but I finally got some time to write the next article on openFrameworks. In this article I'm going to explain the algorithms to change an RGB webcam image to grayscale and how to mirror that grayscale image.

Converting Color to Grayscale

The formula to convert the color image can be found on Wikipedia. I'm using the (11*R + 16*G + 5*B) / 32 formula in this example because the original RGB pixel values are integers and the values for the grayscale image need to be integers too.

for (int i = 0; i < camHeight; i++) {
    for (int j = 0; j < camWidth*3; j+=3) {
        int r = (i*camWidth*3) + j;
        int g = (i*camWidth*3) + (j+1);
        int b = (i*camWidth*3) + (j+2);
        int grayPixel = (11 * pixels[r] + 16 * pixels[g]
            + 5 * pixels[b]) / 32;
        videoGray[counter] = grayPixel;
    }
}

Mirroring the video

This one is a lot easier than the code I explained in the previous article. We only need to swap single pixels, not groups of three.

for (int i = 0; i < camHeight; i++) {
    for (int j = 0; j < camWidth; j++) {
        int pix1 = (i*camWidth) + j;
        int mir1 = (i*camWidth)+1 * (camWidth - j-1);
        videoMirror[pix1] = videoGray[mir1];
    }
}

The final result should look like picture below.

Mirroring a grayscale videofeed with openFrameworks

Downloads

Download the XCode project files for this tutorial: VideoMirror2.zip (openFrameworks project)

Tweet this article