1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 import scipy.ndimage as ndi
37 import numpy as np
38 import pyvision as pv
39 import cv
40
42 ''' TODO: deprecated please use meanStd.'''
43 print '''normalizeMeanStd is deprecated. Please call as normalize.meanStd'''
44 return meanStd(matrix)
45
47 ''' zero mean, one standard deviation '''
48 is_image = False
49 if isinstance(matrix,pv.Image):
50 matrix = matrix.asMatrix2D()
51 is_image = True
52
53 mask = matrix > max_val
54 matrix = max_val*mask + matrix*(~mask)
55
56 mask = matrix < min_val
57 matrix = min_val*mask + matrix*(~mask)
58
59 if is_image:
60 return pv.Image(matrix)
61 return matrix
62
64 ''' zero mean, one standard deviation '''
65 is_image = False
66 if isinstance(matrix,pv.Image):
67 matrix = matrix.asMatrix2D()
68 is_image = True
69
70 matrix = matrix - matrix.mean()
71 matrix = (1.0/(matrix.std()+0.000001)) * matrix
72 if is_image:
73 return pv.Image(matrix)
74 return matrix
75
77 ''' zero mean, unit length '''
78 is_image = False
79 if isinstance(matrix,pv.Image):
80 matrix = matrix.asMatrix2D()
81 is_image = True
82 matrix = matrix - matrix.mean()
83 length = np.sqrt( (matrix*matrix).sum() )
84 if length > 0.0:
85 matrix = (1.0/length) * matrix
86
87 if is_image:
88 return pv.Image(matrix)
89 return matrix
90
92 ''' unit length '''
93 is_image = False
94 if isinstance(matrix,pv.Image):
95 matrix = matrix.asMatrix2D()
96 is_image = True
97 length = np.sqrt( (matrix*matrix).sum() )
98 if length < 0.00001:
99 length = 0.00001
100 matrix = (1.0/length) * matrix
101 if is_image:
102 return pv.Image(matrix)
103 return matrix
104
106 '''
107 Compute a self quotient image.
108
109 Based on work by Wang et.al. "Self Quotient Image for Face Recognition" ICIP 2004
110 '''
111 is_image = False
112 if isinstance(matrix,pv.Image):
113 matrix = matrix.asMatrix2D()
114 is_image = True
115
116 assert matrix.min() >= 0
117 matrix = matrix + 0.01*matrix.max()
118 denom = ndi.gaussian_filter(matrix,sigma)
119
120
121 matrix = matrix/(denom+0.000001)
122
123 if is_image:
124 return pv.Image(matrix)
125 return matrix
126
127
129 '''
130 Smooth an image using a Gaussian filter.
131 '''
132 cvim = cv.CreateImage(im.size,cv.IPL_DEPTH_8U,im.channels)
133
134 cv.Smooth(im.asOpenCV(),cvim,cv.CV_GAUSSIAN,0,0,sigma)
135 return pv.Image(cvim)
136
138 '''
139 This function computes a high and low pass filter. This can be used
140 to reduce the effect of lighting.
141
142 A low pass image is first computed by convolving the image with a
143 Gausian filter of radius sigma. Second, a high pass image is computed
144 by subtracting the low pass image from the original image. This means that
145 the original image can be reconstructed by adding a low pass image and a high
146 pass image.
147
148 @returns: high_pass_image
149 '''
150 is_image = False
151 if isinstance(matrix,pv.Image):
152 matrix = matrix.asMatrix2D()
153 is_image = True
154
155 matrix = matrix - ndi.gaussian_filter(matrix,sigma)
156
157 if is_image:
158 return pv.Image(matrix)
159 return matrix
160
162 '''
163 This function computes a low pass filter. It basically smoothes the image
164 by convolving with a Gaussian. This is often used to reduce the effect of
165 noise in images or to reduce the effect of small registration errors.
166
167 @returns: an pv.Image set from a numpy matrix if input was an image or a numpy
168 matrix otherwize.
169 '''
170 is_image = False
171 if isinstance(matrix,pv.Image):
172 matrix = matrix.asMatrix2D()
173 is_image = True
174
175 matrix = ndi.gaussian_filter(matrix,sigma)
176
177 if is_image:
178 return pv.Image(matrix)
179 return matrix
180
181
183 '''
184 This function computes a high and low pass filter. This can be used
185 to reduce the effect of lighting.
186
187 A low pass image is first computed by convolving the image with a
188 Gausian filter of radius sigma. Second, a high pass image is computed
189 by subtracting the low pass image from the original image. This means that
190 the original image can be reconstructed by adding a low pass image and a high
191 pass image.
192
193 @returns: high_pass_image
194 '''
195 assert sigma_low > sigma_high
196 is_image = False
197 if isinstance(matrix,pv.Image):
198 matrix = matrix.asMatrix2D()
199 is_image = True
200
201 matrix = ndi.gaussian_filter(matrix,sigma_high) - ndi.gaussian_filter(matrix,sigma_low)
202
203 if is_image:
204 return pv.Image(matrix)
205 return matrix
206