You must be logged in to post Login Register


Lost Your Password?

Search Forums:


 






Wildcard Usage:
*    matches any number of characters
%    matches exactly one character

Exporting fibers to MATLAB

UserPost

10:45 pm
March 30, 2010


sramena1

Baltimore, mD

New Member

posts 1

I would like to export each of the fibers to MATLAB. In this regard I would like to know:

1. If there is any feature in TrackVis to export fibers in .mat file format?

2. In what format are the fibers stored?

 

Thanks

9:47 am
March 31, 2010


Ruopeng

Admin

posts 353

Hi, 

There is no such feature in TrackVis. However people have written their own matlab code to read .trk file.

The detailed the description of track file format can be found from here:

http://trackvis.org/docs/?subs…..fileformat

Ruopeng

2:13 am
April 1, 2010


Armen

Member

posts 25

I've been trying to do this for a while now.  I just can't seem to get Matlab to read the data out.  Using 'fopen' and 'fread' gives me a 1897692x1 matrix of numbers.  Those are the only commands that were able to do anything for me.  Any other advice?

Thanks.

12:55 pm
April 1, 2010


Ruopeng

Admin

posts 353

'fread' should be good enough. But you need to follow the description of .trk file format to parse what you read in.

I will put .mat output on my list for next release.

Ruopeng

10:41 pm
April 1, 2010


Armen

Member

posts 25

Post edited 3:56 am – April 5, 2010 by Armen


Ok this is what I have to read the header in Matlab:

 

fid=fopen('dti.trk','r');
%Header

header.id_string=fread(fid,6,'uint8=>char')';

header.dim=fread(fid,3,'uint16=>short')';

header.voxel_size=fread(fid,3,'float')';

header.origin = fread(fid,3,'float')';

header.n_scalars = fread(fid,1,'uint16=>short')';

header.scalar_name = fread(fid,[10,20],'uint8=>char');

header.n_properties = fread(fid,1,'uint16=>short')';

header.property_name = fread(fid,[10,20],'uint8=>char');

header.reserved = fread(fid,508,'uint8=>char');

header.voxel_order = fread(fid,4,'uint8=>char')';

header.pad2 = fread(fid,4,'uint8=>char')';

header.image_orientation_patient = fread(fid,6,'float')';

header.pad1 = fread(fid,2,'uint8=>char');

header.invert_x = fread(fid,1,'uint8=>uchar');

header.invert_y = fread(fid,1,'uint8=>uchar');

header.invert_z = fread(fid,1,'uint8=>uchar');

header.swap_xy = fread(fid,1,'uint8=>uchar');

header.swap_yz = fread(fid,1,'uint8=>uchar');

header.swap_zx = fread(fid,1,'uint8=>uchar');

header.n_count = fread(fid,1,'uint32=>int32');

header.version = fread(fid,1,'uint32=>int32');

header.hdr_size = fread(fid,1,'uint32=>int32');

fclose(fid);

5:51 am
March 31, 2011


nilzzon

New Member

posts 1

This works for me

function s = load_track(file_name_track)

% function s = load_track(file_name_track)

% This function read the structure of the track from file

 

fid = fopen(file_name_track, 'r');

 

if (fid == -1)

    error(['File to open does not exist: ' file_name_track]);

end

 

try

    s.ID            = fread(fid, 6, 'uint8');

    s.dim           = fread(fid, 3, 'int16');

    s.resol         = fread(fid, 3, 'float32');

    s.origin        = fread(fid, 3, 'float32'); 

    s.n_scalars     = fread(fid, 1, 'int16');

    s.scalar_name   = fread(fid, 200, 'uint8');

    s.n_properties  = fread(fid, 1, 'int16');

    s.property_name = fread(fid, 200, 'uint8');

    s.vox_to_ras    = reshape(fread(fid, 16, 'float32'), 4, 4);

    s.reserved      = fread(fid, 508-64, 'uint8');

    s.voxelorder    = fread(fid, 4, 'uint8');

    s.pad1          = fread(fid, 4, 'uint8');

    s.img_ori_pat   = fread(fid, 6, 'float32');

    s.pad2          = fread(fid, 2, 'uint8');

    s.invert_x      = fread(fid, 1, 'uint8'); 

    s.invert_y      = fread(fid, 1, 'uint8'); 

    s.invert_z      = fread(fid, 1, 'uint8'); 

    s.swap_xy       = fread(fid, 1, 'uint8'); 

    s.swap_yz       = fread(fid, 1, 'uint8'); 

    s.swap_zx       = fread(fid, 1, 'uint8'); 

    s.n_track       = fread(fid, 1, 'int32'); 

    s.version       = fread(fid, 1, 'int32');

    s.hdr_size      = fread(fid, 1, 'int32');  

 

    for c_track = 1:s.n_track

 

        % Track    Data type Bytes       Comment

        % Track #1 int       4           Number of points in this track, as m.

        %          float     (3+n_s)*4   Track Point #1. Contains 3 plus n_s float numbers. First 3 float numbers are the x/y/z coordinate of this track point, followed by n_s float numbers representing each of its scalars.

        %          float     (3+n_s)*4   Track Point #2. Same as above.

        %          …       …         …

        %          float     (3+n_s)*4   Track Point #m. Same as above.

        %          float     n_p*4       n_p float numbers representing each of the properties of this track.

        % Track #n    Same as above.

 

        % n is number of points in the track

        n = fread(fid, 1, 'int32'); 

        track = zeros(n, 3 + s.n_scalars); 

 

        for c = 1:n

            track(c,:) = fread(fid, size(track,2), 'float32');

        end

 

        % Save the track and scalars

        s.track{c_track} = track(:,1:3); 

        s.track_scalars{c_track} = track(:,4:end);

 

        % Read the properties

        s.track_properties{c_track} = fread(fid, s.n_properties, 'float32');

 

    end

    fclose(fid);

 

catch me

    fclose(fid);

    rethrow(me);

end

 

 

 

 

1:02 pm
April 22, 2011


johnc

Member

posts 13

I'll add a similar contribution. We like to load the tract groups in the same orientation as the original DWI volumes (for easy FA lookup/etc. in MATLAB), as opposed to always LPS like the TrackVis output:

 

http://github.com/johncolby/al…..trk_read.m

 

Best,

 

John