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.

Downloads
Download the XCode project files for this tutorial: VideoMirror2.zip
Related Articles
Browse Articles
Comments (1)
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.
Popular Articles
- Introduction to openFrameworks
- Creating 3D Shapes with Hemesh
- Mirroring Video with openFrameworks
- An Introduction to colorLib
- How to create a FullScreen iPhone Application
Popular Tags
- processing (95)
- software (50)
- art (48)
- web design (40)
- photography (39)