Manipulating images

Don Blaheta, Longwood University
Why are we working on this? Once we have represented images as numbers, it becomes possible to express filters and transformations of images as computation. Image processing is an enormous area within computer science, but here we touch the surface of what it means to perform computations on images. Skills in this section: Express certain image transformations as numeric computation Concepts: Algorithm representation

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 * originalBlue
If 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.

[img with eight colors and a photo] [same img with all RGB reduced by half]
Figure 1: Darkening transformation

Another important computation is as follows:

    resultRed = (originalRed + originalGreen + originalBlue) / 3
    resultGreen = (originalRed + originalGreen + originalBlue) / 3
    resultBlue = (originalRed + originalGreen + originalBlue) / 3
The 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.

[img with eight colors and a photo] [same img with all RGB values averaged]
Figure 2: Averaging transformation
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 = originalBlue
It 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. [img with eight colors and a photo] [same img with RG values averaged, blue remains]
Figure 3: Averaging only red and green
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

  1. 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?
  2. 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?
  3. 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?
  4. "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?
  5. 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?
  6. Save a copy of the image in Figure 4: [Richmond Main Street Station]
    Figure 4: Main Street Station in Richmond, VA
    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?
  7. Save a copy of the image in Figure 5: [Natural Bridge]
    Figure 5: Natural Bridge in Rockbridge County, VA
    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:

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