changeset 969:bc22f739b54c

image_tiling - added dynamic_range parameter to avoid amplifying noise
author James Bergstra <bergstrj@iro.umontreal.ca>
date Fri, 20 Aug 2010 15:29:13 -0400
parents c96dc085b5b7
children 930b92f88e61
files pylearn/io/image_tiling.py
diffstat 1 files changed, 10 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/pylearn/io/image_tiling.py	Fri Aug 20 15:28:28 2010 -0400
+++ b/pylearn/io/image_tiling.py	Fri Aug 20 15:29:13 2010 -0400
@@ -8,13 +8,14 @@
 def scale_to_unit_interval(ndar,eps=1e-8):
     ndar = ndar.copy()
     ndar -= ndar.min()
-    ndar *= 1.0 / (ndar.max()+eps)
+    ndar *= 1.0 / max(ndar.max(),eps)
     return ndar
 
 def tile_raster_images(X, img_shape, 
         tile_shape=None, tile_spacing=(1,1),
         scale_rows_to_unit_interval=True, 
-        output_pixel_vals=True
+        output_pixel_vals=True,
+        min_dynamic_range=1e-4,
         ):
     """
     Transform an array with one flattened image per row, into an array in which images are
@@ -30,6 +31,10 @@
     :type tile_shape: tuple; (rows, cols)
     :param tile_shape: the number of images to tile (rows, cols) (Defaults to a square-ish
         shape with the right area for the number of images)
+    :type min_dynamic_range: positive float
+    :param min_dynamic_range: the dynamic range of each image is used in scaling to the unit
+        interval, but images with less dynamic range than this will be scaled as if this were
+        the dynamic range.
 
     :returns: array suitable for viewing as an image.  (See:`PIL.Image.fromarray`.)
     :rtype: a 2-d array with same dtype as X.
@@ -82,7 +87,9 @@
             for tile_col in xrange(tile_shape[1]):
                 if tile_row * tile_shape[1] + tile_col < X.shape[0]:
                     if scale_rows_to_unit_interval:
-                        this_img = scale_to_unit_interval(X[tile_row * tile_shape[1] + tile_col].reshape(img_shape))
+                        this_img = scale_to_unit_interval(
+                                X[tile_row * tile_shape[1] + tile_col].reshape(img_shape),
+                                eps=min_dynamic_range)
                     else:
                         this_img = X[tile_row * tile_shape[1] + tile_col].reshape(img_shape)
                     out_array[