spm_dilate.m 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. function ima = spm_dilate(varargin)
  2. % Perform a dilation on an image (2D or 3D)
  3. % It uses either the supplied kernel or a standard 6-connectivity kernel.
  4. % FORMAT: ima = spm_dilate(ima)
  5. % or
  6. % FORMAT: ima = spm_dilate(ima,kernel)
  7. %
  8. % Input:
  9. % ima : 2 or 3D image
  10. % kernel : (Optional) voxel values in ima are replaced by the
  11. % maximum value in a neighbourhood defined by kernel.
  12. % The "standard" dilation operation (in 2D) is realised
  13. % using the kernel
  14. % 0 1 0
  15. % 1 1 1
  16. % 0 1 0
  17. %
  18. % Output:
  19. % ima : Dilated image.
  20. %
  21. % The functionality of this routine has been modelled on the function
  22. % imdilate from the MATLAB Image processing toolbox. It doesn't (yet)
  23. % have a support function such as strel to help the user to define
  24. % kernels (you have to do it yourself if you want anything above
  25. % 6-connectivty) and it doesnt do the clever structuring element
  26. % decomposition that strel does (and imdilate uses). That should
  27. % in principle mean that spm_dilate is slower than imdilate, but
  28. % at least for small (typical) kernels it is actually more than
  29. % twice as fast.
  30. % The actual job is done by spm_dilate_erode.c that serves both
  31. % spm_dilate.m and spm_erode.m
  32. %__________________________________________________________________________
  33. % Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
  34. % Jesper Andersson
  35. % $Id: spm_dilate.m 4310 2011-04-18 16:07:35Z guillaume $
  36. if exist('spm_dilate_erode','file')~=3
  37. error('spm_dilate_erode.c not compiled - see Makefile');
  38. end
  39. if nargin > 1
  40. kernel = varargin{2};
  41. else
  42. if length(size(varargin{1})) == 2
  43. kernel = [0 1 0; 1 1 1; 0 1 0];
  44. elseif length(size(varargin{1})) == 3
  45. kernel = cat(3,[0 0 0; 0 1 0; 0 0 0],[0 1 0; 1 1 1; 0 1 0],[0 0 0; 0 1 0; 0 0 0]);
  46. else
  47. error('Input ima must be 2- or 3-dimensional');
  48. end
  49. end
  50. ima = spm_dilate_erode(varargin{1},kernel,'dilate');