From 68f8c68c3ff286f60e07d571068e01fb70377483 Mon Sep 17 00:00:00 2001 From: Nicolas Pinto Date: Tue, 14 Feb 2012 19:36:07 -0500 Subject: [PATCH] FIX: util.montage2d 'normalize' becomes 'rescale_intensity' and defaults to False --- skimage/util/montage.py | 20 +++++++++----------- skimage/util/tests/test_montage.py | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/skimage/util/montage.py b/skimage/util/montage.py index 0319fe84..a879a0a2 100644 --- a/skimage/util/montage.py +++ b/skimage/util/montage.py @@ -1,11 +1,12 @@ __all__ = ['montage2d'] import numpy as np +from .. import exposure EPSILON = 1e-6 -def montage2d(arr_in, fill='mean', normalize=True): +def montage2d(arr_in, fill='mean', rescale_intensity=False): """Create a 2-dimensional 'montage' from a 3-dimensional input array representing an ensemble of equally shaped 2-dimensional images. @@ -35,8 +36,8 @@ def montage2d(arr_in, fill='mean', normalize=True): How to fill the 2-dimensional output array when sqrt(n_images) is not an integer. If 'mean' is chosen, then fill = arr_in.mean(). - normalize: bool, optional - Whether to normalize each image with zero-mean, unit-variance. + rescale_intensity: bool, optional + Whether to rescale the intensity of each image to [0, 1]. Returns ------- @@ -73,14 +74,11 @@ def montage2d(arr_in, fill='mean', normalize=True): n_images, height, width = arr_in.shape - # -- normalize if necessary - if normalize: - arr_in = arr_in.T - arr_in -= arr_in.mean(0) - astd = arr_in.std(0) - astd[astd < EPSILON] = 1 - arr_in /= astd - arr_in = arr_in.T + # -- rescale intensity if necessary + if rescale_intensity: + for i in xrange(n_images): + arr_in[i] = exposure.rescale_intensity( + arr_in[i], out_range=(0.0, 1.0)) # -- determine alpha alpha = int(np.ceil(np.sqrt(n_images))) diff --git a/skimage/util/tests/test_montage.py b/skimage/util/tests/test_montage.py index cd7151ab..47e5426d 100644 --- a/skimage/util/tests/test_montage.py +++ b/skimage/util/tests/test_montage.py @@ -53,6 +53,28 @@ def test_shape(): assert_equal(arr_out.shape, (alpha * height, alpha * width)) +def test_rescale_intensity(): + n_images = 4 + height, width = 3, 3 + arr_in = np.arange(n_images * height * width, dtype=np.float32) + arr_in = arr_in.reshape(n_images, height, width) + + arr_out = montage2d(arr_in, rescale_intensity=True) + + gt = np.array( + [[ 0. , 0.125, 0.25 , 0. , 0.125, 0.25 ], + [ 0.375, 0.5 , 0.625, 0.375, 0.5 , 0.625], + [ 0.75 , 0.875, 1. , 0.75 , 0.875, 1. ], + [ 0. , 0.125, 0.25 , 0. , 0.125, 0.25 ], + [ 0.375, 0.5 , 0.625, 0.375, 0.5 , 0.625], + [ 0.75 , 0.875, 1. , 0.75 , 0.875, 1. ]] + ) + + assert_equal(arr_out.min(), 0.0) + assert_equal(arr_out.max(), 1.0) + assert_array_equal(arr_out, gt) + + @raises(AssertionError) def test_error_ndim(): arr_error = np.random.randn(1, 2, 3, 4)