From 45d960d64f765dcf1ace17fe6c5baf052c32656f Mon Sep 17 00:00:00 2001 From: Horea Christian Date: Thu, 27 Jun 2013 08:15:49 +0200 Subject: [PATCH] User-specified output ratio Added a keyword argument which enables the user to select other ratios for the output image rather than just 1:1. --- skimage/util/montage.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/skimage/util/montage.py b/skimage/util/montage.py index df3ad12e..4d7deb9d 100644 --- a/skimage/util/montage.py +++ b/skimage/util/montage.py @@ -1,12 +1,12 @@ __all__ = ['montage2d'] import numpy as np -from .. import exposure +#~ from .. import exposure EPSILON = 1e-6 -def montage2d(arr_in, fill='mean', rescale_intensity=False): +def montage2d(arr_in, fill='mean', rescale_intensity=False, output_shape=(0,0)): """Create a 2-dimensional 'montage' from a 3-dimensional input array representing an ensemble of equally shaped 2-dimensional images. @@ -38,6 +38,9 @@ def montage2d(arr_in, fill='mean', rescale_intensity=False): rescale_intensity: bool, optional Whether to rescale the intensity of each image to [0, 1]. + + output_shape: tuple, optional + The desired aspect ratio for the montage (default is square). Returns ------- @@ -80,19 +83,23 @@ def montage2d(arr_in, fill='mean', rescale_intensity=False): arr_in[i] = exposure.rescale_intensity(arr_in[i]) # -- determine alpha - alpha = int(np.ceil(np.sqrt(n_images))) + if output_shape == (0,0): + alpha_y = alpha_x = int(np.ceil(np.sqrt(n_images))) + else: + alpha_y = output_shape[0] + alpha_x = output_shape[1] # -- fill missing patches if fill == 'mean': fill = arr_in.mean() - n_missing = int((alpha**2.) - n_images) + n_missing = int((alpha_y*alpha_x) - n_images) missing = np.ones((n_missing, height, width), dtype=arr_in.dtype) * fill arr_out = np.vstack((arr_in, missing)) # -- reshape to 2d montage, step by step - arr_out = arr_out.reshape(alpha, alpha, height, width) + arr_out = arr_out.reshape(alpha_y, alpha_x, height, width) arr_out = arr_out.swapaxes(1, 2) - arr_out = arr_out.reshape(alpha * height, alpha * width) + arr_out = arr_out.reshape(alpha_y * height, alpha_x * width) return arr_out