Red Filter Algorithm
Red Filter Algorithm
There are many ways you could create a filter with a red hue. One way is to consider the overall
brightness of a pixel and map it to the same brightness on a redhue scale:
This red scale has white and black at 100% brightness and 0% brightness respectively, but in
the middle, where the grayscale color would be rgb(128,128,128), this scale has a red color
rgb(255,0,0). You can see that the red (R) value increases with brightness up to 50%, then it
stays 255. Green (G) and blue (B) values are zero up to 50% and then increase to 255 at 100%.
How can we map the brightness of a pixel to its red scale value? A plot of R value vs. brightness
looks like this:
If we assume the brightness of the pixel correlates with its average value, we can use the
average value of the pixel to determine which pixel in the red spectrum we want to select for the
red filter. Darker pixels will have averages closer to 0 and brighter pixels will have averages
closer to 255. In between, we can choose a lighter or darker shade of red. This piecewise
function of R value vs average value shows how to choose the R value.
For average values less than 128, R = 2*avg, and for average values greater than or equal to
128, R = 255.
What about for the green content of the new, filtered pixel? Here is a plot showing the G value
vs. the average value.
For avg < 128, G = 0, and for avg >= 128, G = 2*avg 255. Try this yourself with the blue value.
This means that one possible algorithm for a red filter is:
1. Start with the image you want
2. For each pixel in the image
a. Calculate the average of the RGB values
b. If the average is less than 128,
i. Set the red value to two times the average
ii. Set the green value to zero
iii. Set the blue value to zero
c. Otherwise
i. Set the red value to 255.
ii. Set the green value to two times the average minus 255
iii. Set the blue value to two times the average minus 255
3. Display the filtered image
You may find it helpful to refer to a color picker tool:
http://www.w3schools.com/colors/colors_picker.asp