Home > vbmeg > external > freesurfer > read_annotation.m

read_annotation

PURPOSE ^

[vertices, label, colortable] = Read_Brain_Annotation(annotfilename.annot)

SYNOPSIS ^

function [vertices, label, colortable] = Read_Brain_Annotation(filename)

DESCRIPTION ^

 [vertices, label, colortable] = Read_Brain_Annotation(annotfilename.annot)

 vertices expected to be simply from 0 to number of vertices - 1;
 label is the vector of annotation

 colortable is empty struct if not embedded in .annot. Else, it will be
 a struct.
 colortable.numEntries = number of Entries
 colortable.orig_tab = name of original colortable
 colortable.struct_names = list of structure names (e.g. central sulcus and so on)
 colortable.table = n x 5 matrix. 1st column is r, 2nd column is g, 3rd column
 is b, 4th column is flag, 5th column is resultant integer values
 calculated from r + g*2^8 + b*2^16 + flag*2^24. flag expected to be all 0.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [vertices, label, colortable] = Read_Brain_Annotation(filename)
0002 % [vertices, label, colortable] = Read_Brain_Annotation(annotfilename.annot)
0003 %
0004 % vertices expected to be simply from 0 to number of vertices - 1;
0005 % label is the vector of annotation
0006 %
0007 % colortable is empty struct if not embedded in .annot. Else, it will be
0008 % a struct.
0009 % colortable.numEntries = number of Entries
0010 % colortable.orig_tab = name of original colortable
0011 % colortable.struct_names = list of structure names (e.g. central sulcus and so on)
0012 % colortable.table = n x 5 matrix. 1st column is r, 2nd column is g, 3rd column
0013 % is b, 4th column is flag, 5th column is resultant integer values
0014 % calculated from r + g*2^8 + b*2^16 + flag*2^24. flag expected to be all 0.
0015 
0016 
0017 %
0018 % read_annotation.m
0019 %
0020 % Original Author: Bruce Fischl
0021 % CVS Revision Info:
0022 %    $Author: nicks $
0023 %    $Date: 2007/01/10 22:55:09 $
0024 %    $Revision: 1.4 $
0025 %
0026 % Copyright (C) 2002-2007,
0027 % The General Hospital Corporation (Boston, MA).
0028 % All rights reserved.
0029 %
0030 % Distribution, usage and copying of this software is covered under the
0031 % terms found in the License Agreement file named 'COPYING' found in the
0032 % FreeSurfer source code root directory, and duplicated here:
0033 % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferOpenSourceLicense
0034 %
0035 % General inquiries: freesurfer@nmr.mgh.harvard.edu
0036 % Bug reports: analysis-bugs@nmr.mgh.harvard.edu
0037 %
0038 
0039 fp = fopen(filename, 'r', 'b');
0040 
0041 if(fp < 0)
0042    disp('Annotation file cannot be opened');
0043    return;
0044 end
0045 
0046 A = fread(fp, 1, 'int');
0047 
0048 tmp = fread(fp, 2*A, 'int');
0049 vertices = tmp(1:2:end);
0050 label = tmp(2:2:end);
0051 
0052 bool = fread(fp, 1, 'int');
0053 if(isempty(bool)) %means no colortable
0054    disp('No Colortable found.');
0055    colortable = struct([]);
0056    fclose(fp);
0057    return; 
0058 end
0059 
0060 if(bool)
0061     
0062     %Read colortable
0063     numEntries = fread(fp, 1, 'int');
0064 
0065     if(numEntries > 0)
0066         
0067         disp(['Reading from Original Version']);
0068         colortable.numEntries = numEntries;
0069         len = fread(fp, 1, 'int');
0070         colortable.orig_tab = fread(fp, len, '*char')';
0071         colortable.orig_tab = colortable.orig_tab(1:end-1);
0072 
0073         colortable.struct_names = cell(numEntries,1);
0074         colortable.table = zeros(numEntries,5);
0075         for i = 1:numEntries
0076             len = fread(fp, 1, 'int');
0077             colortable.struct_names{i} = fread(fp, len, '*char')';
0078             colortable.struct_names{i} = colortable.struct_names{i}(1:end-1);
0079             colortable.table(i,1) = fread(fp, 1, 'int');
0080             colortable.table(i,2) = fread(fp, 1, 'int');
0081             colortable.table(i,3) = fread(fp, 1, 'int');
0082             colortable.table(i,4) = fread(fp, 1, 'int');
0083             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;
0084         end
0085         disp(['colortable with ' num2str(colortable.numEntries) ' entries read (originally ' colortable.orig_tab ')']);
0086 
0087     else
0088         version = -numEntries;
0089         if(version~=2)    
0090             disp(['Error! Does not handle version ' num2str(version)]);
0091         else
0092             disp(['Reading from version ' num2str(version)]);
0093         end
0094         numEntries = fread(fp, 1, 'int');
0095         colortable.numEntries = numEntries;
0096         len = fread(fp, 1, 'int');
0097         colortable.orig_tab = fread(fp, len, '*char')';
0098         colortable.orig_tab = colortable.orig_tab(1:end-1);
0099         
0100         colortable.struct_names = cell(numEntries,1);
0101         colortable.table = zeros(numEntries,5);
0102         
0103         numEntriesToRead = fread(fp, 1, 'int');
0104         for i = 1:numEntriesToRead
0105             structure = fread(fp, 1, 'int')+1;
0106             if (structure < 0)
0107                 disp(['Error! Read entry, index ' num2str(structure)]);
0108             end
0109             if(~isempty(colortable.struct_names{structure}))
0110                 disp(['Error! Duplicate Structure ' num2str(structure)]);
0111             end
0112             len = fread(fp, 1, 'int');
0113             colortable.struct_names{structure} = fread(fp, len, '*char')';
0114             colortable.struct_names{structure} = colortable.struct_names{structure}(1:end-1);
0115             colortable.table(structure,1) = fread(fp, 1, 'int');
0116             colortable.table(structure,2) = fread(fp, 1, 'int');
0117             colortable.table(structure,3) = fread(fp, 1, 'int');
0118             colortable.table(structure,4) = fread(fp, 1, 'int');
0119             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;       
0120         end
0121         disp(['colortable with ' num2str(colortable.numEntries) ' entries read (originally ' colortable.orig_tab ')']);
0122     end    
0123 else
0124     disp('Error! Should not be expecting bool = 0');    
0125 end
0126 
0127 fclose(fp);
0128 
0129

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