Main Content


Normalized vegetation index



    output = ndvi(hcube) computes the normalized vegetation index (NDVI) value for each pixel in the data cube and returns an NDVI image. The NDVI image displays the vegetation cover regions of the input hyperspectral data. The function computes the NDVI value using the red (R) band and the near-infra red (NIR) band images in the data cube. The ndvi function uses the 670 nm and 800 nm band reflectance values for the red and NIR band images respectively.


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


    collapse all

    Read hyperspectral data into the workspace.

    hcube = hypercube('indian_pines.dat');

    Compute the NDVI value for each pixel in the data cube.

    ndviImg = ndvi(hcube);

    Estimate a contrast-stretched RGB image from the original data cube by using the colorize function.

    rgbImg = colorize(hcube,'Method','RGB','ContrastStretching',true);

    Display the original and the NDVI image.

    fig = figure('Position',[0 0 1200 600]);
    axes1 = axes('Parent',fig,'Position',[0 0.1 0.4 0.8]);
    title('RGB Image of Data Cube')
    axes2 = axes('Parent',fig,'Position',[0.45 0.1 0.4 0.8]);
    title('NDVI Image')

    Vegetation regions typically have NDVI values from 0.2 and 0.8. NDVI values less than or equal to 0.2 indicate the absence of vegetation. Perform thresholding of NDVI image to segment the vegetation regions. Specify the threshold value.

    threshold = 0.2;

    Generate a binary image with a intensity value 1 for pixels with a score greater than or equal to the specified threshold. All other pixels have a value 0. The regions in the binary image with a value of 1 correspond to the vegetation regions in the data cube with NDVI values greater than the threshold.

    bw = ndviImg > threshold;

    Overlay the binary image on to the RGB image and display the overlaid image.

    overlayImg = imoverlay(rgbImg,bw,[0 1 0]);
    title('Vegetation Region Overlaid on RGB Image')

    Compute the vegetation cover based on the total number of pixels in a spectral band and the number of pixels with an NDVI value greater than 0.2.

    numVeg = find(bw == 1);
    imgSize = size(hcube.DataCube,1)*size(hcube.DataCube,2);
    vegetationCover = length(numVeg)/imgSize
    vegetationCover = 0.5696

    Input Arguments

    collapse all

    Input hyperspectral data, specified as a hypercube object. The functions reads the hyperspectral data cube from the DataCube property of the object and then computes the NDVI value of each pixel.

    Output Arguments

    collapse all

    Output NDVI image, returned as a matrix of size M-by-N. M and N are spatial dimensions of the input data cube. If the data type of the input data cube is double, the output data type is also double. Otherwise, the output data type is single.

    The function computes the NDVI value for each pixel as


    and the values are in the range [-1, 1]. A value close to 1 indicates healthy vegetation, 0 indicates unhealthy vegetation, and -1 indicates no vegetation.

    Data Types: single | double


    [1] Haboudane, D. “Hyperspectral Vegetation Indices and Novel Algorithms for Predicting Green LAI of Crop Canopies: Modeling and Validation in the Context of Precision Agriculture.” Remote Sensing of Environment 90, no. 3 (April 15, 2004): 337–52.

    Introduced in R2020a