function Z = imgFromHyperSpecReflectance(numXpixels,numYpixels,hyperReflectanceData,k);
% Take a hyperspectral dataset and returns the kth image, correct cropped 
% or zero padded to the size in numXpixels,numYpixels.
%
%   If hyperReflectanceData has fewer bands than k, then a band is selected
%   via the mod operation, i.e. it is cyclically selected.

% Select a crop or zero pad

[y,x,h] = size(hyperReflectanceData);

imgMean = mean(mean(mean(hyperReflectanceData)));

if (numXpixels > x) && (numYpixels > y)
    Z = ones(numYpixels,numXpixels).*imgMean; %set the oversized background to the img mean.
    Z(1:y,1:x) = squeeze(hyperReflectanceData(:,:,k));
end

if (numXpixels > x) && (numYpixels <= y)
    Z = ones(numYpixels,numXpixels).*imgMean; %set the oversized background to the img mean.
    Z(:,1:x) = squeeze(hyperReflectanceData(1:numYpixels,:,k));
end

if (numXpixels <= x) && (numYpixels > y)
    Z = ones(numYpixels,numXpixels).*imgMean; %set the oversized background to the img mean.
    Z(1:y,:) = squeeze(hyperReflectanceData(:,1:numXpixels,k));
end

if (numXpixels <= x) && (numYpixels <= y)
    Z = squeeze(hyperReflectanceData(1:numYpixels,1:numXpixels,k));
end

% zMax = max(max(Z));
% 
% Z = Z./zMax;
Z = Z./255;
end

