python-3.xsatellite-imagespectral-python

How can I generate RGB image from hyperspectral data?


I have a hypercube with dimensions (512, 640, 92), which has 92 bands. How can I trasform this Hypercube to RGB image? Is there any tool or python code available that I can use it?

The spectral range is [980-1680].


Solution

  • It depends on what you mean by "transform". If you just want to read 3 bands out of the hyperspectral image to be displayed in the red, green, & blue channels of an image, you can easily do that with the get_rgb function of the Spectral Python module.

    In [1]: import spectral as spy
    
    In [2]: img = spy.open_image('92AV3C.ref.hdr')
    
    In [3]: rgb = spy.get_rgb(img, (30, 20, 10))
    
    In [4]: rgb.shape
    Out[4]: (145, 145, 3)
    

    If you want to save the save bands as an image file you can do that with the save_rgb function.

    In [5]: spy.save_rgb('rgb.png', img, (30, 20, 10))
    

    If you want to read the raw band data into a numpy array, you can do that like this:

    In [6]: rgb = img.read_bands((30, 20, 10))
    

    If you want to average bands from the source image into an RGB image, you can create a BandResampler to do that.

    If you want to actually extract the red, green, and blue bands from an image, you need to know which specific bands to read from the hyperspectral image. But in your case, that is not possible, since all of your bands are in the infrared part of the optical spectrum.

    If you want to incorporate information from all bands of the hyperspectral image in your RGB image, you can use a feature extraction technique, like principal components transformation, and keep the top 3 components (or other 3 components).

    In [7]: data = img.load()
    
    In [8]: rgb = spy.principal_components(data).reduce(num=3).transform(data)
    
    In [9]: rgb.shape
    Out[9]: (145, 145, 3)