CPSC 120: Lab Assignment 6

Due at 2:59 pm on Thursday, 04/24/2014

Home | Calendar | Announcements | Assignments | Example Code | Notes

Summary

In this assignment, you will implement a number of algorithms for manipulating images, in particular the common techniques used for conversion of color images to black and white. The goal here is to familiarize you with some of the established techniques that are used in image manipulation applications. All of these are stripped-down versions of real algorithms, such as you might see in GIMP, Adobe Photoshop, Apple Aperture, Corel PaintShop, and other commercial-grade image processing programs.

You will complete a total of seven methods, all of which should be placed in a single Processing sketch file named lab6.

Black and White Photographic Filters

Processing offers a number of basic filters for images, via the filter() command. (Try out a few of these. They're pretty neat.) Among the options for filter() is GRAY, which converts a color image into a black and white one.

It's not hard to build our own version of this. The basic idea is to set the red, green, and blue values of each pixel to the same value. This gives us a version of the image consisting only of its luminosity. The value we'll pick is the average of the three channels:

```void grayscale(color[] pxs) {
int i = 0;
while (i < pxs.length) {
color c = pxs[i];

int lum = (int) ( (red(c) + green(c) + blue(c)) / 3.0 );
pxs[i] = color(lum,lum,lum);
i = i + 1;
}
}
```

Unfortunately, this technique, while easy, is quite naive. The resulting black and white images will often lack the punch of more artistic efforts.

In this problem, you will improve on the basic technique by implementing functions that serve as contrast-enhancing filters for an image:

Such filters are common in black and white photography. The idea is to selectively block parts of the spectrum while allowing others to pass through. Depending on the overall type and distribution of color in a scene, a color filter can either enhance or decrease the contrast of the image.

Read this short article (link) before going further. Make sure to try out the examples. Stop reading at the end of the "Digital Color Into Black and White" section (right after the lighthouse example).

Here, for example, is a "red filter"—

```void redfilter(color[] pxs) {
int i = 0;
while (i < pxs.length) {
color c = pxs[i];

int lum = red(c);
pxs[i] = color(lum,0,0);
i = i + 1;
}
}
```

—and here's a version that converts an image to black and white, based on the red filter:

```void redBWfilter(color[] pxs) {
int i = 0;
while (i < pxs.length) {
color c = pxs[i];

int lum = red(c);
pxs[i] = color(lum,lum,lum);
i = i + 1;
}
}
```

Try out this function, and also compare the effect of the "red filter" conversion, versus the naive grayscale approach and the built-in filter() method.

Improved grayscale, with channel mixing

Revisit the cambridgeincolor.com article (link). This time, read the section on "Channel Mixing".

In grayscale conversion, the idea behind channel mixing is that, instead of simply taking the average of the red, green and blue values for each pixel, we assign weights to each of these channels. If the sum of the weights is exactly 1.0, the luminosity of the resulting image is that of the original color image. Weight sums less than 1 result in a darker image, while values greater than 1 produce a brighter version. For example, the call

```channelMixGray(pixels, 0.3, 0.59, 0.11);
```

would compute a luminosity value for each pixel of

```lum = r * 0.3 + g * 0.59 + b * 0.11
```

Likewise, the call

```channelMixGray(pixels, 1.0, 0.0, 0.0);
```

would compute a luminosity value for each pixel of

```lum = r * 1.0 + g * 0.0 + b * 0.0
```

which is essentially the effect that a red filter has on black and white film.

Problems

Write methods that express three similar pairs of filters (a total of six methods):

1. blue filter: allows only the blue component of a color to pass through. Make a version of this that produces a blue image, and another one that uses the blue channel to control luminosity in a black and white image.
2. green filter: allows only the green component of a color to pass through. Again, produce both a green image filter and the corresponding black and white filter.
3. yellow filter: allows only the red and green components of a color to pass through (because red + green = yellow). Note, however, that for the yellow-image filter, you will need a bit of extra thought, in order to avoid any reddish or greenish tinge in the image: the overall effect should be shades of yellow.

And finally,

1. Implement the channelMixGray method.

Standards

• Your code must be syntactically correct. Any solution that contains a syntax error anywhere will receive no credit for that problem. If you run into any trouble here, please ask me for help.

• Naturally, your code must be behaviorally correct, though partial credit will be given for partial solutions.

Turn in

As with all assignments for this course, submit the folder containing your Processing sketches. This should be a single folder named "hw6", which will contain your methods, all in a single source code file. You do not need to submit a paper printout of anything. Again, the turnin directory is

~lasseter/classes/cpsc120/username

John H. E. Lasseter