Skip to content

Instantly share code, notes, and snippets.

@NicoKiaru
Created November 22, 2024 11:56
Show Gist options
  • Save NicoKiaru/7769f139c988dbdabc3f5dc5f0120daa to your computer and use it in GitHub Desktop.
Save NicoKiaru/7769f139c988dbdabc3f5dc5f0120daa to your computer and use it in GitHub Desktop.
/**
* This script takes a 3D input a stack with beads and will output a stack of the same size
* with single points placed at the center of detected beads / spots
* this is the first step to compute a clean PSF with distillation.
*
* The rest of the PSF computation happens in the script ExtractPSF
*
* For detecting beads in 3D the method used is the Laplacian of Gaussian detector by Trackmate which has 2 parameters
* It can be useful to use the TrackMate GUI before using this script in order to check which parameters to use.
*
* Example for this script:
* - Image = https://zenodo.org/records/14203207/files/30x1000_FS-025_FW-60_AC-170.tif
* - Spot diameter = 1.0
* - Quality = 1000
*
* Link to useful threads:
* https://forum.image.sc/t/clij-deconvolution-regularization-fiji-crash/55155/20
* Intro to deconvolution by Brian Northan:
* https://forum.image.sc/t/getting-started-with-deconvolution-in-imagej/32916/1
*
* @author Nicolas Chiaruttini, EPFL, 2024
* */
#@ImagePlus(label="Image") myImg
#@Double(label="Estimated spot diameter", style="format:0.00", value=1.0) blobDiameter
#@Double(label="Spot quality threshold", style="format:0.00", value=1000) qualityTr
#@output ImagePlus psfimage
// ---- Preparation
roiManager = RoiManager.getRoiManager();
if (roiManager==null) {
roiManager = new RoiManager();
}
roiManager.reset(); // -> bug
def spotList = detectSpotsAndPutInRoiManager(myImg,blobDiameter, qualityTr, true)
def psfImage = getChannel(1, myImg)
IJ.run(psfImage, "Multiply...", "value=0 stack")
for (spot in spotList) {
xp = spot.getDoublePosition(0) / cal.pixelWidth
yp = spot.getDoublePosition(1) / cal.pixelHeight
zp = spot.getDoublePosition(2) / cal.pixelDepth
psfImage.getStack().getProcessor((int)(zp+1)).putPixelValue((int)xp,(int)yp,255.0 as float)
}
def ImagePlus getChannel(int channel_index, ImagePlus img_in) {
IJ.run(img_in, "Duplicate...", "duplicate channels="+channel_index)
IJ.run("Grays")
chImage = IJ.getImage()
IJ.run(chImage, "Enhance Contrast", "saturated=0.35")
return chImage
}
def List< Spot > detectSpotsAndPutInRoiManager(ImagePlus img_in, double blobDiameter, double fluoTr, boolean displayInROIManager) {
// Uses LoG detection by Trackmate to detect the spots
// See here for the scripting: https://imagej.net/Scripting_TrackMate
// Trackmate is made to track spots... but here this tracking part will not be used
// Set the parameters for LogDetector
img = ImageJFunctions.wrap(img_in)
interval = img
cal = img_in.getCalibration()
// Get the calibration from the metadata if exists
def calibration = [cal.pixelWidth, cal.pixelHeight, cal.pixelDepth] as double[]
// Values to enter based on the TrackMate GUI
radius = blobDiameter / 2.0 // the radius is half the diameter
threshold = fluoTr
doSubpixel = false
doMedian = false
detector = new LogDetector(img, interval, calibration, radius, threshold, doSubpixel, doMedian)
// Start processing and display the results
if (detector.process()) { // does the job and returns true if everything's ok
// Get the list of peaks found
peaks = detector.getResult()
// Add points to ROI manager
if (displayInROIManager) {
rm = RoiManager.getInstance()
if (rm==null) {
rm = new RoiManager()
}
rm.reset();
}
// Loop through all the peak that were found
for (peak in peaks) {
// Add the current peak to the Roi manager
roi = new PointRoi(peak.getDoublePosition(0) / cal.pixelWidth, peak.getDoublePosition(1) / cal.pixelHeight)
// Set the Z position of the peak otherwise the peaks are all set on the same slice
roi.setPosition((int)(Math.round(peak.getDoublePosition(2) / cal.pixelDepth))+1)
if (displayInROIManager) {
rm.addRoi(roi)
}
}
return peaks;
} else {
print("The detector could not process the data.")
return null;
}
}
import fiji.plugin.trackmate.detection.LogDetector
import net.imglib2.img.display.imagej.ImageJFunctions
import ij.plugin.frame.RoiManager
import ij.gui.PointRoi
import java.util.ArrayList
import ij.IJ
import ij.ImagePlus
import ij.plugin.frame.RoiManager
import ij.Prefs
import fiji.plugin.trackmate.Spot
import net.imglib2.img.Img
import net.imglib2.roi.Masks
import net.imglib2.roi.Regions
import net.imglib2.roi.geom.GeomMasks
import net.imglib2.img.display.imagej.ImageJFunctions //IL
import net.imglib2.view.Views
import net.imglib2.util.Intervals
import ij.IJ
import net.imglib2.view.ExtendedRandomAccessibleInterval
import fiji.plugin.trackmate.SelectionModel
import fiji.plugin.trackmate.visualization.hyperstack.HyperStackDisplayer
import fiji.plugin.trackmate.Model
import ij.WindowManager
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment