8 February 2010

Author

Jan Vantomme

Categories

Generative Design
Tutorial

Mirroring Video with openFrameworks - Part II

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

Top · Tweet about this

Browse Articles

0 Opinions posted so far. Now go post your own. To the comment form!

Commenting is closed for this article.

Subscribe to this blog

About this blog

This is the personal weblog of Jan Vantomme.
I write about the everyday things that move me as a designer. I write shorter things on Twitter.

Add to Technorati Favorites

Some of the blogs I like

A Brief Message
Design opinions expressed in 200 words or less
Max Design
Standards based web design, development and training
The Idée Blog
A blog by Idée about the technology they develop
Digital Web Magazine
The web professional's online magazine of choice
John Nack on Adobe
Blog about Adobe Photoshop &amp; Photography by John Nack