% Autofocus using Standard Deviation
% Detects image with highest contrast in set of grayvalue images
% input: image set (3d-matrix)
function autofocus(frames)

% for each single frame...
for fIndex = 1:size(frames,3)
    frame = frames(:,:,fIndex);
    figure;imshow(frames(:,:,fIndex));
    
    % compute standard deviation of frame
    % since 'frame(:)' is used, it is treated
    % as a 1-dimensional vector, i.e. the columns
    % are appended to each other. Hence only the
    % vertical contrast is computed !
    
    dev(fIndex) = std(frame(:));
end
    
% plot result: std-curve and max. contrast frame

subplot(1,2,1);
plot(1:length(dev),dev);

[v,i] = max(dev); % value, index
subplot(1,2,2);
imshow(frames(:,:,i));


