Utility Functions and Classes

The load_ENVI_file and load_ENVI_spec_lib functions are used by the examples to load the HS cubes that are saved in the ENVI file format. The functions corr and cov are the one defined in the Matlab Hyperspectral Toolbox.


Load the data and the header from an ENVI file

pysptools.util.load_ENVI_file(file_name)[source]

Load the data and the header from an ENVI file. It use the SPy (spectral) library. At ‘file_name’ give the envi header file name.

Parameters:file_namepath string The complete path to the file to load. Use the header file name.
Returns: tuple
data: numpy array
A (m x n x p) HSI cube.
head: dictionary
Starting at version 0.13.1, the ENVI file header

Load an ENVI.sli file

pysptools.util.load_ENVI_spec_lib(file_name)[source]

Load an ENVI.sli file.

Parameters:file_namepath string The complete path to the library file to load.
Returns: numpy array

A (n x p) HSI cube.

head: dictionary
Starting at version 0.13.1, the ENVI file header

Autocorrelation

pysptools.util.corr(M)[source]

Compute the sample autocorrelation matrix of a 2D matrix.

Parameters:Mnumpy array 2d matrix of HSI data (N x p)
Returns: numpy array
Sample autocorrelation matrix.

Covariance

pysptools.util.cov(M)[source]

Compute the sample covariance matrix of a 2D matrix.

Parameters:Mnumpy array 2d matrix of HSI data (N x p)
Returns: numpy array
sample covariance matrix

Display a linear stretched RGB image

pysptools.util.display_linear_stretch(M, R, G, B, suffix=None)[source]

Display a linear stretched RGB image.

Parameters:
  • Mnumpy array A HSI cube (m x n x p).
  • Rint A band number that will render the red color.
  • Gint A band number that will render the green color.
  • Bint A band number that will render the blue color.
  • suffixstring [default None] Add a suffix to the title.

Plot a linear stretched RGB image

pysptools.util.plot_linear_stretch(M, path, R, G, B, suffix=None)[source]

Plot a linear stretched RGB image.

Parameters:
  • Mnumpy array A HSI cube (m x n x p).
  • pathstring The path where to put the plot.
  • Rint A band number that will render the red color.
  • Gint A band number that will render the green color.
  • Bint A band number that will render the blue color.
  • suffixstring [default None] Add a suffix to the file name.

Converts a 3D data cube to a 2D matrix

pysptools.util.convert2d(M)[source]

Converts a 3D data cube (m x n x p) to a 2D matrix of points where N = m*n.

Parameters:Mnumpy array A HSI cube (m x n x p).
Returns: numpy array
2D data matrix (N x p)

Converts a 1D or 2D matrix to a 3D data cube

pysptools.util.convert3d(M, h, w, sigLast=True)[source]

Converts a 1D (N) or 2D matrix (p x N) or (N x p) to a 3D data cube (m x n x p) where N = m * n

Parameters:
  • Nnumpy array 1D (N) or 2D data matrix (p x N) or (N x p)
  • hinteger Height axis length (or y axis) of the cube.
  • winteger Width axis length (or x axis) of the cube.
  • siglastTrue [default False] Determine if input N is (p x N) or (N x p).
Returns: numpy array
A 3D data cube (m x n x p)

Normalize

pysptools.util.normalize(M)[source]

Normalizes M to be in range [0, 1].

Parameters:Mnumpy array 1D, 2D or 3D data.
Returns: numpy array
Normalized data.

Manage Regions of Interests

class pysptools.util.ROIs(x, y)[source]

Manage regions of interest (ROIs).

add(id, *rois)[source]

Add a named ROI.

Parameters:
  • idstring The class (or cluster) name.
  • roisdictionary list Each parameter, a dictionary, represent a rectangle, a polygon or a raw array. They use matrix coordinates. For a raw array: {‘raw’: mask-array}, mask-array is a binary 2D array with the hypercube (x,y) dimensions. For a rectangle: {‘rec’: (upper_left_line, upper_left_column, lower_right_line, lower_right_column)}. For a polygone: {‘poly’: ((l1,c1),(l2,c2), ...)}, l stand for line and c for column. The polygon don’t need to be close. You can define one or more raw, rectangle and/or polygon for a same cluster. The polygon and the rectangle must be well formed.
display(colorMap='Accent', suffix=None)[source]

Display the ROIs.

Parameters:
  • colorMapstring [default ‘Accent’] A color map element of [‘Accent’, ‘Dark2’, ‘Paired’, ‘Pastel1’, ‘Pastel2’, ‘Set1’, ‘Set2’, ‘Set3’], “Accent” is the default and it fall back on “Jet”.
  • suffixstring [default None] Add a suffix to the file name.
get_labels()[source]

Return a labels list.

Return: list
A labels list.
get_next()[source]

Iterator, return at each step: the cluster name and a ROI list.

Return: tuple
Cluster name, ROI list.
plot(path, colorMap='Accent', suffix=None)[source]

Plot the ROIs.

Parameters:
  • pathstring The path where to put the plot.
  • colorMapstring [default ‘Accent’] A color map element of [‘Accent’, ‘Dark2’, ‘Paired’, ‘Pastel1’, ‘Pastel2’, ‘Set1’, ‘Set2’, ‘Set3’], “Accent” is the default and it fall back on “Jet”.
  • suffixstring [default None] Add a suffix to the file name.

The big class that check the inputs (InputValidation)

class pysptools.util.InputValidation(class_id)[source]

Validate method inputs.


Read a 3D MATLAB array

class pysptools.util.load_mat_file[source]

Load a MATLAB file (a 3D array only)


Shrink a hyperspectral image

class pysptools.util.shrink[source]

Reduce a hyperspectral image by half.