Main Content

cropData

Crop regions-of-interest of spectral image

    Description

    newspcube = cropData(spcube,row,column) crops the regions of interest (ROIs), specified by row and column, across all the spectral bands in the hyperspectral or multispectral data spcube. The function returns the cropped data as a new hypercube or multicube object newspcube.

    example

    newspcube = cropData(spcube,row,column,band) crops the ROIs across the specified spectral bands band when spcube is a hypercube object.

    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

    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);

    Specify the row and column indices of the ROI to crop from the extracted bands.

    row = 4000:6000;
    column = 5000:7000;

    Crop the ROI.

    newmcube = cropData(mcube,row,column);

    Display both bands in the original and the cropped versions of a spectral band.

    fig = figure(Position=[0 0 800 500]);
    axes1 = axes(Parent=fig,Position=[0.05 0.05 0.45 0.8]);
    datacube = gather(mcube);
    imagesc(datacube(:,:,5),Parent=axes1)
    title("Original Data")
    axes2 = axes(Parent=fig,Position=[0.55 0.05 0.45 0.8]);
    newdatacube = gather(newmcube);
    imagesc(newdatacube(:,:,5),Parent=axes2)
    title("Cropped Data")   
    colormap gray

    Read hyperspectral data from an ENVI format file.

    hcube = imhypercube("paviaU.dat");

    Crop the first 10 spectral bands of the input data cube.

    newhcube = cropData(hcube,":",":",1:10);

    Specify the row and column indices of the ROI to crop from the extracted bands.

    row = 130:250;
    column = 60:200;

    Crop the ROI.

    newhcube = cropData(newhcube,row,column,":");

    Display both bands in the original and the cropped versions of a spectral band.

    fig = figure(Position=[0 0 800 500]);
    axes1 = axes(Parent=fig,Position=[0.05 0.05 0.45 0.8]);
    datacube = gather(hcube);
    imagesc(datacube(:,:,5),Parent=axes1)
    title("Original Data")
    axes2 = axes(Parent=fig,Position=[0.55 0.05 0.45 0.8]);
    newdatacube = gather(newhcube);
    imagesc(newdatacube(:,:,5),Parent=axes2)
    title("Cropped Data")   
    colormap gray

    Figure contains 2 axes objects. Axes object 1 with title Original Data contains an object of type image. Axes object 2 with title Cropped Data 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.

    Row indices of the data cube, specified as a positive integer or a vector of positive integers.

    To select a particular row or rows to crop, specify the row index as a positive integer or vector of positive integers respectively. If the data cube is of size M-by-N-by-C, the specified row index values must all be less than or equal to M. To specify a range of row indices, or indices at a regular interval, use the colon operator. For example, row = 1:10.

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

    Column indices of the data cube, specified as a positive integer or a vector of positive integers.

    To select a particular column or columns to crop, specify the column index as a positive integer or vector of positive integers respectively. If the data cube is of size M-by-N-by-C, the specified column index values must all be less than or equal to N. To specify a range of column indices, or indices at a regular interval, use the colon operator. For example, column = 1:10.

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

    Spectral band numbers, specified as a positive integer or a vector of positive integers.

    To select a particular band or bands to crop, specify the band number as a positive integer or vector of positive integers respectively. If the data cube is of size M-by-N-by-C, the specified band number values must all be less than or equal to C. To specify a range of band numbers or numbers at a regular interval, use the colon operator. For example, band = 1:10.

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

    Output Arguments

    collapse all

    Output spectral data, returned as a hypercube or multicube object.

    Version History

    Introduced in R2020a