This is one of the 100 recipes of the IPython Cookbook, the definitive guide to high-performance scientific computing and data science in Python.
You need scikit-image for this recipe. You will find the installation instructions here.
You also need to download the Beach dataset. (http://ipython-books.github.io)
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import skimage.exposure as skie
%matplotlib inline
img = plt.imread('data/pic1.jpg')[...,0]
def show(img):
# Display the image.
plt.figure(figsize=(8,2));
plt.subplot(121);
plt.imshow(img, cmap=plt.cm.gray);
plt.axis('off');
# Display the histogram.
plt.subplot(122);
plt.hist(img.ravel(), lw=0, bins=256);
plt.xlim(0, img.max());
plt.yticks([]);
plt.show()
show(img)
The histogram is unbalanced and the image appears slightly over-exposed.
rescale_intensity
function. The in_range
and out_range
define a linear mapping from the original image to the modified image. The pixels that are outside in_range
are clipped to the extremal values of out_range
. Here, the darkest pixels (intensity less than 100) become completely black (0), whereas the brightest pixels (>240) become completely white (255).show(skie.rescale_intensity(img,
in_range=(100, 240), out_range=(0, 255)))
Many intensity values seem to be missing in the histogram, which reflects the poor quality of this exposure correction technique.
show(skie.equalize_adapthist(img))
The histogram seems more balanced, and the image now appears more contrasted.
You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).
IPython Cookbook, by Cyrille Rossant, Packt Publishing, 2014 (500 pages).