Interactive tracking followed by scripting TrackMate

I’m trying to find a way to interactively detect spots using the beautiful user interface in TrackMate, but then take a given SpotCollection and do some downstream processing using scripting, such as running a segmentation, plotting intensity distributions etc.

Ideally, I want to avoid having to display or save the list of spots, but just:

  • browse through the hyperstack displayer that’s showing my spots and tracks as overlay
  • go forth and back changing parameters until I’m satisfied
  • run a script that takes the current TrackMate instance, or some other way to get at the given Model or SpotCollection

I was looking at TrackMateRunner_, the class that starts TrackMate from the UI, but it doesn’t have any method that would return the TrackMate object it starts. All the scripts on the Scripting TrackMate wiki page actually work the opposite way: they create a TrackMate programmatically and set all the parameters in the script, thereby taking away the interactivity (because they’re intended for automation).


The best way I see to accomplish this is replicating these lines from TrackMatePlugin_:

… and then keeping that instance of TrackMate for later processing (likely using a WaitForUserDialog or some such).

Is there a better way, @tinevez? If not, would it make sense to add some code to retrieve a currently running (i.e. displayed) TrackMate instance?

Does anybody have similar use cases?

Hello @imagejan

I know that there are no existing mechanism to catch the TrackMate instance that was launched from the menu. And so far I have been doing it like you did: Creating a TrackMate instance from a script, then launching the GUI from the script.

IJ1 has a convenient WindowManager class that does something like this for IJ. But I fear that TrackMate is not robust enough when it comes to multiple instances running within one IJ1 session. I don’t think I got it right.

This reminds me that we should think of something like this but clean for Mastodon…

Thanks @tinevez!

For what it’s worth, I ended up using SciJava’s ObjectService to keep track of the TrackMate instance.

After starting TrackMate using a script like this one:

#@ ImagePlus imp
#@ ObjectService os
#@ ScriptService scriptService

import fiji.plugin.trackmate.Model
import fiji.plugin.trackmate.Settings
import fiji.plugin.trackmate.TrackMate
import fiji.plugin.trackmate.gui.TrackMateGUIController

/*
 * Add alias to allow easy script parameter usage
 */
scriptService.addAlias(TrackMate.class)

/*
 * Create TrackMate instance linked to imp
 */
model = new Model()
settings = new Settings()
settings.setFrom(imp)
trackmate = new TrackMate(model, settings)

/*
 * Add current TrackMate instance to ObjectService
 * for later retrieval
 */
os.addObject(trackmate)

/*
 * Start TrackMate GUI
 */
controller = new TrackMateGUIController( trackmate )
controller.getGUI()

… you can then ask for the open TrackMate instance using a #@ TrackMate script parameter:

#@ TrackMate trackmate

for (spot in trackmate.getModel().getSpots().iterable(true)) {
  // do some processing with all the visible spots from the GUI
}
1 Like

?!?!? This is again SciJava black-magic?

Can we integrate it directly in TrackMate source code?

Probably yes. In this case, we could even do it a bit cleaner and remove the TrackMate object from the object index when it is closed… :slightly_smiling_face: