Skip to content

Commit

Permalink
Rework the trackmateSpot MATLAB example with ROIs.
Browse files Browse the repository at this point in the history
  • Loading branch information
tinevez committed May 13, 2024
1 parent 43f14d0 commit d8f7e49
Showing 1 changed file with 28 additions and 62 deletions.
90 changes: 28 additions & 62 deletions scripts/ExampleScript_MATLABImportROIs.m
Original file line number Diff line number Diff line change
@@ -1,95 +1,61 @@
%% Import spot ROIs from a TrackMate file.
%
% This example MATLAB script shows how to use the `trackmateSpot.m`
% function to open a TrackMate file that contains spots with ROIs (their
% shape as polygon). They are then painted in a common graph, colored by
% frame.
%
% The `trackmateSpot.m` does not return the track information, and we
% cannot retrieve the tracks solely with this function. See the other
% example `ExampleScript_MATLABImportROIs.m` in this folder for a more
% involved example that does this.
%
% Jean-Yves Tinevez - 2024

close all
clear
clc

% Read movie file
movie_path = 'c:/Users/tinevez/Desktop/MAX_Merged-1.tif';
I = tiffreadVolume( movie_path );
w = size(I, 1);
h = size(I, 2);

% Pixel size is manually specified.
pixel_size = 0.1984666072986889; % microns
% Where are the files?
root_folder = '/Users/tinevez/Google Drive/TrackMate/Applications/MATLABimport/';
% TrackMate file.
file_path = fullfile( root_folder, 'MAX_Merged.xml' );

% Read tracks.
file_path = 'c:/Users/tinevez/Desktop/MAX_Merged.xml';
[S, idmap, rois] = trackmateSpots( file_path );

n_frames = max(S.FRAME)+1;
colors = turbo(n_frames);

n_spots = height(S);

%%

F(n_frames) = struct('cdata',[],'colormap',[]);

figure
hold on

for t = 1 : n_frames

% Clear axes.
cla

% Image data of current frame.
If = I( :, :, t );
imshow( If, [], ...
'XData', [ 0 pixel_size * h ], ...
'YData', [ 0 pixel_size * w ] )

% Spot data of current frame.
frame = t-1;
log_index = S.FRAME == frame;
Sf = S(log_index, :);
index = find( log_index );

nsf = height( Sf );
for i = 1 : nsf
x = Sf.POSITION_X(i);
y = Sf.POSITION_Y(i);
roi = rois{ index(i) };
roi(:,1) = roi(:,1) + x;
roi(:,2) = roi(:,2) + y;

patch( 'XData', roi(:,1), 'YData', roi(:,2), ...
'FaceColor', 'None', ...
'EdgeColor', 'r', ...
'LineWidth', 2, ...
'Marker', 'none')

end
drawnow
F(t) = getframe(gca);

end

cla
movie( F, 10, 3 )



%%
%% Plot the spot contours.

figure
hold on
axis square

for i = 1 : n_spots

% Spot center.
x = S.POSITION_X(i);
y = S.POSITION_Y(i);
% Spot contour (centered on 0).
roi = rois{i};
% Put the contours with respect to the spot center.
roi(:,1) = roi(:,1) + x;
roi(:,2) = roi(:,2) + y;


% Color by frame.
frame = S.FRAME(i)+1;
col = colors( frame, : );

patch( 'XData', roi(:,1), 'YData', roi(:,2), ...
'FaceColor', 'None', ...
'EdgeColor', col )

plot( x, y, 'Color', col, 'Marker', 'x' )

end
end
xlabel( 'X (um)' )
ylabel( 'Y (um)' )
set(gca, 'YDir', 'reverse' )

0 comments on commit d8f7e49

Please sign in to comment.