TrackMate binary detector not finding spots in script

imagej
trackmate

#1

All,

I am trying to utilize the “binary detector” spot detector developed by Ronny Sczech and linked to from the Trackmate ImageJ homepage as an enhancement. I placed Ronny’s .jar file in my /plugins/jar folder.

I would like to run TrackMate in batch mode from my below script but my script is not detecting spots (i.e., the Log window indicates “Found 0 spots”) when I use the binary detector. When I run TrackMate in GUI mode the binary detector is finding the spots but not in my script. I am very new to Pyhthon, any help would be much appreciated.

Ronny’s source code is on Github at https://github.com/chicoronny/RonnyTrackMate.

Thanks,
Daniel

Code:

# Needed packages
import glob # 
import os
import sys
from ij import IJ, ImagePlus
import java.io.File as File
from ij.gui import GenericDialog
# Trackmate dependencies
import fiji.plugin.trackmate.Spot as Spot
from fiji.plugin.trackmate import Model
from fiji.plugin.trackmate import Settings
from fiji.plugin.trackmate import TrackMate
from fiji.plugin.trackmate import SelectionModel
from fiji.plugin.trackmate import Logger
import fiji.plugin.trackmate.visualization.hyperstack.HyperStackDisplayer as HyperStackDisplayer
import fiji.plugin.trackmate.features.FeatureFilter as FeatureFilter
# Spot detectors
from fiji.plugin.trackmate.detection import LogDetectorFactory
from fiji.plugin.trackmate.detection import BinaryDetectorFactory
#from fiji.plugin.trackmate.detection import BinaryDetector
# Tracking routines
from fiji.plugin.trackmate.tracking import LAPUtils
from fiji.plugin.trackmate.tracking.sparselap import SparseLAPTrackerFactory
# Spot Analyzers
import fiji.plugin.trackmate.features.spot.SpotContrastAnalyzerFactory as SpotContrastAnalyzerFactory
import fiji.plugin.trackmate.features.spot.SpotContrastAndSNRAnalyzerFactory as SpotContrastAndSNRAnalyzerFactory
import fiji.plugin.trackmate.features.spot.SpotIntensityAnalyzerFactory as SpotIntensityAnalyzerFactory
import fiji.plugin.trackmate.features.spot.SpotMorphologyAnalyzerFactory as SpotMorphologyAnalyzerFactory
import fiji.plugin.trackmate.features.spot.SpotRadiusEstimatorFactory as SpotRadiusEstimatorFactory
# Edge Analyzers
import fiji.plugin.trackmate.features.edges.EdgeTargetAnalyzer as EdgeTargetAnalyzer
import fiji.plugin.trackmate.features.edges.EdgeTimeLocationAnalyzer as EdgeTimeLocationAnalyzer
import fiji.plugin.trackmate.features.edges.EdgeVelocityAnalyzer as EdgeVelocityAnalyzer
# Track Analyzers
import fiji.plugin.trackmate.features.track.TrackDurationAnalyzer as TrackDurationAnalyzer
import fiji.plugin.trackmate.features.track.TrackBranchingAnalyzer as TrackBranchingAnalyzer
import fiji.plugin.trackmate.features.track.TrackIndexAnalyzer as TrackIndexAnalyzer
import fiji.plugin.trackmate.features.track.TrackLocationAnalyzer as TrackLocationAnalyzer
import fiji.plugin.trackmate.features.track.TrackSpeedStatisticsAnalyzer as TrackSpeedStatisticsAnalyzer
import fiji.plugin.trackmate.features.track.TrackSpotQualityFeatureAnalyzer as TrackSpotQualityFeatureAnalyzer
# XML writing packages
import fiji.plugin.trackmate.action.ExportTracksToXML as ExportTracksToXML
import fiji.plugin.trackmate.io.TmXmlWriter as TmXmlWriter

srcDir = IJ.getDirectory("Input_directory")
#print(srcDir+"*.tif")

