I'm working with remote sensing, using a satellite image where it has 2 bands: 0 = NBR and 1 = NDVI, both of them has a range from 0 to 1. I download the images from google earth engine, where the nodatavalue is set to -9999. To use the image for a deeplearning algorithm that I'm developing, I need to remove those values without to change them to another value. I already made the code to mask the -9999 but when I export it to another folder, it change the maxvalue to 1e20+ and fill those -9999, what can i do so it does not change them?
import os
import numpy as np
import rasterio
from matplotlib import pyplot as plt
# Folder paths
folder_path = '/content/drive/MyDrive/2018-2022 NDVI NBR/zonauno'
processed_folder = '/content/drive/MyDrive/2018-2022 NDVI NBR/zonaunonodata'
# Nodata value
nodata_value = -9999
# Create the "processed_folder" if it doesn't exist
os.makedirs(processed_folder, exist_ok=True)
# Get the list of TIFF files
tiff_files = [file for file in os.listdir(folder_path) if file.endswith('.tif')]
# Process each TIFF file
for tiff_file in tiff_files:
tiff_path = os.path.join(folder_path, tiff_file)
with rasterio.open(tiff_path) as src:
data = src.read(masked=True) # Read data as a numpy array
# Make sure you are working with 2 channels
if data.shape[0] >= 2:
data = data[:2]
# Exclude nodata values and normalize
data_without_nodata = np.ma.masked_where(data == nodata_value, data)
normalized_data = np.zeros_like(data_without_nodata)
for i in range(normalized_data.shape[0]):
min_val = data_without_nodata[i].min()
max_val = data_without_nodata[i].max()
normalized_data[i] = (data_without_nodata[i] - min_val) / (max_val - min_val)
# Save normalized image
save_path = os.path.join(processed_folder, f'processed_{tiff_file}')
with rasterio.open(
save_path,
'w',
driver='GTiff',
height=normalized_data.shape[1],
width=normalized_data.shape[2],
count=normalized_data.shape[0],
dtype=str(normalized_data.dtype),
crs=src.crs,
transform=src.transform,
) as dst:
dst.write(normalized_data)
# Visualize the first processed file (optional)
plt.figure(figsize=(10, 10))
plt.imshow(normalized_data[0], cmap='viridis', vmin=0, vmax=1)
plt.colorbar(label='Pixel value')
plt.title(f'Normalized Image - {tiff_file} - Channel 0')
plt.xlabel('Columns')
plt.ylabel('Rows')
plt.show()
the max value of the range from the output image is 1e+20, where it should only 1
the package is rasterio, what can i do to solve it? I've tried a lot of things but nothing workings.
I've tried to mask the values but it does not work when I export it.