Image processing

In [1]:
import numpy as np
import skimage
from skimage import img_as_float
import skimage.filters as skif
from skimage.color import rgb2gray
import skimage.data as skid
import skimage.exposure as skie
from IPython.html.widgets import interact
import matplotlib.pyplot as plt
import seaborn
%matplotlib inline
In [2]:
chelsea = skid.chelsea()
In [3]:
chelsea.shape, chelsea.dtype
Out[3]:
((300, 451, 3), dtype('uint8'))
In [4]:
plt.imshow(chelsea)
plt.axis('off')
In [5]:
img = rgb2gray(chelsea)
In [6]:
img.shape, img.dtype
Out[6]:
((300, 451), dtype('float64'))
In [7]:
img
Out[7]:
array([[ 0.4852, 0.4852, ..., 0.1169, 0.1169],
       [ 0.4969, 0.4930, ..., 0.1225, 0.1272 ],
       ...,
       [ 0.4248, 0.3688, ..., 0.5544, 0.5583]])
In [8]:
p2, p98 = np.percentile(img, (2, 98))
In [9]:
img_rescale = skie.rescale_intensity(img, in_range=(p2, p98))
In [10]:
img_eq = skie.equalize_hist(img)
In [11]:
img_adapteq = img_as_float(skie.equalize_adapthist(img, clip_limit=0.03))
In [12]:
hist_types = dict([('Contrast stretching', img_rescale),
                   ('Histogram equalization', img_eq),
                   ('Adaptive equalization', img_adapteq)])
In [13]:
@interact(hist_type=list(hist_types.keys()))
def display_result(hist_type):
    result = hist_types[hist_type]

    # We display the processed grayscale image on the left.
    plt.subplot(121)
    plt.imshow(result, cmap='gray')
    plt.axis('off')

    # We display the histogram on the right.
    plt.subplot(122)
    plt.hist(result.ravel(), bins=np.linspace(0., 1., 256),
             histtype='step', color='black')

    plt.show()