# Print present working directory
#cwd = os.getcwd()
#print("What's the current working directory")
#print(cwd)

# Start of Loop Part
#print("Below is the results of the loop")

# Loop through that list made above, preforming your user specified actions

# Using glob
# Read all the .avi files into a list
# mylist = glob.glob('C:\\Users\dlivsey\Desktop\Test\*.tif')
mylist = glob.glob(srcDir+"*.tif")

# The loop
for df in mylist:
    # Paste everything you want to do to each file below here
	
	#### set parameters:
	# USE DECIMALS FOR DOUBLES (e.g., Linking Max distance must be double, Target channel is integer)
	### spot detection 
	DO_SUBPIXEL_LOCALIZATION = False
	RADIUS = .05 # GUI Uses Diameter, Divide by half for radius
	THRESHOLD = 0.2
	DO_MEDIAN_FILTERING = True
	TARGET_CHANNEL = 1
	
	### LAP tracker
	LINKING_MAX_DISTANCE = 4.0
	#gap closing
	ALLOW_GAP_CLOSING = False
	GAP_CLOSING_MAX_DISTANCE = 10.
	MAX_FRAME_GAP = 2
	#splitting
	ALLOW_TRACK_SPLITTING = False	
	SPLITTING_MAX_DISTANCE = 0.0
	#merging
	ALLOW_TRACK_MERGING = False
	MERGING_MAX_DISTANCE = 10.    
	
	# Open Image
	imp = IJ.openImage(df)
	imp.show()
	# Inverting image allows Trackmate to ID particles correctly
	# see https://forum.image.sc/t/possible-to-use-trackmate-to-track-oval-particles/6495/7
	#IJ.run("Invert", "stack") 
	#print imp.getDimensions()
	#print imp.getCalibration
	
	#----------------------------
	# Create the model object now
	#----------------------------
	    
	# Some of the parameters we configure below need to have
	# a reference to the model at creation. So we create an
	# empty model now.
	    
	model = Model()
	model.setPhysicalUnits("mm","secs")   # Settings and Model units need to be defined seperately
	#print(model.getSpaceUnits())
	
	# Send all messages to ImageJ log window.
	model.setLogger(Logger.IJ_LOGGER) 
	       
	#------------------------
	# Prepare settings object
	#------------------------
	       
	settings = Settings()
	settings.setFrom(imp) # Settings and Model units need to be defined seperately, set dimensions from file
	       
	# Configure detector - We use the Strings for the keys
	#settings.detectorFactory = LogDetectorFactory()
	#settings.detectorSettings = { 
	#    'DO_SUBPIXEL_LOCALIZATION' : DO_SUBPIXEL_LOCALIZATION,
	#    'RADIUS' : RADIUS,
	#    'TARGET_CHANNEL' : TARGET_CHANNEL,
	#    'THRESHOLD' : THRESHOLD,
	#    'DO_MEDIAN_FILTERING' : DO_MEDIAN_FILTERING,
	#}  

	# Set below settings = Settings() and settings.setFrom(imp)
	settings.detectorFactory = BinaryDetectorFactory()
	settings.detectorSettings = { 
	    'minSize' : 1,
	    'maxSize' : 100000,
	    'circMin' : 0.0,
	    'circMax' : 1.0,
	}  	    
	
	# Configure spot filters - Classical filter on quality
