/
postprocess.py
225 lines (174 loc) · 6.66 KB
/
postprocess.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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
""" Postprocess Continuous Degradation Detection (CDD) results.
Usage: postprocess_cdd.py [options] (sieve | fz | kmeans) <input> <output>
--seg=<SEG_SIZE> Minimum segment size
--sigma=<SIGMA> Sigma value for FZ test
--scale=<SCALE> Scale value for FZ test
--convdate=<CONVDATE> Convert date to year
"""
import cv2, sys
import pymeanshift as pms
import numpy as np
import gdal
import scipy.stats
from docopt import docopt
from skimage.color import rgb2gray
#from skimage.filters import sobel
from skimage.segmentation import felzenszwalb, slic, quickshift#, watershed
from skimage.segmentation import mark_boundaries
from skimage.util import img_as_float
import time
def convert_date(array):
array[0,:,:][array[0,:,:] > 0] += 1970
array[0,:,:] = array[0,:,:].astype(np.int)
return array
def save_raster(array, path, dst_filename, convdate):
#Convert date from years since 1970 to year
if convdate:
array = convert_date(array)
example = gdal.Open(path)
x_pixels = array.shape[2] # number of pixels in x
y_pixels = array.shape[1] # number of pixels in y
bands = array.shape[0]
driver = gdal.GetDriverByName('GTiff')
dataset = driver.Create(dst_filename,x_pixels, y_pixels, bands ,gdal.GDT_Float64)
geotrans=example.GetGeoTransform() #get GeoTranform from existed 'data0'
proj=example.GetProjection() #you can get from a exsited tif or import
dataset.SetGeoTransform(geotrans)
dataset.SetProjection(proj)
for b in range(bands):
dataset.GetRasterBand(b+1).WriteArray(array[b,:,:])
dataset.FlushCache()
#dataset=None
def save_raster_memory(array, path):
example = gdal.Open(path)
x_pixels = array.shape[1] # number of pixels in x
y_pixels = array.shape[0] # number of pixels in y
driver = gdal.GetDriverByName('MEM')
dataset = driver.Create('',x_pixels, y_pixels, 1,gdal.GDT_Int32) #TODO: bands
dataset.GetRasterBand(1).WriteArray(array[:,:])
# follow code is adding GeoTranform and Projection
geotrans=example.GetGeoTransform() #get GeoTranform from existed 'data0'
proj=example.GetProjection() #you can get from a exsited tif or import
dataset.SetGeoTransform(geotrans)
dataset.SetProjection(proj)
return dataset
def segment_fz(image, output, scale, sigma, minseg, convdate):
original_im = gdal.Open(image)
#Assign median values based on Felzenzwalb segmentation algorithm
three_d_image = original_im.ReadAsArray().astype(np.uint8)
three_d_image = three_d_image.swapaxes(0, 2)
three_d_image = three_d_image.swapaxes(0, 1)
img = three_d_image[:,:,(0,1,3)]
full_image = original_im.ReadAsArray()
full_image = full_image.swapaxes(0, 2)
full_image = full_image.swapaxes(0, 1)
segments_fz = felzenszwalb(img, scale=scale, sigma=sigma, min_size=minseg)
median_image = np.zeros_like(full_image).astype(np.float32)
for band in range(4):
for seg in np.unique(segments_fz):
values = full_image[:,:,band][segments_fz == seg]
if band < 2:
if np.median(values) > 0:
med = np.median(values[values>0])
else:
med = 0
else:
values[np.isnan(values)] = 0
# values *= 1000
if np.median(values) > 0:
med = np.median(values[values>0])
else:
med = 0
median_image[:,:,band][segments_fz == seg] = med
#Reshape
s1, s2, s3 = median_image.shape
median_image = median_image.swapaxes(1, 0)
median_image = median_image.swapaxes(2, 0)
save_raster(median_image, image, output, convdate)
sys.exit()
def segment_km(image, output):
original_im = gdal.Open(image)
#Assign median values based on Felzenzwalb segmentation algorithm
three_d_image = original_im.ReadAsArray().astype(np.uint8)
three_d_image = three_d_image.swapaxes(0, 2)
three_d_image = three_d_image.swapaxes(0, 1)
img = three_d_image
full_image = original_im.ReadAsArray()
full_image = full_image.swapaxes(0, 2)
full_image = full_image.swapaxes(0, 1)
segments_slic = slic(img, n_segments=250, compactness=10, sigma=1)
median_image = np.zeros_like(img).astype(np.float32)
for band in range(3):
for seg in np.unique(segments_slic):
values = full_image[:,:,band][segments_slic == seg]
# mode = scipy.stats.mode(values.astype(int)).mode[0]
# if mode > 0:
med = np.median(values[values>0])
# else:
# med = 0
median_image[:,:,band][segments_slic == seg] = med
save_raster(median_image, image, output)
sys.exit()
def sieve(image, dst_filename, convdate):
# 1. Remove all single pixels
#First create a band in memory that's that's just 1s and 0s
src_ds = gdal.Open( image, gdal.GA_ReadOnly )
srcband = src_ds.GetRasterBand(1)
srcarray = srcband.ReadAsArray()
srcarray[srcarray > 0] = 1
mem_rast = save_raster_memory(srcarray, image)
mem_band = mem_rast.GetRasterBand(1)
#Now the code behind gdal_sieve.py
maskband = None
drv = gdal.GetDriverByName('GTiff')
dst_ds = drv.Create( dst_filename,src_ds.RasterXSize, src_ds.RasterYSize,1,
srcband.DataType )
wkt = src_ds.GetProjection()
if wkt != '':
dst_ds.SetProjection( wkt )
dst_ds.SetGeoTransform( src_ds.GetGeoTransform() )
dstband = dst_ds.GetRasterBand(1)
# Parameters
prog_func = None
threshold = 4
connectedness = 8
result = gdal.SieveFilter(mem_band, maskband, dstband,
threshold, connectedness,
callback = prog_func )
sieved = dstband.ReadAsArray()
sieved[sieved < 0] = 0
src_new = gdal.Open(image)
out_img = src_new.ReadAsArray().astype(np.float)
out_img[np.isnan(out_img)] = 0
for b in range(out_img.shape[0]):
out_img[b,:,:][sieved == 0] = 0
dst_full = dst_filename.split('.')[0] + '_full.tif'
save_raster(out_img, image, dst_full, convdate)
sys.exit()
if __name__ == '__main__':
args = docopt(__doc__, version='0.6.2')
image = args['<input>']
output = args['<output>']
if args['--sigma']:
sigma = float(args['--sigma'])
else:
sigma = .8
if args['--scale']:
scale = float(args['--scale'])
else:
scale = 20
if args['--seg']:
minseg = args['--seg']
else:
minseg = 4
convdate = False
if args['--convdate']:
convdate = True
if args['sieve']:
sieve(image, output, convdate)
elif args['fz']:
segment_fz(image, output, scale, sigma, minseg, convdate)
elif args['kmeans']:
segment_km(image, output)