-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCloudmasking.py
More file actions
56 lines (39 loc) · 1.53 KB
/
Cloudmasking.py
File metadata and controls
56 lines (39 loc) · 1.53 KB
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
from sklearn.cluster import MiniBatchKMeans
import skimage.io as io
import numpy as np
import os, sys
import json
from skimage.morphology import disk
from skimage.morphology import dilation
def cloudmasking(self,):
band = os.path.join(self.workdir, band + ".tif")
img_ds = io.imread(band)
img = np.array(img_ds, dtype='uint16')
row = img.shape[0]
col = img.shape[1]
ras_shape = (row * col, 1)
img_as_array = img[:, :].reshape(ras_shape)
cluster = MiniBatchKMeans(n_clusters=2,
tol=0.00001,
n_init=10,
init='random',
max_no_improvement=100,
reassignment_ratio=0.05)
cluster_labels = cluster.fit_predict(img_as_array)
cluster_center = cluster.cluster_centers_
new_shape = (row, col)
img_clusters = cluster_labels.reshape(new_shape)
centroids_num = len(cluster_center)
liste = []
for i in range(0, centroids_num):
means = np.mean(cluster_center[i])
liste.append(means)
max = np.max(liste)
val = liste.index(max)
if max > 1800:
mask = np.where(img_clusters == val, 1, np.nan)
img_mask = np.array(mask, dtype='uint8').reshape((row, col))
kernel = disk(5)
img_buffer = dilation(img_mask, kernel)
cloudmask = os.path.join(self.workdir, "cloudmask.tif").format(sys.platform, os.sep)
io.imsave(cloudmask, img_buffer)