%% Extract Endmembers and Abundance Maps for Spectral Unmixing
% Read hyperspectral data into the workspace.

hcube = hypercube('jasperRidge2_R198.hdr');

% Extract six endmembers from the hyperspectral data.

endmembers = nfindr(hcube,6);

% Estimate the abundance map for each endmember using the fully constrained least squares method.

abundanceMap = estimateAbundanceLS(hcube.DataCube,endmembers,'Method','fcls');

% Estimate an RGB image of the data cube using the colorize function.

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

% Display the RGB image.

figure
imagesc(rgbImg)
title('RGB Image of Data Cube')

%
% Display the abundance maps for the endmember spectra. The order of the abundance maps is the same as the order of the endmembers in the endmembers input argument.

figure
montage(abundanceMap(:,:,1:3),'Size',[1 3],'BorderSize',[20 20])
colormap default
colorbar
title('Abundance Map for Endmember 1 | Abundance Map for Endmember 2 | Abundance Map for Endmember 3','FontSize',14)
figure
montage(abundanceMap(:,:,4:6),'Size',[1 3],'BorderSize',[20 20])
colormap default
colorbar
title('Abundance Map for Endmember 4 | Abundance Map for Endmember 5 | Abundance Map for Endmember 6','FontSize',14)

% Copyright 2020 The MathWorks, Inc.