forked from nkarasiak/HistoricalMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
267 lines (198 loc) · 7.37 KB
/
functions.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import scipy as sp
def max_filter(im,size=3):
"""The function performs a local max filter on a flat image. Border's
pixels are not processed.
Args:
im: the image to process
size: the size in pixels of the local square window. Default value is 3.
Returns:
out: the filtered image
"""
## Get the size of the image
[nl,nc,d] = im.shape
## Get the size of the moving window
s = (size-1)/2
## Initialization of the output
out = sp.zeros((nl,nc,d),dtype=im.dtype.name)
## Apply the max filter
for i in range(s,nl-s): # Shift the origin to remove border effect
for j in range(s,nc-s):
for k in range(d):
temp = im[i-s:i+1+s,j-s:j+s+1,k]
out[i,j,k] = temp.max()
return out
def max_filter_bord(im,size=3):
"""The function performs a local max filter on a flat image. Border's
pixels are processed.
Args:
im: the image to process
size: the size in pixels of the local square window. Default value is 3.
Returns:
out: the filtered image
"""
## Get the size of the image
[nl,nc,d] = im.shape
## Get the size of the moving window
s = (size-1)/2
## Initialization of the output
out = sp.empty((nl,nc,d),dtype=im.dtype.name)
temp = sp.empty((nl+2*s,nc+2*s,d),dtype=im.dtype.name) # A temporary file is created
temp[0:s,:,:]=sp.NaN
temp[:,0:s,:]=sp.NaN
temp[-s:,:,:]=sp.NaN
temp[:,-s:,:]=sp.NaN
temp[s:s+nl,s:nc,:]=im
## Apply the max filter
for i in range(s,nl+s): # Shift the origin to remove border effect
for j in range(s,nc+s):
for k in range(d):
out[i-s,j-s,k] = sp.nanmax(temp[i-s:i+1+s,j-s:j+s+1,k])
return out.astype(im.dtype.name)
def min_filter(im,size=3):
"""The function performs a local min filter on a flat image. Border's
pixels are not processed.
Args:
im: the image to process
size: the size in pixels of the local square window. Default value is 3.
Returns:
out: the filtered image
"""
## Get the size of the image
[nl,nc,d] = im.shape
## Get the size of the moving window
s = (size-1)/2
## Initialization of the output
out = sp.zeros((nl,nc,d),dtype=im.dtype.name)
## Apply the min filter
for i in range(s,nl-s): # Shift the origin to remove border effect
for j in range(s,nc-s):
for k in range(d):
temp = im[i-s:i+1+s,j-s:j+s+1,k]
out[i,j,k] = temp.min()
return out
def min_filter_bord(im,size=3):
"""The function performs a local min filter on a flat image. Border's
pixels are processed.
Args:
im: the image to process
size: the size in pixels of the local square window. Default value is 3.
Returns:
out: the filtered image
"""
## Get the size of the image
[nl,nc,d] = im.shape
## Get the size of the moving window
s = (size-1)/2
## Initialization of the output
out = sp.empty((nl,nc,d))
temp = sp.empty((nl+2*s,nc+2*s,d)) # A temporary file is created
temp[0:s,:]=sp.NaN
temp[:,0:s]=sp.NaN
temp[-s:,:]=sp.NaN
temp[:,-s:]=sp.NaN
temp[s:s+nl,s:nc+s]=im
## Apply the max filter
for i in range(s,nl+s): # Shift the origin to remove border effect
for j in range(s,nc+s):
for k in range(d):
out[i-s,j-s,k] = sp.nanmin(temp[i-s:i+1+s,j-s:j+s+1,k])
return out.astype(im.dtype.name)
def mean_filter(im,size=3):
"""The function performs a local mean filter on a flat image. Border's
pixels are not processed.
Args:
im: the image to process
size: the size in pixels of the local square window. Default value is 3.
Returns:
out: the filtered image
"""
## Get the size of the image
[nl,nc] = im.shape
## Get the size of the moving window
s = (size-1)/2
## Initialization of the output
out = sp.zeros((nl,nc))
## Apply the max filter
for i in range(s,nl-s): # Shift the origin to remove border effect
for j in range(s,nc-s):
temp = im[i-s:i+1+s,j-s:j+s+1]
out[i,j] = sp.mean(temp)
return out
def median_filter(im,size=3):
"""The function performs a local median filter on a flat image. Border's
pixels are not processed.
Args:
im: the image to process
size: the size in pixels of the local square window. Default value is 3.
Returns:
out: the filtered image
"""
## Get the size of the image
[nl,nc,d] = im.shape
## Get the size of the moving window
s = (size-1)/2
## Initialization of the output
out = sp.zeros((nl,nc,d),dtype=im.dtype.name)
## Apply the max filter
for i in range(s,nl-s): # Shift the origin to remove border effect
for j in range(s,nc-s):
for k in range(d):
temp = im[i-s:i+1+s,j-s:j+s+1,k]
out[i,j,k] = sp.median(temp)
return out
def median_filter_bord(im,size=3):
"""The function performs a local median filter on a flat
image. Border's pixels are processed.
Args:
im: the image to process
size: the size in pixels of the local square window. Default value is 3.
Returns:
out: the filtered image
"""
## Get the size of the image
[nl,nc,d] = im.shape
## Get the size of the moving window
s = (size-1)/2
## Initialization of the output
out = sp.empty((nl,nc,d))
temp = sp.empty((nl+2*s,nc+2*s,d)) # A temporary file is created
temp[0:s,:]=sp.NaN
temp[:,0:s]=sp.NaN
temp[-s:,:]=sp.NaN
temp[:,-s:]=sp.NaN
temp[s:s+nl,s:nc+s]=im
## Apply the max filter
for i in range(s,nl+s): # Shift the origin to remove border effect
for j in range(s,nc+s):
for k in range(d):
window = temp[i-s:i+1+s,j-s:j+s+1,k]
out[i-s,j-s,k] = sp.median(window[sp.isfinite(window)])
return out.astype(im.dtype.name)
def compute_ndvi(im,r=0,ir=1,NODATA=-10000):
"""The function computes the NDVI of a multivalued image. It checks if
there is NODATA value or division per zeros.
Args:
im: the image to process
r: the number of the band that corresponds to the red band.
ir: the number of the band that corresponds to the infra-red band.
NODATA: the value of the NODATA
Returns:
ndvi = the ndvi of the image
"""
## Get the size fo the image
[nl,nc,nb]=im.shape
## Be sure that we can do 'floating operation'
imf = im.astype(sp.float64)
ndvi = sp.empty((nl,nc))
if nb < 2:
print "Two bands are needed to compute the NDVI"
return None
else:
den = (imf[:,:,ir-1]+imf[:,:,r-1]) # Pre compute the denominator
t = sp.where((den>0) & (imf[:,:,1]!= NODATA))
ndvi[t] = (imf[t[0],t[1],ir-1]-imf[t[0],t[1],r-1])/den[t] # compute the ndvi on the safe samples
if len(t[0]) < nl*nc:
t = sp.where((den==0) | (imf[:,:,1]== NODATA)) # check for problematic pixels
ndvi[t] = NODATA
imf = []
return ndvi