function log_images(ims, logbase) % log_images(images, logbase) % Logarithm of images, to base logbase. % % If images are not specified, GUI-selection is used, % if logbase is not specified, base-2 is assumed. % check spm version: if exist('spm_select','file') % should be true for spm5 spm5 = 1; select = @(msg) spm_select(inf, 'image', msg); float32 = spm_type('float32'); elseif exist('spm_get','file') % should be true for spm2 spm5 = 0; select = @(msg) spm_get(inf, 'img', msg); float32 = spm_type('float'); else error('log_images:spm_missing',... 'Failed to find spm_get/spm_select, please add SPM to Matlab path') end spm_defaults; % default to base-2 if ~exist('logbase', 'var') || isempty(logbase) logbase = 2; end % get filenames if ~exist('ims', 'var') || isempty(ims) ims = select('Select images'); end N = size(ims, 1); % process files for n = 1:N invol = spm_vol(ims(n, :)); [pth fname ext] = fileparts(invol.fname); lgvol = invol; lgvol.fname = fullfile(pth, ['log_' fname ext]); lgvol.descrip = 'Log image'; if (spm5) lgvol.dt(1) = float32; else lgvol.dim(4) = float32; end inimg = spm_read_vols(invol); lgimg = log2(inimg) / log2(logbase); lgimg(~isfinite(lgimg)) = 0; % (treat inf/nan as no change) spm_write_vol(lgvol, lgimg); end