Class SomaUtils

java.lang.Object
sc.fiji.snt.tracing.auto.SomaUtils

public class SomaUtils extends Object
Utilities for automatic soma detection and ROI generation.

Provides methods to detect the soma (cell body) in fluorescent images using a combined EDT-intensity approach, flood fill the region, extract contours, and convert to ImageJ ROIs.

Three detection modes are available:
Author:
Tiago Ferreira
See Also:
  • Field Details

  • Method Details

    • detectSoma

      public static SomaUtils.SomaResult detectSoma(net.imagej.ImgPlus<?> source, double threshold, int zSlice)
      Full soma detection using ImgPlus with spacing extracted automatically.

      Convenience method that extracts spacing from the ImgPlus calibration.

      Parameters:
      source - input image (2D or 3D ImgPlus)
      threshold - intensity threshold. Use -1 for Otsu, NaN for mean.
      zSlice - Z-slice to use (0-indexed), or -1 for middle slice
      Returns:
      SomaResult containing center, mask, contour, and radius; or null if detection fails
      See Also:
    • detectSoma

      public static SomaUtils.SomaResult detectSoma(net.imagej.ImgPlus<?> source)
      Full soma detection using ImgPlus with automatic thresholding.

      Convenience method using Otsu's threshold and spacing from ImgPlus calibration.

      Parameters:
      source - input image (2D or 3D ImgPlus)
      Returns:
      SomaResult containing center, mask, contour, and radius; or null if detection fails
      See Also:
    • detectSoma

      public static SomaUtils.SomaResult detectSoma(net.imglib2.RandomAccessibleInterval<? extends net.imglib2.type.numeric.RealType<?>> source, double threshold, double[] spacing)
      Full soma detection: finds center, floods region, extracts contour.
      Parameters:
      source - input image (2D)
      threshold - intensity threshold. Use -1 for Otsu, NaN for mean.
      spacing - voxel spacing [x, y], or null for unit spacing
      Returns:
      SomaResult containing center, mask, contour, and radius; or null if detection fails
    • detectSomaAt

      public static SomaUtils.SomaResult detectSomaAt(net.imglib2.RandomAccessibleInterval<? extends net.imglib2.type.numeric.RealType<?>> source, long seedX, long seedY, double threshold, double[] spacing)
      Detects soma at a specific seed point.

      Useful for interactive detection where user clicks near a soma.

      Parameters:
      source - input image (2D)
      seedX - X coordinate of seed point (voxels)
      seedY - Y coordinate of seed point (voxels)
      threshold - intensity threshold. Use -1 for Otsu, NaN for mean.
      spacing - voxel spacing [x, y], or null for unit spacing
      Returns:
      SomaResult, or null if seed is in background or detection fails
    • detectSomaAt

      public static SomaUtils.SomaResult detectSomaAt(net.imagej.ImgPlus<?> source, long seedX, long seedY, double threshold, int zSlice)
      Detects soma at a specific seed point using ImgPlus.
      Parameters:
      source - input image (2D or 3D ImgPlus)
      seedX - X coordinate of seed point (voxels)
      seedY - Y coordinate of seed point (voxels)
      threshold - intensity threshold. Use -1 for Otsu, NaN for mean.
      zSlice - Z-slice to use (0-indexed), or -1 for middle slice
      Returns:
      SomaResult, or null if seed is in background or detection fails
    • detectAllSomas

      public static List<SomaUtils.SomaResult> detectAllSomas(net.imglib2.RandomAccessibleInterval<? extends net.imglib2.type.numeric.RealType<?>> source, double threshold, double[] spacing, double minRadius)
      Detects all somas using EDT local maxima approach. Finds local maxima in the distance transform - these correspond to centers of thick structures (somas), not thin structures (neurites).
      Parameters:
      source - input image (2D)
      threshold - intensity threshold. Use -1 for Otsu, NaN for mean.
      spacing - voxel spacing [x, y], or null for unit spacing
      minRadius - minimum soma radius in voxels to include (filters small debris)
      Returns:
      list of SomaResult, sorted by radius (largest first); empty list if none found
    • detectAllSomas

      public static List<SomaUtils.SomaResult> detectAllSomas(net.imglib2.RandomAccessibleInterval<? extends net.imglib2.type.numeric.RealType<?>> source, double threshold, double[] spacing)
      Detects all somas using default minimum radius.
      Parameters:
      source - input image (2D)
      threshold - intensity threshold. Use -1 for Otsu, NaN for mean.
      spacing - voxel spacing [x, y], or null for unit spacing
      Returns:
      list of SomaResult, sorted by radius (largest first)
    • detectAllSomas

      public static List<SomaUtils.SomaResult> detectAllSomas(net.imagej.ImgPlus<?> source, double threshold, int zSlice, double minRadius)
      Detects all somas using ImgPlus with automatic spacing.
      Parameters:
      source - input image (2D or 3D ImgPlus)
      threshold - intensity threshold. Use -1 for Otsu, NaN for mean.
      zSlice - Z-slice to use (0-indexed), or -1 for middle slice
      minRadius - minimum soma radius in voxels
      Returns:
      list of SomaResult, sorted by radius (largest first)
    • detectAllSomas

      public static List<SomaUtils.SomaResult> detectAllSomas(net.imagej.ImgPlus<?> source)
      Detects all somas using ImgPlus with default settings.
      Parameters:
      source - input image (2D or 3D ImgPlus)
      Returns:
      list of SomaResult, sorted by radius (largest first)
    • floodFillSoma

      public static net.imglib2.img.Img<net.imglib2.type.logic.BitType> floodFillSoma(net.imglib2.RandomAccessibleInterval<? extends net.imglib2.type.numeric.RealType<?>> source, long seedX, long seedY, double threshold)
      Creates a binary mask of the soma region using flood fill from seed point.
      Parameters:
      source - the input image (2D)
      seedX - seed X coordinate (voxels)
      seedY - seed Y coordinate (voxels)
      threshold - pixels above this value are considered foreground
      Returns:
      binary mask where TRUE = soma region, or null if seed is in background
    • extractContour

      public static Polygon extractContour(net.imglib2.img.Img<net.imglib2.type.logic.BitType> mask)
      Extracts the boundary contour from a binary mask using Moore boundary tracing.
      Parameters:
      mask - binary mask (TRUE = foreground)
      Returns:
      polygon contour, or null if mask is empty
    • computeMaskCentroid

      public static double[] computeMaskCentroid(net.imglib2.img.Img<net.imglib2.type.logic.BitType> mask, double[] spacing)
      Computes the centroid of a binary mask.
      Parameters:
      mask - binary mask
      spacing - voxel spacing [x, y] for physical coordinates, or null for voxel coords
      Returns:
      centroid as [x, y], or null if mask is empty
    • computeMaskArea

      public static double computeMaskArea(net.imglib2.img.Img<net.imglib2.type.logic.BitType> mask, double[] spacing)
      Computes the area of a binary mask.
      Parameters:
      mask - binary mask
      spacing - voxel spacing [x, y], or null for voxel count
      Returns:
      area in physical units squared, or voxel count if spacing is null
    • computeOtsuThreshold

      public static double computeOtsuThreshold(net.imglib2.RandomAccessibleInterval<? extends net.imglib2.type.numeric.RealType<?>> source)
      Computes Otsu's threshold using ImageJ Ops.
      Parameters:
      source - the input image
      Returns:
      Otsu threshold value
    • roiToSomaPath

      public static Path roiToSomaPath(ij.gui.Roi roi, Path referencePath)
      Creates a single-node soma Path from an ImageJ ROI using calibration from a reference Path.
      Parameters:
      roi - the ROI to convert (must not be null)
      referencePath - a Path from which to extract spacing and units (may be null for pixel units)
      Returns:
      a single-node soma path, or null if ROI is null
      See Also:
    • roiToSomaPath

      public static Path roiToSomaPath(ij.gui.Roi roi, double[] spacing)
      Creates a single-node soma Path from an ImageJ ROI.

      Converts any ROI to a soma path centered at the ROI's centroid. For area ROIs (polygon, oval, freehand, etc.), the radius is estimated from the equivalent circle area. For non-area ROIs (point, line), no radius is assigned.

      Parameters:
      roi - the ROI to convert (must not be null)
      spacing - voxel spacing [x, y, z] for physical coordinates, or null for pixel units
      Returns:
      a single-node soma path, or null if ROI is null