Measure spectral similarity using spectral angle mapper
measures the spectral similarity between the spectra of each pixel in the hyperspectral data
score = sam(
inputData and the specified reference spectra
refSpectra by using the spectral angle mapper (SAM) classification
algorithm. Use this syntax to identify different regions or materials in a hyperspectral
measures the spectral similarity between the specified test spectra
score = sam(
testSpectra and reference spectra
using the SAM classification algorithm. Use this syntax to compare the spectral signature of
an unknown material against the reference spectra or to compute spectral variability between
two spectral signatures.
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.
Distinguish different regions in a hyperspectral data cube by computing the spectral angle distance between each pixel and the endmember spectra of the data cube.
Read hyperspectral data into the workspace.
hcube = hypercube('jasperRidge2_R198.hdr');
Identify the number of spectrally distinct bands in the data cube by using the
numEndmembers = countEndmembersHFC(hcube)
numEndmembers = 14
Extract the endmember spectral signatures from the data cube by using the NFINDR algorithm.
endmembers = nfindr(hcube,numEndmembers);
Plot the spectral signatures of the endmembers. The result shows the 14 spectrally distinct regions in the data cube.
figure plot(endmembers) legend('Location','Bestoutside')
Compute the spectral angular distance between each endmember and the spectra of each pixel in the data cube.
score = zeros(size(hcube.DataCube,1),size(hcube.DataCube,2),numEndmembers); for i = 1:numEndmembers score(:,:,i) = sam(hcube,endmembers(:,i)); end
Compute the minimum score value from the distance scores obtained for each pixel spectra with respect to all the endmembers. The index of each minimum score identifies the endmember spectra to which a pixel spectra exhibits maximum similarity. An index value, n, at the spatial location (x, y) in the score matrix indicates that the spectral signature of the pixel at spatial location (x, y) in the data cube best matches the spectral signature of the nth endmember.
[~,matchingIndx] = min(score,,3);
Estimate an RGB image of the hyperspectral data cube by using the
colorize function. Display both the RGB image and the matrix of matched index values.
rgbImg = colorize(hcube,'Method','RGB'); figure('Position',[0 0 1100 500]) subplot('Position',[0 0.15 0.4 0.8]) imagesc(rgbImg) axis off title('RGB Image of Hyperspectral Data') subplot('Position',[0.45 0.15 0.4 0.8]) imagesc(matchingIndx) axis off title('Indices of Matching Endmembers') colorbar
Read hyperspectral data into the workspace.
hcube = hypercube('indian_pines.dat');
Find ten endmembers of the hyperspectral data.
numEndmembers = 10; endmembers = nfindr(hcube,numEndmembers);
Consider the first endmember as the reference spectra and the rest of the endmembers as the test spectra. Compute the SAM score between the reference and test spectra.
score = zeros(1,numEndmembers-1); refSpectra = endmembers(:,1); for i = 2:numEndmembers testSpectra = endmembers(:,i); score(i-1) = sam(testSpectra,refSpectra); end
Find the test spectra that exhibit maximum similarity (minimum distance) to the reference spectra. Then find the test spectra that exhibit minimum similarity (maximum distance) to the reference spectra.
[minval,minidx] = min(score); maxMatch = endmembers(:,minidx); [maxval,maxidx] = max(score); minMatch = endmembers(:,maxidx);
Plot the reference spectra, the maximum similarity and the minimum similarity test spectra. The test spectra with the minimum score value indicates highest similarity to the reference endmember. On the other hand, the test spectra with the maximum score value has the highest spectral variability and characterises the spectral behaviour of two different materials.
figure plot(refSpectra) hold on plot(maxMatch,'k') plot(minMatch,'r') xlabel('Band Number') ylabel('Data Values') legend('Reference spectra','Minimum match test spectra','Maximum match test spectra',... 'Location','Southoutside') title('Similarity Between Spectra') annotation('textarrow',[0.25 0.25],[0.4 0.5],'String',['Max score: ' num2str(maxval)]) annotation('textarrow',[0.6 0.55],[0.6 0.45],'String',['Min score: ' num2str(minval)])
inputData— Input hyperspectral data
hypercubeobject | 3-D numeric array
Input hyperspectral data, specified as a
object or a 3-D numeric array containing the data cube. If the input is a
hypercube object, the data is read from the
DataCube property of the object.
testSpectra— Test spectra
Test spectra, specified as a C-element vector. The test spectra is the spectral signature of an unknown region or material.
refSpectra— Reference spectra
Reference spectra, specified as a C-element vector. The reference spectra is the spectral signature of a known region or material. The function matches the test spectra against these values.
score— SAM score
SAM score, returned as a scalar or matrix. The output is a
scalar — If you specify the
testSpectra input argument.
The function matches the test spectral signature against the reference spectral
signature and returns a scalar value. Both the test and the reference spectra must
be vectors of same length.
matrix — If you specify the
inputData input argument. The
function matches the spectral signature of each pixel in the data cube against the
reference spectral signature and returns a matrix. If the data cube is of size
M-by-N-by-C and the
reference spectra is a vector of length C, the output matrix is
of size M-by-N.
Each element of the SAM score is a spectral angle in radians in the range [0, 3.142]. A smaller SAM score indicates a strong match between the test signature and the reference signature.
Given the test spectra t and a reference spectra r of length C, the SAM score α is calculated as
 Kruse, F.A., A.B. Lefkoff, J.W. Boardman, K.B. Heidebrecht, A.T. Shapiro, P.J. Barloon, and A.F.H. Goetz. “The Spectral Image Processing System (SIPS)—Interactive Visualization and Analysis of Imaging Spectrometer Data.” Remote Sensing of Environment 44, no. 2–3 (May 1993): 145–63. https://doi.org/10.1016/0034-4257(93)90013-N.