-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_composite_image.py
executable file
·374 lines (273 loc) · 13.4 KB
/
generate_composite_image.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 4 22:44:19 2022
@author: karan
"""
'''
Get the Composite Image using minimum Reflectance value.
'''
# importing necessary libraries
import h5py
import numpy as np
import matplotlib.pyplot as plt
import os
import glob
import pandas as pd
INPUT_DATA_DIR = "/home/karan/Jan2021_data_merged"
SAVE_COMPOSITE_IMG_AT = "/media/karan/Studyzz/CHANGA/SEM 8/00Jan_Final_Output/composite_images"
def get_composite_image_data(time, span):
all_data = []
for file_path in sorted(glob.glob(INPUT_DATA_DIR + "/**/*_" + time + "_*.h5", recursive=True)):
# Load the .h5 file
file = h5py.File(name=file_path, mode='r')
# get the file_name from the file_path.
file_name = os.path.basename(file_path).split(".")[0]
########################################################################
'''Generate image for IMG_VIS channel '''
vis_image = np.array(file["IMG_VIS"])
vis_image = vis_image[0, :, :]
# '''Convert VIS Image into ALBEDO domain.'''
vis_look_up = np.array(file["IMG_VIS_ALBEDO"])
vis_image = vis_look_up[vis_image]
vis_image = np.uint16(vis_image)
# Cropping to keep only the Indian region.
vis_image = vis_image[300:1192+1, 623:1391+1]
########################################################################
'''
Read `Sun_Elevation` data. Derive the Solar Zenith angle.
Perform Reflectance Normalization w.r.t Solar Zenith Angle.
'''
# read the Sun Elevation data for the database (.h5 file).
sun_elevation = np.array(file["Sun_Elevation"])
sun_elevation = sun_elevation[0, :, :]
# initialize the solar_zenith_angle matrix
solar_zenith_angle = np.zeros(sun_elevation.shape)
# calculate Solar Zenith angle from the Sun_Elevation data
solar_zenith_angle = 90 - (sun_elevation * 0.01)
# Cropping to keep only the Indian region.
solar_zenith_cropped = solar_zenith_angle[300:1192+1, 623:1391+1]
# initialize the new VIS data containing the reflectance normalized data.
vis_image = vis_image / (np.cos(solar_zenith_cropped * (np.pi / 180)))
############################################################################
all_data.append(vis_image)
print("[INFO] Completed:", file_name)
# Convert list to array: Final shape becomes: (None, 893, 769)
all_data = np.array(all_data)
# Find the resultant minimum matrix
vis_composite_image = np.min(all_data, axis=0)
############################################################################
# Creating plot of VIS Composite Image
fig = plt.figure(figsize=(7, 7), dpi=150)
plt.subplot(1, 1, 1)
plt.imshow(X=vis_composite_image, cmap='gray')
# plt.plot(250, 450, color='red', marker='o', markersize=6)
plt.colorbar(label="Reflectance (%)")
plt.title(label=time + "Composite Image:" + span)
# plt.xticks([]); plt.yticks([]);
# plt.savefig(SAVE_COMPOSITE_IMG_AT + "/" + span + "_composite_image_" + time + ".png", bbox_inches='tight')
# plt.show()
plt.close()
############################################################################
# vis_composite_image[vis_composite_image > 25] = 20
############################################################################
# Creating plot of VIS Composite Image
fig = plt.figure(figsize=(7, 7), dpi=150)
plt.subplot(1, 1, 1)
plt.imshow(X=vis_composite_image, cmap='gray')
plt.plot(200, 200, color='red', marker='o', markersize=3)
plt.text(200+1, 200+1, "(200, 200)", color='red', fontsize='medium', rotation=35)
plt.plot(245, 245, color='red', marker='o', markersize=3)
plt.text(245+1, 245+1, "(245, 245)", color='red', fontsize='medium', rotation=35)
plt.plot(300, 300, color='red', marker='o', markersize=3)
plt.text(300+1, 300+1, "(300, 300)", color='red', fontsize='medium', rotation=35)
plt.plot(350, 300, color='red', marker='o', markersize=3)
plt.text(350+1, 300+1, "(350, 300)", color='red', fontsize='medium', rotation=35)
plt.plot(410, 330, color='red', marker='o', markersize=3)
plt.text(410+1, 330+1, "(430, 330)", color='red', fontsize='medium', rotation=35)
plt.plot(480, 340, color='red', marker='o', markersize=3)
plt.text(480+1, 340+1, "(480, 340)", color='red', fontsize='medium', rotation=35)
plt.colorbar(label="Reflectance (%)")
plt.title(label=time + "Composite Image:" + span)
# plt.grid(which='both')
# plt.xticks([]); plt.yticks([]);
plt.savefig(SAVE_COMPOSITE_IMG_AT + "/" + span + "_composite_image_with_points_" + time + ".png", bbox_inches='tight')
plt.show()
# plt.close()
############################################################################
to_return = [vis_composite_image[200, 200], vis_composite_image[245, 245],
vis_composite_image[300, 300], vis_composite_image[300, 350],
vis_composite_image[330, 410], vis_composite_image[340, 480]]
return to_return
times = ["0" + str(i) + "15" for i in range(3, 10)]
times.extend(["0" + str(i) + "45" for i in range(3, 10)])
times = sorted(times)
# times = ["0515"]
span = "15day"
df = {"time" : [], "image_200_200" : [], "image_245_245" : [],
"image_300_300" : [], "image_300_350" : [],
"image_330_410" : [], "image_340_480" : []}
for time in times:
# Call the function
a, b, c, d, e, f = get_composite_image_data(time, span)
df["time"].append(time)
df["image_200_200"].append(a)
df["image_245_245"].append(b)
df["image_300_300"].append(c)
df["image_300_350"].append(d)
df["image_330_410"].append(e)
df["image_340_480"].append(f)
df = pd.DataFrame(df)
# df_sorted = df.sort_values(by=["time"], inplace=False, ignore_index=True)
# plt.xticks(np.arange(0, len(df), 1))
plt.rcParams["figure.dpi"] = 150
df.plot(x="time", y=["image_200_200", "image_245_245", "image_300_300", "image_300_350", "image_330_410", "image_340_480"], figsize=(7, 5))
plt.ylabel("Reflectance values")
plt.grid()
plt.legend()
plt.savefig(SAVE_COMPOSITE_IMG_AT + "/" + span + "_composite_image_reflectance_variation" + ".png", bbox_inches='tight')
plt.show(); plt.close();
############################################################################
############################################################################
############################################################################
############################################################################
'''
Get the Composite Image using minimum Reflectance value.
Save the composite images in a .h5 file.
'''
# # importing necessary libraries
# import h5py
# import numpy as np
# import matplotlib.pyplot as plt
# import os
# import glob
# import pandas as pd
# INPUT_DATA_DIR = "/home/karan/Jan2021_data_merged"
# SAVE_COMPOSITE_IMG_AT = "/media/karan/Studyzz/CHANGA/SEM 8/00Jan_Final_Output/composite_images"
# def get_composite_image_data(time, span):
# all_data = []
# for file_path in sorted(glob.glob(INPUT_DATA_DIR + "/**/*_" + time + "_*.h5", recursive=True)):
# # Load the .h5 file
# file = h5py.File(name=file_path, mode='r')
# # get the file_name from the file_path.
# file_name = os.path.basename(file_path).split(".")[0]
# ########################################################################
# '''Generate image for IMG_VIS channel '''
# vis_image = np.array(file["IMG_VIS"])
# vis_image = vis_image[0, :, :]
# # '''Convert VIS Image into ALBEDO domain.'''
# vis_look_up = np.array(file["IMG_VIS_ALBEDO"])
# vis_image = vis_look_up[vis_image]
# vis_image = np.uint16(vis_image)
# # Cropping to keep only the Indian region.
# vis_image = vis_image[300:1192+1, 623:1391+1]
# ########################################################################
# '''
# Read `Sun_Elevation` data. Derive the Solar Zenith angle.
# Perform Reflectance Normalization w.r.t Solar Zenith Angle.
# '''
# # read the Sun Elevation data for the database (.h5 file).
# sun_elevation = np.array(file["Sun_Elevation"])
# sun_elevation = sun_elevation[0, :, :]
# # initialize the solar_zenith_angle matrix
# solar_zenith_angle = np.zeros(sun_elevation.shape)
# # calculate Solar Zenith angle from the Sun_Elevation data
# solar_zenith_angle = 90 - (sun_elevation * 0.01)
# # Cropping to keep only the Indian region.
# solar_zenith_cropped = solar_zenith_angle[300:1192+1, 623:1391+1]
# # initialize the new VIS data containing the reflectance normalized data.
# vis_image = vis_image / (np.cos(solar_zenith_cropped * (np.pi / 180)))
# ############################################################################
# all_data.append(vis_image)
# print("[INFO] Completed:", file_name)
# # Convert list to array: Final shape becomes: (None, 893, 769)
# all_data = np.array(all_data)
# # Find the resultant minimum matrix
# vis_composite_image = np.min(all_data, axis=0)
# ############################################################################
# return vis_composite_image
# times = ["0" + str(i) + "15" for i in range(3, 10)]
# times.extend(["0" + str(i) + "45" for i in range(3, 10)])
# times = sorted(times)
# # times = ["0515"]
# span = "15day"
# # creating a file
# f = h5py.File(SAVE_COMPOSITE_IMG_AT + "/15day_composite_images.h5", "w")
# for time in times:
# # Call the function
# vis_composite_image = get_composite_image_data(time, span)
# ############################################################################
# # Writing the Final Fog patch region into a HDF5 file.
# dset = f.create_dataset(time, data=vis_composite_image)
# f.close()
############################################################################
############################################################################
############################################################################
############################################################################
"""temp"""
# '''
# Get the approximate Reflectance value of Clear image i.e. land.
# '''
# # importing necessary libraries
# import h5py
# import numpy as np
# import matplotlib.pyplot as plt
# import os
# import glob
# import pandas as pd
# DATE = "15Jan2021"
# INPUT_DATA_DIR = "/home/karan/Jan2021_data_merged/" + DATE
# df = {"date_time" : [], "reflectance_450_250" : []}
# for file_path in sorted(glob.glob(INPUT_DATA_DIR + "/*.h5")):
# # Load the .h5 file
# file = h5py.File(name=file_path, mode='r')
# # get the file_name from the file_path.
# file_name = os.path.basename(file_path).split(".")[0]
# ########################################################################
# '''Generate image for IMG_VIS channel '''
# vis_image = np.array(file["IMG_VIS"])
# vis_image = vis_image[0, :, :]
# # '''Convert VIS Image into ALBEDO domain.'''
# vis_look_up = np.array(file["IMG_VIS_ALBEDO"])
# vis_image = vis_look_up[vis_image]
# vis_image = np.uint16(vis_image)
# # Cropping to keep only the Indian region.
# vis_image = vis_image[300:1192+1, 623:1391+1]
# ########################################################################
# '''
# Read `Sun_Elevation` data. Derive the Solar Zenith angle.
# Perform Reflectance Normalization w.r.t Solar Zenith Angle.
# '''
# # read the Sun Elevation data for the database (.h5 file).
# sun_elevation = np.array(file["Sun_Elevation"])
# sun_elevation = sun_elevation[0, :, :]
# # initialize the solar_zenith_angle matrix
# solar_zenith_angle = np.zeros(sun_elevation.shape)
# # calculate Solar Zenith angle from the Sun_Elevation data
# solar_zenith_angle = 90 - (sun_elevation * 0.01)
# # Cropping to keep only the Indian region.
# solar_zenith_cropped = solar_zenith_angle[300:1192+1, 623:1391+1]
# # initialize the new VIS data containing the reflectance normalized data.
# vis_image = vis_image / (np.cos(solar_zenith_cropped * (np.pi / 180)))
# ############################################################################
# # Creating plot of VIS image after Reflectance Normalization
# fig = plt.figure(figsize=(7, 7), dpi=150)
# plt.subplot(1, 1, 1)
# plt.imshow(X=vis_image, cmap='gray')
# plt.plot(250, 450, color='red', marker='o', markersize=6)
# plt.colorbar(label="Reflectance (%)")
# plt.title(label="")
# # plt.xticks([]); plt.yticks([]);
# plt.suptitle(t=file_name)
# # plt.savefig(OUR_PRODUCT_DIR + "/2fog_validation_plots_" + DATE + "/" + k + ".png", bbox_inches='tight')
# # plt.show()
# plt.close()
# ############################################################################
# ############################################################################
# # Append Date_time and reflectance values
# date_time = file_name.split("_")[1] + "_" + file_name.split("_")[2]
# df["date_time"].append(date_time)
# df["reflectance_450_250"].append(vis_image[450, 250])
# ############################################################################
# file.close()
# df = pd.DataFrame(df)