spectralpython / spectral

Python module for hyperspectral image processing
MIT License
573 stars 139 forks source link

Pass numpy-nDarray directly to spectralpython #3

Closed epifanio closed 10 years ago

epifanio commented 10 years ago

In my workflow the data are coming from GRASS - GIS, from which i'm exporting the raster layers as an Envi multiband dataset which i use as input to spectralpython. In GRASS the same layers are available as numpy N-Darray without involve any export, how can i use spectralpython to read a numpy N-DArray as input instead of use the envi dataformat ?

tboggs commented 10 years ago

Most of the spectral functions can operate on numpy.ndarray objects so you should be able to stack the GRASS layers into an ndarray and pass it to the spectral functions. If you need to read the data from a file, rather than interface GRASS directly from python, you can use numpy.save to write the array to a .npy file, then use numpy.load to read it.

epifanio commented 10 years ago

It worked as aspected :) i first stored the image name in a list then i read the dataset as a numpy array and I append each array in a empty list then i concatenate all the layer from the list of arrays in a single one

#img = ['layername1', 'layername2', ... , ....]
import numpy as np
arraylist = []
for i in img[]:
    rl = garray.array()
    rl.read(i)
    arraylist.append(rl)
imagegroup = np.concatenate([aux[..., np.newaxis] for aux in arraylist], axis=2)

maybe there is abetter way to concatenate my layers, but as it is works great! Thank you so much!!!

tboggs commented 10 years ago

The way you are doing it looks fine. If you know the shape, data type, and number layers, you could save some memory by pre-allocating imagegroup, then setting the bands directly (e.g., imagegroup[:, :, i] = rl). That way, you wouldn't have all the layers in memory twice (in both arraylist and imagegroup).

epifanio commented 10 years ago

:+1:

i changed the code to :

rl = garray.array()
imagegroup = np.empty((rl.shape[0], rl.shape[1], 8), dtype='float')
for i,v in enumerate(img[3:]):
    rl.read(v)
    imagegroup[:, :, i] = rl

Thanks a lot!