function lagValue = bestShift(imageData)

if nargin < 1
    imageData = evalin('base', 'zImage.stack');
end

imageData = double(imageData - mean(mean(imageData)));

savedCorr = nan(size(imageData, 1) / 2, 1);
for i = 1:size(imageData, 1) / 2
    % only compare the parts of the image that are shared
    savedCorr(i) = max([mean(mean((imageData(1:end - i, 1:2:end) .* imageData(i + 1:end, 2:2:end)).^2))...
        mean(mean((imageData(i + 1:end, 1:2:end) .* imageData(1:end - i, 2:2:end)).^2))]);
end

[lagValue lagValue] = max(savedCorr);

if ~nargout
    figure, plot(savedCorr)
    line(lagValue, savedCorr(lagValue), 'marker', '*', 'markerEdge', 'r');
    title(['Maximum similarity at a shift of ' num2str(lagValue) ' pixels for even rows']);
    xlabel('Shift (pixels)');
    ylabel('Correlation');
end