Skip to content
Snippets Groups Projects
Commit 9b4006bf authored by Verouden, Niels's avatar Verouden, Niels
Browse files

Improve code

Improve code for plotting images
Solve typo
parent 6ec0d448
No related branches found
No related tags found
No related merge requests found
......@@ -25,7 +25,7 @@ def thresholding(array):
threshold = (cluster_center[0] + cluster_center[1]) / 2
threshold = threshold[0]
# crate binary flood map
# create binary flood map
# all pixels below the threshold (1) are flooded, all pixels above (0) are non-flooded
output = copy.copy(array)
output[np.where(output < threshold)] = 0.0001
......@@ -41,10 +41,10 @@ def apply_thresholding(dest_name):
for i in os.listdir(dest_name):
dates.append(i[0:10])
data = os.path.join(dest_name, i) # open and read tif files
data2 = rio.open(data)
data2 = data2.read(1) # get only first band (vv)
images.append(data2)
output.append(thresholding(data2)) # call thresholding function on all files
sar = rio.open(data)
array = sar.read(1) # get only first band (vv)
images.append(array)
output.append(thresholding(array)) # call thresholding function on all files
return output, images, dates
......@@ -52,14 +52,14 @@ def apply_thresholding(dest_name):
# STEP 2: VISUALISE
# =============================================================================
def show_stats(image):
def show_stats(image, bins, title):
""" Function that computes statistics and shows histogram for one image """
stats = []
stats.append({'min': np.nanmin(image),
'mean': np.nanmean(image),
'median': np.nanmedian(image),
'max': np.nanmax(image)})
hist = show_hist(image, bins = 50, lw = 0.0, stacked = False, alpha = 0.3, histtype = 'stepfilled', title = "Histogram")
hist = show_hist(image, bins = bins, lw = 0.0, stacked = True, histtype = 'stepfilled', title = title)
return stats, hist
def visualise(images, nrows, ncols, dates, title='Title'):
......@@ -67,8 +67,7 @@ def visualise(images, nrows, ncols, dates, title='Title'):
plt.figure()
plt.suptitle(title, fontsize = 18, x = 0.51, y = 0.98)
for n, image in enumerate(images):
ax = plt.subplot(nrows, ncols, n + 1)
ax = plt.subplot(2, 2, n + 1)
ax.imshow(image)
ax.set_title(dates[n])
ax.set_xticks([])
ax.set_yticks([])
plt.axis('off')
\ No newline at end of file
......@@ -8,15 +8,13 @@ an impression of the situation in a certain area.
# IMPORTS
# =============================================================================
from thresholding_functions import apply_thresholding
from thresholding_functions import show_stats
from thresholding_functions import visualise
from Thresholding.thresholding_functions import apply_thresholding, show_stats, visualise
# =============================================================================
# FILE SETUP
# =============================================================================
folder_name = 'data/SentinelTimeSeriesStacked' # folder with unzipped, stacked and filtered SAR images
folder_name = 'data/CapHaitien_Flood2021' # folder with unzipped, stacked and filtered SAR images
# =============================================================================
# STEP 1: APPLY THRESHOLDING
......@@ -28,6 +26,6 @@ thresholding, images, dates = apply_thresholding(folder_name)
# STEP 2: VISUALISE
# =============================================================================
show_stats(images[1])
show_stats(image = images[0], bins=50, title='Histogram')
visualise(thresholding, 2, 3, dates, 'Thresholding method') # 2 = nrows, 3 = ncols
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment