File size: 9,143 Bytes
ecf08bc |
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 |
import cv2
import numpy as np
import os
import plotly.express as px
import plotly.figure_factory as ff
import datetime
import plotly.io as pio
import plotly.graph_objs as go
pio.kaleido.scope.mathjax = None
import math
# import pylab
from matplotlib.colors import LinearSegmentedColormap
from PIL import ImageColor
def distribute_glacier(list_of_samples):
list_of_glaciers = {}
for glacier in ['JAC']:
#for glacier in [ 'COL', 'Mapple', 'Crane', 'Jorum','DBE','SI', 'JAC']:
list_of_glaciers[glacier] = [sample for sample in list_of_samples if glacier in sample]
return list_of_glaciers
def create_dict(list_of_samples):
list_dict = []
for sample in list_of_samples:
sample_split = sample.split('_')
finish_date = datetime.datetime.fromisoformat(sample_split[1]) + datetime.timedelta(days=50)
sample_dict = {
'Glacier': sample_split[0],
'Start': sample_split[1],
'Finish': str(finish_date),
'Satellite:': sample_split[2]
}
list_dict.append(sample_dict)
return list_dict
if __name__ == '__main__':
train_dir = '/home/ho11laqe/PycharmProjects/data_raw/fronts/train/'
test_dir = '/home/ho11laqe/PycharmProjects/data_raw/fronts/test/'
list_of_train_samples = os.listdir(train_dir)
list_of_test_samples = os.listdir(test_dir)
list_of_samples = list_of_train_samples + list_of_test_samples
list_of_glaciers = distribute_glacier(list_of_samples)
list_dict = create_dict(list_of_samples)
# define color map
colormap = px.colors.sequential.Reds[-1::-1]
for glacier in list_of_glaciers:
print(glacier)
list_of_glaciers[glacier].sort()
if glacier in ['COL', 'Mapple']:
data_directory = test_dir
image_directory = '/home/ho11laqe/PycharmProjects/data_raw/sar_images/test/'
else:
data_directory = train_dir
image_directory = '/home/ho11laqe/PycharmProjects/data_raw/sar_images/train/'
# define SAR blackground image
if glacier == 'COL':
canvas = cv2.imread(image_directory + 'COL_2011-11-13_TDX_7_1_092.png')
shape = canvas.shape
elif glacier == 'JAC':
canvas = cv2.imread(image_directory + 'JAC_2009-06-21_TSX_6_1_005.png')
shape = canvas.shape
elif glacier == 'Jorum':
canvas = cv2.imread(image_directory + 'Jorum_2011-09-04_TSX_7_4_034.png')
shape = canvas.shape
elif glacier == 'Mapple':
canvas = cv2.imread(image_directory + 'Mapple_2008-10-13_TSX_7_2_034.png')
shape = canvas.shape
elif glacier == 'SI':
canvas = cv2.imread(image_directory + 'SI_2013-08-14_TSX_7_1_125.png')
elif glacier == 'Crane':
canvas = cv2.imread(image_directory + 'Crane_2008-10-13_TSX_7_3_034.png')
elif glacier == 'DBE':
canvas = cv2.imread(image_directory + 'DBE_2008-03-30_TSX_7_3_049.png')
else:
print('No image for background')
exit()
number_images = len(list_of_glaciers[glacier])
kernel = np.ones((3, 3), np.uint8)
# iterate over all fronts of one glacier
for i, image_name in enumerate(list_of_glaciers[glacier]):
front = cv2.imread(data_directory + image_name)
# if front label has to be resized to fit background image
# the front is not dilated.
if front.shape != canvas.shape:
front = cv2.resize(front, (shape[1], shape[0]))
else:
front = cv2.dilate(front, kernel)
# color interpolation based on position in dataset
# TODO based on actual date
index = (1 - i / number_images) * (len(colormap) - 1)
up = math.ceil(index)
down = up - 1
color_up = np.array(ImageColor.getcolor(colormap[up], 'RGB'))
color_down = np.array(ImageColor.getcolor(colormap[down], 'RGB'))
dif = up - down
color = color_up * (1 - dif) + color_down * dif
# draw front on canvas
non_zeros = np.nonzero(front)
canvas[non_zeros[:2]] = np.uint([color for _ in non_zeros[0]])
#scale reference for fontsize
ref_x = 15000 / 7
if glacier == 'COL':
image = canvas[750:, 200:2800]
new_shape = image.shape
res = 7
scale = new_shape[1] / ref_x
fig = px.imshow(image, height=new_shape[0]- int(80 * scale), width=new_shape[1])
legend = dict(thickness=int(50 * scale), tickvals=[-4.4, 4.4],
ticktext=['2011<br>(+0.8°C)', '2020<br>(+1.2°C)'],
outlinewidth=0)
elif glacier == 'Mapple':
image = canvas
new_shape = image.shape
res = 7
scale = new_shape[1] / ref_x
fig = px.imshow(image, height=new_shape[0] - int(150 * scale), width=new_shape[1])
legend = dict(thickness=int(50 * scale), tickvals=[-4.8, 4.8], ticktext=['2006', '2020 '],
outlinewidth=0)
elif glacier == 'Crane':
image = canvas[:2500,:]
new_shape = image.shape
res = 7
scale = new_shape[1] / ref_x
fig = px.imshow(image, height=new_shape[0] - int(150 * scale), width=new_shape[1])
legend = dict(thickness=int(50 * scale), tickvals=[-4.8, 4.8], ticktext=['2002', '2014'],
outlinewidth=0)
elif glacier == 'Jorum':
image = canvas#[200:1600, 1500:]
new_shape = image.shape
res = 7
scale = new_shape[1] / ref_x
fig = px.imshow(image, height=new_shape[0] - int(240 * scale), width=new_shape[1])
legend = dict(thickness=int(50 * scale), tickvals=[-4.8, 4.8], ticktext=['2003', '2020'],
outlinewidth=0)
elif glacier == 'DBE':
image = canvas[700:, 750:]
new_shape = image.shape
res = 7
scale = new_shape[1] / ref_x
fig = px.imshow(image, height=new_shape[0] - int(150 * scale), width=new_shape[1])
legend = dict(thickness=int(50 * scale), tickvals=[-4.7, 4.7], ticktext=['1995', '2014'],
outlinewidth=0)
elif glacier == 'SI':
image = canvas
new_shape = image.shape
res = 7
scale = new_shape[0] / ref_x
fig = px.imshow(image, height=new_shape[0] - int(240 * scale), width=new_shape[1])
legend = dict(thickness=int(50 * scale), tickvals=[-4.8, 4.8], ticktext=['1995', '2014'],
outlinewidth=0)
elif glacier == 'JAC':
image = canvas[:, :]
new_shape = image.shape
res = 6
scale = new_shape[1] / ref_x
fig = px.imshow(image, height=new_shape[0] - int(340 * scale), width=new_shape[1])
legend = dict(thickness=int(50 * scale), tickvals=[-4.6, 4.7],
ticktext=['2009<br>(+0.7°C)', '2015<br>(+0.9°C)'],
outlinewidth=0)
else:
fig = px.imshow(canvas)
res = 7
scale = 1
colorbar_trace = go.Scatter(x=[None],
y=[None],
mode='markers',
marker=dict(
colorscale=colormap[::-1],
showscale=True,
cmin=-5,
cmax=5,
colorbar=legend
),
hoverinfo='none'
)
fig.update_layout(yaxis=dict(tickmode='array',
tickvals=[0, 5000 / res, 10000 / res, 15000 / res, 20000 / res, 25000 / res],
ticktext=[0, 5, 10, 15, 20, 25],
title='km'))
fig.update_layout(xaxis=dict(tickmode='array',
tickvals=[0, 5000 / res, 10000 / res, 15000 / res, 20000 / res, 25000 / res],
ticktext=[0, 5, 10, 15, 20, 25],
title='km'))
fig.update_xaxes(tickfont=dict(size=int(40 * scale)))
fig.update_yaxes(tickfont=dict(size=int(40 * scale)))
fig.update_layout(font=dict(size=int(60 * scale), family="Computer Modern"))
fig.update_coloraxes(colorbar_x=0)
fig['layout']['xaxis']['title']['font']['size'] = int(60 * scale)
fig['layout']['yaxis']['title']['font']['size'] = int(60 * scale)
fig['layout']['showlegend'] = False
fig.add_trace(colorbar_trace)
fig.write_image('output/' + glacier + "_front_change.pdf", format='pdf')
# fig.show() |