diff transformations/thick.py @ 14:ebf61603489b

Added different image format handling (minibatches,32*32,lines of 1024...) and some commentaries
author Xavier Glorot <glorotxa@iro.umontreal.ca>
date Thu, 28 Jan 2010 11:50:01 -0500
parents a25474d4d34f
children c91d9f70206d
line wrap: on
line diff
--- a/transformations/thick.py	Thu Jan 28 11:28:46 2010 -0500
+++ b/transformations/thick.py	Thu Jan 28 11:50:01 2010 -0500
@@ -5,11 +5,11 @@
 Simple implementation of random thickness deformation using morphological
 operation of scipy.
 Only one morphological operation applied (dilation or erosion), the kernel is random
-out of a list of 11 symmetric kernels.
+out of a list of 12 symmetric kernels. (only 5 to be chosen for erosion because it can
+hurt the recognizability of the charater and 12 for dilation).
 
 Author: Xavier Glorot
 
-Usage:
 '''
 
 import scipy.ndimage.morphology
@@ -19,10 +19,10 @@
 class Thick():
     def __init__(self,complexity = 1):
         #---------- private attributes
-        self.__nx__ = 32
-        self.__ny__ = 32
-        self.__erodemax__ = 4
-        self.__dilatemax__ = 11
+        self.__nx__ = 32 #xdim of the images
+        self.__ny__ = 32 #ydim of the images
+        self.__erodemax__ = 4 #nb of index max of erode structuring elements
+        self.__dilatemax__ = 11 #nb of index max of dilation structuring elements
         self.__structuring_elements__ = [N.asarray([[1,1]]),N.asarray([[1],[1]]),\
                                         N.asarray([[1,1],[1,1]]),N.asarray([[0,1,0],[1,1,1],[0,1,0]]),\
                                         N.asarray([[1,1,1],[1,1,1]]),N.asarray([[1,1],[1,1],[1,1]]),\
@@ -63,7 +63,7 @@
             self.nb = -1
         return self._get_current_parameters()
     
-    def transform_1_image(self,image):
+    def transform_1_image(self,image): #the real transformation method
         if self.meth!=0:
             maxi = float(N.max(image))
             mini = float(N.min(image))
@@ -87,16 +87,23 @@
         else:
             return image
     
-    def transform_image(self,image):
-        if image.ndim == 2:
+    def transform_image(self,image): #handling different format
+        if image.shape == (self.__nx__,self.__ny__):
+            return self.transform_1_image(image)
+        if image.ndim == 3:
+            for i in range(image.shape[0]):
+                image[i,:,:] = self.transform_1_image(image[i,:,:])
+            return N.reshape(newimage,image.shape)
+        if image.ndim == 2 and image.shape != (self.__nx__,self.__ny__):
             newimage = N.reshape(image,(image.shape[0],self.__nx__,self.__ny__))
             for i in range(image.shape[0]):
                 newimage[i,:,:] = self.transform_1_image(newimage[i,:,:])
             return N.reshape(newimage,image.shape)
-        else:
+        if image.ndim == 1:
             newimage = N.reshape(image,(self.__nx__,self.__ny__))
             newimage = self.transform_1_image(newimage)
             return N.reshape(newimage,image.shape)
+        assert False #should never go there