spectralMatch
Syntax
Description
identifies a region or material by matching its spectral reflectance values, specified as
score
= spectralMatch(libData
,reflectance
,wavelength
)reflectance
and wavelength
, with the values
available in the ECOSTRESS spectral library libData
.
specifies options using one or more namevalue pair arguments in addition to any combination
of input arguments in previous syntaxes.score
= spectralMatch(___,Name,Value
)
Note
This function requires the Image Processing Toolbox™ Hyperspectral Imaging Library. You can install the Image Processing Toolbox Hyperspectral Imaging Library from AddOn Explorer. For more information about installing addons, see Get and Manage AddOns.
The Image Processing Toolbox Hyperspectral Imaging Library requires desktop MATLAB^{®}, as MATLAB Online™ or MATLAB Mobile™ do not support the library.
Examples
Segment Vegetation Regions in Hyperspectral Data Using Spectral Matching
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.tsca147.ucsb.asd.spectrum.txt'); libData = readEcostressSig(filename);
Read the hyperspectral data into the workspace.
hcube = hypercube('paviaU.hdr');
Compute the distance scores of the spectrum of the hyperspectral data pixels with respect to the reference spectrum.
score = spectralMatch(libData,hcube);
Display the distance scores. The pixels with low distance scores are stronger matches to the reference spectrum 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'])
Identify Unknown Spectral Signature Using Spectral Matching
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 spectrum.
load spectralData 'reflectance' 'wavelength'
Compute the spectral match between the reference spectrum and test spectrum using spectral information divergence (SID) method. The function computes the distance score for only those reference spectra that have bandwidth overlap with the test spectrum. 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 spectrum. The pixels with lower distance scores are stronger matches to the reference spectrum. A distance score value of NaN
indicates that the corresponding reference spectrum and the test spectrum 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 spectrum 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 spectrum and the corresponding reference spectrum. 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
libData
— ECOSTRESS Spectral data
structure
Spectral data from ECOSTRESS files, returned as a 1byK 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 Names  Description 
Name  Name of the measured sample or material 
Type  Type of sample, such as "mineral" ,
"rock" , "tree" , or
"manmade" 
Class  Class of the sample type For example, if the sample
type is 
SubClass  Subclass of the sample type This field contains an
empty array or 
ParticleSize  Particle size of the sample type This field contains an
empty array unless the 
Genus  Genus of the sample This field contains an empty array
unless the 
Species  Species of the sample This field contains an empty
array unless the 
SampleNo  Sample number This value is an identifier for the associated sample. 
Owner  Owner of the sample 
WavelengthRange  Wavelength range of the measured sample The value must
be 
Origin  Location from which the data was obtained 
CollectionDate  Date on which the sample was collected This value is in

Description  Description of the measured sample This field provides additional information about the characteristics of the sample. 
Measurement  Spectral measurement mode used to measure the sample 
FirstColumn  First column of data values in the spectrum file 
SecondColumn  Second 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

DataUnit  Unit of the spectral measurement mode Spectral
measurement mode includes reflectance, transmittance, and transmission. The
unit is percentage. This field corresponds to the 
FirstXValue  First value in the first column of data values in the spectrum file 
LastXValue  Last value in the first column of data values in the spectrum file 
NumberofXValues  Total 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. 
Wavelength  Wavelength values at which the reflectances were measured 
Reflectance  Reflectance values measured at each wavelengths 
hcube
— Input hyperspectral data
hypercube
object
Input hyperspectral data, specified as a hypercube
object. The
DataCube
property of the hypercube
object
contains the hyperspectral datacube.
reflectance
— Reflectance values
Celement vector
Reflectance values, specified as a Celement vector. C is the number of wavelengths for which the reflectance values have been measured.
wavelength
— Wavelength values
Celement vector
Wavelength values, specified as a Celement vector. C is the number of wavelengths for which the reflectance values have been measured.
NameValue Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Namevalue arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: spectralMatch(libData,hcube,'MinBandWidth',0.5)
Method
— Spectral matching method
'sam'
(default)  'sid'
 'sidsam'
 'jmsam'
 'ns3'
Spectral matching method, specified as the commaseparated 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.'sidsam'
— Mixed spectral similarity method, which measures the similarity between two spectra by combining the SID and SAM distance measures.'jmsam'
— Jeffries MatusitaSpectral Angle Mapper (JMSAM), which measures the similarity between two spectra by combining the Jeffries Matusita (JM) and SAM distance measures.'ns3'
— Normalized spectral similarity score (NS3) method, which measures the similarity between two spectra by combining the Euclidean and SAM distance measures.
For details about these spectral matching methods, see More About.
Data Types: char
 string
MinBandWidth
— Minimum overlap bandwidth
300
(default)  positive scalar
Minimum overlap bandwidth, specified as the commaseparated pair consisting of
'MinBandWidth'
and a positive scalar in nanometers. The overlap
bandwidth between the reference spectrum and the test spectra is defined as:
BW_{overlap} = W_{max} − W_{min}
W_{min} is the maximum of minimum wavelengths in the reference and test spectra.
W_{max} 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
score
— Distance scores
3D numeric array  matrix  Kelement column vector  scalar
Distance scores, returned as a 3D numeric array, matrix,
Kelement 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
MbyNbyC:
Dimension of input argument,
libData  Dimension of output,
score 
1byK, containing K reference signatures read from K number of spectrum files  3D numeric array of size MbyNbyK 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

