Search code examples
pythonsignal-processinghypercubespectral-python

How to extract the spectra range within a roi mask?


I am learning hyperspectral data analysis, so my question may sound simple.

I am reading a hypercube by using the following command:

import spectral.io.envi as envi

hc = envi.open('cube_envi32.hdr','cube_envi32.dat')

'hc' has the following shape:

# Rows:            512
    # Samples:         640
    # Bands:            92
    Interleave:        BSQ
    Quantization:  32 bits
    Data format:   float32
(512, 640, 92)

I want to extract the spectral (or pixel values of within a specific binary mask, as shown with rectangle here:

enter image description here

My question includes two parts:

  1. which python library is suitable for spectra analysis and working with hypercubes?
  2. what command should I write to extract the spectra values of the region of interest?

Thanks


Solution

  • You can use the spectral module and numpy to read the data.

    If you know the bounding coordinates of your rectangle, you can simply use

    region = hc[i_start:i_stop, j_start:j_stop]
    

    If you have an arbitrary mask (mask is not necessarily a rectangular subregion) and can load the entire image into memory, then you can use numpy syntax to read the spectra. Suppose your mask m has a value of 1 for pixels you want to read. Then you can do this:

    pixels = hc.load().asarray[m == 1]
    

    If you can't (or don't want to) read the entire image, you can read the masked pixels into a list like so:

    pixels = [hc[i, j] for (i, j) in np.argwhere(m == 1)]