Main Content

spectralMatch

Identify unknown regions or materials using spectral library

    Description

    example

    score = spectralMatch(libData,hcube) identifies regions in a hyperspectral data cube by matching spectra signature of each pixel to the spectral data read from the ECOSTRESS spectral library libData.

    example

    score = spectralMatch(libData,reflectance,wavelength) identifies a region or material by matching its spectral reflectance values, specified as reflectance and wavelength, with the values available in the ECOSTRESS spectral library libData.

    example

    score = spectralMatch(___,Name,Value) specifies options using one or more name-value pair arguments in addition to any combination of input arguments in previous syntaxes.

    Note

    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.

    Examples

    collapse all

    The spectral matching method compares the spectral signature of each pixel in the hyperspectral data cube with a reference spectral signature for vegetation from an ECOSTRESS spectrum file.

    Read the spectral signature of vegetation from the ECOSTRESS spectral library.

    fileroot = matlabshared.supportpkg.getSupportPackageRoot();
    filename = fullfile(fileroot,'toolbox','images','supportpackages','hyperspectral','hyperdata',...
                   'ECOSTRESSSpectraFiles','vegetation.tree.tsuga.canadensis.vswir.tsca-1-47.ucsb.asd.spectrum.txt');
    libData = readEcostressSig(filename);

    Read the hyperspectral data into the workspace.

    hcube = hypercube('paviaU.hdr');

    Compute the distance scores of the spectra of the hyperspectral data pixels with respect to the reference spectra.

    score = spectralMatch(libData,hcube);

    Display the distance scores. The pixels with low distance scores are stronger matches to the reference spectra and are more likely to belong to the vegetation region.

    figure
    imagesc(score)
    colorbar

    Define a threshold for detecting distance scores that correspond to the vegetation region.

    threshold = 0.3;

    Generate a binary image by assigning a intensity value 1 for pixels with score less than a specified threshold. Other regions are assigned the intensity value 0. The maximum intensity regions in the binary image correspond to the vegetation regions in the hyperspectral data cube.

    bw = score < threshold;

    Segment the vegetation regions of the hyperspectral data cube by using the indices of the maximum intensity regions in the binary image.

    T = reshape(hcube.DataCube,[size(hcube.DataCube,1)*size(hcube.DataCube,2) size(hcube.DataCube,3)]);
    Ts = zeros(size(T));
    Ts(bw == 1,:) = T( bw==1 ,:);
    Ts = reshape(Ts,[size(hcube.DataCube,1) size(hcube.DataCube,2) size(hcube.DataCube,3)]);

    . Create a new hypercube object that contains only the segmented vegetation regions.

    segmentedDataCube = hypercube(Ts,hcube.Wavelength);

    Estimate the RGB colour image of the original data cube and the segmented data cube by using the colorize function.

    rgbImg = colorize(hcube,'Method','rgb','ContrastStretching',true);
    segmentedImg = colorize(segmentedDataCube,'Method','rgb','ContrastStretching',true);

    Overlay the binary image on the RGB version of the original data cube by using the imoverlay function.

    B = imoverlay(rgbImg,bw,'Yellow');

    Display the RGB colour images of the original data cube and the segmented data cube along with the overlaid image. The segmented image contains only the vegetation regions that are segmented from the original data cube.

    figure
    montage({rgbImg segmentedImg B},'Size',[1 3])
    title(['Original Image | ' 'Segmented Image | ' 'Overlayed Image'])

    Read reference spectral signatures from the ECOSTRESS spectral library. The library consists of 15 spectral signatures belonging to manmade materials, soil, water, and vegetation. The output is a structure array that stores the spectral data read from ECOSTRESS library files.

    fileroot = matlabshared.supportpkg.getSupportPackageRoot();
    dirname = fullfile(fileroot,'toolbox','images','supportpackages','hyperspectral','hyperdata','ECOSTRESSSpectraFiles');
    libData = readEcostressSig(dirname);

    Load a .mat file that contains the reflectance and the wavelength values of an unknown material into the workspace. The reflectance and the wavelength values together comprise the test spectra.

    load spectralData 'reflectance' 'wavelength'

    Compute the spectral match between the reference spectra and test spectra using spectral information divergence (SID) method. The function computes the distance score for only those reference spectra that have bandwidth overlap with the test spectra. The function displays a warning message for all other spectra.

    score = spectralMatch(libData,reflectance,wavelength,'Method','SID');
    Warning: Unable to find overlapping wavelengths between test spectra and library signature number 8
    
    Warning: Unable to find overlapping wavelengths between test spectra and library signature number 9
    
    Warning: Unable to find overlapping wavelengths between test spectra and library signature number 11
    

    Display the distance scores of the test spectra. The pixels with lower distance scores are stronger matches to the reference spectra. A distance score value of NaN indicates that the corresponding reference spectra and the test spectra do not meet the overlap bandwidth threshold.

    score
    score = 1×15
    
      297.8016  122.5567  203.5864  103.3351  288.7747  275.5321  294.2341       NaN       NaN  290.4887       NaN  299.5762  171.6919   46.2072  176.6637
    
    

    Find the minimum distance score and the corresponding index. 'The returned index value indicates the row of the structure array libData that contains the reference spectrum that most closely matches a test spectrum.

    [value,ind] = min(score);

    Find the matching reference spectrum by using the index of the minimum distance score, and display the details of the matching spectral data in the ECOSTRESS library. The result shows that the test spectra match most closely with the spectral signature of sea water.

    matchingSpectra = libData(ind)
    matchingSpectra = struct with fields:
                         Name: "Sea Foam"
                         Type: "Water"
                        Class: "Sea Water"
                     SubClass: "none"
                 ParticleSize: "Liquid"
                        Genus: [0×0 string]
                      Species: [0×0 string]
                     SampleNo: "seafoam"
                        Owner: "Dept. of Earth and Planetary Science, John Hopkins University"
              WavelengthRange: "TIR"
                       Origin: "JHU IR Spectroscopy Lab."
               CollectionDate: "N/A"
                  Description: "Sea foam water. Original filename FOAM Original ASTER Spectral Library name was jhu.becknic.water.sea.none.liquid.seafoam.spectrum.txt"
                  Measurement: "Directional (10 Degree) Hemispherical Reflectance"
                  FirstColumn: "X"
                 SecondColumn: "Y"
               WavelengthUnit: "micrometer"
                     DataUnit: "Reflectance (percent)"
                  FirstXValue: "14.0112"
                   LastXValue: "2.0795"
              NumberOfXValues: "2110"
        AdditionalInformation: "none"
                   Wavelength: [2110×1 double]
                  Reflectance: [2110×1 double]
    
    

    Plot the reflectance values of the test spectra and the corresponding reference spectra. For the purpose of plotting and visualizing the shape of the reflectance curves, rescale the reflectance values to the range [0, 1] and interpolate test reflectance values to match the reference reflectance values in number.

    figure
    testReflectance = rescale(reflectance,0,1);
    refReflectance = rescale(matchingSpectra.Reflectance,0,1);
    testLength = length(testReflectance);
    newLength = length(testReflectance)/length(refReflectance);
    testReflectance = interp1(1:testLength,testReflectance,1:newLength:testLength);
    
    plot(refReflectance)
    hold on
    plot(testReflectance,'r')
    hold off
    legend('Matching reference reflectance','Test reflectance')
    xlabel('Number of samples')
    ylabel('Reflectance value')

    Input Arguments

    collapse all

    Spectral data from ECOSTRESS files, returned as a 1-by-K structure array. K is the number of spectrum files read by the function. Each element of the structure array has 24 fields that contain the header information of the spectrum files.

    Field NamesDescription
    NameName of the measured sample or material
    TypeType of sample, such as "mineral", "rock", "tree", or "manmade"
    Class

    Class of the sample type

    For example, if the sample type is "mineral" then the class can be: "native elements","silicates", "oxides", "sulfides", "sulfates", "halides", "carbonates", "phosphates", or "mineraloids".

    SubClass

    Subclass of the sample type

    This field contains an empty array or "none", unless the Type value is "mineral", "rock", "manmade", "soil", "lunar", or "meteorite".

    ParticleSize

    Particle size of the sample type

    This field contains an empty array unless the Type value is "mineral", "rock", "manmade", "soil", "lunar", or "meteorite".

    Genus

    Genus of the sample

    This field contains an empty array unless the Type value is "vegetation" or "nonphotosynthetic".

    Species

    Species of the sample

    This field contains an empty array unless the Type value is "vegetation" or "nonphotosynthetic".

    SampleNo

    Sample number

    This value is an identifier for the associated sample.

    OwnerOwner of the sample
    WavelengthRange

    Wavelength range of the measured sample

    The value must be "All", "TIR", or "VSWIR".

    OriginLocation from which the data was obtained
    CollectionDate

    Date on which the sample was collected

    This value is in mm/dd/yy format.

    Description

    Description of the measured sample

    This field provides additional information about the characteristics of the sample.

    MeasurementSpectral measurement mode used to measure the sample
    FirstColumnFirst column of data values in the spectrum file
    SecondColumnSecond column of data values in the spectrum file
    WavelengthUnit

    Measuring unit for the spectral wavelengths of the samples

    The value for every sample type is "micrometer". This field corresponds to the X Units field of the header data in the ECOSTRESS spectrum file.

    DataUnit

    Unit of the spectral measurement mode

    Spectral measurement mode includes reflectance, transmittance, transittance, and transmission. The unit is percentage. This field corresponds to the Y Units field of the header data in the ECOSTRESS spectrum file.

    FirstXValueFirst value in the first column of data values in the spectrum file
    LastXValueLast value in the first column of data values in the spectrum file
    NumberofXValuesTotal number of data values in the first column of the spectrum file
    AdditionalInformation

    Additional information about the sample

    This field includes information that is not part of the spectral data.

    WavelengthWavelength values at which the reflectances were measured
    ReflectanceReflectance values measured at each wavelengths

    Input hyperspectral data, specified as a hypercube object. The DataCube property of the hypercube object contains the hyperspectral datacube.

    Reflectance values, specified as a C-element vector. C is the number of wavelengths for which the reflectance values have been measured.

    Wavelength values, specified as a C-element vector. C is the number of wavelengths for which the reflectance values have been measured.

    Name-Value Pair Arguments

    Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

    Example: spectralMatch(libData,hcube,'MinBandWidth',0.5)

    Spectral matching method, specified as the comma-separated pair consisting of 'Method' and one of these values:

    • 'sam' — Spectral angle mapper (SAM) method, which measures the similarity between two spectra by computing the angular distance between them.

    • 'sid' — Spectral information divergence (SID) method, which measures the similarity between two spectra by computing the difference between their probability distribution values.

    Data Types: char | string

    Minimum overlap bandwidth, specified as the comma-separated pair consisting of 'MinBandWidth' and a positive scalar in nanometers. The overlap bandwidth between the reference spectrum and the test spectra is defined as:

    BWoverlap = WmaxWmin

    Wmin is the maximum of minimum wavelengths in the reference and test spectra.

    Wmax is the maximum of maximum wavelengths in the reference and test spectra.

    The 'MinBandWidth' argument defines the minimum expected value for the overlap bandwidth between the spectral values of the test material and the ECOSTRESS spectral data.

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

    Output Arguments

    collapse all

    Distance scores, returned as a 3-D numeric array, matrix, K-element column vector, or scalar. The dimensions of the output score depend on the dimensions of the libData and whether the test data is a hypercube object or a wavelength and reflectance pair.

    If the test spectral signatures are specified as a hypercube object, hcube and the data cube is of size M-by-N-by-C:

    Dimension of input argument, libDataDimension of output, score
    1-by-K, containing K reference signatures read from K number of spectrum files

    3-D numeric array of size M-by-N-by-K containing the distance score for each pixel with respect to K reference signatures

    The values in each channel of K are the distance scores of the spectra of each pixel with respect to the spectral data in the corresponding row of libData. Similarly, the values in the second channel relate to the spectral data in the second row of libData.

    1-by-1, containing reference signature read from one spectrum file (K = 1)matrix of size M-by-N, The matrix contains the distance score for each pixel's spectra with respect to a reference signature.

    If the test spectral signature is specified as reflectance and wavelength values:

    Dimension of input argument, libDataDimension of output, score
    1-by-K, containing K reference signatures read from K number of spectrum filesK-element vector containing the distance score of the test spectra with respect to K reference signatures. Each element of the vector is the distance score of the test reflectance values with respect to the spectral data in the corresponding row of libData.
    1-by-1, containing reference signature read from one spectrum file (K = 1)scalar

    Data Types: double

    Introduced in R2020a