1by1, containing reference signature read from one spectrum file (K = 1)  matrix of size MbyN, 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,
libData  Dimension of output,
score 
1byK, containing K reference signatures read from K number of spectrum files  Kelement 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 . 
1by1, containing reference signature read from one spectrum file (K = 1)  scalar 
Data Types: double
More About
Spectral Angle Mapper (SAM)
Given the test spectra t and a reference spectra r of length C, the SAM score α is calculated as
$$\alpha =\text{\hspace{0.17em}}{\mathrm{cos}}^{1}\left(\frac{{\displaystyle \sum _{i=1}^{C}{t}_{i}{r}_{i}}}{\sqrt{{\displaystyle \sum _{i=1}^{C}{t}_{i}^{2}}}\sqrt{{\displaystyle \sum _{i=1}^{C}{r}_{i}^{2}}}}\right).$$
Spectral Information Divergence (SID)
The spectral information divergence (SID) method computes spectral similarity based on the divergence between the probability distributions of the two spectra. Let r and t be the reference and test spectra respectively. Calculate the distribution values for the reference spectra as:
$${q}_{i}=\frac{{r}_{i}}{{\displaystyle \sum _{i=1}^{C}{r}_{i}}}.$$
.
Calculate the distribution values for the test spectra as:
$${p}_{i}=\frac{{t}_{i}}{{\displaystyle \sum _{i=1}^{C}{t}_{i}}}.$$
.
Then, compute the SID value by using the probability distributions of the reference and the test spectra:
$$\text{\hspace{0.17em}}\text{\hspace{0.17em}}SID={\displaystyle \sum _{i=1}^{C}{p}_{i}\mathrm{log}\left(\frac{{p}_{i}}{{q}_{i}}\right)+}{\displaystyle \sum _{i=1}^{C}{q}_{i}\mathrm{log}\left(\frac{{q}_{i}}{{p}_{i}}\right)}.$$
SIDSAM
The SIDSAM method computes spectral similarity as:
$$SIDSAM\text{\hspace{0.17em}}=\text{\hspace{0.17em}}SID\times \mathrm{tan}\left(\alpha \right)$$
Jeffries MatusitaSpectral Angle Mapper (JMSAM)
The JMSAM method computes spectral similarity based on the Jeffries Matusita (JM) and SAM distances between two spectra. Let r and t be the reference and test spectra respectively.
First, compute the JM distance,
$$J{M}_{\text{distance}}=2\left(1{e}^{B}\right)$$
where B is the Bhattacharyya distance,
$$B=\frac{1}{8}{\left({\mu}_{t}{\mu}_{r}\right)}^{T}{\left[\frac{{\sigma}_{t}+{\sigma}_{r}}{2}\right]}^{1}\left({\mu}_{t}{\mu}_{r}\right)\text{\hspace{0.17em}}+\text{\hspace{0.17em}}\frac{1}{2}\mathrm{ln}\left[\frac{\left\frac{{\sigma}_{t}+{\sigma}_{r}}{2}\right}{\sqrt{\left{\sigma}_{t}\right\left{\sigma}_{r}\right}}\right]$$
μ_{r} and μ_{t} are the mean values of the reference and test spectra respectively. σ_{r} and σ_{t} are the covariance values of the reference and test spectra respectively.
Then, compute the SAM value α by using the test spectra t and the reference spectra r of length C,
$$\alpha =\text{\hspace{0.17em}}{\mathrm{cos}}^{1}\left(\frac{{\displaystyle \sum _{i=1}^{C}{t}_{i}{r}_{i}}}{\sqrt{{\displaystyle \sum _{i=1}^{C}{t}_{i}^{2}}}\sqrt{{\displaystyle \sum _{i=1}^{C}{r}_{i}^{2}}}}\right).$$
Finally, compute the JMSAM score as:
$$JMSAM\text{\hspace{0.17em}}=\text{\hspace{0.17em}}\text{\hspace{0.17em}}J{M}_{distance}\times \mathrm{tan}\left(\alpha \right)\text{\hspace{0.17em}}$$
Normalized Spectral Similarity Score (NS3)
The NS3 method computes spectral similarity based on the Euclidean and SAM distances between two spectra. Let r and t be the reference and test spectra respectively. Compute the Euclidean distance between two spectra as:
$${A}_{Euclidean}=\text{\hspace{0.17em}}\sqrt{\frac{1}{C}{\displaystyle \sum _{i=1}^{C}{\left({t}_{i}{r}_{i}\right)}^{2}}}$$
Then, compute the SAM value α
$$\alpha =\text{\hspace{0.17em}}{\mathrm{cos}}^{1}\left(\frac{{\displaystyle \sum _{i=1}^{C}{t}_{i}{r}_{i}}}{\sqrt{{\displaystyle \sum _{i=1}^{C}{t}_{i}^{2}}}\sqrt{{\displaystyle \sum _{i=1}^{C}{r}_{i}^{2}}}}\right).$$
Finally, compute the NS3 score as:
$$NS3\text{\hspace{0.17em}}=\text{\hspace{0.17em}}\sqrt{{A}_{Euclidean}{}^{2}+{\left(1\mathrm{cos}\left(\alpha \right)\right)}^{2}}$$
Version History
Introduced in R2020a
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
 América Latina (Español)
 Canada (English)
 United States (English)
Europe
 Belgium (English)
 Denmark (English)
 Deutschland (Deutsch)
 España (Español)
 Finland (English)
 France (Français)
 Ireland (English)
 Italia (Italiano)
 Luxembourg (English)
 Netherlands (English)
 Norway (English)
 Österreich (Deutsch)
 Portugal (English)
 Sweden (English)
 Switzerland
 United Kingdom (English)