From 530fbbb2fc4cb739c8af075570246f2db3e0398a Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Tue, 27 Nov 2012 16:26:47 -0800 Subject: [PATCH] DOC: Minor clean-ups of equalization demo. --- doc/examples/plot_equalize.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/doc/examples/plot_equalize.py b/doc/examples/plot_equalize.py index 13ffa320..f57cea9c 100644 --- a/doc/examples/plot_equalize.py +++ b/doc/examples/plot_equalize.py @@ -18,7 +18,7 @@ that fall within the 2nd and 98th percentiles [2]_. """ -from skimage import data, img_as_ubyte +from skimage import data, img_as_float from skimage.util.dtype import dtype_range from skimage import exposure @@ -30,6 +30,7 @@ def plot_img_and_hist(img, axes, bins=256): """Plot an image along with its histogram and cumulative histogram. """ + img = img_as_float(img) ax_img, ax_hist = axes ax_cdf = ax_hist.twinx() @@ -38,16 +39,16 @@ def plot_img_and_hist(img, axes, bins=256): ax_img.set_axis_off() # Display histogram - ax_hist.hist(img.ravel(), bins=bins, histtype='stepfilled') + ax_hist.hist(img.ravel(), bins=bins, histtype='step', color='black') ax_hist.ticklabel_format(axis='y', style='scientific', scilimits=(0, 0)) ax_hist.set_xlabel('Pixel intensity') - - xmin, xmax = dtype_range[img.dtype.type] - ax_hist.set_xlim(xmin, xmax) + ax_hist.set_xlim(0, 1) + ax_hist.set_yticks([]) # Display cumulative distribution img_cdf, bins = exposure.cumulative_distribution(img, bins) ax_cdf.plot(bins, img_cdf, 'r') + ax_cdf.set_yticks([]) return ax_img, ax_hist, ax_cdf @@ -62,29 +63,31 @@ img_rescale = exposure.rescale_intensity(img, in_range=(p2, p98)) # Equalization img_eq = exposure.equalize_hist(img) -img_eq = img_as_ubyte(img_eq) # Adaptive Equalization img_adapteq = exposure.equalize_adapthist(img, clip_limit=0.03) -img_adapteq = img_as_ubyte(img_adapteq) # Display results f, axes = plt.subplots(2, 4, figsize=(8, 4)) ax_img, ax_hist, ax_cdf = plot_img_and_hist(img, axes[:, 0]) ax_img.set_title('Low contrast image') + +y_min, y_max = ax_hist.get_ylim() ax_hist.set_ylabel('Number of pixels') +ax_hist.set_yticks(np.linspace(0, y_max, 5)) ax_img, ax_hist, ax_cdf = plot_img_and_hist(img_rescale, axes[:, 1]) ax_img.set_title('Contrast stretching') ax_img, ax_hist, ax_cdf = plot_img_and_hist(img_eq, axes[:, 2]) ax_img.set_title('Histogram equalization') -ax_cdf.set_ylabel('Fraction of total intensity') ax_img, ax_hist, ax_cdf = plot_img_and_hist(img_adapteq, axes[:, 3]) ax_img.set_title('Adaptive equalization') + ax_cdf.set_ylabel('Fraction of total intensity') +ax_cdf.set_yticks(np.linspace(0, 1, 5)) # prevent overlap of y-axis labels plt.subplots_adjust(wspace=0.4)