Mirroring Video with openFrameworks - Part II

Posted by Jan Vantomme on 08 February 2010.
Tags: , ,

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 0camHeighti++) {
    
for (int j 0camWidth*3j+=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]
            
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 0camHeighti++) {
    
for (int j 0camWidthj++) {
        int pix1 
= (i*camWidth) + j;
        
int mir1 = (i*camWidth)+* (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

Oldskool Comments (1)

Gravatar for Jared Parnell

From: Jared Parnell
Date: 11.12.2010

Hey, just getting started with CV and openFrameWorks, this is a pleasantly straight forward and understandable intro. Look forward to hearing more.

Top · Permanent link to this comment