ENH: add a 'normalize' kwarg to util.montage2d()

This commit is contained in:
Nicolas Pinto
2012-02-14 00:04:42 -05:00
parent 99efa1b771
commit 64177b2000
2 changed files with 20 additions and 5 deletions
+4 -3
View File
@@ -2,8 +2,9 @@
Project coordination
- Nicolas Pinto
Colour spaces and filters
Shape views: ``shape.view_as_windows`` and ``shape.view_as_blocks``
Colour spaces and filters.
Shape views: ``util.shape.view_as_windows`` and ``util.shape.view_as_blocks``
Montage helpers: ``util.montage``
- Damian Eads
Morphological operators
@@ -96,4 +97,4 @@
Harris corner detector
- Nicolas Poilvert
Shape views: ``shape.view_as_windows`` and ``shape.view_as_blocks``
Shape views: ``util.shape.view_as_windows`` and ``util.shape.view_as_blocks``
+16 -2
View File
@@ -2,8 +2,10 @@ __all__ = ['montage2d']
import numpy as np
EPSILON = 1e-6
def montage2d(arr_in, fill='mean'):
def montage2d(arr_in, fill='mean', normalize=True):
"""Create a 2-dimensional 'montage' from a 3-dimensional input array
representing an ensemble of equally shaped 2-dimensional images.
@@ -29,10 +31,13 @@ def montage2d(arr_in, fill='mean'):
3-dimensional input array representing an ensemble of n_images
of equal shape (i.e. [height, width]).
fill: float or 'mean'
fill: float or 'mean', optional
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.
Returns
-------
arr_out: ndarray, shape=[alpha * height, alpha * width]
@@ -68,6 +73,15 @@ def montage2d(arr_in, fill='mean'):
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
# -- determine alpha
alpha = int(np.ceil(np.sqrt(n_images)))