Attachment 'read_annotation.m'

Download

   1 function [vertices, label, colortable] = Read_Brain_Annotation(filename)
   2 % [vertices, label, colortable] = Read_Brain_Annotation(annotfilename.annot)
   3 %
   4 % vertices expected to be simply from 0 to number of vertices - 1;
   5 % label is the vector of annotation
   6 %
   7 % colortable is empty struct if not embedded in .annot. Else, it will be
   8 % a struct.
   9 % colortable.numEntries = number of Entries
  10 % colortable.orig_tab = name of original colortable
  11 % colortable.struct_names = list of structure names (e.g. central sulcus and so on)
  12 % colortable.table = n x 5 matrix. 1st column is r, 2nd column is g, 3rd column
  13 % is b, 4th column is flag, 5th column is resultant integer values
  14 % calculated from r + g*2^8 + b*2^16 + flag*2^24. flag expected to be all 0.
  15 
  16 
  17 %
  18 % read_annotation.m
  19 %
  20 % Original Author: Bruce Fischl
  21 % CVS Revision Info:
  22 %    $Author: nicks $
  23 %    $Date: 2007/01/10 22:55:09 $
  24 %    $Revision: 1.4 $
  25 %
  26 % Copyright (C) 2002-2007,
  27 % The General Hospital Corporation (Boston, MA). 
  28 % All rights reserved.
  29 %
  30 % Distribution, usage and copying of this software is covered under the
  31 % terms found in the License Agreement file named 'COPYING' found in the
  32 % FreeSurfer source code root directory, and duplicated here:
  33 % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferOpenSourceLicense
  34 %
  35 % General inquiries: freesurfer@nmr.mgh.harvard.edu
  36 % Bug reports: analysis-bugs@nmr.mgh.harvard.edu
  37 %
  38 
  39 fp = fopen(filename, 'r', 'b');
  40 
  41 if(fp < 0)
  42    disp('Annotation file cannot be opened');
  43    return;
  44 end
  45 
  46 A = fread(fp, 1, 'int');
  47 
  48 tmp = fread(fp, 2*A, 'int');
  49 vertices = tmp(1:2:end);
  50 label = tmp(2:2:end);
  51 
  52 bool = fread(fp, 1, 'int');
  53 if(isempty(bool)) %means no colortable
  54    disp('No Colortable found.');
  55    colortable = struct([]);
  56    fclose(fp);
  57    return; 
  58 end
  59 
  60 if(bool)
  61     
  62     %Read colortable
  63     numEntries = fread(fp, 1, 'int');
  64 
  65     if(numEntries > 0)
  66         
  67         disp(['Reading from Original Version']);
  68         colortable.numEntries = numEntries;
  69         len = fread(fp, 1, 'int');
  70         colortable.orig_tab = fread(fp, len, '*char')';
  71         colortable.orig_tab = colortable.orig_tab(1:end-1);
  72 
  73         colortable.struct_names = cell(numEntries,1);
  74         colortable.table = zeros(numEntries,5);
  75         for i = 1:numEntries
  76             len = fread(fp, 1, 'int');
  77             colortable.struct_names{i} = fread(fp, len, '*char')';
  78             colortable.struct_names{i} = colortable.struct_names{i}(1:end-1);
  79             colortable.table(i,1) = fread(fp, 1, 'int');
  80             colortable.table(i,2) = fread(fp, 1, 'int');
  81             colortable.table(i,3) = fread(fp, 1, 'int');
  82             colortable.table(i,4) = fread(fp, 1, 'int');
  83             colortable.table(i,5) = colortable.table(i,1) + colortable.table(i,2)*2^8 + colortable.table(i,3)*2^16 + colortable.table(i,4)*2^24;
  84         end
  85         disp(['colortable with ' num2str(colortable.numEntries) ' entries read (originally ' colortable.orig_tab ')']);
  86 
  87     else
  88         version = -numEntries;
  89         if(version~=2)    
  90             disp(['Error! Does not handle version ' num2str(version)]);
  91         else
  92             disp(['Reading from version ' num2str(version)]);
  93         end
  94         numEntries = fread(fp, 1, 'int');
  95         colortable.numEntries = numEntries;
  96         len = fread(fp, 1, 'int');
  97         colortable.orig_tab = fread(fp, len, '*char')';
  98         colortable.orig_tab = colortable.orig_tab(1:end-1);
  99         
 100         colortable.struct_names = cell(numEntries,1);
 101         colortable.table = zeros(numEntries,5);
 102         
 103         numEntriesToRead = fread(fp, 1, 'int');
 104         for i = 1:numEntriesToRead
 105             structure = fread(fp, 1, 'int')+1;
 106             if (structure < 0)
 107                 disp(['Error! Read entry, index ' num2str(structure)]);
 108             end
 109             if(~isempty(colortable.struct_names{structure}))
 110                 disp(['Error! Duplicate Structure ' num2str(structure)]);
 111             end
 112             len = fread(fp, 1, 'int');
 113             colortable.struct_names{structure} = fread(fp, len, '*char')';
 114             colortable.struct_names{structure} = colortable.struct_names{structure}(1:end-1);
 115             colortable.table(structure,1) = fread(fp, 1, 'int');
 116             colortable.table(structure,2) = fread(fp, 1, 'int');
 117             colortable.table(structure,3) = fread(fp, 1, 'int');
 118             colortable.table(structure,4) = fread(fp, 1, 'int');
 119             colortable.table(structure,5) = colortable.table(structure,1) + colortable.table(structure,2)*2^8 + colortable.table(structure,3)*2^16 + colortable.table(structure,4)*2^24;       
 120         end
 121         disp(['colortable with ' num2str(colortable.numEntries) ' entries read (originally ' colortable.orig_tab ')']);
 122     end    
 123 else
 124     disp('Error! Should not be expecting bool = 0');    
 125 end
 126 
 127 fclose(fp);

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2015-01-14 12:35:40, 2.5 KB) [[attachment:README-DKT40.txt]]
  • [get | view] (2009-03-20 18:13:00, 25.7 KB) [[attachment:annot-desikan-m.jpg]]
  • [get | view] (2009-03-20 18:05:30, 28.1 KB) [[attachment:annot-desikan.jpg]]
  • [get | view] (2009-03-20 18:13:08, 29.7 KB) [[attachment:annot-destrieux-m.jpg]]
  • [get | view] (2009-03-20 18:05:38, 35.7 KB) [[attachment:annot-destrieux.jpg]]
  • [get | view] (2009-01-26 22:35:18, 4.7 KB) [[attachment:read_annotation.m]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.