Time for action – manipulating Lena

In the scipy.misc module, there is a utility that loads the image of “Lena”. This is the image of Lena Soderberg traditionally used for image processing examples. We will apply some filters on this image and rotate it. Perform the following steps to do so:

  1. Load the “Lena” image and display it in a subplot with grayscale colormap.
    image = misc.lena().astype(np.float32)
    
    plt.subplot(221)
    plt.title(“Original Image”)
    img = plt.imshow(image, cmap=plt.cm.gray)

    Note that we are dealing with a float32 array.

  2. The median filter scans the signal and replaces each item by the median of neighboring data points. Apply a median filter to the image and display it in a second subplot.
    plt.subplot(222)
    plt.title(“Median Filter”)
    filtered = ndimage.median_filter(image, size=(42,42))
    plt.imshow(filtered, cmap=plt.cm.gray)
  3. Rotate the image and display it in the third subplot.
    plt.subplot(223)
    plt.title(“Rotated”)
    rotated = ndimage.rotate(image, 90)
    plt.imshow(rotated, cmap=plt.cm.gray)
  4. The Prewitt filter is based on computing the gradient of image intensity. Apply a Prewitt filter to the image and display it in the fourth subplot.
    plt.subplot(224)
    plt.title(“Prewitt Filter”)
    filtered = ndimage.prewitt(image)
    plt.imshow(filtered, cmap=plt.cm.gray)
    plt.show()

    The following are the resulting images:

    Time for action – manipulating Lena

What just happened?

We manipulated the image of “Lena” in several ways using the scipy.ndimage module (see images.py).

from scipy import misc
import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage


image = misc.lena().astype(np.float32)

plt.subplot(221)
plt.title(“Original Image”) 
img = plt.imshow(image, cmap=plt.cm.gray) 
plt.axis(“off”)

plt.subplot(222) 
plt.title(“Median Filter”) 
filtered = ndimage.median_filter(image, size=(42,42))
plt.imshow(filtered, cmap=plt.cm.gray) 
plt.axis(“off”)

plt.subplot(223) 
plt.title(“Rotated”) 
rotated = ndimage.rotate(image, 90)
plt.imshow(rotated, cmap=plt.cm.gray) 
plt.axis(“off”)

plt.subplot(224) 
plt.title(“Prewitt Filter”) 
filtered = ndimage.prewitt(image)
plt.imshow(filtered, cmap=plt.cm.gray) 
plt.axis(“off”)
plt.show()
..................Content has been hidden....................

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