Main Content

customSpectralIndex

Compute hyperspectral index using custom formula

Since R2023a

    Description

    Custom spectral indices can ease workflows where standard hyperspectral indices are not applicable. You can use a custom spectral index to define and compute an index specific to your application. You can also use a custom spectral index to fine-tune commonly used spectral indices to your specific hyperspectral data.

    indexImage = customSpectralIndex(hCube,wavelengths,func) computes a custom spectral index using the custom formula specified by the function handle func. The formula uses the specified wavelengths, wavelengths, in the hyperspectral data cube hCube.

    example

    indexImage = customSpectralIndex(hCube,wavelengths,func,BlockSize=blocksize) specifies the block size, blocksize, for block processing of the hyperspectral data cube. The function divides the input image into distinct blocks, processes each block, and then concatenates the processed output of the blocks to form the final output.

    Note

    This function requires the Hyperspectral Imaging Library for Image Processing Toolbox™. You can install the Hyperspectral Imaging Library for Image Processing Toolbox from Add-On Explorer. For more information about installing add-ons, see Get and Manage Add-Ons.

    The Hyperspectral Imaging Library for Image Processing Toolbox requires desktop MATLAB®, as MATLAB Online™ or MATLAB Mobile™ do not support the library.

    Examples

    collapse all

    Load a hyperspectral image into the workspace.

    hCube = hypercube("jasperRidge2_R198.img");  

    Colorize the hyperspectral image to get an RGB image.

    rgbImg = colorize(hCube,Method="rgb",ContrastStretching=true);

    You can use the wavelengths in the green band and the short-wave infrared (SWIR) band to estimate the position of a water body in the hyperspectral image. The range of wavelengths of the green band is 500–600 nm, while the range of wavelengths of the SWIR band is 1550–1750 nm. Find the wavelengths in the hyperspectral image closest to the midpoints of the green band and the SWIR band.

    AllWavelengths = hCube.Wavelength;
    GreenRange = [500 600];
    [~,GreenIdx] = min(abs(AllWavelengths-mean(GreenRange)));
    GreenWavelength = AllWavelengths(GreenIdx);
    SWIRRange = [1550 1750];
    [~,SWIRIdx] = min(abs(AllWavelengths-mean(SWIRRange)));
    SWIRWavelength = AllWavelengths(SWIRIdx);
    wavelengths = [GreenWavelength SWIRWavelength];

    Create a function handle for a custom spectral index formula to estimate a water body from the green band and SWIR band wavelengths.

    func = @(green,swir) (green-swir)./(green+swir);

    Apply the custom water estimation formula to the hyperspectral image to get the water body index image.

    waterImg = customSpectralIndex(hCube,wavelengths,func);

    Threshold the water body index image to get a water body mask. Index values greater than 0.5 indicate open water bodies.

    waterMask = waterImg > 0.5;

    Overlay the water body mask on the RGB image.

    overlayImg = imoverlay(rgbImg,waterMask,"b");

    Visualize the original RGB image, the water body index image, and the water body mask overlaid on the RGB image.

    figure
    t = tiledlayout(1,3);
    t.TileSpacing = "compact";
    t.Padding = "compact";
    nexttile
    imshow(rgbImg)
    title("RGB Image")
    nexttile
    imagesc(waterImg)
    colorbar
    axis image off
    title("Water Body Index Image")
    nexttile
    imagesc(overlayImg)
    axis image off
    title("Overlay Image")

    Figure contains 3 axes objects. Hidden axes object 1 with title RGB Image contains an object of type image. Hidden axes object 2 with title Water Body Index Image contains an object of type image. Hidden axes object 3 with title Overlay Image contains an object of type image.

    Input Arguments

    collapse all

    Hyperspectral data cube, specified as a hypercube object. The function computes the custom spectral index on the specified hyperspectral data cube.

    Wavelengths, specified as a numeric vector. Wavelengths must be unique, must be specified in nanometers, and must lie within the range of wavelengths of the hyperspectral data cube. The function computes the custom spectral index using the specified wavelengths in the hyperspectral data cube.

    Data Types: single | double

    Custom spectral index formula, specified as a function handle.

    Data Types: function_handle

    Size of blocks, specified as a two-element vector of positive integers. The first element specifies the number of rows in each block, and the second element specifies the number of columns. If the DataCube property of hCube is of size M-by-N-by-C, the size of the hyperspectral images is M-by-N, and C is the number of spectral channels. By default, the values of blocksize are the same as the size of the input hyperspectral images.

    The size of the blocks must be less than the size of the input hyperspectral images. Dividing the hyperspectral images into smaller blocks enables you to process large data sets without running out of memory.

    • If you reduce the blocksize value, then the function uses less memory at the cost of increased execution time.

    • If you increase the blocksize value , then the function requires less execution time at the cost of increased memory usage.

    Example: BlockSize=[20 20] specifies the size of each block as 20-by-20.

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

    Output Arguments

    collapse all

    Custom spectral index image, returned as a 2-D numeric matrix. If the DataCube property of hCube is of size M-by-N-by-C, the size of the indexImage is M-by-N.

    Data Types: single

    Version History

    Introduced in R2023a