#	filter1 = FeatureFilter('QUALITY', 0.25, True)
#	settings.addSpotFilter(filter1)
	     
	# Configure tracker
	settings.trackerFactory = SparseLAPTrackerFactory()
	settings.trackerSettings = LAPUtils.getDefaultLAPSettingsMap() # almost good enough
	settings.trackerSettings['LINKING_MAX_DISTANCE'] = LINKING_MAX_DISTANCE
	settings.trackerSettings['LINKING_FEATURE_PENALTIES'] = {}
	#gap closing
	settings.trackerSettings['ALLOW_GAP_CLOSING'] = ALLOW_GAP_CLOSING
	settings.trackerSettings['GAP_CLOSING_MAX_DISTANCE'] = GAP_CLOSING_MAX_DISTANCE
	settings.trackerSettings['MAX_FRAME_GAP'] = MAX_FRAME_GAP
	settings.trackerSettings['GAP_CLOSING_FEATURE_PENALTIES'] = {}
	#splitting
	settings.trackerSettings['ALLOW_TRACK_SPLITTING'] = ALLOW_TRACK_SPLITTING
	settings.trackerSettings['SPLITTING_MAX_DISTANCE'] = SPLITTING_MAX_DISTANCE
	settings.trackerSettings['SPLITTING_FEATURE_PENALTIES'] = {}
	#merging
	settings.trackerSettings['ALLOW_TRACK_MERGING'] = ALLOW_TRACK_MERGING
	settings.trackerSettings['MERGING_MAX_DISTANCE'] = MERGING_MAX_DISTANCE
	settings.trackerSettings['MERGING_FEATURE_PENALTIES'] = {}

	# ADD ALL ANALYZERS    
	# Spot Analyzers
	settings.addSpotAnalyzerFactory(SpotContrastAnalyzerFactory())
	settings.addSpotAnalyzerFactory(SpotIntensityAnalyzerFactory())
	settings.addSpotAnalyzerFactory(SpotContrastAndSNRAnalyzerFactory())
	settings.addSpotAnalyzerFactory(SpotMorphologyAnalyzerFactory())
	settings.addSpotAnalyzerFactory(SpotRadiusEstimatorFactory())
	# Edge Analyzers
	settings.addEdgeAnalyzer(EdgeTargetAnalyzer())
	settings.addEdgeAnalyzer(EdgeTimeLocationAnalyzer())
	settings.addEdgeAnalyzer(EdgeVelocityAnalyzer())
	# Track Analyzers
	settings.addTrackAnalyzer(TrackBranchingAnalyzer())
	settings.addTrackAnalyzer(TrackDurationAnalyzer())
	settings.addTrackAnalyzer(TrackIndexAnalyzer())
	settings.addTrackAnalyzer(TrackLocationAnalyzer())
	settings.addTrackAnalyzer(TrackSpeedStatisticsAnalyzer())
	settings.addTrackAnalyzer(TrackSpotQualityFeatureAnalyzer())

	# Configure track filters - We want to get rid of the two immobile spots at 
	# the bottom right of the image. Track displacement must be above 10 pixels.
	#filter2 = FeatureFilter('TRACK_DISPLACEMENT', 10, True)
	#settings.addTrackFilter(filter2)
	    
	#-------------------
	# Instantiate plugin
	#-------------------    
	trackmate = TrackMate(model, settings)
	       
	#--------
	# Process
	#--------
	    
	ok = trackmate.checkInput()
	if not ok:
	    sys.exit(str(trackmate.getErrorMessage()))
	    
	ok = trackmate.process()
	if not ok:
	    sys.exit(str(trackmate.getErrorMessage()))
	     
	#----------------
	# Display results
	#----------------
	
	selectionModel = SelectionModel(model)
	displayer =  HyperStackDisplayer(model, selectionModel, imp)
	displayer.render()
	displayer.refresh()
	#IJ.run("Invert", "stack") # revert image (see comment above)     
	# Export to xml
	filename = imp.title
	outputFolder= srcDir
	outFile = File(outputFolder, filename+"exportTracks.xml")
	ExportTracksToXML.export(model, settings, outFile)
	outFile = File(outputFolder, filename+"exportModel.xml")
	writer = TmXmlWriter(outFile)
	writer.appendModel(model)
	writer.appendSettings(settings)
	writer.writeToFile()
	#imp.close()

	# Echo results with the logger we set at start:
	#model.getLogger().log(str(model))
	print("DONE")

Substack.tif (5.5 MB)