Consider that I have a 4x4 binary image, that is, from a single channel, each pixel value is located in a specific location (latitude, longitude), and consequently the map has a specific spatial resolution (in latitude and longitude). I want to resize this map to another size, 7x11, and also change its spatial resolution. Below is a code in Python that initially addresses this resolution, but I don’t know if the code is correct. Can anyone help me so I can clarify whether this code is correct? Thanks in advance.
import numpy as np
import skimage.transform
import matplotlib.pyplot as plt
def resize_to_new_resolution_and_range(image, old_lat_resolution, old_lon_resolution, old_lat_min, old_lat_max, old_lon_min, old_lon_max, new_lat_min, new_lat_max, new_lon_min, new_lon_max, new_lat_resolution, new_lon_resolution):
original_height, original_width = image.shape[:2]
new_height = int((new_lat_max - new_lat_min) / new_lat_resolution)
new_width = int((new_lon_max - new_lon_min) / new_lon_resolution)
resized_image = skimage.transform.resize(image, (new_height, new_width), mode='reflect', preserve_range=True)
latitudes = new_lat_min + np.arange(new_height) * new_lat_resolution
longitudes = new_lon_min + np.arange(new_width) * new_lon_resolution
return resized_image, latitudes, longitudes
image = np.random.rand(4, 4)
old_lat_min, old_lat_max = -25, -17.5
old_lon_min, old_lon_max = -55, -40
old_lat_resolution = -2.5
old_lon_resolution = 5.0
new_lat_min, new_lat_max = -25, -18
new_lon_min, new_lon_max = -54, -43
new_lat_resolution = 1.0
new_lon_resolution = 1.0
resized_image, new_latitudes, new_longitudes = resize_to_new_resolution_and_range(
image, old_lat_resolution, old_lon_resolution, old_lat_min, old_lat_max, old_lon_min, old_lon_max, new_lat_min, new_lat_max, new_lon_min, new_lon_max, new_lat_resolution, new_lon_resolution
)
print("Original image:")
print(image)
print("Resized image:")
print(resized_image)
print("Resized image shape:")
print(resized_image.shape)
print("New latitudes:")
print(new_latitudes)
print("New longitudes:")
print(new_longitudes)
plt.imshow(image, interpolation='gaussian')
plt.title('Original image')
plt.show()
plt.imshow(resized_image, interpolation='gaussian')
plt.title('Resized image')
plt.show()