spm_vol.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. function V = spm_vol(P)
  2. % Get header information for images
  3. % FORMAT V = spm_vol(P)
  4. % P - a char or cell array of filenames
  5. % V - a structure array containing image volume information
  6. % The elements of the structures are:
  7. % V.fname - the filename of the image.
  8. % V.dim - the x, y and z dimensions of the volume
  9. % V.dt - A 1x2 array. First element is datatype (see spm_type).
  10. % The second is 1 or 0 depending on the endian-ness.
  11. % V.mat - a 4x4 affine transformation matrix mapping from
  12. % voxel coordinates to real world coordinates.
  13. % V.pinfo - plane info for each plane of the volume.
  14. % V.pinfo(1,:) - scale for each plane
  15. % V.pinfo(2,:) - offset for each plane
  16. % The true voxel intensities of the jth image are given
  17. % by: val*V.pinfo(1,j) + V.pinfo(2,j)
  18. % V.pinfo(3,:) - offset into image (in bytes).
  19. % If the size of pinfo is 3x1, then the volume is assumed
  20. % to be contiguous and each plane has the same scalefactor
  21. % and offset.
  22. %__________________________________________________________________________
  23. %
  24. % The fields listed above are essential for the mex routines, but other
  25. % fields can also be incorporated into the structure.
  26. %
  27. % Note that spm_vol can also be applied to the filename(s) of 4-dim
  28. % volumes. In that case, the elements of V will point to a series of 3-dim
  29. % images.
  30. %__________________________________________________________________________
  31. % Copyright (C) 1999-2014 Wellcome Trust Centre for Neuroimaging
  32. % John Ashburner
  33. % $Id: spm_vol.m 5958 2014-04-16 17:13:41Z guillaume $
  34. if ~nargin
  35. V = struct('fname', {},...
  36. 'dim', {},...
  37. 'dt', {},...
  38. 'pinfo', {},...
  39. 'mat', {},...
  40. 'n', {},...
  41. 'descrip', {},...
  42. 'private', {});
  43. elseif isempty(P)
  44. V = spm_vol;
  45. if iscell(P), V = {V}; end
  46. elseif isstruct(P)
  47. V = P;
  48. elseif iscell(P)
  49. V = cellfun(@spm_vol,P, 'UniformOutput',false);
  50. else
  51. V = spm_vol;
  52. cnt = 0;
  53. for i=1:size(P,1)
  54. v = spm_vol_hdr(deblank(P(i,:)));
  55. f = fieldnames(v);
  56. for j=1:numel(f)
  57. [V(cnt+1:cnt+size(v,2),1).(f{j})] = deal(v.(f{j}));
  58. end
  59. cnt = cnt + size(v,2);
  60. end
  61. end
  62. %==========================================================================
  63. % function V = spm_vol_hdr(p)
  64. %==========================================================================
  65. function V = spm_vol_hdr(p)
  66. [pth,nam,ext,n] = spm_fileparts(p);
  67. p = fullfile(pth,[nam ext]);
  68. n = str2num(n);
  69. if ~spm_existfile(p)
  70. error('File "%s" does not exist.', p);
  71. end
  72. switch ext
  73. case {'.nii','.NII'}
  74. % Do nothing
  75. case {'.img','.IMG'}
  76. if ~spm_existfile(fullfile(pth,[nam '.hdr'])) && ...
  77. ~spm_existfile(fullfile(pth,[nam '.HDR']))
  78. error('File "%s" does not exist.', fullfile(pth,[nam '.hdr']));
  79. end
  80. case {'.hdr','.HDR'}
  81. ext = '.img';
  82. p = fullfile(pth,[nam ext]);
  83. if ~spm_existfile(p)
  84. error('File "%s" does not exist.', p);
  85. end
  86. case {'.gz','.GZ'}
  87. fprintf('Compressed NIfTI files are not supported.\n');
  88. tmpname = tempname;
  89. try
  90. tmpname = char(gunzip(p,tmpname));
  91. catch
  92. error('Cannot uncompress "%s".',p);
  93. end
  94. try
  95. if isempty(n), n = ''; else n = [',' num2str(n)]; end
  96. V = spm_vol_hdr([tmpname n]);
  97. for i=1:numel(V)
  98. V(i).dat = spm_read_vols(V(i));
  99. V(i).private.dat.fname = spm_file(p,'ext','');
  100. V(i).fname = p;
  101. V(i).dt(1) = 64;
  102. V(i).pinfo = [1 0 0]';
  103. end
  104. catch
  105. warning('Cannot read uncompressed file "%s".',p);
  106. end
  107. spm_unlink(tmpname);
  108. rmdir(fileparts(tmpname));
  109. return
  110. otherwise
  111. error('File "%s" is not of a recognised type.', p);
  112. end
  113. V = spm_vol_nifti(p,n);
  114. if isempty(n) && length(V.private.dat.dim) > 3
  115. V0(1) = V;
  116. for i = 2:V.private.dat.dim(4)
  117. V0(i) = spm_vol_nifti(V.private, i);
  118. end
  119. V = V0;
  120. end