4. Tutorial: segment

4.1. Sample data

Sample imagery captured from a Resonon Pika II VIS-NIR line scanning imager and ancillary sample files can be downloaded from this link.

Before trying this tutorial on your own machine, please download the sample files and place into a local directory of your choosing (and do not change the file names). Indicate the location of your sample files by modifying data_dir:

[1]:
data_dir = r'F:\\nigo0024\Documents\hs_process_demo'

4.2. Confirm your environment

Before trying the tutorials, be sure hs_process and its dependencies are properly installed. If you installed in a virtual environment, first check we are indeed using the Python instance that was installed with the virtual environment:

[2]:
import sys

print('Python install location: {0}'.format(sys.executable))
Python install location: C:\Users\nigo0024\AppData\Local\Continuum\anaconda3\envs\spec\python.exe

The spec folder that contains python.exe tells me that the activated Python instance is indeed in the spec environment, just as I intend. If you created a virtual environment, but your python.exe is not in the envs\spec directory, you either did not properly create your virtual environment or you are not pointing to the correct Python installation in your IDE (e.g., Spyder, Jupyter notebook, etc.).


4.3. segment.band_math_derivative

Calculates a derivative-type spectral index from two input bands and/or wavelengths. Bands/wavelengths can be input as two individual bands, two sets of bands (i.e., list of bands), or range of bands (i.e., list of two bands indicating the lower and upper range). [API]

Load hsio and segment modules

[3]:
import os
import numpy as np
from hs_process import hsio
from hs_process import segment

fname_in = os.path.join(data_dir, 'Wells_rep2_20180628_16h56m_pika_gige_7-Radiance Conversion-Georectify Airborne Datacube-Convert Radiance Cube to Reflectance from Measured Reference Spectrum.bip.hdr')
io = hsio(fname_in)
my_segment = segment(io.spyfile)

Calculate the MERIS Terrestrial Chlorophyll Index (MTCI; Dash and Curran, 2004) via segment.band_math_derivative

[4]:
array_mtci, metadata = my_segment.band_math_derivative(wl1=754, wl2=709, wl3=681, spyfile=io.spyfile)
print('Array shape: {0}'.format(array_mtci.shape))
print('Array mean value: {0}'.format(np.nanmean(array_mtci)))
Bands used (``b1``): [176]
Bands used (``b2``): [154]
Bands used (``b3``): [141]

Wavelengths used (``b1``): [753.84]
Wavelengths used (``b2``): [708.6784]
Wavelengths used (``b3``): [681.992]

Array shape: (617, 1300)
Array mean value: 9.401103973388672
C:\Users\nigo0024\AppData\Local\Continuum\anaconda3\envs\spec\lib\site-packages\hs_process\segment.py:270: RuntimeWarning: invalid value encountered in true_divide
  array_der = (array_b1-array_b2)/(array_b2-array_b3)

Show MTCI image via hsio.show_img

[5]:
io.show_img(array_mtci, vmin=-2, vmax=15)

_images/tutorial_segment_9_1.png

4.4. segment.band_math_mcari2

Calculates the MCARI2 (Modified Chlorophyll Absorption Ratio Index Improved; Haboudane et al., 2004) spectral index from three input bands and/or wavelengths. Bands/wavelengths can be input as two individual bands, two sets of bands (i.e., list of bands), or range of bands (i.e., list of two bands indicating the lower and upper range). [API]

Load hsio and segment modules

[6]:
from hs_process import hsio
from hs_process import segment

fname_in = os.path.join(data_dir, 'Wells_rep2_20180628_16h56m_pika_gige_7-Radiance Conversion-Georectify Airborne Datacube-Convert Radiance Cube to Reflectance from Measured Reference Spectrum.bip.hdr')
io = hsio(fname_in)
my_segment = segment(io.spyfile)

Calculate the MCARI2 spectral index (Haboudane et al., 2004) via segment.band_math_mcari2

[7]:
array_mcari2, metadata = my_segment.band_math_mcari2(wl1=800, wl2=670, wl3=550, spyfile=io.spyfile)
print('Array shape: {0}'.format(array_mcari2.shape))
print('Array mean value: {0}'.format(np.nanmean(array_mcari2)))
Bands used (``b1``): [198]
Bands used (``b2``): [135]
Bands used (``b3``): [77]

Wavelengths used (``b1``): [799.0016]
Wavelengths used (``b2``): [669.6752]
Wavelengths used (``b3``): [550.6128]

Array shape: (617, 1300)
Array mean value: 0.5737694501876831

Show MCARI2 image via hsio.show_img

[8]:
io.show_img(array_mcari2)

_images/tutorial_segment_15_1.png

4.5. segment.band_math_ndi

Calculates a normalized difference spectral index from two input bands and/or wavelengths. Bands/wavelengths can be input as two individual bands, two sets of bands (i.e., list of bands), or range of bands (i.e., list of two bands indicating the lower and upper range). [API]

Load hsio and segment modules

[9]:
from hs_process import hsio
from hs_process import segment

fname_in = os.path.join(data_dir, 'Wells_rep2_20180628_16h56m_pika_gige_7-Radiance Conversion-Georectify Airborne Datacube-Convert Radiance Cube to Reflectance from Measured Reference Spectrum.bip.hdr')
io = hsio(fname_in)
my_segment = segment(io.spyfile)

Calculate the Normalized difference vegetation index using 10 nm bands centered at 800 nm and 680 nm via segment.band_math_ndi

