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.
We will start by initializing the polar plots, after which we will blur the Lena image and plot in the polar coordinates.
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']
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')
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:
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()