Home > vbmeg > external > NIFTI > load_nii_img_cbi.m

load_nii_img_cbi

PURPOSE ^

Load NIFTI dataset body after its header is loaded using load_nii_hdr.

SYNOPSIS ^

function [img,hdr] = load_nii_img_cbi(hdr,filetype,fileprefix,machine,img_idx,old_RGB)

DESCRIPTION ^

  Load NIFTI dataset body after its header is loaded using load_nii_hdr.
  
  Usage: [img,hdr] = ...
       load_nii_img_cbi(hdr,filetype,fileprefix,machine,[img_idx],[old_RGB]);
  
  Where: [hdr,filetype,fileprefix,machine] = load_nii_hdr(filename);
  
  img_idx    - a numerical array of image indices. Only the specified images
    will be loaded. If there is no img_idx, all images will be loaded. The
       number of images scans can be obtained from hdr.dime.dim(5)

  old_RGB    - an optional boolean variable to handle special RGB data 
       sequence [R1 R2 ... G1 G2 ... B1 B2 ...] that is used only by 
       AnalyzeDirect (Analyze Software). Since both NIfTI and Analyze
       file format use RGB triple [R1 G1 B1 R2 G2 B2 ...] sequentially
       for each voxel, this variable is set to FALSE by default. If the 
       image that you displayed is garbled, try to set old_RGB variable
       to TRUE (or 1) and load the image again, because it could be using
       AnalyzeDirect RGB data sequence.

  Returned values:
  
  img - 3D (or 4D) matrix of NIFTI data.
  
  Part of this file is copied and modified under GNU license from
  MRI_TOOLBOX developed by CNSP in Flinders University, Australia
  
  NIFTI data format can be found on: http://nifti.nimh.nih.gov
  
  - Jimmy Shen (pls@rotman-baycrest.on.ca)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 %  Load NIFTI dataset body after its header is loaded using load_nii_hdr.
