-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdal_read.py
724 lines (621 loc) · 22.4 KB
/
gdal_read.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
import numpy as np
import scipy
from scipy import signal
import scipy.ndimage as ndimage
import scipy.ndimage.filters as filters
import matplotlib.pyplot as plt
import sys
from osgeo import gdal
from osgeo.gdalconst import *
import numpy as np
import numpy.ma as ma
import os.path
import cv2
import inpaint
gdal.AllRegister()
gdal.UseExceptions()
#dtmin = filters.minimum_filter(dt,(1,100))
#fname = "J:\\DSM_DTM\\clip_terrassa.tif"
#ds = gdal.Open(fname,gdal.GA_ReadOnly)
#data=ds.GetRasterBand(1).ReadAsArray().astype(np.float)
#data[data==-9999]=np.nan
#OutDataType=gdal.GDT_Float32
#driver=gdal.GetDriverByName("Gtiff")
#ods=driver.Create(oname,data.shape[1],data.shape[0],2,OutDataType)
#ob=ods.GetRasterBand(1)
#ob.WriteArray(minf_x,0,0)
#dddd = np.copy(data)
#nans = np.array(np.where(dif>10)).T
#nonans = np.array(np.where(dif<=10)).T
#for p in nans:
# dddd[p[0],p[1]]=sum(dddd[q[0],q[1]]*np.exp(-(sum((p-q)**2))/2.) for q in nonans)
#x,y = np.indices(dddd.shape)
#interp = np.array(dddd)
#interp[np.isnan(interp)] = scipy.interpolate.griddata((x[~np.isnan(dddd)],y[~np.isnan(dddd)]),dddd[~np.isnan(dddd)],(x[np.isnan(dddd)],y[np.isnan(dddd)]))
def interp(dddd):
x,y = np.indices(dddd.shape)
interp = np.array(dddd)
interp[np.isnan(interp)] = scipy.interpolate.griddata((x[~np.isnan(dddd)],y[~np.isnan(dddd)]),dddd[~np.isnan(dddd)],(x[np.isnan(dddd)],y[np.isnan(dddd)]),method='cubic')
return interp
def dtm_v3(data,rank,shape,tr):
tdata = np.copy(data)
data=None
ranked = filters.rank_filter(tdata,rank,shape)
diff = tdata - ranked
tdata[diff>tr]=np.nan
lerp = filters.gaussian_filter(interp(tdata),1.2)
return lerp
def dtm_rank2(data,size,tr):
datat = np.copy(data)
data = None
rmin = int(size[0]*size[1]*0.05)
rmax = int(size[0]*size[1]*0.95)
# print "flter param:", rmin,rmax,size[0],size[1]
rX = filters.rank_filter(datat,rmin,(size[0],size[1]))
rY = filters.rank_filter(datat,rmin,(size[1],size[0]))
rX = filters.rank_filter(rX,rmax,(size[0],size[1]))
rY = filters.rank_filter(rY,rmax,(size[1],size[0]))
diff = datat-((rX+rY)/2.0)
out = datat[:,:]
mask = diff>tr
out[mask]=datat[mask]-diff[mask]
return out
def dtm_krauss_2015(dsm,ps,minf_r):
nx,ny = dsm.shape
scale = 20.0/ps
nnx = int(nx/scale+0.5)
nny = int(ny/scale+0.5)
scale_int = int(scale+0.5)
print scale
print scale_int
minf = filters.minimum_filter(dsm,minf_r)
dwn = cv2.resize(minf,(nny,nnx),interpolation = cv2.INTER_NEAREST)
# dwn = scipy.misc.imresize(minf,(nnx,nny),interp='cubic')
dwn_o = ndimage.grey_opening(dwn,5)
dwn_g = filters.gaussian_filter(dwn_o,2.5)
# return scipy.misc.imresize(dwn_g,(nx,ny),interp='cubic')
return cv2.resize(dwn_g,(ny,nx),interpolation = cv2.INTER_CUBIC)
def dtm_krauss_2015_rank(dsm,ps,minf_r,t_ps):
nx,ny = dsm.shape
scale = t_ps/ps
nnx = int(nx/scale+0.5)
nny = int(ny/scale+0.5)
scale_int = int(scale+0.5)
print scale
print scale_int
rmin = int(minf_r*minf_r*0.05)
# minf = filters.minimum_filter(dsm,minf_r)
minf = filters.rank_filter(dsm,rmin,(minf_r,minf_r))
dwn = cv2.resize(minf,(nny,nnx),interpolation = cv2.INTER_NEAREST)
# dwn = scipy.misc.imresize(minf,(nnx,nny),interp='cubic')
dwn_o = ndimage.grey_opening(dwn,5)
dwn_g = filters.gaussian_filter(dwn_o,2.5)
# return scipy.misc.imresize(dwn_g,(nx,ny),interp='cubic')
return cv2.resize(dwn_g,(ny,nx),interpolation = cv2.INTER_CUBIC)
def dtm_kraus_median(data,tr,lo,hi,ml=(4,4),mh=(40,40)):
med4 = filters.median_filter(data,ml)
med40 = filters.median_filter(data,mh)
diff = med4 - med40
med4 = None
med40 = None
mask = diff > tr
mask = ndimage.morphology.binary_erosion(mask,iterations=1)
out = np.copy(data)
out[mask] = out[mask] - diff[mask]
out = denoise(out,lo,hi,(3,3))
return out
def dtm_my_median(data,size,tr,lo,hi):
med4 = filters.median_filter(data,(4,4))
med40 = filters.median_filter(data,(size,size))
diff = med4 - med40
med4 = None
med40 = None
mask = diff > tr
mask = ndimage.morphology.binary_erosion(mask,iterations=1)
out = np.copy(data)
out[mask] = out[mask] - diff[mask]
out = denoise(out,lo,hi,(3,3))
return out
def gdal_write(oname,data,nodata=-9999):
OutDataType=gdal.GDT_Float32
driver=gdal.GetDriverByName("Gtiff")
nbands=1
ods=driver.Create(oname,data.shape[1],data.shape[0],nbands,OutDataType)
ob=ods.GetRasterBand(1)
ob.SetNoDataValue(nodata)
ob.WriteArray(data,0,0)
ob = None
ods= None
def gdal_read(fname,band):
ds = gdal.Open(fname,gdal.GA_ReadOnly)
return ds.GetRasterBand(band).ReadAsArray().astype(np.float)
def reg_mean(data,mask,min_size):
label_im, nb_labels = ndimage.label(mask)
sizes = ndimage.sum(mask,label_im,range(nb_labels+1))
mask_size = sizes < min_size
remove_pixel = mask_size[label_im]
label_im[remove_pixel] = 0
labels = np.unique(label_im)
label_im = np.searchsorted(labels, label_im)
labels = np.unique(label_im)
out = np.array(label_im,dtype=np.float)
for lab in labels:
if( lab==0 ): continue
try:
slice_x, slice_y = ndimage.find_objects(label_im==lab)[0]
except IndexError:
print ("Bad index: "%lab)
continue
# print lab
rois = data[slice_x, slice_y]
tmask = label_im==lab
roim = tmask[slice_x, slice_y]
roio = out[slice_x, slice_y]
mean = np.ma.mean(np.ma.array(rois,mask=~roim))
roio[roim] = mean
return out
def reg_median(data,mask,min_size):
label_im, nb_labels = ndimage.label(mask)
sizes = ndimage.sum(mask,label_im,range(nb_labels+1))
mask_size = sizes < min_size
remove_pixel = mask_size[label_im]
label_im[remove_pixel] = 0
labels = np.unique(label_im)
label_im = np.searchsorted(labels, label_im)
labels = np.unique(label_im)
out = np.array(label_im,dtype=np.float)
for lab in labels:
if( lab==0 ): continue
try:
slice_x, slice_y = ndimage.find_objects(label_im==lab)[0]
except IndexError:
print ("Bad index: "%lab)
continue
# print lab
rois = data[slice_x, slice_y]
tmask = label_im==lab
roim = tmask[slice_x, slice_y]
roio = out[slice_x, slice_y]
mean = np.ma.median(np.ma.array(rois,mask=~roim))
roio[roim] = mean
return out
def reg_median_cont(data,mask,min_size):
emask = ndimage.morphology.binary_erosion(mask,iterations=2)
cmask = mask[:]
cmask[emask==1]=0
label_im, nb_labels = ndimage.label(cmask)
sizes = ndimage.sum(cmask,label_im,range(nb_labels+1))
mask_size = sizes < min_size
remove_pixel = mask_size[label_im]
label_im[remove_pixel] = 0
labels = np.unique(label_im)
label_im = np.searchsorted(labels, label_im)
labels = np.unique(label_im)
out = np.array(label_im,dtype=np.float)
for lab in labels:
if( lab==0 ): continue
try:
slice_x, slice_y = ndimage.find_objects(label_im==lab)[0]
except IndexError:
print ("Bad index: "%lab)
continue
# print lab
rois = data[slice_x, slice_y]
tmask = label_im==lab
roim = tmask[slice_x, slice_y]
roio = out[slice_x, slice_y]
mean = np.ma.median(np.ma.array(rois,mask=~roim))
roio[roim] = mean
return out
def reg_median_cont1(data,label_im):
print "Start regional statistic calculation"
label_con = extract_contour(label_im)
out = np.array(label_im,dtype=np.float)
labels = np.unique(label_im)
for lab in labels:
if( lab==0 ): continue
try:
# print "n %d from %d"%(lab,len(labels))
slice_x, slice_y = ndimage.find_objects(label_im==lab)[0]
except IndexError:
print ("Bad index: "%lab)
continue
# print lab
roi_data = data[slice_x, slice_y]
tmask = label_im==lab
cmask = label_con==lab
roi_lab = tmask[slice_x, slice_y]
roi_con = cmask[slice_x, slice_y]
roi_out = out[slice_x, slice_y]
mean = np.ma.median(np.ma.array(roi_data,mask=~roi_con))
roi_out[roi_lab] = mean
print "Done. Total regions: %d"%lab
return out
def denoise(data,lo,hi,size):
medf = filters.median_filter(data,size)
diff = data - medf
hi_mask = diff > hi
lo_mask = diff < lo
out = np.copy(data)
out[hi_mask] = medf[hi_mask]
out[lo_mask] = medf[lo_mask]
return out
def geodesic_dilation(marker,mask):
""" Perform geodesic dilation based on algorithm
on p. 185 of Soille (2002).
"""
# Compute the size for the filter based on the shape
# of the marker array
mshape = len(marker.shape)
msize = (3,) * mshape
marker_dilation = ndimage.maximum_filter(marker, size=msize)
return np.where(mask <= marker_dilation, mask, marker_dilation)
def geodesic_dilation_v2(marker,mask):
marker_dilation = ndimage.morphology.grey_dilation(marker,(3,3))
return np.where(mask <= marker_dilation, mask, marker_dilation)
def recon_by_dilation(marker,mask,max_iter=10000):
""" Iterate over geodesic dilation operations
until dilation(j+1) = dilation(j).
Perform Geodesic dilation based on
algorithm on p. 190-191, P. Soille, 2002.
This can be referenced as:
R^delta_mask(marker)
"""
print "Start reconstruction"
dilation_i = marker
# mask_mean = np.nanmean(mask)
#mean_prev = np.nanmean(dilation_i)
for i in xrange(max_iter):
# mean_cur = np.nanmean(dilation_i)
# print "iteration %d %f %f"%(i,mean_cur,mask_mean)
sys.stdout.write("Iteration %d \r"%(i+1))
sys.stdout.flush()
dilation_i1 = geodesic_dilation(dilation_i,mask)
if i > 0 and np.sum(np.equal(dilation_i.ravel(),dilation_i1.ravel())) == dilation_i.size:
break
else:
dilation_i = dilation_i1.copy()
del dilation_i1
print "Done. Total iterations: %d"%i
return dilation_i
def recon_by_dilation_v2(marker,mask,max_iter=10000):
""" Iterate over geodesic dilation operations
until dilation(j+1) = dilation(j).
Perform Geodesic dilation based on
algorithm on p. 190-191, P. Soille, 2002.
This can be referenced as:
R^delta_mask(marker)
"""
print "Start reconstruction"
dilation_i = marker
# mask_mean = np.nanmean(mask)
# mean_prev = np.nanmean(dilation_i)
for i in xrange(max_iter):
# mean_cur = np.nanmean(dilation_i)
# print "iteration %d %f %f"%(i,mean_cur,mask_mean)
dilation_i1 = geodesic_dilation_v2(dilation_i,mask)
if i > 0 and np.sum(np.equal(dilation_i.ravel(),dilation_i1.ravel())) == dilation_i.size:
break
else:
dilation_i = dilation_i1.copy()
del dilation_i1
print "Done. Total iterations: %d"%i
return dilation_i
def calc_LRV(data,size):
min_area = ndimage.minimum_filter(data,size)
max_area = ndimage.maximum_filter(data,size)
return max_area-min_area
def std_convoluted(image, N):
im = np.array(image, dtype=float)
im2 = im**2
ones = np.ones(im.shape)
kernel = np.ones((2*N+1, 2*N+1))
s = scipy.signal.convolve2d(im, kernel, mode="same")
s2 = scipy.signal.convolve2d(im2, kernel, mode="same")
ns = scipy.signal.convolve2d(ones, kernel, mode="same")
return np.sqrt((s2 - s**2 / ns) / ns)
def fill(sdata, invalid=None):
"""
Replace the value of invalid 'data' cells (indicated by 'invalid')
by the value of the nearest valid data cell
Input:
data: numpy array of any dimension
invalid: a binary array of same shape as 'data'.
data value are replaced where invalid is True
If None (default), use: invalid = np.isnan(data)
Output:
Return a filled array.
"""
if invalid is None: invalid = np.isnan(sdata)
ind = ndimage.distance_transform_edt(invalid,
return_distances=False,
return_indices=True)
return sdata[tuple(ind)]
def mean_convoluted(image, m, n):
kernel=np.full((m,n),1.0/(m*n))
return scipy.signal.convolve2d(image, kernel, mode="same", boundary="symm")
def recon_by_median(data,flt,ml=(4,4),mh=(40,40)):
lo = -0.5
hi = 0.5
i = 1
out_dtm = data[:]
for tr in flt:
print "filtration threshold %.2f interation %d from %d"%(tr,i,len(flt))
out_dtm = dtm_kraus_median(out_dtm,tr,lo,hi,ml,mh)
if i > 1 and np.sum(np.equal(out_dtm.ravel(),prev.ravel())) == out_dtm.size:
break
else:
i+=1
prev = np.copy(out_dtm)
return out_dtm
def fill_by_median(data,big,small):
filled = denoise(data,-1000,1000,big)
return denoise(filled,-0.5,0.5,small)
def extract_labels(data,min_size=10,ext=0):
label_im, nb_labels = ndimage.label(data)
sizes = ndimage.sum(data,label_im,range(nb_labels+1))
if(ext!=0):
mask_size = sizes > min_size
else:
mask_size = sizes < min_size
remove_pixel = mask_size[label_im]
label_im[remove_pixel] = 0
labels = np.unique(label_im)
label_im = np.searchsorted(labels, label_im)
labels = np.unique(label_im)
return label_im
def extract_contour(mask, iteration=1):
erode = ndimage.morphology.binary_erosion(mask,iterations=iteration)
out = np.copy(mask)
out[erode==1]=0
return out
def filter_none_ground(data,lrv,h,tsd=3,t=2,nd=-9999):
ndmask = data==nd
mask = np.copy(data)
data = None
marker = mask[:]-h
marker[ndmask]=nd
imrec=recon_by_dilation(marker,mask)
print "Mask & Marker means: %f %f"%(np.mean(mask[mask!=nd]),np.mean(marker[mask!=nd]))
nDSM0 = mask[:]-imrec[:]
lab = extract_labels(nDSM0 > np.std(nDSM0)*tsd)
print "Total regions: %d"%len(lab)
reg = reg_median_cont1(lrv,lab)
print "Regions mean: %f"%np.mean(reg)
mask[reg>t]=nd
return mask, imrec#(imrec, nDSM0, lab, reg, mask)
def flt_minmax(data,size,it):
fdata = np.copy(data)
data = None
for i in range(0,it):
print "dtm rank filter iteration %d"%(i+1)
out = dtm_rank2(fdata,size,0.5)
fdata = out
return out
def recon_by_rank(data,size_hi,size_lo,tr=2,it=10):
ldata = np.copy(data)
hdata = np.copy(data)
out = np.copy(data)
data = None
for i in range(0,it):
# print "dtm filter iteration %d"%(i+1)
rank_lo = dtm_rank2(ldata,size_lo,0.5)
ldata = rank_lo
rank_hi = dtm_rank2(hdata,size_hi,0.5)
hdata = rank_hi
mask = (out[:]-rank_lo[:])>tr
out[mask]=rank_hi[mask]
return out
def flt_rank_recon(data,size_hi,size_lo,tr,it,int_it=10):
out = np.copy(data)
data = None
for i in range(0,it):
print "dtm filter iteration %d mean %f"%(i+1,np.mean(out[out>-9999]))
out = recon_by_rank(out,size_hi,size_lo,tr,int_it)
if i > 0 and np.sum(np.equal(out.ravel(),prev.ravel())) == out.size:
break
else:
i+=1
prev = np.copy(out)
return out
def set_range(data, out, nd=-9999):
out[out<np.min(data[data!=nd])]=nd
return out
def recon_by_wtophat(data,size,th=10,it=10):
out = np.copy(data)
data = None
for i in range(0,it):
print "dtm wh filter iteration %d mean %f"%(i+1,np.mean(out[out>-9999]))
wth = ndimage.morphology.white_tophat(out,size)
out[wth>th]-=wth[wth>th]
if i > 0 and np.sum(np.equal(out.ravel(),prev.ravel())) == out.size:
break
else:
i+=1
prev = np.copy(out)
return out
def tosha_dtm(data):
flt_hi = ([1,21],[1,17],[1,15],[1,11],[1,7])
flt_lo = ([1,11],[1,9], [1,7], [1,5], [1,3])
flt_tr = (10,8,4,1,0.5)
out = np.copy(data)
for i in range(0,len(flt_tr)):
print "tosha iteration %d"%(i+1)
out = flt_rank_recon(out,flt_hi[i],flt_lo[i],flt_tr[i],2000,int_it=7)
return set_range(data,out)
def tosha_dtm_0(data):
flt_hi = (1,40)
flt_lo = (1,9)
flt_tr = 1
return set_range(data,flt_rank_recon(data,flt_hi,flt_lo,flt_tr,20,int_it=3))
def recon_by_gauss(data,sigma,th=2,it=10000):
out = np.copy(data)
data = None
for i in range(0,it):
print "dtm gauss filter iteration %d mean %f"%(i+1,np.mean(out[out>-1000]))
gauss = filters.gaussian_filter(out,sigma)
diff = out[:]-gauss[:]
out[diff>th]=gauss[diff>th]
if i > 0 and np.sum(np.equal(out.ravel(),prev.ravel())) == out.size:
break
else:
i+=1
prev = np.copy(out)
return out
def fspecial_gauss(size, sigma):
"""Function to mimic the 'fspecial' gaussian MATLAB function
"""
x, y = np.mgrid[-size//2 + 1:size//2 + 1, -size//2 + 1:size//2 + 1]
g = np.exp(-((x**2 + y**2)/(2.0*sigma**2)))
return g/g.sum()
def recon_by_gauss1(data,ps,th=1,it=10000):
out = np.copy(data)
data = None
size = int(100/ps+0.5)
sigma = 25.0/ps
kernel = fspecial_gauss(size,sigma)
for i in range(0,it):
print "dtm gauss filter iteration %d mean %f"%(i+1,np.mean(out[out>-1000]))
gauss = scipy.signal.convolve2d(out,kernel,mode="same", boundary="symm")
diff = out[:]-gauss[:]
nmask = diff>th
out = fill(out,nmask)
if i > 0 and np.sum(np.equal(out.ravel(),prev.ravel())) == out.size:
break
else:
i+=1
prev = np.copy(out)
return out
def calc_col_perc(data,perc,low):
out = np.zeros((1,data.shape[1]))
for i in range(0,data.shape[1]):
tbuf = data[:,i]
try:
out[0,i] = np.percentile(tbuf[tbuf>low],perc,interpolation='linear')
except IndexError:
out[0,i] = -9999.0
return out[0,:]
def calc_col_mean(data,low):
out = np.zeros((1,data.shape[1]))
for i in range(0,data.shape[1]):
tbuf = data[:,i]
try:
out[0,i] = np.mean(tbuf[tbuf>low])
except IndexError:
out[0,i] = -9999.0
return out[0,:]
def window_stdev(arr, radius):
c1 = ndimage.filters.uniform_filter(arr, radius*2, mode='constant', origin=-radius)
c2 = ndimage.filters.uniform_filter(arr*arr, radius*2, mode='constant', origin=-radius)
# return ((c2 - c1*c1)**.5)[:-radius*2+1,:-radius*2+1]
return ((c2 - c1*c1)**.5)
def windowed_sum(a, win):
table = np.cumsum(np.cumsum(a, axis=0), axis=1)
win_sum = np.empty(tuple(np.subtract(a.shape, win-1)))
win_sum[0,0] = table[win-1, win-1]
win_sum[0, 1:] = table[win-1, win:] - table[win-1, :-win]
win_sum[1:, 0] = table[win:, win-1] - table[:-win, win-1]
win_sum[1:, 1:] = (table[win:, win:] + table[:-win, :-win] -
table[win:, :-win] - table[:-win, win:])
return win_sum
def windowed_var(a, win):
win_a = windowed_sum(a, win)
win_a2 = windowed_sum(a*a, win)
return (win_a2 - win_a * win_a / win/ win) / win / win
def mkgrid(data,method='cubic'):
x = np.arange(0,data.shape[1])
y = np.arange(0,data.shape[0])
data = np.ma.masked_invalid(data)
xx,yy = np.meshgrid(x,y)
x1 = xx[~data.mask]
y1 = yy[~data.mask]
new = data[~data.mask]
return scipy.interpolate.griddata((x1,y1),new.ravel(),(xx,yy),method=method)
def nan_helper(y):
"""Helper to handle indices and logical indices of NaNs.
Input:
- y, 1d numpy array with possible NaNs
Output:
- nans, logical indices of NaNs
- index, a function, with signature indices= index(logical_indices),
to convert logical indices of NaNs to 'equivalent' indices
Example:
>>> # linear interpolation of NaNs
>>> nans, x= nan_helper(y)
>>> y[nans]= np.interp(x(nans), x(~nans), y[~nans])
"""
return np.isnan(y), lambda z: z.nonzero()[0]
def remove_peaks(data,hi,low,fill=np.nan):
out = np.copy(data)
out[data>np.nanpercentile(data,hi)]=fill
out[data<np.nanpercentile(data,low)]=fill
return out
def remove_low(data,min_size):
out = np.copy(data)
j=1
for i in range(1,50,5):
threshold = np.percentile(out[out!=9999],i)
print "Iteration %d threshold=%f"%(j,threshold)
mask = extract_labels(out<threshold,min_size,ext=1)
out[mask>0]=9999
j+=1
return out
def remove_hi(data,min_size):
out = np.copy(data)
j=1
for i in range(100,75,-5):
threshold = np.percentile(out[out!=-9999],i)
print "Iteration %d threshold=%f"%(j,threshold)
mask = extract_labels(out>threshold,min_size,ext=1)
out[mask>0]=-9999
j+=1
return out
#meanshift
#cv2.cvtColor(data.astype(np.uint8),cv2.COLOR_GRAY2BGR)
#save no nan
#>>> xyz[xyz==-9999]=np.nan
#>>> xyz1 = xyz[~np.isnan(xyz).any(axis=1)]
#flt_hi = ([1,21],[1,17],[1,15],[1,11],[1,7])
#flt_lo = ([1,11],[1,9], [1,7], [1,5], [1,3])
#flt_tr = (10,8,4,1,0.5)
#step1 - recon by rank (1,21) (1,5)
#step2 - recon by white tophat, start from 21,17,15,9,5 - 10,8,6,4,2
#step3 = x3
#fill holes
#scipy.ndimage.morphology.binary_fill_holes
#data = interp(data)
#filled = inpaint.replace_nans(data,5,0.5,3,'localmean')
print "Done"
#flt
#dtm_med = recon_by_median(data,([15,10,5,5,2,2,1,1,0.5,0.5]))
#recim_hi = flt_rank_recon(data,(1,17),(1,5),1,30)
__END__
data_i1 = data[:]
data_i1[regmed50>2]=-9999
marker40 = data_i1[:]-40
marker40[regmed50>2]=-9999
imrec40=recon_by_dilation(marker40,data_i1)
dif40 = data_i1[:]-imrec40[:]
nDSM1 = extract_labels(dif40 > np.std(dif40)*3)
regmed40 = reg_median_cont1(lrv,nDSM1)
flt_tr = [0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25]#[5,5,2,2,1,1,0.5,0.5,0.5,0.5,0.5,0.5,0.2,0.2,0.2,0.2]#,0.8,0.7,0.6,0.5,0.4,0.3,0.2]
lo = -0.5
hi = 0.5
i = 1
denoised = denoise(data,lo,hi,(3,3))
out_dtm = np.copy(denoised)
for tr in flt_tr:
print "filtration threshold %.2f interation %d from %d"%(tr,i,len(flt_tr))
oname = "dtm_%.2f_%d.tif"%(tr,i)
out_dtm = dtm_kraus_median(out_dtm,tr,lo,hi)
# gdal_write(oname,out_dtm)
i+=1
lo = -2
hi = 2
#todo:
#1. generate nDTM0 - smoothed DSM, using iterative median filtering
#2. generate difference map, with threshold dif > std(dif)*t (t=0.9)
#3. generate local height variation - max(loc)-min(loc)
#4. generate contour of the difference map - by erosion with 2 iterations and subtraction
#5. generate separate polygons from diference map
#6. get median height variation and insert it in diference map polygons