Manipulating images
Having done the hard work of figuring out how to represent images as a bunch of numbers, image processing—such as might be done to adjust an image in your camera or apply a filter in an app like Instagram—is reduced to a bunch of numeric computations. In the simplest cases, each pixel of the resulting image is based on exactly one pixel of the original image, and so each number (RGB color component) in the result is just a combination of up to three numbers (RGB color components) from the original. For instance, consider the following computations:
resultRed = 0.5 * originalRed resultGreen = 0.5 * originalGreen resultBlue = 0.5 * originalBlueIf these computations are applied to each pixel of an original image, the result image will look much the same, but darker: each pixel will have red, green, and blue in the same proportions, but less of each one. In Figure 1, you can look at a demonstration image comprised of eight blocks of pure color and a photograph (of the lighthouse on Assateague Island); on the left is the original image, and on the right is the image resulting from the above set of computations. →
Another important computation is as follows:
resultRed = (originalRed + originalGreen + originalBlue) / 3 resultGreen = (originalRed + originalGreen + originalBlue) / 3 resultBlue = (originalRed + originalGreen + originalBlue) / 3The first thing to notice here is that all three RGB components in the result will be the same for any particular pixel (although possibly different from other pixels). That means that all the pixels will be white, or grey, or black. How light or dark will they be? The right side of each equation here computes the average of the original pixel's RGB values—yielding a result that is about as bright, overall, as the original pixel, but in a neutral grey. In fact, this set of computations produces an image that is a black-and-white rendering of the original. Figure 2 shows the results of this transformation on the same demonstration image as before. →
It is perhaps worth pointing out here that pure black and pure white, as in the top blocks, are completely unchanged; and that the three blocks with pure red, green, and blue—which each have a maximum value in their own color and zero for the other two—result in identical medium-dark grey blocks after averaging. The other three color blocks, representing varying mixes of the three primary colors, yield different grey values. The pink block, with maximum red and blue but also a great deal of green to lighten it, averages out to an extremely light grey. The dark cyan block has no red but considerable amounts of both blue and green, so its grey value is in the middle.
Here is a third set of computations.
resultRed = (originalRed + originalGreen) / 2 resultGreen = (originalRed + originalGreen) / 2 resultBlue = originalBlueIt is similar to the previous one, in that it computes an average, but it retains the blue value of the original; only the red and green values are averaged. Figure 3 shows this transformation on our demonstration image. →
As you can see, the white and black and blue parts of the image are unchanged, or nearly so, while the more red or green parts show the most change—to a sort of muddy olive-brown color, in various levels of lightness or darkness. This transformation is a simplified way of simulating red-green colorblindness; while the image on the right is not exactly what a colorblind person would see, it replicates the way that reds and greens are not distinguishable, while blues remain distinct. (Compared to the black-and-white image from the previous figure, much of the color information is clearly preserved.)
Exercises
- Save a copy of the demonstration image and use software to
process the following computation on it:
resultRed = originalRed resultGreen = originalGreen resultBlue = 0
What happens? - Save a copy of the demonstration image and use software to
process the following computation on it:
resultRed = originalRed resultGreen = 255 resultBlue = originalBlue
What happens? - Save a copy of the demonstration image and use software to
process the following computation on it:
resultRed = originalGreen resultGreen = originalRed resultBlue = originalBlue
What happens? - "Sepia toning" is a technique to take a monochrome image and make it feel "warmer" by using brown and cream colors rather than black and white. (The opening sequence of The Wizard of Oz is a famous example of this, and you can google for many other examples.) How might you mimic this effect using an RGB transformation?
- Due to the physical film and chemicals in use at the time (and the way they fade over time), photographs from the 1970s tend to have a reddish appearance, with more red than a perfect reproduction and somewhat less blue and green. (Instagram's "1977" filter is an attempt to mimic this effect.) How might you mimic this effect using an RGB transformation?
- Save a copy of the image in Figure 4:
Use software to apply some of the transformations discussed above to this image. How do they affect this image the same as the others? How are the effects different? - Save a copy of the image in Figure 5:
Use software to apply some of the transformations discussed above to this image. How do they affect this image the same as the others? How are the effects different?
Credits and licensing
Some images were sourced from Wikimedia Commons:
- Assateague lighthouse image, by user Dough4872 (with slight modifications)
- Richmond's Main Street Station image, by user Onemoregain (cropped and resized)
- Natural Bridge image, by user forestfufighting
Other images and all text are by Don Blaheta, licensed under a Creative Commons BY-SA 3.0 license.
Version 2017-Jan-13 23:00