function Im=readall(dirname,basename,num_files,dim)

stack = zeros(dim(2),dim(1),num_files);
Im = zeros(dim(2),dim(1));

for j=1:8:num_files
    filename = sprintf('%s%04d%s', basename, j, '.tif')
    Im=Im+imageread(strcat(dirname,filename), 'tif', dim, 32);
%    stack(:,:,j) = imageread(strcat(dirname,filename), 'tif', dim, 32);

end
