I have NumPy ndarray with shape (1063,2116,12) (has 12 bands). The ndarray has non geographical data, but I do have its' transform affine and the crs. I want to give this geographical data to the ndarray I have. However, I keep getting as a result blank image.
#details regard my data:
#I have dictionary ("all_images") with two keys, each key has two values: the image as ndarray and the bbox of the image, which has geographical data, as shapely geometry
Here is my code,based on this example:
from rasterio.warp import reproject, Resampling
from rasterio import Affine
for i in np.arange(1,len(all_images)+1):
#this is the image:
img_bbox=all_images[i][0]
#this is the shapely geoemtry bbox:
bbox_shapely=all_images[i][1]
#get the coordinates in order to create transform using sentinelhub
minx, miny, maxx, maxy = bbox_shapely.geometry.bounds
bbox_coords_wgs84=[minx, miny, maxx, maxy]
bbox = BBox(bbox=bbox_coords_wgs84, crs=CRS.WGS84)
#get tranform wih sentinelhub
transf = bbox.get_transform_vector(resx=10, resy=10)
print(transf)
#get in gdal as rasterio requires
transf= Affine.from_gdal(*transf)
print(transf)
#create destination array for the reproject data
destination = np.zeros(all_images[i][0].shape, np.uint8)
reproject(
img_bbox,
destination,
src_transform=transf,
src_crs={'init': 'EPSG:4326'},
dst_transform=transf,
dst_crs={'init': 'EPSG:4326'},
resampling=Resampling.nearest)
assert destination.any()
assert not destination.all()
However,when I show the result I get blank image,that seems to not be projected:
#the image:
plt.imshow(all_images[i][0][:,:,0])
plt.imshow(destination[:,:,0])
and if I use show it doesn't work at all:
show((destination, 2), cmap='viridis')
>>>AttributeError: 'numpy.ndarray' object has no attribute 'read'
I know I can also save it as tiff but in this case I prefer not to save it yet at this point as I want to do more processing on the images in the dictionary.
Where is my mistake?How can I reproject my ndarrays?
Edit: adding here my transform data:
transf = bbox.get_transform_vector(resx=10, resy=10)
print(transf)
>>>(-45.53101999856795, 10.0, 0, -10.3395264554344, 0, -10.0)
transf= Affine.from_gdal(*transf)
print(transf)
>>>
| 10.00, 0.00,-45.53|
| 0.00,-10.00,-10.34|
| 0.00, 0.00, 1.00|