WICHTIG: Der Betrieb von goMatlab.de wird privat finanziert fortgesetzt. - Mehr Infos...

Mein MATLAB Forum - goMatlab.de

Mein MATLAB Forum

 
Gast > Registrieren       Autologin?   

Partner:




Forum
      Option
[Erweitert]
  • Diese Seite per Mail weiterempfehlen
     


Gehe zu:  
Neues Thema eröffnen Neue Antwort erstellen

Einlesen von .nii-Dateien und Ausgabe eines Vektorfeldes

 

Viertann
Forum-Newbie

Forum-Newbie


Beiträge: 3
Anmeldedatum: 19.09.13
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 20.09.2013, 16:35     Titel: Einlesen von .nii-Dateien und Ausgabe eines Vektorfeldes
  Antworten mit Zitat      
Hallo,
Ich bin absoluter Neuling in Matlab und habe ein Problem, was sich wohl am einfachsten in Matlab lösen lässt. Ich möchte nifti-Dateien in Matlab einlesen um anschließend das dahintersteckende Vektorfeld heraus zu bekommen. Hierzu habe ich ein Script gefunden, welches nifti-Dateien einlesen soll. Hier das Script mit Dokumentation:

Code:

%  Load NIFTI or ANALYZE dataset. Support both *.nii and *.hdr/*.img
%  file extension. If file extension is not provided, *.hdr/*.img will
%  be used as default.
%
%  A subset of NIFTI transform is included. For non-orthogonal rotation,
%  shearing etc., please use 'reslice_nii.m' to reslice the NIFTI file.
%  It will not cause negative effect, as long as you remember not to do
%  slice time correction after reslicing the NIFTI file. Output variable
%  nii will be in RAS orientation, i.e. X axis from Left to Right,
%  Y axis from Posterior to Anterior, and Z axis from Inferior to
%  Superior.
%  
%  Usage: nii = load_nii(filename, [img_idx], [dim5_idx], [dim6_idx], ...
%         [dim7_idx], [old_RGB], [tolerance], [preferredForm])
%  
%  filename  -    NIFTI or ANALYZE file name.
%  
%  img_idx (optional)  -  a numerical array of 4th dimension indices,
%   which is the indices of image scan volume. The number of images
%   scan volumes can be obtained from get_nii_frame.m, or simply
%   hdr.dime.dim(5). Only the specified volumes will be loaded.
%   All available image volumes will be loaded, if it is default or
%   empty.
%
%  dim5_idx (optional)  -  a numerical array of 5th dimension indices.
%   Only the specified range will be loaded. All available range
%   will be loaded, if it is default or empty.
%
%  dim6_idx (optional)  -  a numerical array of 6th dimension indices.
%   Only the specified range will be loaded. All available range
%   will be loaded, if it is default or empty.
%
%  dim7_idx (optional)  -  a numerical array of 7th dimension indices.
%   Only the specified range will be loaded. All available range
%   will be loaded, if it is default or empty.
%
%  old_RGB (optional)  -  a scale number to tell difference of new RGB24
%   from old RGB24. New RGB24 uses RGB triple sequentially for each
%   voxel, like [R1 G1 B1 R2 G2 B2 ...]. Analyze 6.0 from AnalyzeDirect
%   uses old RGB24, in a way like [R1 R2 ... G1 G2 ... B1 B2 ...] for
%   each slices. If the image that you view is garbled, try to set
%   old_RGB variable to 1 and try again, because it could be in
%   old RGB24. It will be set to 0, if it is default or empty.
%
%  tolerance (optional) - distortion allowed in the loaded image for any
%   non-orthogonal rotation or shearing of NIfTI affine matrix. If
%   you set 'tolerance' to 0, it means that you do not allow any
%   distortion. If you set 'tolerance' to 1, it means that you do
%   not care any distortion. The image will fail to be loaded if it
%   can not be tolerated. The tolerance will be set to 0.1 (10%), if
%   it is default or empty.
%
%  preferredForm (optional)  -  selects which transformation from voxels
%   to RAS coordinates; values are s,q,S,Q.  Lower case s,q indicate
%   "prefer sform or qform, but use others if preferred not present".
%   Upper case indicate the program is forced to use the specificied
%   tranform or fail loading.  'preferredForm' will be 's', if it is
%   default or empty.   - Jeff Gunter
%
%  Returned values:
%  
%  nii structure:
%
%   hdr -      struct with NIFTI header fields.
%
%   filetype -   Analyze format .hdr/.img (0);
%         NIFTI .hdr/.img (1);
%         NIFTI .nii (2)
%
%   fileprefix -    NIFTI filename without extension.
%
%   machine -    machine string variable.
%
%   img -       3D (or 4D) matrix of NIFTI data.
%
%   original -   the original header before any affine transform.
%  
%  Part of this file is copied and modified from:
%  http://www.mathworks.com/matlabcentral/fileexchange/1878-mri-analyze-tools
%  
%  NIFTI data format can be found on: http://nifti.nimh.nih.gov
%  
%  - Jimmy Shen (jimmy !AT! rotman-baycrest.on.ca)
%
function nii = load_nii(filename, img_idx, dim5_idx, dim6_idx, dim7_idx, ...
         old_RGB, tolerance, preferredForm)

   if ~exist('filename','var')
      error('Usage: nii = load_nii(filename, [img_idx], [dim5_idx], [dim6_idx], [dim7_idx], [old_RGB], [tolerance], [preferredForm])'); %FEHLER HIER (ZEILE 89)
   end
   if ~exist('img_idx','var') | isempty(img_idx)
      img_idx = [];
   end

   if ~exist('dim5_idx','var') | isempty(dim5_idx)
      dim5_idx = [];
   end

   if ~exist('dim6_idx','var') | isempty(dim6_idx)
      dim6_idx = [];
   end

   if ~exist('dim7_idx','var') | isempty(dim7_idx)
      dim7_idx = [];
   end

   if ~exist('old_RGB','var') | isempty(old_RGB)
      old_RGB = 0;
   end

   if ~exist('tolerance','var') | isempty(tolerance)
      tolerance = 0.1;         % 10 percent
   end

   if ~exist('preferredForm','var') | isempty(preferredForm)
      preferredForm= 's';      % Jeff
   end

   v = version;

   %  Check file extension. If .gz, unpack it into temp folder
   %
   if length(filename) > 2 & strcmp(filename(end-2:end), '.gz')

      if ~strcmp(filename(end-6:end), '.img.gz') & ...
    ~strcmp(filename(end-6:end), '.hdr.gz') & ...
    ~strcmp(filename(end-6:end), '.nii.gz')

         error('Please check filename.');
      end

      if str2num(v(1:3)) < 7.1 | ~usejava('jvm')
         error('Please use MATLAB 7.1 (with java) and above, or run gunzip outside MATLAB.');
      elseif strcmp(filename(end-6:end), '.img.gz')
         filename1 = filename;
         filename2 = filename;
         filename2(end-6:end) = '';
         filename2 = [filename2, '.hdr.gz'];

         tmpDir = tempname;
         mkdir(tmpDir);
         gzFileName = filename;

         filename1 = gunzip(filename1, tmpDir);
         filename2 = gunzip(filename2, tmpDir);
         filename = char(filename1);   % convert from cell to string
      elseif strcmp(filename(end-6:end), '.hdr.gz')
         filename1 = filename;
         filename2 = filename;
         filename2(end-6:end) = '';
         filename2 = [filename2, '.img.gz'];

         tmpDir = tempname;
         mkdir(tmpDir);
         gzFileName = filename;

         filename1 = gunzip(filename1, tmpDir);
         filename2 = gunzip(filename2, tmpDir);
         filename = char(filename1);   % convert from cell to string
      elseif strcmp(filename(end-6:end), '.nii.gz')
         tmpDir = tempname;
         mkdir(tmpDir);
         gzFileName = filename;
         filename = gunzip(filename, tmpDir);
         filename = char(filename);   % convert from cell to string
      end
   end

   %  Read the dataset header
   %
   [nii.hdr,nii.filetype,nii.fileprefix,nii.machine] = load_nii_hdr(filename);

   %  Read the header extension
   %
%   nii.ext = load_nii_ext(filename);

   %  Read the dataset body
   %
   [nii.img,nii.hdr] = load_nii_img(nii.hdr,nii.filetype,nii.fileprefix, ...
      nii.machine,img_idx,dim5_idx,dim6_idx,dim7_idx,old_RGB);

   %  Perform some of sform/qform transform
   %
   nii = xform_nii(nii, tolerance, preferredForm);

   %  Clean up after gunzip
   %
   if exist('gzFileName', 'var')

      %  fix fileprefix so it doesn't point to temp location
      %
      nii.fileprefix = gzFileName(1:end-7);
      rmdir(tmpDir,'s');
   end

   return               % load_nii

 


So wie ich die Dokumentation verstehe, ist die einzige zwingend notwendige Eingabe die Variable 'filename'. Jetzt habe ich versucht die Variable mit

Code:

 filename = uigetfile('tensor.nii','Import file')
 

zu definieren. Allerdings scheint das nicht so richtig zu funktionieren.
Matlab gibt mir dann folgenden Fehler aus

Code:

Error using load_nii (line 89)
Usage: nii = load_nii(filename, [img_idx], [dim5_idx], [dim6_idx], [dim7_idx], [old_RGB], [tolerance],
[preferredForm])
 


So wie ich das aus dem Script verstehe, gibt er diesen Fehler aus, wenn die Variable filename nicht existiert. Habe ich da beim definieren was falsch gemacht?
Private Nachricht senden Benutzer-Profile anzeigen


Winkow
Moderator

Moderator



Beiträge: 3.842
Anmeldedatum: 04.11.11
Wohnort: Dresden
Version: R2014a 2015a
     Beitrag Verfasst am: 20.09.2013, 17:46     Titel:
  Antworten mit Zitat      
befinden sich denn die funktion und die ausgewählte datei im selben ordner ?
_________________

richtig Fragen
Private Nachricht senden Benutzer-Profile anzeigen
 
Viertann
Themenstarter

Forum-Newbie

Forum-Newbie


Beiträge: 3
Anmeldedatum: 19.09.13
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 20.09.2013, 18:01     Titel:
  Antworten mit Zitat      
Die Datei tensor.nii befindet sich im Current Folder. Das Script habe ich allerdings aus einem anderen Ordner in Matlab geladen. Ich werde versuchen es noch einmal aus dem Current Folder zu laden. Danke für den Tipp.
Private Nachricht senden Benutzer-Profile anzeigen
 
Harald
Forum-Meister

Forum-Meister


Beiträge: 24.501
Anmeldedatum: 26.03.09
Wohnort: Nähe München
Version: ab 2017b
     Beitrag Verfasst am: 20.09.2013, 22:13     Titel:
  Antworten mit Zitat      
Hallo,

ich würde uigetfile immer so verwenden:
Code:
[FileName,PathName] = uigetfile(...);
fullFileName = fullfile(PathName, FileName);


So ist sichergestellt, dass es nicht zu Problemen kommt, weil die Datei nicht gefunden werden kann.

Grüße,
Harald
Private Nachricht senden Benutzer-Profile anzeigen
 
Viertann
Themenstarter

Forum-Newbie

Forum-Newbie


Beiträge: 3
Anmeldedatum: 19.09.13
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 26.09.2013, 13:23     Titel:
  Antworten mit Zitat      
Hey Leute,
Danke an alle die sich Gedanken gemacht haben. Ich habe beide Vorschläge versucht umzusetzen, aber es hat so bisher nicht funktioniert. Mag aber auch an meinem Unvermögen liegen. Letztendlich hat es aber auf einem anderen Weg funktioniert. Dieser ist hier beschrieben:

http://www.thevelveteenbrain.com/pr.....ad-nii-files-into-matlab/

Vielen Dank nochmal

Olivier
Private Nachricht senden Benutzer-Profile anzeigen
 
Neues Thema eröffnen Neue Antwort erstellen



Einstellungen und Berechtigungen
Beiträge der letzten Zeit anzeigen:

Du kannst Beiträge in dieses Forum schreiben.
Du kannst auf Beiträge in diesem Forum antworten.
Du kannst deine Beiträge in diesem Forum nicht bearbeiten.
Du kannst deine Beiträge in diesem Forum nicht löschen.
Du kannst an Umfragen in diesem Forum nicht mitmachen.
Du kannst Dateien in diesem Forum posten
Du kannst Dateien in diesem Forum herunterladen
.





 Impressum  | Nutzungsbedingungen  | Datenschutz | FAQ | goMatlab RSS Button RSS

Hosted by:


Copyright © 2007 - 2025 goMatlab.de | Dies ist keine offizielle Website der Firma The Mathworks

MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, SimBiology, SimHydraulics, SimEvents, and xPC TargetBox are registered trademarks and The MathWorks, the L-shaped membrane logo, and Embedded MATLAB are trademarks of The MathWorks, Inc.