Needed Libraries

Import all the needed libraries

from spectral import imshow, view_cube
import spectral.io.envi as envi
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import cv2
import os
import pandas as pd

Read the hyperspectral image

The hyperspectral image needs three different images to get a corrected image - raw data (raw reflectance values from the camera), *white ref* - which is the reflectance from white calibration surface, and dark ref - the refelctance from dark (usually zeros). The white and dark references are used to calibrate and normalise the raw refelctance values using the equation: Corrected Image = {RawReflectance - DarkReflectance \\over WhiteReflectance - DarkReflectance}

dark_ref = envi.open('path_to_dark_ref.hdr','path_to_dark_ref')
white_ref = envi.open('path_to_white_ref.hdr','path_to_white_ref')
raw_ref = envi.open('path_to_raw.hdr','path_to_raw)

Load the images

bands = data_ref.bands.centers
white_data = np.array(white_ref.load())
dark_data = np.array(dark_ref.load())
raw_data = np.array(data_ref.load())

Normalised (corrected) image

corrected_data = np.divide(
            np.subtract(raw_data, dark_data),
            np.subtract(white_data, dark_data))

View the Image

To view the iimage, we can either select a band or select multiple bands to create a RGB image (select 1 band each from appropriate wavelenth in Red, Green, and Blue spectrum and arrange accordingly).

The code below visualises RGB image using prebuilt function from spectral library.

#Get RGB Image
img = get_rgb(corrected_data, bands=None)

#Select single band
#sel = 70
#img = data_ref[:,:,sel]

image = cv2.normalize(img, None, alpha = 0, beta = 255, norm_type = cv2.NORM_MINMAX, dtype = cv2.CV_32F)
image = image.astype(np.uint8)
image = cv2.rotate(image,cv2.ROTATE_90_CLOCKWISE)

#Uncomment to view the RGB Image
#cv2.namedWindow("main", cv2.WINDOW_NORMAL)
#cv2.imshow('main', image)
#cv2.waitKey(0)
#cv2.destroyAllWindows()