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 chelsea = skid.chelsea() chelsea.shape, chelsea.dtype plt.imshow(chelsea) plt.axis('off') img = rgb2gray(chelsea) img.shape, img.dtype img p2, p98 = np.percentile(img, (2, 98)) img_rescale = skie.rescale_intensity(img, in_range=(p2, p98)) img_eq = skie.equalize_hist(img) img_adapteq = img_as_float(skie.equalize_adapthist(img, clip_limit=0.03)) hist_types = dict([('Contrast stretching', img_rescale), ('Histogram equalization', img_eq), ('Adaptive equalization', img_adapteq)]) @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()