Blurring images

We can blur images with a Gaussian filter (for more information on Gaussian filter visit http://en.wikipedia.org/wiki/Gaussian_filter). This filter is based on the normal distribution. A corresponding SciPy function requires the standard deviation as a parameter.

In this recipe, we will also plot a polar rose and a spiral (for more information on Polar coordinate system visit http://en.wikipedia.org/wiki/Polar_coordinate_system). These figures are not directly related, but it seemed more fun to combine them here.

How to do it...

We will start by initializing the polar plots, after which we will blur the Lena image and plot in the polar coordinates.

  1. Initialization.

    Initialize the polar plots as follows:

    NFIGURES = int(sys.argv[1])
    k = numpy.random.random_integers(1, 5, NFIGURES)
    a = numpy.random.random_integers(1, 5, NFIGURES)
    
    colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
  2. Blur Lena.

    In order to blur Lena, we will apply the Gaussian filter with standard deviation of four:

    matplotlib.pyplot.subplot(212)
    blurred = scipy.ndimage.gaussian_filter(lena, sigma=4)
    
    matplotlib.pyplot.imshow(blurred)
    matplotlib.pyplot.axis('off')
  3. Plot in polar coordinates.

    Matplotlib has a polar function, which plots in polar coordinates:

    theta = numpy.linspace(0, k[0] * numpy.pi, 200)
    matplotlib.pyplot.polar(theta, numpy.sqrt(theta), choice(colors))
    
    for i in xrange(1, NFIGURES):
      theta = numpy.linspace(0, k[i] * numpy.pi, 200)
      matplotlib.pyplot.polar(theta, a[i] * numpy.cos(k[i] * theta), choice(colors))

    The result will look like the following image:

    How to do it...

The following is the complete code for this recipe:

import numpy
import matplotlib.pyplot
from random import choice
import sys
import scipy
import scipy.ndimage

# Initialization
NFIGURES = int(sys.argv[1])
k = numpy.random.random_integers(1, 5, NFIGURES)
a = numpy.random.random_integers(1, 5, NFIGURES)

colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k']

lena = scipy.misc.lena()
matplotlib.pyplot.subplot(211)
matplotlib.pyplot.imshow(lena)
matplotlib.pyplot.axis('off')

# Blur Lena
matplotlib.pyplot.subplot(212)
blurred = scipy.ndimage.gaussian_filter(lena, sigma=4)

matplotlib.pyplot.imshow(blurred)
matplotlib.pyplot.axis('off')

# Plot in polar coordinates
theta = numpy.linspace(0, k[0] * numpy.pi, 200)
matplotlib.pyplot.polar(theta, numpy.sqrt(theta), choice(colors))

for i in xrange(1, NFIGURES):
  theta = numpy.linspace(0, k[i] * numpy.pi, 200)
  matplotlib.pyplot.polar(theta, a[i] * numpy.cos(k[i] * theta), choice(colors))

matplotlib.pyplot.axis('off')

matplotlib.pyplot.show()

How it works...

We made use of the following functions in this tutorial:

Function

Description

gaussian_filter

Applies a Gaussian filter.

random_integers

Returns an array with random integer values between a high and low bound.

polar

Plots a figure using polar coordinates.

..................Content has been hidden....................

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