-
Notifications
You must be signed in to change notification settings - Fork 11
/
ndsar.pyx
252 lines (189 loc) · 8.15 KB
/
ndsar.pyx
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
cdef extern from "src/ndsar_lib.h":
cdef void ndsar_nlm_cpp(float complex* arr, float complex* arr2, int* c_shp,
float gs, float gr, int Psiz, bint trick, bint flat,
int method)
cdef void ndsar_blf_cpp(float complex* arr, float complex* arr2, int* c_shp,
float gs, float gr, bint trick, bint flat, int method)
import numpy as np
def ndsarnlm(float complex[:,:,:,::1] varr, float gs=2.8, float gr=1.4,
int psiz=3 ,method = 'ai', bint trick=True, bint flat=False):
'''ndsarnlm(cov, gs=2.8, gr=1.4, psiz=3 , method='ai', trick=True, flat=False)
NDSAR-NLM: Nonlocal means filtering of N-dimensional SAR images.
Parameters
----------
cov: array, shape (naz, nrg, dim, dim), dtype: np.complex64
complex covariance image computed from SLC data.
naz and nrg are the number of pixels in azimuth and range.
dim is the matrix dimension.
gs: float
spatial scale parameter.
gr: float
radiometric scale parameter.
psiz: int
size of the patches to compute pixel similarities.
method: string
distance used to compute radiometric similarities.
'ai': Affine Invariant
'le': Log-Euclidean (much faster, recommended)
'ld': Log Diagonal (equivalent to 'ai' and 'le' but assumes
covariance is diagonal, uses only intensity information)
trick: boolean
underweights central pixel to enforce more filtering (recommended)
default: True
flat: boolean
uses uniform spatial weights instead of Gaussian ones.
default: False
Returns
-------
fcov: array, shape (naz, nrg, dim, dim)
filtered covariance image.
Notes
-----
Covariance matrices must have full rank for the 'ai' and 'le' distances
to be defined. Please ensure that the number of looks of your data is at
least of dim. Pre-summing is preferred to boxcar multilooking to avoid
introducing spatial correlation between the pixels.
'''
cdef int[::1] c_shp = np.asarray([varr.shape[0], varr.shape[1], varr.shape[2],
varr.shape[3]], dtype=np.int32)
arr2 = np.zeros_like(varr)
cdef float complex[:,:,:,::1] varr2 = arr2
if method == 'ai':
meth_int = 1
elif method == 'le':
meth_int = 2
elif method == 'ld':
meth_int = 3
else:
raise ValueError("Method does not exist")
ndsar_nlm_cpp(&varr[0,0,0,0], &varr2[0,0,0,0], &c_shp[0], gs, gr, psiz,
trick, flat, meth_int)
return arr2
def ndsarblf(float complex[:,:,:,::1] varr, float gs=2.8, float gr=1.4,
method = 'ai', bint trick=True, bint flat=False):
'''ndsarblf(cov, gs=2.8, gr=1.4, method = 'ai', trick=True, flat=False)
NDSAR-BLF: Bilateral filtering of N-dimensional SAR images.
Parameters
----------
cov: array, shape (naz, nrg, dim, dim), dtype: np.complex64
complex covariance image computed from SLC data.
naz and nrg are the number of pixels in azimuth and range.
dim is the matrix dimension.
gs: float
spatial scale parameter.
gr: float
radiometric scale parameter.
method: string
distance used to compute radiometric similarities.
'ai': Affine Invariant
'le': Log-Euclidean (much faster, recommended)
'ld': Log Diagonal (equivalent to 'ai' and 'le' but assumes
covariance is diagonal, uses only intensity information)
trick: boolean
underweights central pixel to enforce more filtering (recommended)
default: True
flat: boolean
uses uniform spatial weights instead of Gaussian ones.
default: False
Returns
-------
fcov: array, shape (naz, nrg, dim, dim)
filtered covariance image.
Notes
-----
Covariance matrices must have full rank for the 'ai' and 'le' distances
to be defined. Please ensure that the number of looks of your data is at
least of dim. Pre-summing is preferred to boxcar multilooking to avoid
introducing spatial correlation between the pixels.
The NDSAR-BLF filter is equivalent to NDSAR-NLM with a patch size of 1.
'''
cdef int[::1] c_shp = np.asarray([varr.shape[0], varr.shape[1], varr.shape[2],
varr.shape[3]], dtype=np.int32)
arr2 = np.zeros_like(varr)
cdef float complex[:,:,:,::1] varr2 = arr2
if method == 'ai':
meth_int = 1
elif method == 'le':
meth_int = 2
elif method == 'ld':
meth_int = 3
else:
raise ValueError("Method does not exist")
ndsar_blf_cpp(&varr[0,0,0,0], &varr2[0,0,0,0], &c_shp[0], gs, gr, trick,
flat, meth_int)
return arr2
def sarnlm(float[:,::1] varr, float gs=2.8, float gr=1.4, int psiz=3,
bint trick=True, bint flat=False):
'''sarnlm(img, gs=2.8, gr=1.4, psiz=3 , trick=True, flat=False)
SAR-NLM: Nonlocal means filtering of single channel SAR images.
Parameters
----------
img: array, shape (naz, nrg), dtype: np.float32
intensity image computed from SLC data.
naz and nrg are the number of pixels in azimuth and range.
gs: float
spatial scale parameter.
gr: float
radiometric scale parameter.
psiz: int
size of the patches to compute pixel similarities.
trick: boolean
underweights central pixel to enforce more filtering (recommended)
default: True
flat: boolean
uses uniform spatial weights instead of Gaussian ones.
default: False
Returns
-------
fimg: array, shape (naz, nrg)
filtered image.
Notes
-----
This function calls a c++ routine which is optimized for single
channel images.
For single channel images, 'ai', 'le' and 'ld' distances are equivalent
to the Euclidean distance on the log of intensity.
'''
cdef int[::1] c_shp = np.asarray([varr.shape[0], varr.shape[1], 1, 1],
dtype=np.int32)
cdef float complex[:,::1] varrclx = np.asarray(varr, dtype=np.complex64)
cdef float complex[:,::1] varr2 = np.zeros_like(varr, dtype=np.complex64)
ndsar_nlm_cpp(&varrclx[0,0], &varr2[0,0], &c_shp[0], gs, gr, psiz, trick,
flat, 0)
return np.ascontiguousarray(np.real(varr2))
def sarblf(float[:,::1] varr, float gs=2.8, float gr=1.4, bint trick=True,
bint flat=False):
'''sarblf(img, gs=2.8, gr=1.4, trick=True, flat=False)
SAR-BLF: Bilateral filtering of single channel SAR images.
Parameters
----------
img: array, shape (naz, nrg), dtype: np.float32
intensity image computed from SLC data.
naz and nrg are the number of pixels in azimuth and range.
gs: float
spatial scale parameter.
gr: float
radiometric scale parameter.
trick: boolean
underweights central pixel to enforce more filtering (recommended)
default: True
flat: boolean
uses uniform spatial weights instead of Gaussian ones.
default: False
Returns
-------
fimg: array, shape (naz, nrg)
filtered image.
Notes
-----
This function calls a c++ routine which is optimized for single
channel images.
For single channel images, 'ai', 'le' and 'ld' distances are equivalent
to the Euclidean distance on the log of intensity.
'''
cdef int[::1] c_shp = np.asarray([varr.shape[0], varr.shape[1], 1, 1],
dtype=np.int32)
cdef float complex[:,::1] varrclx = np.asarray(varr, dtype=np.complex64)
cdef float complex[:,::1] varr2 = np.zeros_like(varr, dtype=np.complex64)
ndsar_blf_cpp(&varrclx[0,0], &varr2[0,0], &c_shp[0], gs, gr, trick, flat, 0)
return np.ascontiguousarray(np.real(varr2))