Package sc.fiji.snt.util
Class ImpUtils
java.lang.Object
sc.fiji.snt.util.ImpUtils
Static utilities for handling and manipulation of
ImagePluss- Author:
- Tiago Ferreira
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidapplyColorTable(ij.ImagePlus imp, net.imglib2.display.ColorTable cTable) static Stringascii(ij.ImagePlus imp, boolean dilate, int width, int height) Converts the specified image into ascii art.static voidbinarize(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 Calibrationstatic voidClears both tag registries entirely.static ij.ImagePluscombineSkeletons(Collection<Tree> trees) static ij.ImagePluscombineSkeletons(Collection<Tree> trees, boolean asMontage) static ij.ImagePlusconvertRGBtoComposite(ij.ImagePlus imp) static voidconvertTo16bit(ij.ImagePlus imp) static voidconvertTo32bit(ij.ImagePlus imp) static voidconvertTo8bit(ij.ImagePlus imp) static ij.ImagePlusconvertToSimple2D(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.ImagePlusstatic voidCrops the image around non-background values.static ij.ImagePlusReturns one of the demo images bundled with SNT image associated with the demo (fractal) tree.static voidforgetTags(ij.ImagePlus imp) Removesimpfrom both the materialized-crop and display-canvas tag registries.static ij.ImagePlusfromBufferedImage(String title, BufferedImage image) static ij.ImagePlusgetChannel(ij.ImagePlus imp, int channel) static ij.ImageStackgetChannelStack(ij.ImagePlus imp, int channel) Returns a lightweight read-only view of a single channel as an ImageStack.static ij.ImagePlusgetCT(ij.ImagePlus imp, int channel, int frame) static ij.ImagePlusstatic StringgetFilePath(ij.ImagePlus imp) Returns the file path of an image.static ij.gui.RoigetForegroundRect(ij.ImagePlus imp, Number backgroundValue) Returns the cropping rectangle around non-background values, considering all slices of the stack.static ij.ImagePlusgetFrame(ij.ImagePlus imp, int frame) static doublegetMagnification(ij.ImagePlus imp) static double[]getMinMax(ij.ImagePlus imp) static ij.ImagePlusgetMIP(ij.ImagePlus imp) static ij.ImagePlusgetMIP(ij.ImagePlus imp, int startSlice, int stopSlice) static ij.ImagePlusgetMIP(Collection<ij.ImagePlus> imps) static ij.ImagePlus[]Returns all currently open images in ImageJ that were not created by SNT.static ij.ImagePlus[]Returns all currently open images in ImageJ'sWindowManager.getSliceLabels(ij.ImageStack stack) static ij.ImagePlusgetSystemClipboard(boolean asMultiChannel) Retrieves an ImagePlus from the system clipboardstatic StringimageTypeToString(int type) static voidinvertLut(ij.ImagePlus imp) static booleanisBinary(ij.ImagePlus imp) static booleanisDisplayCanvas(ij.ImagePlus imp) Checks whether an image is a placeholder canvasstatic booleanisMaterializedCrop(ij.ImagePlus imp) Checks whether an image is a materialized crop of a streamed volume.static booleanisPointVisible(ij.ImagePlus imp, int x, int y) Checks if a given point (in image coordinates) is currently visible in an imagestatic booleanisVirtualStack(ij.ImagePlus imp) static ij.ImagePlusmergeChannels(Object... imps) Script-friendly method for merging separate channels into a multichannel composite image.static doublenextZoomLevel(double level) static ij.ImagePlusstatic ij.ImagePlusstatic ij.ImagePlusstatic ij.ImagePlusstatic doublepreviousZoomLevel(double level) static voidremoveIsolatedPixels(ij.ImagePlus binaryImp) static voidremoveSlices(ij.ImageStack stack, Collection<String> labels) static voidRotates an image 90 degrees.static booleansameCalibration(ij.ImagePlus imp1, ij.ImagePlus imp2) static booleansameCTDimensions(ij.ImagePlus imp1, ij.ImagePlus imp2) static booleansameXYZDimensions(ij.ImagePlus imp1, ij.ImagePlus imp2) static voidSaves the specified image.static voidsetIsDisplayCanvas(ij.ImagePlus imp) Used by SNT to tag an image as a Display Canvas (typically a blank placeholder image).static voidsetIsMaterializedCrop(ij.ImagePlus imp) Used by SNT to tag an eagerly-materialized pixel crop of a streamed volume as such.static voidstatic voidsetSliceLabels(ij.ImageStack stack, String commonLabel) static net.imagej.DatasettoDataset(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.ImagePlustoStack(Collection<ij.ImagePlus> imps) static voidzoomTo(ij.ImagePlus imp, double zoomMagnification, int x, int y) static voidzoomTo(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 voidzoomTo(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'scanvas offset.static doublezoomTo(ij.ImagePlus imp, Collection<Path> paths) Zooms the image canvas to optimally display the specified paths in the XY plane.static doublezoomTo(ij.ImagePlus imp, Collection<Path> paths, int plane) Zooms the image canvas to optimally display the specified paths in the given plane.
-
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
-
toStack
-
convertTo32bit
- 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
-
open
-
open
-
open
-
save
Saves the specified image.- Parameters:
imp- The image to be savedfilePath- The file path should end with ".tif", or one of the supported extensions (".jpg", ".zip", etc.)
-
getFilePath
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. UnlikegetChannel(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 imagechannel- 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
-
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 binarizedlower- 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
-
setSliceLabels
-
toDataset
public static net.imagej.Dataset toDataset(ij.ImagePlus imp) -
applyColorTable
public static void applyColorTable(ij.ImagePlus imp, net.imglib2.display.ColorTable cTable) -
setLut
-
create
-
isBinary
public static boolean isBinary(ij.ImagePlus imp) -
isVirtualStack
public static boolean isVirtualStack(ij.ImagePlus imp) -
combineSkeletons
-
combineSkeletons
-
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
-
demo
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() -
getMagnification
public static double getMagnification(ij.ImagePlus imp) - Returns:
- the Image current magnification. A value between 0.0139 (1.4%), and 32 (3200%) or -1, if the zoom factor could not be assessed.
-
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'scanvas offset. Also sets the image position to the appropriate Z-slice.- Parameters:
imp- the ImagePlus whose canvas should be adjustedzoomMagnification- 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 benull)
-
zoomTo
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)withRoiConverter.XY_PLANE.- Parameters:
imp- the ImagePlus whose canvas should be adjustedpaths- the collection of paths to zoom to- Returns:
- the resulting magnification level, or 0 if no zoom was performed
- See Also:
-
zoomTo
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 adjustedpaths- the collection of paths to zoom toplane- 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
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 adjustedzoomLevel- the exact magnification level to applypaths- the collection of paths to center onplane- 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 checkx- the x coordinate in image pixelsy- 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
-
ascii
Converts the specified image into ascii art.- Parameters:
imp- The image to be converted to ascii artdilate- whether the image should be dilated before conversion. Ignored if image is not binarywidth- Desired width for the ascii art. Set it to -1 to use image widthheight- 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 convertedframe- The frame to be considered (ignored if image is not a timelapse)- Returns:
- a 2D 'flattened' version of the image
-
crop
Crops the image around non-background values. Does nothing if the image does not have non-background values.- Parameters:
imp- The image to be croppedbackgroundValue- 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
Rotates an image 90 degrees.- Parameters:
imp- the image to be rotateddirection- either 'left' or 'right'
-
getForegroundRect
Returns the cropping rectangle around non-background values, considering all slices of the stack.- Parameters:
imp- The image to be parsedbackgroundValue- 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 ImagePluschannel- channel to extract (1-based), ignored if single channelframe- frame to extract (1-based), ignored if single frame- Returns:
- 3D ImgPlus with X, Y, Z axes
-
setIsMaterializedCrop
public static void setIsMaterializedCrop(ij.ImagePlus imp) Used by SNT to tag an eagerly-materialized pixel crop of a streamed volume as such.- Parameters:
imp- the image to be tagged
-
isMaterializedCrop
public static boolean isMaterializedCrop(ij.ImagePlus imp) Checks whether an image is a materialized crop of a streamed volume.- Parameters:
imp- the image to be checked- Returns:
- true if
setIsMaterializedCrop(ij.ImagePlus)was previous called onimp
-
setIsDisplayCanvas
public static void setIsDisplayCanvas(ij.ImagePlus imp) Used by SNT to tag an image as a Display Canvas (typically a blank placeholder image).- Parameters:
imp- the image to be tagged
-
isDisplayCanvas
public static boolean isDisplayCanvas(ij.ImagePlus imp) Checks whether an image is a placeholder canvas- Parameters:
imp- the image to be checked- Returns:
- true if
setIsDisplayCanvas(ij.ImagePlus)was previous called onimp
-
forgetTags
public static void forgetTags(ij.ImagePlus imp) Removesimpfrom both the materialized-crop and display-canvas tag registries. Call once an image is known to be closed/discarded, for deterministic (rather than GC-timed) cleanup; harmless ifimpwas never tagged.- Parameters:
imp- the image to forget
-
clearAllTags
public static void clearAllTags()Clears both tag registries entirely. Intended as a session-teardown safety net, not for routine use: individual images are already forgotten viaforgetTags(ImagePlus)as they close. -
getOpenImages
public static ij.ImagePlus[] getOpenImages()Returns all currently open images in ImageJ'sWindowManager.- Returns:
- array of open
ImagePlusinstances, or an empty array if none are open
-
getNonSNTOpenImages
public static ij.ImagePlus[] getNonSNTOpenImages()Returns all currently open images in ImageJ that were not created by SNT.- Returns:
- array of open
ImagePlusinstances, or an empty array if non-SNT created images are open
-
mergeChannels
Script-friendly method for merging separate channels into a multichannel composite image.- Parameters:
imps- The channels to be merged (Array, Collection, Iterable, or comma-separated images).- Returns:
- the composite image, or if
impsis a single image, that same image.
-