0002 %
0003 %  Usage: [img,hdr] = ...
0004 %       load_nii_img_cbi(hdr,filetype,fileprefix,machine,[img_idx],[old_RGB]);
0005 %
0006 %  Where: [hdr,filetype,fileprefix,machine] = load_nii_hdr(filename);
0007 %
0008 %  img_idx    - a numerical array of image indices. Only the specified images
0009 %    will be loaded. If there is no img_idx, all images will be loaded. The
0010 %       number of images scans can be obtained from hdr.dime.dim(5)
0011 %
0012 %  old_RGB    - an optional boolean variable to handle special RGB data
0013 %       sequence [R1 R2 ... G1 G2 ... B1 B2 ...] that is used only by
0014 %       AnalyzeDirect (Analyze Software). Since both NIfTI and Analyze
0015 %       file format use RGB triple [R1 G1 B1 R2 G2 B2 ...] sequentially
0016 %       for each voxel, this variable is set to FALSE by default. If the
0017 %       image that you displayed is garbled, try to set old_RGB variable
0018 %       to TRUE (or 1) and load the image again, because it could be using
0019 %       AnalyzeDirect RGB data sequence.
0020 %
0021 %  Returned values:
0022 %
0023 %  img - 3D (or 4D) matrix of NIFTI data.
0024 %
0025 %  Part of this file is copied and modified under GNU license from
0026 %  MRI_TOOLBOX developed by CNSP in Flinders University, Australia
0027 %
0028 %  NIFTI data format can be found on: http://nifti.nimh.nih.gov
0029 %
0030 %  - Jimmy Shen (pls@rotman-baycrest.on.ca)
0031 %
0032 function [img,hdr] = load_nii_img_cbi(hdr,filetype,fileprefix,machine,img_idx,old_RGB)
0033    
0034    if ~exist('hdr','var') | ~exist('filetype','var') | ~exist('fileprefix','var') | ~exist('machine','var')
0035       error('Usage: [img,hdr] = load_nii_img_cbi(hdr,filetype,fileprefix,machine,[img_idx]);');
0036    end
0037    
0038    if ~exist('img_idx','var') | hdr.dime.dim(5)<1, img_idx = []; end
0039    if ~exist('old_RGB','var'), old_RGB = 0; end
0040    
0041    %  check img_idx
0042    %
0043    if ~isempty(img_idx) & ~isnumeric(img_idx)
0044       error('"img_idx" should be a numerical array.');
0045    end
0046    
0047    if length(unique(img_idx)) ~= length(img_idx)
0048       error('Duplicate image index in "img_idx"');
0049    end
0050    
0051    if ~isempty(img_idx) & (min(img_idx) < 1 | max(img_idx) > hdr.dime.dim(5))
0052       max_range = hdr.dime.dim(5);
0053 
0054       if max_range == 1
0055          error(['"img_idx" should be 1.']);
0056       else
0057          range = ['1 ' num2str(max_range)];
0058          error(['"img_idx" should be an integer within the range of [' range '].']);
0059       end
0060    end
0061    
0062    [img,hdr] = read_image(hdr,filetype,fileprefix,machine,img_idx,old_RGB);
0063    
0064    return                    % load_nii_img_cbi
0065 
0066 
0067 %---------------------------------------------------------------------
0068 function [img,hdr] = read_image(hdr, filetype,fileprefix,machine,img_idx,old_RGB)
0069 
0070    switch filetype
0071    case {0, 1}
0072       fn = [fileprefix '.img'];
0073    case 2
0074       fn = [fileprefix '.nii'];
0075    end
0076 
0077    fid = fopen(fn,'r',machine);
0078 
0079    if fid < 0,
0080       msg = sprintf('Cannot open file %s.',fn);
0081       error(msg);
0082    end
0083 
0084    %  Set bitpix according to datatype
0085    %
0086    %  /*Acceptable values for datatype are*/
0087    %
0088    %     0 None                     (Unknown bit per voxel) % DT_NONE, DT_UNKNOWN
0089    %     1 Binary                         (ubit1, bitpix=1) % DT_BINARY
0090    %     2 Unsigned char         (uchar or uint8, bitpix=8) % DT_UINT8, NIFTI_TYPE_UINT8
0091    %     4 Signed short                  (int16, bitpix=16) % DT_INT16, NIFTI_TYPE_INT16
0092    %     8 Signed integer                (int32, bitpix=32) % DT_INT32, NIFTI_TYPE_INT32
0093    %    16 Floating point    (single or float32, bitpix=32) % DT_FLOAT32, NIFTI_TYPE_FLOAT32
0094    %    32 Complex, 2 float32      (Use float32, bitpix=64) % DT_COMPLEX64, NIFTI_TYPE_COMPLEX64
0095    %    64 Double precision  (double or float64, bitpix=64) % DT_FLOAT64, NIFTI_TYPE_FLOAT64
0096    %   128 uint8 RGB                 (Use uint8, bitpix=24) % DT_RGB24, NIFTI_TYPE_RGB24
0097    %   256 Signed char            (schar or int8, bitpix=8) % DT_INT8, NIFTI_TYPE_INT8
0098    %   511 Single RGB              (Use float32, bitpix=96) % DT_RGB96, NIFTI_TYPE_RGB96
0099    %   512 Unsigned short               (uint16, bitpix=16) % DT_UNINT16, NIFTI_TYPE_UNINT16
0100    %   768 Unsigned integer             (uint32, bitpix=32) % DT_UNINT32, NIFTI_TYPE_UNINT32
0101    %  1024 Signed long long              (int64, bitpix=64) % DT_INT64, NIFTI_TYPE_INT64
0102    %  1280 Unsigned long long           (uint64, bitpix=64) % DT_UINT64, NIFTI_TYPE_UINT64
0103    %  1536 Long double, float128  (Unsupported, bitpix=128) % DT_FLOAT128, NIFTI_TYPE_FLOAT128
0104    %  1792 Complex128, 2 float64  (Use float64, bitpix=128) % DT_COMPLEX128, NIFTI_TYPE_COMPLEX128
0105    %  2048 Complex256, 2 float128 (Unsupported, bitpix=256) % DT_COMPLEX128, NIFTI_TYPE_COMPLEX128
0106    %
0107    switch hdr.dime.datatype
0108    case   1,
0109       hdr.dime.bitpix = 1;  precision = 'ubit1';
0110    case   2,
0111       hdr.dime.bitpix = 8;  precision = 'uint8';
0112    case   4,
0113       hdr.dime.bitpix = 16; precision = 'int16';
0114    case   8,
0115       hdr.dime.bitpix = 32; precision = 'int32';
0116    case  16,
0117       hdr.dime.bitpix = 32; precision = 'float32';
0118    case  32,
0119       hdr.dime.bitpix = 64; precision = 'float32';
0120    case  64,
0121       hdr.dime.bitpix = 64; precision = 'float64';
0122    case 128,
0123       hdr.dime.bitpix = 24; precision = 'uint8';
0124    case 256 
0125       hdr.dime.bitpix = 8;  precision = 'int8';
0126    case 511 
0127       hdr.dime.bitpix = 96; precision = 'float32';
0128    case 512 
0129       hdr.dime.bitpix = 16; precision = 'uint16';
0130    case 768 
0131       hdr.dime.bitpix = 32; precision = 'uint32';
0132    case 1024
0133       hdr.dime.bitpix = 64; precision = 'int64';
0134    case 1280
0135       hdr.dime.bitpix = 64; precision = 'uint64';
0136    case 1792,
0137       hdr.dime.bitpix = 128; precision = 'float64';
0138    otherwise
0139       error('This datatype is not supported'); 
0140    end
0141 
0142    if hdr.dime.dim(5) < 1
0143       hdr.dime.dim(5) = 1;
0144    end
0145 
0146    %  move pointer to the start of image block
0147    %
0148    switch filetype
0149    case {0, 1}
0150       fseek(fid, 0, 'bof');
0151    case 2
0152       fseek(fid, hdr.dime.vox_offset, 'bof');
0153    end
0154 
0155    %  Load whole image block for old Analyze format, or binary image,
0156    %  or img_idx is empty; otherwise, load images that are specified
0157    %  in img_idx
0158    %
0159    %  For binary image, we have to read all because pos can not be
0160    %  seeked in bit and can not be calculated the way below.
0161    %
0162    if filetype == 0 | hdr.dime.datatype == 1 | isempty(img_idx)
0163 
0164       %  For each frame, precision of value will be read
0165       %  in img_siz times, where img_siz is only the
0166       %  dimension size of an image, not the byte storage
0167       %  size of an image.
0168       %
0169       img_siz = prod(hdr.dime.dim(2:5));
0170 
0171       %  For complex float32 or complex float64, voxel values
0172       %  include [real, imag]
0173       %
0174       if hdr.dime.datatype == 32 | hdr.dime.datatype == 1792
0175          img_siz = img_siz * 2;
0176       end
0177      
0178       %MPH: For RGB24, voxel values include 3 separate color planes
0179       %
0180       if hdr.dime.datatype == 128 | hdr.dime.datatype == 511
0181      img_siz = img_siz * 3;
0182       end
0183 
0184       img = fread(fid, img_siz, sprintf('*%s',precision));
0185 
0186    else
0187       img = [];
0188       
0189       for i=1:length(img_idx)
0190          
0191          %  For each frame, precision of value will be read
0192          %  in img_siz times, where img_siz is only the
0193          %  dimension size of an image, not the byte storage
0194          %  size of an image.
0195          %
0196          img_siz = prod(hdr.dime.dim(2:4));
0197 
0198      %  Position is seeked in bytes. To convert dimension size
0199          %  to byte storage size, hdr.dime.bitpix/8 will be
0200          %  applied.
0201          %
0202          %  (MPH: This offset must be calculated BEFORE altering img_siz
0203      %  for the datatypes for which 'bitpix' and 'precision' do not
0204          %  "match").
0205          %
0206          pos = (img_idx(i) - 1) * img_siz * hdr.dime.bitpix/8;
0207 
0208          %  For complex float32 or complex float64, voxel values
0209          %  include [real, imag]
0210          %
0211          if hdr.dime.datatype == 32 | hdr.dime.datatype == 1792
0212             img_siz = img_siz * 2;
0213          end
0214 
0215          %MPH: For RGB24, voxel values include 3 separate color planes
0216          %
0217          if hdr.dime.datatype == 128 | hdr.dime.datatype == 511
0218         img_siz = img_siz * 3;
0219          end
0220          
0221          if filetype == 2
0222             fseek(fid, pos + hdr.dime.vox_offset, 'bof');
0223          else
0224             fseek(fid, pos, 'bof');
0225          end
0226 
0227          %  For each frame, fread will read precision of value
0228          %  in img_siz times
0229          %
0230          img = [img fread(fid, img_siz, sprintf('*%s',precision))];
0231       end
0232    end
0233 
0234    %  For complex float32 or complex float64, voxel values
0235    %  include [real, imag]
0236    %
0237    if hdr.dime.datatype == 32 | hdr.dime.datatype == 1792
0238       img = reshape(img, [2, length(img)/2]);
0239       img = complex(img(1,:)', img(2,:)');
0240    end
0241 
0242    fclose(fid);
0243 
0244    %  Update the global min and max values
0245    %
0246    hdr.dime.glmax = max(double(img(:)));
0247    hdr.dime.glmin = min(double(img(:)));
0248 
0249    if isempty(img_idx)
0250       img_idx = 1:hdr.dime.dim(5);
0251    end
0252 
0253    if old_RGB & hdr.dime.datatype == 128 & hdr.dime.bitpix == 24
0254       img = squeeze(reshape(img, [hdr.dime.dim(2:3) 3 hdr.dime.dim(4) length(img_idx)]));
0255       img = permute(img, [1 2 4 3 5]);
0256    elseif hdr.dime.datatype == 128 & hdr.dime.bitpix == 24
0257       img = squeeze(reshape(img, [3 hdr.dime.dim(2:4) length(img_idx)]));
0258       img = permute(img, [2 3 4 1 5]);
0259    elseif hdr.dime.datatype == 511 & hdr.dime.bitpix == 96
0260       img = double(img);
0261       img = (img - min(img))/(max(img) - min(img));
0262       img = squeeze(reshape(img, [3 hdr.dime.dim(2:4) length(img_idx)]));
0263       img = permute(img, [2 3 4 1 5]);
0264    else
0265       img = squeeze(reshape(img, [hdr.dime.dim(2:4) length(img_idx)]));
0266    end
0267 
0268    if ~isempty(img_idx)
0269       hdr.dime.dim(5) = length(img_idx);
0270    end
0271 
0272    return                        % read_image
0273

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005