comparison 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
comparison
equal deleted inserted replaced
13:a25474d4d34f 14:ebf61603489b
3 3
4 ''' 4 '''
5 Simple implementation of random thickness deformation using morphological 5 Simple implementation of random thickness deformation using morphological
6 operation of scipy. 6 operation of scipy.
7 Only one morphological operation applied (dilation or erosion), the kernel is random 7 Only one morphological operation applied (dilation or erosion), the kernel is random
8 out of a list of 11 symmetric kernels. 8 out of a list of 12 symmetric kernels. (only 5 to be chosen for erosion because it can
9 hurt the recognizability of the charater and 12 for dilation).
9 10
10 Author: Xavier Glorot 11 Author: Xavier Glorot
11 12
12 Usage:
13 ''' 13 '''
14 14
15 import scipy.ndimage.morphology 15 import scipy.ndimage.morphology
16 import numpy as N 16 import numpy as N
17 17
18 18
19 class Thick(): 19 class Thick():
20 def __init__(self,complexity = 1): 20 def __init__(self,complexity = 1):
21 #---------- private attributes 21 #---------- private attributes
22 self.__nx__ = 32 22 self.__nx__ = 32 #xdim of the images
23 self.__ny__ = 32 23 self.__ny__ = 32 #ydim of the images
24 self.__erodemax__ = 4 24 self.__erodemax__ = 4 #nb of index max of erode structuring elements
25 self.__dilatemax__ = 11 25 self.__dilatemax__ = 11 #nb of index max of dilation structuring elements
26 self.__structuring_elements__ = [N.asarray([[1,1]]),N.asarray([[1],[1]]),\ 26 self.__structuring_elements__ = [N.asarray([[1,1]]),N.asarray([[1],[1]]),\
27 N.asarray([[1,1],[1,1]]),N.asarray([[0,1,0],[1,1,1],[0,1,0]]),\ 27 N.asarray([[1,1],[1,1]]),N.asarray([[0,1,0],[1,1,1],[0,1,0]]),\
28 N.asarray([[1,1,1],[1,1,1]]),N.asarray([[1,1],[1,1],[1,1]]),\ 28 N.asarray([[1,1,1],[1,1,1]]),N.asarray([[1,1],[1,1],[1,1]]),\
29 N.asarray([[1,1,1],[1,1,1],[1,1,1]]),\ 29 N.asarray([[1,1,1],[1,1,1],[1,1,1]]),\
30 N.asarray([[1,1,1,1],[1,1,1,1],[1,1,1,1]]),\ 30 N.asarray([[1,1,1,1],[1,1,1,1],[1,1,1,1]]),\
61 else: 61 else:
62 self.meth = 0 62 self.meth = 0
63 self.nb = -1 63 self.nb = -1
64 return self._get_current_parameters() 64 return self._get_current_parameters()
65 65
66 def transform_1_image(self,image): 66 def transform_1_image(self,image): #the real transformation method
67 if self.meth!=0: 67 if self.meth!=0:
68 maxi = float(N.max(image)) 68 maxi = float(N.max(image))
69 mini = float(N.min(image)) 69 mini = float(N.min(image))
70 70
71 if maxi>1.0: 71 if maxi>1.0:
85 #-------- 85 #--------
86 return trans 86 return trans
87 else: 87 else:
88 return image 88 return image
89 89
90 def transform_image(self,image): 90 def transform_image(self,image): #handling different format
91 if image.ndim == 2: 91 if image.shape == (self.__nx__,self.__ny__):
92 return self.transform_1_image(image)
93 if image.ndim == 3:
94 for i in range(image.shape[0]):
95 image[i,:,:] = self.transform_1_image(image[i,:,:])
96 return N.reshape(newimage,image.shape)
97 if image.ndim == 2 and image.shape != (self.__nx__,self.__ny__):
92 newimage = N.reshape(image,(image.shape[0],self.__nx__,self.__ny__)) 98 newimage = N.reshape(image,(image.shape[0],self.__nx__,self.__ny__))
93 for i in range(image.shape[0]): 99 for i in range(image.shape[0]):
94 newimage[i,:,:] = self.transform_1_image(newimage[i,:,:]) 100 newimage[i,:,:] = self.transform_1_image(newimage[i,:,:])
95 return N.reshape(newimage,image.shape) 101 return N.reshape(newimage,image.shape)
96 else: 102 if image.ndim == 1:
97 newimage = N.reshape(image,(self.__nx__,self.__ny__)) 103 newimage = N.reshape(image,(self.__nx__,self.__ny__))
98 newimage = self.transform_1_image(newimage) 104 newimage = self.transform_1_image(newimage)
99 return N.reshape(newimage,image.shape) 105 return N.reshape(newimage,image.shape)
106 assert False #should never go there
100 107
101 108
102 109
103 110
104 #test on NIST (you need pylearn and access to NIST to do that) 111 #test on NIST (you need pylearn and access to NIST to do that)