-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbands_analysis.py
More file actions
92 lines (73 loc) · 3.48 KB
/
bands_analysis.py
File metadata and controls
92 lines (73 loc) · 3.48 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
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
import numpy as np
import matplotlib.pyplot as plt
def analyze_image_bands(images, masks):
"""
Analyzes image bands to determine which have the highest values
corresponding to the segmentation masks.
"""
nr_samples, h, w, channels = images.shape
# Initialize arrays to store the sum of band values for each channel for mask 0 and 1
total_band_values_0 = np.zeros(channels)
total_band_values_1 = np.zeros(channels)
# Initialize arrays to store the count of '0' and '1' pixels for each image.
total_mask_pixels_0 = 0
total_mask_pixels_1 = 0
# Iterate over each image and its corresponding mask
for i in range(nr_samples):
image = images[i] # Shape: (channels, height, width)
mask = masks[i]
# Ensure the mask is binary (0 or 1)
binary_mask = (mask > 0).astype(int)
#print(binary_mask)
binary_mask_0 = 1 - binary_mask # create the opposite mask
# get the sum of pixel values for each band within the masked region
# we do that iterating over each band in the image
# returns band_data, 2D array containing the pixel values for the j-th band of the image.
for j in range(channels):
band_data = image[:, :, j]
total_band_values_0[j] += np.sum(band_data * binary_mask_0)
total_band_values_1[j] += np.sum(band_data * binary_mask)
total_mask_pixels_0 += np.sum(binary_mask_0)
total_mask_pixels_1 += np.sum(binary_mask)
# Calculate the average band values
avg_band_values_0 = total_band_values_0 / total_mask_pixels_0 if total_mask_pixels_0 > 0 else total_band_values_0
avg_band_values_1 = total_band_values_1 / total_mask_pixels_1 if total_mask_pixels_1 > 0 else total_band_values_1
# Create band names
band_names = [f"Band_{i+1}" for i in range(channels)]
return avg_band_values_0, avg_band_values_1, band_names
def plot_band_values(avg_band_values_0, avg_band_values_1, band_names):
"""
Plots the average band values for mask 0 and 1 in two subplots.
"""
# Create a figure with two subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 10))
fig.suptitle("Band analysis")
colors = ['blue', 'green', 'yellow', 'red', 'darkred', 'firebrick', 'indigo']
# Plot for mask 0
ax1.title.set_text("Average Band pixel values (non-tree)")
ax1.bar(band_names, avg_band_values_0, color=colors)
ax1.set_title("Mask = 0")
ax1.set_xlabel("Bands")
ax1.set_ylabel("Average Magnitude")
# Plot for mask 1
ax2.title.set_text("Average Band pixel values (tree)")
ax2.bar(band_names, avg_band_values_1, color=colors)
ax2.set_title("Mask = 1")
ax2.set_xlabel("Bands")
ax2.set_ylabel("Average Magnitude")
plt.tight_layout() # Adjust layout to prevent labels from overlapping
plt.show()
if __name__ == '__main__':
# load data
images = np.load('imagesBengaluru_ndvi.npy', allow_pickle=True)
masks = np.load('masksBengaluru_ndvi.npy', allow_pickle=True)
# analyze the image bands
avg_band_values_0, avg_band_values_1, band_names = analyze_image_bands(images, masks)
print("Average Band Values (Mask = 0):")
for band, value in zip(band_names, avg_band_values_0):
print(f"{band}: {value:.4f}")
print("\nAverage Band Values (Mask = 1):")
for band, value in zip(band_names, avg_band_values_1):
print(f"{band}: {value:.4f}")
# Plot the band magnitudes
plot_band_values(avg_band_values_0, avg_band_values_1, band_names)