Main Content

customSpectralIndex

Compute spectral index using custom formula

Since R2023a

    Description

    Custom spectral indices can ease workflows where standard spectral 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 or multispectral data.

    indexImage = customSpectralIndex(spcube,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 or multispectral data spcube.

    example

    indexImage = customSpectralIndex(spcube,wavelengths,func,BlockSize=blocksize) specifies the block size, blocksize, for block processing of the spectral data.

    The function divides the input image into distinct blocks, processes each block, and then concatenates the processed output of each block to form the output matrix. Spectral images are multi-dimensional data sets that can be too large to fit in system memory in their entirety. This can cause the system to run out of memory while running the customSpectralIndex function. If you encounter such an issue, perform block processing by using this syntax.

    For example, customSpectralIndex(spcube,wavelengths,func,BlockSize=[50 50]) divides the input image into non-overlapping blocks of size 50-by-50 and then computes the custom spectral index for pixels in each block.

    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™ and MATLAB Mobile™ do not support the library.

    Examples

    collapse all

    Load a hyperspectral image into the workspace.

    hcube = imhypercube("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

    Input spectral data, specified as a hypercube or multicube object. If spcube is a multicube object, all its spectral bands must have the same data resolution. If all spectral bands of the multicube object do not have the same resolution, resample the bands using the resampleBands function, or select bands with uniform resolution using the selectBands function.

    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 spectral data. The function computes the custom spectral index using the specified wavelengths in the spectral data.

    Data Types: single | double

    Custom spectral index formula, specified as a function handle.

    Data Types: function_handle

    Size of the data blocks, specified as a 2-element vector of positive integers. The elements of the vector correspond to the number of rows and columns in each block, respectively. The size of the data blocks must be less than the size of the input image. Dividing the spectral images into smaller blocks enables you process large data sets without running out of memory.

    • If the blocksize value is too small, the memory usage of the function reduces at the cost of increased execution time.

    • If the blocksize value is large or equal to the input image size, the execution time reduces at the cost of increased memory usage.

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

    Output Arguments

    collapse all

    Custom spectral index image, returned as a 2-D numeric matrix. If the spectral data cube 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

    expand all