Help with Groovy scripting for TrackMate

Hi all,

Continuing with the Groovy scripting rewrite, I’m getting stuck when trying to convert a function I had in Python in Groovy. I found some help from @imagejan here Jython macro for multi-channel TrackMate analysis but I’m still not managing to get back the seeds from the TrackMate log detector.

Here is the python function

def cellDetection3D(implus, rad, thresh, subpix, med):
    """Function to detect the cells in 3D using TrackMate

    Arguments:
        implus {imagePlus} -- ImagePlus of the image to use for detection
        rad    {int}       -- Radius of the cell to detect, half the diameter
        thresh {int}       -- Intensity threshold for the detection
        subpix {bool}      -- Option for subpixel detection
        med {bool}         -- Option for median filter before detection

    Returns:
        cellCount {int} -- Number of cells found
    """
    dim = implus.getDimensions()
    cal = implus.getCalibration()

    implus2 = implus.duplicate()

    # Set the parameters for LogDetector
    img           = ImageJFunctions.wrap(implus2)
    interval      = img
    # cal           = implus.getCalibration()
    # calibration = [round(cal.pixelWidth,3), round(cal.pixelHeight,3), round(cal.pixelDepth,3)]
    calibration   = [cal.pixelWidth, cal.pixelHeight, cal.pixelDepth]

    radius     = rad  # the radius is half the diameter
    threshold  = thresh
    doSubpixel = subpix
    doMedian   = med

    # print cal.pixelDepth

    # Setup spot detector (see http://javadoc.imagej.net/Fiji/fiji/plugin/trackmate/detection/LogDetector.html)
    #
    # public LogDetector(RandomAccessible<T> img,
    #            Interval interval,
    #            double[] calibration,
    #            double radius,
    #            double threshold,
    #            boolean doSubPixelLocalization,
    #            boolean doMedianFilter)

    detector = LogDetector(img, interval, calibration, radius,
                           threshold, doSubpixel, doMedian)

    # Start processing and display the results
    if detector.process():
        # Get the list of peaks found
        peaks = detector.getResult()
        # print str(len(peaks)), "peaks were found."

        # Add points to ROI manager
        rm = RoiManager.getInstance()
        if not rm:
            rm = RoiManager()
        rm.reset()

        # Loop through all the peak that were found
        for peak in peaks:
            # Print the current coordinates
            # print peak.getDoublePosition(0), peak.getDoublePosition(1), peak.getDoublePosition(2)
            # Add the current peak to the Roi manager
            roi = PointRoi(peak.getDoublePosition(0) / cal.pixelWidth,
                           peak.getDoublePosition(1) / cal.pixelHeight)
            # print peak.getDoublePosition(2)/cal.pixelDepth
            roi.setPosition(
                int(round(peak.getDoublePosition(2) / cal.pixelDepth)) + 1)
            rm.addRoi(roi)
        # Show all ROIs on the image
        # rm.runCommand(imp2, "Show All")
        cellCount = rm.getCount()
        # Close the duplicate
        implus2.changes = False
        implus2.close()
    else:
        print "The detector could not process the data."

    # create an empty black (all intensities are 0) hyperstack with the same dimensions as the original
    markerimage = IJ.createImage(
        "Markerimage", "8-bit black", dim[0], dim[1], dim[2], dim[3], dim[4])

    # set the forground color to 255 for the fill command
    # resulting image will be binary
    IJ.setForegroundColor(255, 255, 255)
    for roi in range(rm.getCount()):
        # TODO: requires A (any) image window to be shown. not clean...
        rm.select(roi)
        rm.runCommand(markerimage, "Fill")
    # markerimage.show()
    IJ.run(markerimage, "Select None", "")
    IJ.run(implus, "Select None", "")
    return markerimage

In Groovy I’m getting a NullPointerException when running this

/**
 * Function to detect the seeds in 3D using TrackMate
 * @param implus ImagePlus of the image to use for detection
 * @param rad Radius of the cell to detect, half the diameter
 * @param thresh Intensity threshold for the detection
 * @param subpix Option for subpixel detection
 * @param med Option for median filter before detection
 * @return ImagePlus of the marker image
 */
def cellDetection3D(ImagePlus implus, rad, thresh, subpix, med)
{
    dim     = implus.getDimensions()
    cal     = implus.getCalibration()
    implus2 = implus.duplicate()

    settings = new Settings()
    settings.setFrom(implus)
    settings.dt = 0.05

    settings.detectorFactory = new LogDetectorFactory()
    // Get the info about the settings
    settings.detectorSettings = settings.detectorFactory.getDefaultSettings()
    settings.detectorSettings['RADIUS'] = rad
    settings.detectorSettings['THRESHOLD'] = (double) thresh
    settings.detectorSettings['DO_SUBPIXEL_LOCALIZATION'] = subpix
    settings.detectorSettings['DO_MEDIAN_FILTERING'] = med
    // Print all settings. Helpful for finding what settings are there
    // println settings.detectorSettings

    model = new Model()
    trackmate = new TrackMate(model, settings)

    println "Input"
    println trackmate.checkInput()
    println "Process"
    println trackmate.process()
    println "Error messages"
    println trackmate.getErrorMessage()

Thanks a lot !

I actually managed to make it work. Turns out that my variable calibration was an ArrayList and the LogDetector needed a double[].

1 Like