Main Content

apply

Apply function to each block of spectral image

Since R2025a

    Description

    output = apply(spcube,fcn) applies the function fcn to each block of the hyperspectral or multispectral data cube spcube.

    output = apply(spcube,fcn,Name=Value) modifies aspects of the block processing using one or more name-value arguments. For example, DisplayWaitbar=true displays a wait bar while the function processes data.

    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.

    example

    Examples

    collapse all

    Read hyperspectral data into the workspace.

    hcube = imhypercube("paviaU.dat");

    Estimate and visualize the RGB image of the hyperspectral data.

    rgbImg = colorize(hcube,Method="rgb");
    figure
    imshow(rgbImg)

    Compute and visualize the NDVI index for the hyperspectral data using block processing.

    ndviImg = apply(hcube,@ndvi,BlockSize=[50 50]);
    figure
    imagesc(ndviImg)
    axis equal tight
    colorbar

    Download Landsat 8 multispectral data.

    zipfile = "LC08_L1TP_113082_20211206_20211215_02_T1.zip";
    landsat8Data_url = "https://ssd.mathworks.com/supportfiles/image/data/" + zipfile;
    hyper.internal.downloadLandsatDataset(landsat8Data_url,zipfile)
    filepath = fullfile("LC08_L1TP_113082_20211206_20211215_02_T1","LC08_L1TP_113082_20211206_20211215_02_T1_MTL.txt");

    Read a multispectral image into the workspace, and resample it to a uniform resolution.

    mcube = immulticube(filepath);
    mcube = resampleBands(mcube,30);

    Estimate and visualize the RGB image of the multispectral data.

    rgbImg = colorize(mcube);
    figure
    imshow(rgbImg)

    Compute and visualize the NDVI index for the multispectral data using block processing.

    ndviImg = apply(mcube,@ndvi,BlockSize=[50 50]);
    figure
    imagesc(ndviImg)
    axis equal tight
    colorbar

    Input Arguments

    collapse all

    Input spectral data, specified as a hypercube or multicube object.

    Processing function, specified as a function handle. For more information, see Create Function Handle. If spcube is a hypercube or multicube object, fcn must accept a hypercube or multicube object, respectively, as input. To pass additional arguments, specify fcn as an anonymous function. For more information, see Anonymous Functions. The function fcn typically returns the results for one block. The output of fcn can be numeric, categorical, a structure, or a hypercube or multicube object.

    Data Types: function_handle

    Name-Value Arguments

    collapse all

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Example: apply(spcube,fcn,DisplayWaitbar=true) displays a wait bar for long-running operations.

    Size of the data blocks, specified as a 2-element vector of positive integers. The first element specifies the number of rows and the second element specifies the number of columns in each data block. The number of rows and columns in each data block must be less than the number of rows and columns in the spectral image, respectively.

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

    Display a wait bar, specified as a numeric or logical 1 (true) or 0 (false). If you specify DisplayWaitbar as true, the function displays a wait bar for long-running operations.

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

    Output Arguments

    collapse all

    Processed output, returned as a numeric matrix, hypercube object, multicube object, or any other form in which the function fcn returns outputs.

    Version History

    Introduced in R2025a