Project: Image Filters

Many common image manipulations can be described as the application of a filter. An image filter is a square array of coefficients that describes how old pixel values will be combined to find a new pixel value. Each entry in the array is a coefficient to be multiplied by the color in that location. The resulting RGB values are then added together to produce the final result. The center of the filter corresponds to the position of the pixel whose color is currently being computed.

For example, this is a 3 × 3 filter that takes 0 times the current color (in the center) plus 1/8 times each of the colors in the surrounding pixels:

1/8

1/8

1/8

1/8

0

1/8

1/8

1/8

1/8

The fraction 1/8 is used so that the resulting image has approximately the same brightness as the original, because the sum of the pixel values from the filter is one “pixel’s worth” of color. The effect of this filter is to blur the original image since the (possibly) distinctive color that used to be in the center location has been replaced by a combination of all the colors around it.

The following filter performs a type of edge detection by highlighting whatever is different in the center pixel while removing the colors that surround it:

0

−1

0

−1

4

−1

0

−1

0

It gives a combined zero pixel’s worth of color, which means that the resulting image will be quite dark. Either integers or fractions may be used in filters, and coefficients may be positive, negative, or zero.

Applying Filters

In order to apply a filter to an image, the for loops that iterate over all pixels in the original image must be adjusted to avoid the edges. At each pixel, we need to be able to access the pixels above, below, left, and right of it. So, for a 3 × 3 filter, the main loops look like this:

for i in range(1, width−1):
 for j in range(1, height−1):
   # apply filter to pixel (i, j) in original image

More refined effects may be achieved with 5×5 filters, and they require moving in two pixels from every edge.

Exercises

  1. Write a function applyfilter(f, img) that returns a new image created by applying the filter f to the given image img. Assume f returns an RGB tuple and takes three parameters: the original image img and the coordinates i and j.

    ⇒ Caution: Do not modify the original image, because the filter depends on having the old values at every pixel.

  2. Write a function createfilter(coeff, pixels, i, j) that can be used to create filter functions to pass to applyfilter(). The coeff parameter is a list of the filter coefficients in this order:

    0

    1

    2

    3

    4

    5

    6

    7

    8

    The idea is that by writing this function you can then define a specific filter like this:

     def blur(img, i, j):
      return createfilter([1/8, 1/8, 1/8,
          1/8, 0, 1/8,
          1/8, 1/8, 1/8], img, i, j)

    Then the blur() function could be passed to applyfilter().

    The createfilter() function contains all the messy work of the filter calculation. For each component (R, G, B), it computes the sum over the 3 × 3 grid:

     coeff[0] * img.getpixel((i − 1, j − 1)) +
      coeff[1] * img.getpixel((i, j − 1)) + ...

    By putting this calculation in one place, you do not have to rewrite it for every different filter. Remember that image RGB components must be integers.

  3. Implement these filters:
    1. (a) Blur

      1/8

      1/8

      1/8

      1/8

      0

      1/8

      1/8

      1/8

      1/8

    2. (b) Sharpen

      0

      −1

      0

      −1

      5

      −1

      0

      −1

      0

    3. (c) Edge detection

      0

      −1

      0

      −1

      4

      −1

      0

      −1

      0

    4. (d) Emboss

      2

      0

      0

      0

      −1

      0

      0

      0

      −1

      Try this one on grayscale images.

    5. (e) Mean (averaging)

      1/9

      1/9

      1/9

      1/9

      1/9

      1/9

      1/9

      1/9

      1/9

  4. Implement a 3 × 3 filter of your choice. Describe the effect you intend it to produce.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.55.75