%This function computes the pixelwise standard deviation of an array of
%images, indexed by the highest dimension of the array, and returns an
%equal-dimension array of threshold images, where the threshold is a
%Z-score specified by the user.

%This assumes gray-value images.
function [outArr] = sdthreshold(imgArray, bgImg, threshold)
    stdev = std(imgArray, 0, ndims(imgArray));    

    outArr = [];
    for imgindex = 1:size(imgArray, ndims(imgArray))
        outArr(:,:,imgindex)=(abs(imgArray(:,:,imgindex) - bgImg) > (threshold .* stdev));
    end
end

    