From 5e9ad08b8031752473d182a7b8f9858ee92f2e52 Mon Sep 17 00:00:00 2001 From: Nicolas Pinto Date: Tue, 14 Feb 2012 19:45:38 -0500 Subject: [PATCH] DOC: update view_as_blocks and gabors_from_lena example using comments from @stevanv (PR #138) --- doc/examples/plot_gabors_from_lena.py | 39 +++++++++++++-------------- doc/examples/plot_view_as_blocks.py | 27 ++++++++++--------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/doc/examples/plot_gabors_from_lena.py b/doc/examples/plot_gabors_from_lena.py index 0f483207..187289b7 100644 --- a/doc/examples/plot_gabors_from_lena.py +++ b/doc/examples/plot_gabors_from_lena.py @@ -7,20 +7,20 @@ Gabors / Primary Visual Cortex "Simple Cells" from Lena How to build a (bio-plausible) "sparse" dictionary (or 'codebook', or 'filterbank') for e.g. image classification without any fancy math and -with just standard python scientific librairies? +with just standard python scientific libraries? Please find below a short answer ;-) This simple example shows how to get Gabor-like filters [1]_ using just the famous Lena image. Gabor filters are good approximations of the "Simple Cells" [2]_ receptive fields [3]_ found in the mammalian primary -visual cortex (V1) (for details, see e.g. the Nobel-prize winning work of Hubel -& Wiesel done in the 60s). +visual cortex (V1) (for details, see e.g. the Nobel-prize winning work +of Hubel & Wiesel done in the 60s [4]_ [5]_). -Here we use McQueen's 'kmeans' algorithm [4]_, as a simple bio-plausible -hebbian-like learning rule and we apply it (a) to patches of the -original Lena image (retinal projection), and (b) to patches of an -LGN-like [5]_ Lena image using a simple difference of gaussians (DoG) +Here we use McQueen's 'kmeans' algorithm [6]_, as a simple biologically +plausible hebbian-like learning rule and we apply it (a) to patches of +the original Lena image (retinal projection), and (b) to patches of an +LGN-like [7]_ Lena image using a simple difference of gaussians (DoG) approximation. Enjoy ;-) And keep in mind that getting Gabors on natural image patches @@ -31,39 +31,36 @@ is not rocket science. .. [3] http://en.wikipedia.org/wiki/Receptive_field .. [4] http://en.wikipedia.org/wiki/K-means_clustering .. [5] http://en.wikipedia.org/wiki/Lateral_geniculate_nucleus - -References ----------- -D. H. Hubel and T. N. Wiesel Receptive Fields of Single Neurones in the -Cat's Striate Cortex J. Physiol. pp. 574-591 (148) 1959 - -D. H. Hubel and T. N. Wiesel Receptive Fields, Binocular Interaction and -Functional Architecture in the Cat's Visual Cortex J. Physiol. 160 pp. -106-154 1962 +.. [6] D. H. Hubel and T. N. Wiesel Receptive Fields of Single Neurones +in the Cat's Striate Cortex J. Physiol. pp. 574-591 (148) 1959 +.. [7] D. H. Hubel and T. N. Wiesel Receptive Fields, Binocular +Interaction and Functional Architecture in the Cat's Visual Cortex J. +Physiol. 160 pp. 106-154 1962 """ import numpy as np -from scipy import misc from scipy.cluster.vq import kmeans2 +from scipy import ndimage as ndi import matplotlib.pyplot as plt +from skimage import data +from skimage import color from skimage.util.shape import view_as_windows from skimage.util.montage import montage2d -from scipy import ndimage as ndi np.random.seed(42) patch_shape = 8, 8 n_filters = 49 -lena = misc.lena() / 255. +lena = color.rgb2gray(data.lena()) / 255. # -- filterbank1 on original Lena patches1 = view_as_windows(lena, patch_shape) patches1 = patches1.reshape(-1, patch_shape[0] * patch_shape[1])[::8] fb1, _ = kmeans2(patches1, n_filters, minit='points') fb1 = fb1.reshape((-1,) + patch_shape) -fb1_montage = montage2d(fb1) +fb1_montage = montage2d(fb1, rescale_intensity=True) # -- filterbank2 LGN-like Lena lena_dog = ndi.gaussian_filter(lena, .5) - ndi.gaussian_filter(lena, 1) @@ -71,7 +68,7 @@ patches2 = view_as_windows(lena_dog, patch_shape) patches2 = patches2.reshape(-1, patch_shape[0] * patch_shape[1])[::8] fb2, _ = kmeans2(patches2, n_filters, minit='points') fb2 = fb2.reshape((-1,) + patch_shape) -fb2_montage = montage2d(fb2) +fb2_montage = montage2d(fb2, rescale_intensity=True) # -- plt.figure(figsize=(9, 3)) diff --git a/doc/examples/plot_view_as_blocks.py b/doc/examples/plot_view_as_blocks.py index 305192dc..3815d3ec 100644 --- a/doc/examples/plot_view_as_blocks.py +++ b/doc/examples/plot_view_as_blocks.py @@ -3,25 +3,28 @@ Block views on images/arrays ============================ -This example illustrates the use of `view_as_blocks` from `skimage.util.shape`. -Block views can be incredibly useful when one wants to perform local operations -on non-overlapping image patches. +This example illustrates the use of `view_as_blocks` from +`skimage.util.shape`. Block views can be incredibly useful when one +wants to perform local operations on non-overlapping image patches. -We use `lena` from `scipy.misc` and virtually 'slice' it into square blocks. -Then, on each block, we either pool the mean, the max or the median value of -that block. The results are displayed altogether, along with a 'classic' -`bicubic` rescaling of the original `lena` image. +We use `lena` from `skimage.data` and virtually 'slice' it into square +blocks. Then, on each block, we either pool the mean, the max or the +median value of that block. The results are displayed altogether, along +with a 'classic' `bicubic` rescaling of the original `lena` image. """ import numpy as np -from scipy.misc import lena, imresize +from scipy.misc import imresize from matplotlib import pyplot as plt import matplotlib.cm as cm + +from skimage import data +from skimage import color from skimage.util.shape import view_as_blocks -# -- get `lena` from scipy in grayscale -l = lena() +# -- get `lena` from skimage.data in grayscale +l = color.rgb2gray(data.lena()) / 255. # -- size of blocks block_shape = (4, 4) @@ -43,7 +46,7 @@ median_view = np.median(flatten_view, axis=2) plt.figure(figsize=(10, 10)) plt.subplot(221) -plt.title("Original rescaled\n in bicubic mode"); +plt.title("Original rescaled\n in bicubic mode") l_resized = imresize(l, view.shape[:2], interp='bicubic') plt.imshow(l_resized, cmap=cm.Greys_r) @@ -52,7 +55,7 @@ plt.title("Block view with\n local mean pooling") plt.imshow(mean_view, cmap=cm.Greys_r) plt.subplot(223) -plt.title("Block view with\n local max pooling"); +plt.title("Block view with\n local max pooling") plt.imshow(max_view, cmap=cm.Greys_r) plt.subplot(224)