Trackmate Trackfilter in Matlab Script

Hi everyone!

I used the LAP tracker to connect the spots. But when I add trackfilter such as TRACK_DURATION and TRACK_DISPLACEMENT, it won’t work well. In fact, the result echoing back seems that there still exist some tracks which haven’t been filted.

Here’s my total code. I use filter3 to set the TRACK_DURATION at 100, but as a result there’re some tracks’ duration under 10 frames. That’s weird. Did I miss some settings?

ImageJ;

import java.util.HashMap
import ij.*
import ImagePlus.*
import fiji.plugin.trackmate.*
import fiji.plugin.trackmate.detection.*
import fiji.plugin.trackmate.tracking.sparselap.*
import fiji.plugin.trackmate.tracking.*
import fiji.plugin.trackmate.visualization.hyperstack.*
import fiji.plugin.trackmate.providers.*
import fiji.plugin.trackmate.features.*
import fiji.plugin.trackmate.features.spot.*
import fiji.plugin.trackmate.action.*
import fiji.plugin.trackmate.io.*
import fiji.plugin.trackmate.features.track.*
import fiji.plugin.trackmate.util.*

% ---------------------------------------
% Pick a source image
% ---------------------------------------

% Get currently selected image

imp = IJ.openImage('test.tif');
imp_nChannels = imp.getNChannels;
imp_nSlices = 1;
imp_nFrames = imp.getNSlices;
imp.setDimensions(imp_nChannels,imp_nSlices,imp_nFrames);
% imp.show()

% -------------------------
% Instantiate model object
% -------------------------
   
model = Model();
   
% Set logger
model.setLogger(Logger.IJ_LOGGER)
   
% ------------------------
%  Prepare settings object
% ------------------------
      
settings = Settings();
settings.setFrom(imp);

% Configure detector - We use a java map
settings.detectorFactory = LogDetectorFactory();
map = HashMap();
map.put('DO_SUBPIXEL_LOCALIZATION', true);
map.put('RADIUS', 4.);
map.put('TARGET_CHANNEL', 1);
map.put('THRESHOLD', 1.);
map.put('DO_MEDIAN_FILTERING', false);
settings.detectorSettings = map;
     
% Configure spot filters - Classical filter on quality
filter1 = FeatureFilter('QUALITY', 5.0, true);
settings.addSpotFilter(filter1);
      
% Configure tracker - We want to allow splits and fusions
settings.trackerFactory = SparseLAPTrackerFactory();
settings.trackerSettings = LAPUtils.getDefaultLAPSettingsMap(); % almost good enough
% settings.trackerSettings.put('ALLOW_TRACK_SPLITTING', false);
% settings.trackerSettings.put('ALLOW_TRACK_MERGING', false);
settings.trackerSettings.put('LINKING_MAX_DISTANCE',15.0);
settings.trackerSettings.put('GAP_CLOSING_MAX_DISTANCE',15.0);
settings.trackerSettings.put('MAX_FRAME_GAP',java.lang.Integer(3));
 
spotAnalyzerProvider = SpotAnalyzerProvider();
for key = 0:spotAnalyzerProvider.getKeys().size-1
    settings.addSpotAnalyzerFactory(spotAnalyzerProvider.getFactory(spotAnalyzerProvider.getKeys().get(key)));
end
edgeAnalyzerProvider = EdgeAnalyzerProvider();
for  key = 0:edgeAnalyzerProvider.getKeys().size-1
    settings.addEdgeAnalyzer(edgeAnalyzerProvider.getFactory(edgeAnalyzerProvider.getKeys().get(key)));
end
trackAnalyzerProvider = TrackAnalyzerProvider();
for key = 0:trackAnalyzerProvider.getKeys().size-1
    settings.addTrackAnalyzer(trackAnalyzerProvider.getFactory(trackAnalyzerProvider.getKeys().get(key)));
end

% settings.initialSpotFilterValue = java.lang.Double(1);

filter2 = FeatureFilter('TRACK_DISPLACEMENT',10,true);
settings.addTrackFilter(filter2);

filter3 = FeatureFilter('TRACK_DURATION',100,true);
settings.addTrackFilter(filter3);

%-------------------
% Instantiate plugin
%-------------------
     
trackmate = TrackMate(model, settings);
        
%--------
% Process
%--------

ok = trackmate.checkInput();
if ~ok
    display(trackmate.getErrorMessage());
end
  
ok = trackmate.process();
if ~ok
    display(trackmate.getErrorMessage());
end

%----------------
% Display results
%----------------
model.getLogger().log(strcat('Found ',num2str(model.getTrackModel().nTracks(true)),' tracks.'));

selectionModel = SelectionModel(model);
displayer = HyperStackDisplayer(model, selectionModel, imp2);
displayer.render();
displayer.refresh();
     
% Echo results
fm = model.getFeatureModel();

Any hints would be appreciated.

Best Regards!

Hello @Andrew_li

Beautiful script!
It should work normally so I don’t know what is wrong.
Maybe the frame interval and the time units are not set correctly? Could you check this?
best

You mean that the parameter for filtering the TRACK_DURATION does not mean how many frames?

By the way, is there any command to clear the memory occupied by java object rather than reopen the matlab?

Thanks.

Nope, I mean that this feature is in physical units: seconds, minutes, etc. whatever is set in the Image Properties.

Sorry I don’t know.

How can I check the Image Properties and set them into a proper unit? I check the model.getTimeUnits, that gives me ‘frames’.

Can you check the value of imp.getCalibration().frameInterval?

This sentense gave me 0.

Tadam!

Set it to 1.
sdaskdlkaajddalskdjalskdjasld