Class ImpUtils

java.lang.Object
sc.fiji.snt.util.ImpUtils

public class ImpUtils extends Object
Static utilities for handling and manipulation of ImagePluss
Author:
Tiago Ferreira
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    applyColorTable(ij.ImagePlus imp, net.imglib2.display.ColorTable cTable)
     
    static String
    ascii(ij.ImagePlus imp, boolean dilate, int width, int height)
    Converts the specified image into ascii art.
    static void
    binarize(ij.ImagePlus imp, double lower, double upper)
    Binarize an ImagePlus using lower and upper thresholds.
    static net.imagej.axis.CalibratedAxis[]
    calibrationToAxes(ij.measure.Calibration cal, int numDimensions)
    Creates ImgPlus axes from IJ1 Calibration
    static ij.ImagePlus
     
    static ij.ImagePlus
    combineSkeletons(Collection<Tree> trees, boolean asMontage)
     
    static ij.ImagePlus
    convertRGBtoComposite(ij.ImagePlus imp)
     
    static void
    convertTo16bit(ij.ImagePlus imp)
     
    static void
    convertTo32bit(ij.ImagePlus imp)
     
    static void
    convertTo8bit(ij.ImagePlus imp)
     
    static ij.ImagePlus
    convertToSimple2D(ij.ImagePlus imp, int frame)
    Converts the specified image into an easy displayable form, i.e., a non-composite 2D image If the image is a timelapse, only the first frame is considered; if 3D, a MIP is retrieved; if multichannel, an RGB version is obtained.
    static ij.ImagePlus
    create(String title, int width, int height, int depth, int bitDepth)
     
    static void
    crop(ij.ImagePlus imp, Number backgroundValue)
    Crops the image around non-background values.
    static ij.ImagePlus
    demo(String img)
    Returns one of the demo images bundled with SNT image associated with the demo (fractal) tree.
    static ij.ImagePlus
     
    static ij.ImagePlus
    getChannel(ij.ImagePlus imp, int channel)
     
    static ij.ImageStack
    getChannelStack(ij.ImagePlus imp, int channel)
    Returns a lightweight read-only view of a single channel as an ImageStack.
    static ij.ImagePlus
    getCT(ij.ImagePlus imp, int channel, int frame)
     
    static ij.ImagePlus
     
    static String
    getFilePath(ij.ImagePlus imp)
    Returns the file path of an image.
    static ij.gui.Roi
    getForegroundRect(ij.ImagePlus imp, Number backgroundValue)
    Returns the cropping rectangle around non-background values, considering all slices of the stack.
    static ij.ImagePlus
    getFrame(ij.ImagePlus imp, int frame)
     
    static double[]
    getMinMax(ij.ImagePlus imp)
     
    static ij.ImagePlus
    getMIP(ij.ImagePlus imp)
     
    static ij.ImagePlus
    getMIP(ij.ImagePlus imp, int startSlice, int stopSlice)
     
    static ij.ImagePlus
    getMIP(Collection<ij.ImagePlus> imps)
     
    static ij.ImagePlus[]
    Returns all currently open images in ImageJ's WindowManager.
    static List<String>
    getSliceLabels(ij.ImageStack stack)
     
    static ij.ImagePlus
    getSystemClipboard(boolean asMultiChannel)
    Retrieves an ImagePlus from the system clipboard
    static String
     
    static void
    invertLut(ij.ImagePlus imp)
     
    static boolean
    isBinary(ij.ImagePlus imp)
     
    static boolean
    isPointVisible(ij.ImagePlus imp, int x, int y)
    Checks if a given point (in image coordinates) is currently visible in an image
    static boolean
    isVirtualStack(ij.ImagePlus imp)
     
    static double
    nextZoomLevel(double level)
     
    static ij.ImagePlus
    open(File file)
     
    static ij.ImagePlus
    open(File file, String title)
     
    static ij.ImagePlus
    open(String filePathOrUrl)
     
    static ij.ImagePlus
    open(String filePathOrUrl, String title)
     
    static double
    previousZoomLevel(double level)
     
    static void
    removeIsolatedPixels(ij.ImagePlus binaryImp)
     
    static void
    removeSlices(ij.ImageStack stack, Collection<String> labels)
     
    static void
    rotate90(ij.ImagePlus imp, String direction)
    Rotates an image 90 degrees.
    static boolean
    sameCalibration(ij.ImagePlus imp1, ij.ImagePlus imp2)
     
    static boolean
    sameCTDimensions(ij.ImagePlus imp1, ij.ImagePlus imp2)
     
    static boolean
    sameXYZDimensions(ij.ImagePlus imp1, ij.ImagePlus imp2)
     
    static void
    save(ij.ImagePlus imp, String filePath)
    Saves the specified image.
    static void
    setLut(ij.ImagePlus imp, String lutName)
     
    static net.imagej.Dataset
    toDataset(ij.ImagePlus imp)
     
    static <T extends net.imglib2.type.numeric.RealType<T> & net.imglib2.type.NativeType<T>>
    net.imagej.ImgPlus<T>
    toImgPlus(ij.ImagePlus imp)
    Convert an ImagePlus to an ImgPlus with calibration and origin metadata.
    static <T extends net.imglib2.type.numeric.RealType<T> & net.imglib2.type.NativeType<T>>
    net.imagej.ImgPlus<T>
    toImgPlus3D(ij.ImagePlus imp, int channel, int frame)
    Convert an ImagePlus to a 3D (XYZ) ImgPlus, extracting a single channel/frame if needed.
    static ij.ImagePlus
    toStack(Collection<ij.ImagePlus> imps)
     
    static void
    zoomTo(ij.ImagePlus imp, double zoomMagnification, int x, int y)
     
    static void
    zoomTo(ij.ImagePlus imp, double zoomLevel, Collection<Path> paths, int plane)
    Zooms the image canvas to the specified magnification level, centered on the bounding box of the given paths.
    static void
    zoomTo(ij.ImagePlus imp, double zoomMagnification, PointInImage location, Path path)
    Zooms the image canvas to a specific location on a path, correctly accounting for the path's canvas offset.
    static double
    zoomTo(ij.ImagePlus imp, Collection<Path> paths)
    Zooms the image canvas to optimally display the specified paths in the XY plane.
    static double
    zoomTo(ij.ImagePlus imp, Collection<Path> paths, int plane)
    Zooms the image canvas to optimally display the specified paths in the given plane.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • calibrationToAxes

      public static net.imagej.axis.CalibratedAxis[] calibrationToAxes(ij.measure.Calibration cal, int numDimensions)
      Creates ImgPlus axes from IJ1 Calibration
    • removeIsolatedPixels

      public static void removeIsolatedPixels(ij.ImagePlus binaryImp)
    • getMIP

      public static ij.ImagePlus getMIP(ij.ImagePlus imp)
    • getMIP

      public static ij.ImagePlus getMIP(ij.ImagePlus imp, int startSlice, int stopSlice)
    • getMIP

      public static ij.ImagePlus getMIP(Collection<ij.ImagePlus> imps)
    • toStack

      public static ij.ImagePlus toStack(Collection<ij.ImagePlus> imps)
    • convertTo32bit

      public static void convertTo32bit(ij.ImagePlus imp) throws IllegalArgumentException
      Throws:
      IllegalArgumentException
    • convertTo8bit

      public static void convertTo8bit(ij.ImagePlus imp)
    • convertTo16bit

      public static void convertTo16bit(ij.ImagePlus imp)
    • convertRGBtoComposite

      public static ij.ImagePlus convertRGBtoComposite(ij.ImagePlus imp)
    • open

      public static ij.ImagePlus open(File file)
    • open

      public static ij.ImagePlus open(String filePathOrUrl)
    • open

      public static ij.ImagePlus open(File file, String title)
    • open

      public static ij.ImagePlus open(String filePathOrUrl, String title)
    • save

      public static void save(ij.ImagePlus imp, String filePath)
      Saves the specified image.
      Parameters:
      imp - The image to be saved
      filePath - The file path should end with ".tif", or one of the supported extensions (".jpg", ".zip", etc.)
    • getFilePath

      public static String getFilePath(ij.ImagePlus imp)
      Returns the file path of an image.
      Parameters:
      imp - the image to be parsed
      Returns:
      the absolute file path of the image or "unknown" if not known
    • sameXYZDimensions

      public static boolean sameXYZDimensions(ij.ImagePlus imp1, ij.ImagePlus imp2)
    • sameCTDimensions

      public static boolean sameCTDimensions(ij.ImagePlus imp1, ij.ImagePlus imp2)
    • sameCalibration

      public static boolean sameCalibration(ij.ImagePlus imp1, ij.ImagePlus imp2)
    • getFrame

      public static ij.ImagePlus getFrame(ij.ImagePlus imp, int frame)
    • getChannel

      public static ij.ImagePlus getChannel(ij.ImagePlus imp, int channel)
    • getChannelStack

      public static ij.ImageStack getChannelStack(ij.ImagePlus imp, int channel)
      Returns a lightweight read-only view of a single channel as an ImageStack. Unlike getChannel(ImagePlus, int), the returned stack shares the underlying pixel arrays with the source image (no data copy), making it suitable for read-only access on large multichannel images.
      Parameters:
      imp - the source image
      channel - the 1-based channel index
      Returns:
      the channel's ImageStack (shared pixel data)
      See Also:
    • getCT

      public static ij.ImagePlus getCT(ij.ImagePlus imp, int channel, int frame)
    • removeSlices

      public static void removeSlices(ij.ImageStack stack, Collection<String> labels)
    • binarize

      public static void binarize(ij.ImagePlus imp, double lower, double upper)
      Binarize an ImagePlus using lower and upper thresholds. Pixels within [lower, upper] become 255 (white), others become 0 (black).
      Parameters:
      imp - the image to be binarized
      lower - the lower threshold (inclusive)
      upper - the upper threshold (inclusive)
      Throws:
      IllegalArgumentException - if image is not grayscale
    • getMinMax

      public static double[] getMinMax(ij.ImagePlus imp)
    • getSliceLabels

      public static List<String> getSliceLabels(ij.ImageStack stack)
    • toDataset

      public static net.imagej.Dataset toDataset(ij.ImagePlus imp)
    • applyColorTable

      public static void applyColorTable(ij.ImagePlus imp, net.imglib2.display.ColorTable cTable)
    • setLut

      public static void setLut(ij.ImagePlus imp, String lutName)
    • create

      public static ij.ImagePlus create(String title, int width, int height, int depth, int bitDepth)
    • isBinary

      public static boolean isBinary(ij.ImagePlus imp)
    • isVirtualStack

      public static boolean isVirtualStack(ij.ImagePlus imp)
    • combineSkeletons

      public static ij.ImagePlus combineSkeletons(Collection<Tree> trees)
    • combineSkeletons

      public static ij.ImagePlus combineSkeletons(Collection<Tree> trees, boolean asMontage)
    • getSystemClipboard

      public static ij.ImagePlus getSystemClipboard(boolean asMultiChannel)
      Retrieves an ImagePlus from the system clipboard
      Parameters:
      asMultiChannel - if true and clipboard contains RGB data image is returned as composite (RGB/8-bit grayscale otherwise)
      Returns:
      the image stored in the system clipboard or null if no image found
    • fromBufferedImage

      public static ij.ImagePlus fromBufferedImage(String title, BufferedImage image)
    • demo

      public static ij.ImagePlus demo(String img)
      Returns one of the demo images bundled with SNT image associated with the demo (fractal) tree.
      Parameters:
      img - a string describing the type of demo image. Options include: 'fractal' for the L-system toy neuron; 'ddaC' for the C4 ddaC drosophila neuron (demo image initially distributed with the Sholl plugin); 'OP1'/'OP_1' for the DIADEM OP_1 dataset; 'cil701', 'cil810', or 'ci41458' for the respective Cell Image Library entries, 'microglia' for a MIP of tiled microglia cells in the mouse retina, and 'binary timelapse' for a small 4-frame sequence of neurite growth
      Returns:
      the demo image, or null if data could not be retrieved
    • getCurrentImage

      public static ij.ImagePlus getCurrentImage()
    • zoomTo

      public static void zoomTo(ij.ImagePlus imp, double zoomMagnification, int x, int y)
    • zoomTo

      public static void zoomTo(ij.ImagePlus imp, double zoomMagnification, PointInImage location, Path path)
      Zooms the image canvas to a specific location on a path, correctly accounting for the path's canvas offset. Also sets the image position to the appropriate Z-slice.
      Parameters:
      imp - the ImagePlus whose canvas should be adjusted
      zoomMagnification - the target zoom level (e.g., 6.0 for 600%)
      location - the spatial location to center on (in calibrated coordinates)
      path - the path that provides canvas offset context (may be null)
    • zoomTo

      public static double zoomTo(ij.ImagePlus imp, Collection<Path> paths)
      Zooms the image canvas to optimally display the specified paths in the XY plane.

      This is a convenience method equivalent to calling zoomTo(ImagePlus, Collection, int) with RoiConverter.XY_PLANE.

      Parameters:
      imp - the ImagePlus whose canvas should be adjusted
      paths - the collection of paths to zoom to
      Returns:
      the resulting magnification level, or 0 if no zoom was performed
      See Also:
    • zoomTo

      public static double zoomTo(ij.ImagePlus imp, Collection<Path> paths, int plane)
      Zooms the image canvas to optimally display the specified paths in the given plane.

      The zoom behavior depends on the relationship between the paths' bounding box and the current visible area:

      • Zoom in: When the bounding box is fully visible but occupies less than 25% of the visible area, the view zooms in to better frame the paths.
      • Zoom out: When the bounding box extends beyond the visible area (paths partially or fully outside view), the view zooms out to show all paths.
      • No change (but re-center): When the bounding box already fills more than 25% of the visible area, no zoom adjustment is made but the view is centered on the bounding box.
      Parameters:
      imp - the ImagePlus whose canvas should be adjusted
      paths - the collection of paths to zoom to
      plane - the viewing plane (RoiConverter.XY_PLANE, ZY_PLANE, or XZ_PLANE)
      Returns:
      the resulting magnification level, or 0 if no zoom was performed (e.g., canvas is null)
    • zoomTo

      public static void zoomTo(ij.ImagePlus imp, double zoomLevel, Collection<Path> paths, int plane)
      Zooms the image canvas to the specified magnification level, centered on the bounding box of the given paths.
      Parameters:
      imp - the ImagePlus whose canvas should be adjusted
      zoomLevel - the exact magnification level to apply
      paths - the collection of paths to center on
      plane - the viewing plane (RoiConverter.XY_PLANE, ZY_PLANE, or XZ_PLANE)
    • isPointVisible

      public static boolean isPointVisible(ij.ImagePlus imp, int x, int y)
      Checks if a given point (in image coordinates) is currently visible in an image
      Parameters:
      imp - the ImagePlus to check
      x - the x coordinate in image pixels
      y - the y coordinate in image pixels
      Returns:
      true if the point is visible in the current view, false otherwise
    • nextZoomLevel

      public static double nextZoomLevel(double level)
    • previousZoomLevel

      public static double previousZoomLevel(double level)
    • imageTypeToString

      public static String imageTypeToString(int type)
    • ascii

      public static String ascii(ij.ImagePlus imp, boolean dilate, int width, int height)
      Converts the specified image into ascii art.
      Parameters:
      imp - The image to be converted to ascii art
      dilate - whether the image should be dilated before conversion. Ignored if image is not binary
      width - Desired width for the ascii art. Set it to -1 to use image width
      height - Desired height for the ascii art. Set it to -1 to use image height
      Returns:
      ascii art
    • convertToSimple2D

      public static ij.ImagePlus convertToSimple2D(ij.ImagePlus imp, int frame)
      Converts the specified image into an easy displayable form, i.e., a non-composite 2D image If the image is a timelapse, only the first frame is considered; if 3D, a MIP is retrieved; if multichannel, an RGB version is obtained. The image is flattened if its Overlay has ROIs.
      Parameters:
      imp - The image to be converted
      frame - The frame to be considered (ignored if image is not a timelapse)
      Returns:
      a 2D 'flattened' version of the image
    • crop

      public static void crop(ij.ImagePlus imp, Number backgroundValue)
      Crops the image around non-background values. Does nothing if the image does not have non-background values.
      Parameters:
      imp - The image to be cropped
      backgroundValue - the background value typically 'black': 0 for 8-/16bit, 0x000000 for RGB, or white (255 for 8-bit, 65535 for 16-bir, 0xFFFFFF for RGB)
    • rotate90

      public static void rotate90(ij.ImagePlus imp, String direction)
      Rotates an image 90 degrees.
      Parameters:
      imp - the image to be rotated
      direction - either 'left' or 'right'
    • getForegroundRect

      public static ij.gui.Roi getForegroundRect(ij.ImagePlus imp, Number backgroundValue)
      Returns the cropping rectangle around non-background values, considering all slices of the stack.
      Parameters:
      imp - The image to be parsed
      backgroundValue - the background value typically 'black': 0, or white (255 for 8-bit/RGB, or 65535 for 16-bit)
      Returns:
      the rectangular ROI defining non-background bounds, or null if all pixels are background
    • invertLut

      public static void invertLut(ij.ImagePlus imp)
    • toImgPlus

      public static <T extends net.imglib2.type.numeric.RealType<T> & net.imglib2.type.NativeType<T>> net.imagej.ImgPlus<T> toImgPlus(ij.ImagePlus imp)
      Convert an ImagePlus to an ImgPlus with calibration and origin metadata.

      Creates an ImgPlus with proper axis types (X, Y, Z, Channel, Time) and transfers calibration including pixel sizes, units, and origin offsets.

      Parameters:
      imp - the source ImagePlus
      Returns:
      ImgPlus with calibrated axes
    • toImgPlus3D

      public static <T extends net.imglib2.type.numeric.RealType<T> & net.imglib2.type.NativeType<T>> net.imagej.ImgPlus<T> toImgPlus3D(ij.ImagePlus imp, int channel, int frame)
      Convert an ImagePlus to a 3D (XYZ) ImgPlus, extracting a single channel/frame if needed.

      Useful for analysis that expects simple 3D images without channel/time dimensions.

      Parameters:
      imp - the source ImagePlus
      channel - channel to extract (1-based), ignored if single channel
      frame - frame to extract (1-based), ignored if single frame
      Returns:
      3D ImgPlus with X, Y, Z axes
    • getOpenImages

      public static ij.ImagePlus[] getOpenImages()
      Returns all currently open images in ImageJ's WindowManager.
      Returns:
      array of open ImagePlus instances, or an empty array if none are open