Skip to main content
1 of 3
ReutKeller
  • 2.2k
  • 4
  • 37
  • 91

Reproject numpy ndarray without saving as tiff

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 nd-array I have. However, I keep getting as a result blank image.

#details regard my data:
#I have dictionry ("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 geoemtry

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])

enter image description here

plt.imshow(destination[:,:,0])

enter image description here

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?

ReutKeller
  • 2.2k
  • 4
  • 37
  • 91