[10]:
array_ndvi, metadata = my_segment.band_math_ndi(wl1=[795, 805], wl2=[675, 685], spyfile=io.spyfile)
print('Array shape: {0}'.format(array_ndvi.shape))
print('Array mean value: {0}'.format(np.nanmean(array_ndvi)))
Bands used (``b1``): [197, 198, 199, 200]
Bands used (``b2``): [138, 139, 140, 141, 142]

Wavelengths used (``b1``): [796.9488, 799.0016, 801.0544, 803.1072]
Wavelengths used (``b2``): [675.8336, 677.8864, 679.9392, 681.992, 684.0448]

Array shape: (617, 1300)
Array mean value: 0.8184887766838074
C:\Users\nigo0024\AppData\Local\Continuum\anaconda3\envs\spec\lib\site-packages\hs_process\segment.py:541: RuntimeWarning: invalid value encountered in true_divide
  array_ndi = (array_b1-array_b2)/(array_b1+array_b2)

Show NDVI image via hsio.show_img

[11]:
io.show_img(array_ndvi)

_images/tutorial_segment_21_1.png

4.6. segment.band_math_ratio

Calculates a simple ratio spectral index from two input band and/or wavelengths. Bands/wavelengths can be input as two individual bands, two sets of bands (i.e., list of bands), or a range of bands (i.e., list of two bands indicating the lower and upper range). [API]

Load hsio and segment modules

[12]:
from hs_process import hsio
from hs_process import segment

fname_in = os.path.join(data_dir, 'Wells_rep2_20180628_16h56m_pika_gige_7-Radiance Conversion-Georectify Airborne Datacube-Convert Radiance Cube to Reflectance from Measured Reference Spectrum.bip.hdr')
io = hsio(fname_in)
my_segment = segment(io.spyfile)

Calculate a red/near-infrared band ratio using a range of bands (i.e., mimicking a broadband sensor) via segment.band_math_ratio

[13]:
array_ratio, metadata = my_segment.band_math_ratio(wl1=[630, 690], wl2=[800, 860], list_range=True)
print('Array shape: {0}'.format(array_ratio.shape))
print('Array mean value: {0}'.format(np.nanmean(array_ratio)))
Bands used (``b1``): [116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144]
Bands used (``b2``): [199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227]

Wavelengths used (``b1``): [630.672, 632.7248, 634.7776, 636.8304, 638.8832, 640.936, 642.9888, 645.0416, 647.0944, 649.1472, 651.2, 653.2528, 655.3056, 657.3584, 659.4112, 661.464, 663.5168, 665.5696, 667.6224, 669.6752, 671.728, 673.7808, 675.8336, 677.8864, 679.9392, 681.992, 684.0448, 686.0976, 688.1504]
Wavelengths used (``b2``): [801.0544, 803.1072, 805.16, 807.2128, 809.2656, 811.3184, 813.3712, 815.424, 817.4768, 819.5296, 821.5824, 823.6352, 825.688, 827.7408, 829.7936, 831.8464, 833.8992, 835.952, 838.0048, 840.0576, 842.1104, 844.1632, 846.216, 848.2688, 850.3216, 852.3744, 854.4272, 856.48, 858.5328]

(659/830)
Array shape: (617, 1300)
Array mean value: 0.10981176793575287
C:\Users\nigo0024\AppData\Local\Continuum\anaconda3\envs\spec\lib\site-packages\hs_process\segment.py:683: RuntimeWarning: invalid value encountered in true_divide
  array_ratio = (array_b1/array_b2)

Notice that 29 spectral bands were consolidated (i.e., averaged) to mimic a single broad band. We can take the mean of two bands by changing list_range to False, and this slightly changes the result.

[14]:
array_ratio, metadata = my_segment.band_math_ratio(wl1=[630, 690], wl2=[800, 860], list_range=False)
print('Array shape: {0}'.format(array_ratio.shape))
print('Array mean value: {0}'.format(np.nanmean(array_ratio)))
Bands used (``b1``): [116, 145]
Bands used (``b2``): [198, 228]

Wavelengths used (``b1``): [630.672, 690.2032]
Wavelengths used (``b2``): [799.0016, 860.5856]

(660/830)
Array shape: (617, 1300)
Array mean value: 0.11360744386911392

Show the red/near-infrared ratio image via hsio.show_img

[15]:
io.show_img(array_ratio, vmax=0.3)

_images/tutorial_segment_29_1.png

4.7. segment.load_spyfile

Loads a SpyFile (Spectral Python object) for data access and/or manipulation by the hstools class. [API]

Load hsio and segment modules

[16]:
from hs_process import hsio
from hs_process import segment

fname_in = os.path.join(data_dir, 'Wells_rep2_20180628_16h56m_pika_gige_7-Radiance Conversion-Georectify Airborne Datacube-Convert Radiance Cube to Reflectance from Measured Reference Spectrum.bip.hdr')
io = hsio(fname_in)
my_segment = segment(io.spyfile)

Load datacube via segment.load_spyfile

[17]:
my_segment.load_spyfile(io.spyfile)
my_segment.spyfile
[17]:
        Data Source:   'F:\\nigo0024\Documents\hs_process_demo\Wells_rep2_20180628_16h56m_pika_gige_7-Radiance Conversion-Georectify Airborne Datacube-Convert Radiance Cube to Reflectance from Measured Reference Spectrum.bip'
        # Rows:            617
        # Samples:        1300
        # Bands:           240
        Interleave:        BIP
        Quantization:  32 bits
        Data format:   float32