Class SpectralSimilarity<T extends net.imglib2.type.numeric.RealType<T>,U extends net.imglib2.type.numeric.RealType<U>>

java.lang.Object
net.imagej.ops.AbstractOp
net.imagej.ops.special.AbstractUnaryOp<net.imglib2.RandomAccessibleInterval<T>,net.imglib2.RandomAccessibleInterval<U>>
net.imagej.ops.special.computer.AbstractUnaryComputerOp<net.imglib2.RandomAccessibleInterval<T>,net.imglib2.RandomAccessibleInterval<U>>
sc.fiji.snt.filter.SpectralSimilarity<T,U>
Type Parameters:
T - the pixel type of the input multichannel image
U - the pixel type of the output scalar image
All Implemented Interfaces:
Runnable, Consumer<net.imglib2.RandomAccessibleInterval<U>>, net.imagej.ops.Environmental, net.imagej.ops.Initializable, net.imagej.ops.Op, net.imagej.ops.special.computer.NullaryComputerOp<net.imglib2.RandomAccessibleInterval<U>>, net.imagej.ops.special.computer.UnaryComputerOp<net.imglib2.RandomAccessibleInterval<T>,net.imglib2.RandomAccessibleInterval<U>>, net.imagej.ops.special.NullaryOp<net.imglib2.RandomAccessibleInterval<U>>, net.imagej.ops.special.Output<net.imglib2.RandomAccessibleInterval<U>>, net.imagej.ops.special.OutputMutable<net.imglib2.RandomAccessibleInterval<U>>, net.imagej.ops.special.SpecialOp, net.imagej.ops.special.UnaryInput<net.imglib2.RandomAccessibleInterval<T>>, net.imagej.ops.special.UnaryOp<net.imglib2.RandomAccessibleInterval<T>,net.imglib2.RandomAccessibleInterval<U>>, net.imagej.ops.Threadable, org.scijava.command.Command, org.scijava.Initializable, org.scijava.plugin.SciJavaPlugin

public class SpectralSimilarity<T extends net.imglib2.type.numeric.RealType<T>,U extends net.imglib2.type.numeric.RealType<U>> extends net.imagej.ops.special.computer.AbstractUnaryComputerOp<net.imglib2.RandomAccessibleInterval<T>,net.imglib2.RandomAccessibleInterval<U>> implements Consumer<net.imglib2.RandomAccessibleInterval<U>>
Computes a spectral similarity map from a multichannel (e.g., Brainbow) image. For each voxel, the output encodes how well the voxel's channel-intensity vector matches a reference color vector. The output is a scalar image suitable for use as a secondary tracing layer.

The per-voxel score combines two terms:

  1. Cosine similarity: dot product of unit-normalized voxel and reference color vectors (1 = identical direction, 0 = orthogonal)
  2. Intensity factor: sigmoid falloff based on how much the voxel's total intensity deviates from the reference intensity. This prevents bright background or dim noise from producing high scores
The final output is cosineSimilarity × intensityFactor, scaled to [0, 1]. High values indicate voxels that match the target neuron's color and brightness.

The input image is a 4D (X, Y, Z, C) RandomAccessibleInterval where the last dimension is channels. The output is a 3D (X, Y, Z) scalar image.

This filter is designed for integration with SNT's secondary layer tracing infrastructure, where any standard cost function (e.g., Reciprocal) applied to the output produces spectrally-aware path searches.

The color-vector approach to neurite identification in multichannel images is also validated in:

Leiwe et al., "Automated neuronal reconstruction with super-multicolour Tetbow labelling and threshold-based clustering of colour hues", Nat Commun 15, 5279 (2024). doi:10.1038/s41467-024-49455-y
Author:
Tiago Ferreira
See Also:
  • Nested Class Summary

    Nested classes/interfaces inherited from interface net.imagej.ops.special.SpecialOp

    net.imagej.ops.special.SpecialOp.Flavor
  • Constructor Summary

    Constructors
    Constructor
    Description
    SpectralSimilarity(double[] referenceColor, int numThreads)
    Constructs a spectral similarity filter.
    SpectralSimilarity(double[] referenceColor, int numThreads, double intensityTolerance)
    Constructs a spectral similarity filter with custom intensity tolerance.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    accept(net.imglib2.RandomAccessibleInterval<U> output)
     
    static double[]
    averageColorAtPositions(ij.ImageStack[] channelStacks, int[][] positions)
    Computes the average color vector from a set of 3D positions in a multichannel image represented as per-channel ImageStacks.
    static <T extends net.imglib2.type.numeric.RealType<T>>
    double[]
    averageColorAtPositions(net.imglib2.RandomAccessibleInterval<T> input, int[][] positions)
    Computes the average color vector from a set of 3D positions in a multichannel image.
    static <T extends net.imglib2.type.numeric.RealType<T>>
    double[]
    averageColorFromPaths(net.imglib2.RandomAccessibleInterval<T> input, List<Path> paths, double xSpacing, double ySpacing, double zSpacing)
    Computes the average color vector from path node positions.
    static double
    channelSum(double[] vec)
    Sums all elements of a vector.
    void
    compute(net.imglib2.RandomAccessibleInterval<T> input, net.imglib2.RandomAccessibleInterval<U> output)
    Computes the spectral similarity map.
    double[]
    Returns the reference color vector used by this filter.
    static int[]
    nodeToPixelCoords(PointInImage node, double xSpacing, double ySpacing, double zSpacing)
    Converts a path node from calibrated (real-world) coordinates to pixel coordinates by dividing by the voxel spacing and rounding.
    static void
    normalizeVector(double[] vec)
    Normalizes a vector to unit length in place.
    void
    run()
     
    net.imglib2.RandomAccessibleInterval<U>
    run(net.imglib2.RandomAccessibleInterval<U> output)
     

    Methods inherited from class net.imagej.ops.special.computer.AbstractUnaryComputerOp

    in, out, setInput, setOutput

    Methods inherited from class net.imagej.ops.AbstractOp

    ops, setEnvironment

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface java.util.function.Consumer

    andThen

    Methods inherited from interface net.imagej.ops.Environmental

    ops, setEnvironment

    Methods inherited from interface net.imagej.ops.Initializable

    initialize

    Methods inherited from interface net.imagej.ops.special.computer.UnaryComputerOp

    compute, getIndependentInstance, run

    Methods inherited from interface net.imagej.ops.special.UnaryOp

    getArity
  • Constructor Details

    • SpectralSimilarity

      public SpectralSimilarity(double[] referenceColor, int numThreads)
      Constructs a spectral similarity filter.
      Parameters:
      referenceColor - the reference color vector (one value per channel, unnormalized intensities). Typically the average color sampled from representative paths.
      numThreads - number of threads for parallel computation
    • SpectralSimilarity

      public SpectralSimilarity(double[] referenceColor, int numThreads, double intensityTolerance)
      Constructs a spectral similarity filter with custom intensity tolerance.
      Parameters:
      referenceColor - the reference color vector (one value per channel)
      numThreads - number of threads for parallel computation
      intensityTolerance - controls how tolerant the intensity matching is. A value of 2.0 means voxels with total intensity between 0.25× and 4× the reference are scored highly. Higher values = more tolerant.
  • Method Details

    • getReferenceColor

      public double[] getReferenceColor()
      Returns the reference color vector used by this filter.
      Returns:
      a copy of the reference color array
    • nodeToPixelCoords

      public static int[] nodeToPixelCoords(PointInImage node, double xSpacing, double ySpacing, double zSpacing)
      Converts a path node from calibrated (real-world) coordinates to pixel coordinates by dividing by the voxel spacing and rounding.
      Parameters:
      node - the node in calibrated coordinates
      xSpacing - voxel width
      ySpacing - voxel height
      zSpacing - voxel depth
      Returns:
      pixel coordinates as [x, y, z]
    • normalizeVector

      public static void normalizeVector(double[] vec)
      Normalizes a vector to unit length in place. If the vector has zero magnitude, it is left unchanged.
      Parameters:
      vec - the vector to normalize
    • channelSum

      public static double channelSum(double[] vec)
      Sums all elements of a vector. Typically used to compute the total intensity across channels of a color vector.
      Parameters:
      vec - the vector
      Returns:
      the sum of all elements
    • run

      public void run()
      Specified by:
      run in interface net.imagej.ops.special.computer.NullaryComputerOp<T extends net.imglib2.type.numeric.RealType<T>>
      Specified by:
      run in interface net.imagej.ops.special.NullaryOp<T extends net.imglib2.type.numeric.RealType<T>>
      Specified by:
      run in interface Runnable
      Specified by:
      run in interface net.imagej.ops.special.computer.UnaryComputerOp<T extends net.imglib2.type.numeric.RealType<T>,U extends net.imglib2.type.numeric.RealType<U>>
      Specified by:
      run in interface net.imagej.ops.special.UnaryOp<T extends net.imglib2.type.numeric.RealType<T>,U extends net.imglib2.type.numeric.RealType<U>>
    • run

      public net.imglib2.RandomAccessibleInterval<U> run(net.imglib2.RandomAccessibleInterval<U> output)
      Specified by:
      run in interface net.imagej.ops.special.computer.NullaryComputerOp<T extends net.imglib2.type.numeric.RealType<T>>
      Specified by:
      run in interface net.imagej.ops.special.NullaryOp<T extends net.imglib2.type.numeric.RealType<T>>
    • accept

      public void accept(net.imglib2.RandomAccessibleInterval<U> output)
      Specified by:
      accept in interface Consumer<T extends net.imglib2.type.numeric.RealType<T>>
    • compute

      public void compute(net.imglib2.RandomAccessibleInterval<T> input, net.imglib2.RandomAccessibleInterval<U> output)
      Computes the spectral similarity map.
      Specified by:
      compute in interface net.imagej.ops.special.computer.UnaryComputerOp<T extends net.imglib2.type.numeric.RealType<T>,U extends net.imglib2.type.numeric.RealType<U>>
      Parameters:
      input - 4D input image (X, Y, Z, C) where the last dimension is channels
      output - 3D output image (X, Y, Z): same spatial dimensions as input
    • averageColorAtPositions

      public static <T extends net.imglib2.type.numeric.RealType<T>> double[] averageColorAtPositions(net.imglib2.RandomAccessibleInterval<T> input, int[][] positions)
      Computes the average color vector from a set of 3D positions in a multichannel image. This is the standard way to derive a reference color for spectral similarity tracing from existing traced paths.
      Type Parameters:
      T - pixel type
      Parameters:
      input - 4D image (X, Y, Z, C)
      positions - array of [x, y, z] pixel coordinates to sample
      Returns:
      the average color vector (one value per channel)
    • averageColorAtPositions

      public static double[] averageColorAtPositions(ij.ImageStack[] channelStacks, int[][] positions)
      Computes the average color vector from a set of 3D positions in a multichannel image represented as per-channel ImageStacks.
      Parameters:
      channelStacks - one ImageStack per channel
      positions - array of [x, y, z] pixel coordinates to sample
      Returns:
      the average color vector (one value per channel)
    • averageColorFromPaths

      public static <T extends net.imglib2.type.numeric.RealType<T>> double[] averageColorFromPaths(net.imglib2.RandomAccessibleInterval<T> input, List<Path> paths, double xSpacing, double ySpacing, double zSpacing)
      Computes the average color vector from path node positions. Convenience method that extracts pixel positions from SNT Paths using the provided spacing.
      Type Parameters:
      T - pixel type
      Parameters:
      input - 4D image (X, Y, Z, C)
      paths - list of paths to sample
      xSpacing - x pixel spacing
      ySpacing - y pixel spacing
      zSpacing - z pixel spacing
      Returns:
      the average color vector (one value per channel)