Package qupath.imagej.tools
Class IJTools
java.lang.Object
qupath.imagej.tools.IJTools
Collection of static methods to help with using ImageJ with QuPath.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcalibrateImagePlus(ImagePlus imp, RegionRequest request, ImageServer<BufferedImage> server) Set an ImagePlus's Calibration and FileInfo properties based on a RegionRequest and PathImageServer.static voidcalibrateObject(PathObject pathObject, Roi roi) Set the properties of aPathObjectbased upon an ImageJ Roi.static voidcalibrateRoi(Roi roi, PathObject pathObject) Set properties of an ImageJ Roi based upon the property of aPathObject.static FloatProcessor[]colorDeconvolve(ColorProcessor cp, ColorDeconvolutionStains stains) Apply color deconvolution to an RGB image, outputting 3 'stain' images in the same order as the stain vectors.static FloatProcessor[]colorDeconvolve(ImageProcessor ipRed, ImageProcessor ipGreen, ImageProcessor ipBlue, ColorDeconvolutionStains stains) Apply color deconvolution to RGB channels, outputting 3 'stain' images in the same order as the stain vectors.static booleancontainsImageJRois(File file) Check whether a file is likely to contain an ImageJ ROI, based upon its extension or .zip file contents.static SortedMap<Number, PathObject> convertLabelsToCells(ImageProcessor ipNuclei, ImageProcessor ipCells, double xOrigin, double yOrigin, double downsample, ImagePlane plane) Convert integer labeled images into cell objects.static SortedMap<Number, PathObject> convertLabelsToCells(ImageProcessor ipNuclei, ImageProcessor ipCells, Calibration cal, double downsample, ImagePlane plane) Convert integer labeled images into cell objects.static PathObjectconvertToAnnotation(Roi roi, double xOrigin, double yOrigin, double downsampleFactor, ImagePlane plane) Create an annotation object for a specific ImageJ Roi, using anImagePlusto help set properties.static PathObjectconvertToAnnotation(Roi roi, double downsampleFactor, ImagePlus imp) Create an annotation object for a specific ImageJ Roi.static PathObjectconvertToAnnotation(ImagePlus imp, ImageServer<?> server, Roi roi, double downsampleFactor, ImagePlane plane) Deprecated.static PathObjectconvertToDetection(Roi roi, double xOrigin, double yOrigin, double downsampleFactor, ImagePlane plane) Create a detection object for a specific ImageJ Roi, using anImagePlusto help set properties.static PathObjectconvertToDetection(Roi roi, double downsampleFactor, ImagePlus imp) Create a detection object for a specific ImageJ Roi.static PathObjectconvertToDetection(ImagePlus imp, ImageServer<?> server, Roi roi, double downsampleFactor, ImagePlane plane) Deprecated.static ImageProcessorConvert aSimpleImageto anImageProcessor.static RoiconvertToIJRoi(ROI pathROI, double xOrigin, double yOrigin, double downsampleFactor) Convert a QuPath ROI to an ImageJ Roi.convertToIJRoi(ROI pathROI, Calibration cal, double downsampleFactor) Convert a QuPath ROI to an ImageJ Roi for an image with the specified calibration.static RoiconvertToIJRoi(ROI roi, RegionRequest request) Convert a QuPath ROI to an ImageJ Roi.convertToIJRoi(ROI pathROI, T pathImage) Create an ImageJ Roi from a ROI, suitable for displaying on the ImagePlus of anPathImage<ImagePlus>.convertToImagePlus(String title, ImageServer<BufferedImage> server, BufferedImage img, RegionRequest request) Convert aBufferedImageinto aPathImage<ImagePlus>.convertToImagePlus(ImageServer<BufferedImage> server, RegionRequest request) Read a region from anImageServer<BufferedImageas aPathImage<ImagePlus>.static ImageProcessorconvertToImageProcessor(BufferedImage img, int band) Extract pixels as an an ImageProcessor from a single band of a BufferedImage.static FloatProcessorconvertToOpticalDensitySum(ColorProcessor cp, double maxRed, double maxGreen, double maxBlue) Calculate optical density values for the red, green and blue channels, then add these all together.static PathObjectconvertToPathObject(Roi roi, double xOrigin, double yOrigin, double downsampleFactor, Function<ROI, PathObject> creator, ImagePlane plane) Create aPathObjectfor a specific ImageJ Roi.static PathObjectconvertToPathObject(Roi roi, double downsampleFactor, Function<ROI, PathObject> creator, ImagePlus imp) Create aPathObjectfor a specific ImageJ Roi, using anImagePlusto help set properties.static PathObjectconvertToPathObject(ImagePlus imp, ImageServer<?> server, Roi roi, double downsampleFactor, Function<ROI, PathObject> creator, ImagePlane plane) Deprecated.static PolygonROIconvertToPolygonROI(PolygonRoi roi, Calibration cal, double downsampleFactor, ImagePlane plane) Convert an ImageJ PolygonRoi to a QuPath PolygonROI.static ROIconvertToROI(Roi roi, double xOrigin, double yOrigin, double downsampleFactor, ImagePlane plane) Create a ROI from an ImageJ Roi.static ROIconvertToROI(Roi roi, Calibration cal, double downsampleFactor, ImagePlane plane) Create a ROI from an ImageJ Roi, using the Calibration object of an ImagePlus.static ROIconvertToROI(Roi roi, RegionRequest request) Convert an ImageJ Roi to a QuPath ROI.convertToROI(Roi roi, T pathImage) Create a ROI from an ImageJ Roi.static ImagePlusconvertToUncalibratedImagePlus(String title, BufferedImage img) Convert a BufferedImage to an ImagePlus, without pixel size information or other calibration.static ImageStackcreateImageStack(ImageProcessor... ips) Create an ImageStack containing the specified ImageProcessors.createPathImage(ImageServer<BufferedImage> server, ImagePlus imp, RegionRequest request) Create aPathImagefrom an ImagePlus and region.static doubleestimateDownsampleFactor(ImagePlus imp, ImageServer<?> server) Estimate the downsample factor for an image region extracted from an image server, based upon the ratio of pixel sizes if possible or ratio of dimensions if necessary.static ImagePlusextractHyperstack(ImageServer<BufferedImage> server, RegionRequest request) Extract a full ImageJ hyperstack for a specific region, using all z-slices and time points.static ImagePlusextractHyperstack(ImageServer<BufferedImage> server, RegionRequest request, int zStart, int zEnd, int tStart, int tEnd) Extract a full ImageJ hyperstack for a specific region, for specified ranges of z-slices and time points.static ImagePlanegetImagePlane(Roi roi, ImagePlus imp) Get theImagePlaneof an ImageJ Roi, based upon its stored positions.static booleanisMemorySufficient(RegionRequest region, ImageData<BufferedImage> imageData) Check if sufficient memory is available to request pixels for a specific region, and the number of pixels is less than the maximum length of a Java array.static voidquickShowImage(String name, Roi roi, ImageProcessor... ips) Show an ImageProcessor (or array of similar ImageProcessors as a stack).static voidquickShowImage(String name, ImageProcessor... ips) Show an ImageProcessor (or array of similar ImageProcessors as a stack).readImageJRois(File file) Read ImageJ Rois from a .roi or a RoiSet.zip file.static voidsetMemoryThreshold(double threshold) static voidsetTitleFromObject(PathImage<ImagePlus> pathImage, PathObject pathObject) Set the name of an image based on a PathObject.static doubletryToParseMicrons(double value, String unit) Based on a value and its units, try to get something suitable in microns.
-
Constructor Details
-
IJTools
public IJTools()
-
-
Method Details
-
setMemoryThreshold
public static void setMemoryThreshold(double threshold) - Parameters:
threshold- - value in the interval ]0;1] defining the maximum remaining memory fraction an image can have when importing an image to ImageJ
-
isMemorySufficient
public static boolean isMemorySufficient(RegionRequest region, ImageData<BufferedImage> imageData) throws Exception Check if sufficient memory is available to request pixels for a specific region, and the number of pixels is less than the maximum length of a Java array.- Parameters:
region- the requested region coming fromimageData- this BufferedImage- Returns:
- true if the memory is sufficient
- Throws:
Exception- either the fact that ImageJ cannot handle the image size or that the memory is insufficient
-
extractHyperstack
public static ImagePlus extractHyperstack(ImageServer<BufferedImage> server, RegionRequest request) throws IOException Extract a full ImageJ hyperstack for a specific region, using all z-slices and time points.- Parameters:
server-request-- Returns:
- Throws:
IOException
-
extractHyperstack
public static ImagePlus extractHyperstack(ImageServer<BufferedImage> server, RegionRequest request, int zStart, int zEnd, int tStart, int tEnd) throws IOException Extract a full ImageJ hyperstack for a specific region, for specified ranges of z-slices and time points.- Parameters:
server- server from which to extract pixelsrequest- region and downsample value; if null, the entire image is usedzStart- starting z-slice index (0-based index)zEnd- ending z-slice index (exclusive)tStart- starting timepoint index (0-based index)tEnd- ending timepoint index (exclusive)- Returns:
- the ImageJ hyperstack
- Throws:
IOException
-
convertToFloatProcessor
Convert aSimpleImageto anImageProcessor.- Parameters:
image-- Returns:
-
setTitleFromObject
Set the name of an image based on a PathObject.Useful whenever the ROI for an object is being extracted for display separately.
- Parameters:
pathImage-pathObject-
-
calibrateImagePlus
public static void calibrateImagePlus(ImagePlus imp, RegionRequest request, ImageServer<BufferedImage> server) Set an ImagePlus's Calibration and FileInfo properties based on a RegionRequest and PathImageServer. It is assumed at the image contained in the ImagePlus has been correctly read from the server.- Parameters:
imp-request-server-
-
estimateDownsampleFactor
Estimate the downsample factor for an image region extracted from an image server, based upon the ratio of pixel sizes if possible or ratio of dimensions if necessary.Note that the ratio of dimensions is only suitable if the full image has been extracted!
- Parameters:
imp-server-- Returns:
-
convertToPathObject
@Deprecated public static PathObject convertToPathObject(ImagePlus imp, ImageServer<?> server, Roi roi, double downsampleFactor, Function<ROI, PathObject> creator, ImagePlane plane) Deprecated.Create aPathObjectfor a specific ImageJ Roi. This method has been deprecated, since its signature was misleading (the server was not used).- Parameters:
imp-server-roi-downsampleFactor-creator-plane-- Returns:
-
convertToPathObject
public static PathObject convertToPathObject(Roi roi, double xOrigin, double yOrigin, double downsampleFactor, Function<ROI, PathObject> creator, ImagePlane plane) Create aPathObjectfor a specific ImageJ Roi.- Parameters:
roi- the ImageJ ROIxOrigin- the x-origin to translate the Roi; should beCalibration.xOriginif available, or 0 otherwiseyOrigin- the y-origin to translate the Roi; should beCalibration.yOriginif available, or 0 otherwisedownsampleFactor- the downsample factor used for rescaling (or 1.0 for no rescaling)creator- a functionplane- the specific plane to use for the QuPath ROI; if null, the ImageJ Roi position properties will be used instead, where possible- Returns:
- a
PathObjector null if no object could be created (e.g. the ImageJ roi is null or incompatible) - Since:
- v0.4.0
- See Also:
-
convertToPathObject
public static PathObject convertToPathObject(Roi roi, double downsampleFactor, Function<ROI, PathObject> creator, ImagePlus imp) Create aPathObjectfor a specific ImageJ Roi, using anImagePlusto help set properties.- Parameters:
roi- the ImageJ ROIdownsampleFactor- the downsample factor used for rescaling (or 1.0 for no rescaling)creator- a functionimp- theImagePlusassociated with this Roi; it is used to determine the xOrigin, yOrigin and image plane- Returns:
- a
PathObjector null if no object could be created (e.g. the ImageJ roi is null or incompatible) - Since:
- v0.4.0
- See Also:
-
convertToAnnotation
public static PathObject convertToAnnotation(Roi roi, double xOrigin, double yOrigin, double downsampleFactor, ImagePlane plane) Create an annotation object for a specific ImageJ Roi, using anImagePlusto help set properties.- Parameters:
roi- the ImageJ ROIxOrigin- the x-origin to translate the Roi; should beCalibration.xOriginif available, or 0 otherwiseyOrigin- the y-origin to translate the Roi; should beCalibration.yOriginif available, or 0 otherwisedownsampleFactor- the downsample factor used for rescaling (or 1.0 for no rescaling)plane- the specific plane to use for the QuPath ROI; if null, the ImageJ Roi position properties will be used instead, where possible- Returns:
- a
PathObjector null if no object could be created (e.g. the ImageJ roi is null or incompatible) - Since:
- v0.4.0
- See Also:
-
convertToDetection
public static PathObject convertToDetection(Roi roi, double xOrigin, double yOrigin, double downsampleFactor, ImagePlane plane) Create a detection object for a specific ImageJ Roi, using anImagePlusto help set properties.- Parameters:
roi- the ImageJ ROIxOrigin- the x-origin to translate the Roi; should beCalibration.xOriginif available, or 0 otherwiseyOrigin- the y-origin to translate the Roi; should beCalibration.yOriginif available, or 0 otherwisedownsampleFactor- the downsample factor used for rescaling (or 1.0 for no rescaling)plane- the specific plane to use for the QuPath ROI; if null, the ImageJ Roi position properties will be used instead, where possible- Returns:
- a
PathObjector null if no object could be created (e.g. the ImageJ roi is null or incompatible) - Since:
- v0.4.0
- See Also:
-
convertToAnnotation
Create an annotation object for a specific ImageJ Roi.- Parameters:
roi- the ImageJ ROIdownsampleFactor- the downsample factor used for rescaling (or 1.0 for no rescaling)imp- theImagePlusassociated with this Roi; it is used to determine the xOrigin, yOrigin and image plane- Returns:
- a
PathObjector null if no object could be created (e.g. the ImageJ roi is null or incompatible) - Since:
- v0.4.0
- See Also:
-
convertToDetection
Create a detection object for a specific ImageJ Roi.- Parameters:
roi- the ImageJ ROIdownsampleFactor- the downsample factor used for rescaling (or 1.0 for no rescaling)imp- theImagePlusassociated with this Roi; it is used to determine the xOrigin, yOrigin and image plane- Returns:
- a
PathObjector null if no object could be created (e.g. the ImageJ roi is null or incompatible) - Since:
- v0.4.0
- See Also:
-
readImageJRois
Read ImageJ Rois from a .roi or a RoiSet.zip file.- Parameters:
file-- Returns:
- Implementation Note
- this currently uses the
RoiManagerinternally.
-
containsImageJRois
Check whether a file is likely to contain an ImageJ ROI, based upon its extension or .zip file contents.- Parameters:
file-- Returns:
- true if the file seems to contain ImageJ ROIs, false otherwise
-
calibrateObject
Set the properties of aPathObjectbased upon an ImageJ Roi. This attempts to extract as much useful information as is relevant, including name, color and group.- Parameters:
pathObject-roi-
-
calibrateRoi
Set properties of an ImageJ Roi based upon the property of aPathObject.- Parameters:
roi-pathObject-
-
convertToAnnotation
@Deprecated public static PathObject convertToAnnotation(ImagePlus imp, ImageServer<?> server, Roi roi, double downsampleFactor, ImagePlane plane) Deprecated.Create an annotation object for a specific ImageJ Roi.- Parameters:
imp-server-roi-downsampleFactor-plane-- Returns:
-
convertToDetection
@Deprecated public static PathObject convertToDetection(ImagePlus imp, ImageServer<?> server, Roi roi, double downsampleFactor, ImagePlane plane) Deprecated.Create an detection object for a specific ImageJ Roi.- Parameters:
imp-server-roi-downsampleFactor-plane-- Returns:
-
convertLabelsToCells
public static SortedMap<Number,PathObject> convertLabelsToCells(ImageProcessor ipNuclei, ImageProcessor ipCells, Calibration cal, double downsample, ImagePlane plane) Convert integer labeled images into cell objects.- Parameters:
ipNuclei- labels corresponding to cell nuclei; non-zero values here must be identical to the values in ipCellsipCells- labels corresponding to full cell areascal- aCalibrationobject used to aid conversion between ImageJ and QuPath ROIsdownsample- the downsample value for the ImageProcessors, used to aid conversion between ImageJ and QuPath ROIsplane- theImagePlanedefining where ROIs should be added- Returns:
- a
SortedMapcontaining integer labels from the original labeled images mapped to the corresponding cells that have been created
-
convertLabelsToCells
public static SortedMap<Number,PathObject> convertLabelsToCells(ImageProcessor ipNuclei, ImageProcessor ipCells, double xOrigin, double yOrigin, double downsample, ImagePlane plane) Convert integer labeled images into cell objects.- Parameters:
ipNuclei- labels corresponding to cell nuclei; non-zero values here must be identical to the values in ipCellsipCells- labels corresponding to full cell areasxOrigin- the x pixel coordinate for the top left corner of the image, used to aid conversion between ImageJ and QuPath ROIs; equivalent toCalibration.xOriginyOrigin- the y pixel coordinate for the top left corner of the image, used to aid conversion between ImageJ and QuPath ROIs; equivalent toCalibration.yOrigindownsample- the downsample value for the ImageProcessors, used to aid conversion between ImageJ and QuPath ROIsplane- theImagePlanedefining where ROIs should be added- Returns:
- a
SortedMapcontaining integer labels from the original labeled images mapped to the corresponding cells that have been created
-
quickShowImage
Show an ImageProcessor (or array of similar ImageProcessors as a stack). This is really intended for use with debugging... it takes care of creating an ImagePlus with the specified title, reseting brightness/contrast suitably, setting a roi (if required) and showing the result.- Parameters:
name-roi-ips-
-
quickShowImage
Show an ImageProcessor (or array of similar ImageProcessors as a stack). This is really intended for use with debugging... it takes care of creating an ImagePlus with the specified title, reseting brightness/contrast suitably and showing the result.- Parameters:
name-ips-
-
tryToParseMicrons
Based on a value and its units, try to get something suitable in microns. (In other words, see if the units are 'microns' in some sense, and if not check if they are something else that can easily be converted).- Parameters:
value-unit-- Returns:
- the parsed value in microns, or NaN if the unit couldn't be parsed
-
convertToUncalibratedImagePlus
Convert a BufferedImage to an ImagePlus, without pixel size information or other calibration.- Parameters:
title-img-- Returns:
-
createImageStack
Create an ImageStack containing the specified ImageProcessors.- Parameters:
ips- the ImageProcessors. Each must be the same width, height and type. If empty, an empty stack is returned.- Returns:
-
convertToImageProcessor
Extract pixels as an an ImageProcessor from a single band of a BufferedImage.- Parameters:
img-band-- Returns:
-
convertToImagePlus
public static PathImage<ImagePlus> convertToImagePlus(String title, ImageServer<BufferedImage> server, BufferedImage img, RegionRequest request) throws IOException Convert aBufferedImageinto aPathImage<ImagePlus>.An
ImageServerand aRegionRequestare required to appropriate calibration.- Parameters:
title- a name to use as theImagePlustitle.server- theImageServerfrom which the image was requestedimg- the image to convert - ifnullthis will be requested fromserver.request- the region to request, or that was requested to provideimg- Returns:
- Throws:
IOException
-
convertToImagePlus
public static PathImage<ImagePlus> convertToImagePlus(ImageServer<BufferedImage> server, RegionRequest request) throws IOException Read a region from anImageServer<BufferedImageas aPathImage<ImagePlus>.The
PathImageelement wraps up handy metadata that can be used for translating ROIs.- Parameters:
server-request-- Returns:
- Throws:
IOException
-
createPathImage
public static PathImage<ImagePlus> createPathImage(ImageServer<BufferedImage> server, ImagePlus imp, RegionRequest request) throws IOException Create aPathImagefrom an ImagePlus and region. If imp is null, it is read from the server.- Parameters:
server-imp-request-- Returns:
- Throws:
IOException
-
convertToIJRoi
Create an ImageJ Roi from a ROI, suitable for displaying on the ImagePlus of anPathImage<ImagePlus>.- Parameters:
pathROI-pathImage-- Returns:
-
convertToIJRoi
public static <T extends PathImage<ImagePlus>> Roi convertToIJRoi(ROI pathROI, Calibration cal, double downsampleFactor) Convert a QuPath ROI to an ImageJ Roi for an image with the specified calibration.- Type Parameters:
T-- Parameters:
pathROI-cal-downsampleFactor-- Returns:
- See Also:
-
convertToROI
Create a ROI from an ImageJ Roi, using the Calibration object of an ImagePlus.- Parameters:
roi- ImageJ Roical- calibration object, including original informationdownsampleFactor- the downsample factor of the original imageplane- plane defining c, z and t indices- Returns:
-
convertToROI
Convert an ImageJ Roi to a QuPath ROI.- Parameters:
roi- the ImageJ ROIrequest- the region request for the image that defines the coordinate space for the Roi- Returns:
- the QuPath ROI
- Since:
- v0.6.0
-
getImagePlane
Get theImagePlaneof an ImageJ Roi, based upon its stored positions.- Parameters:
roi- ImageJ roi that may have c, z, t or position properties set.imp- associated image; if not null, this will be used to convert the Roi's 'position' property, if non-zero- Returns:
- the
ImagePlanethat is the best approximation of this Roi's position.
-
convertToROI
Create a ROI from an ImageJ Roi.- Parameters:
roi-pathImage-- Returns:
-
convertToPolygonROI
public static PolygonROI convertToPolygonROI(PolygonRoi roi, Calibration cal, double downsampleFactor, ImagePlane plane) Convert an ImageJ PolygonRoi to a QuPath PolygonROI.- Parameters:
roi-cal-downsampleFactor-plane-- Returns:
-
convertToIJRoi
Convert a QuPath ROI to an ImageJ Roi.- Parameters:
roi- the QuPath ROIrequest- the request encoding the bounding box and downsample of the image for which the Roi should be scaled and translated. If null, no scaling or translation will be applied.- Returns:
- the converted ROI, or null if no input ROI was provided
-
convertToIJRoi
public static Roi convertToIJRoi(ROI pathROI, double xOrigin, double yOrigin, double downsampleFactor) Convert a QuPath ROI to an ImageJ Roi.- Parameters:
pathROI-xOrigin- x-origin indicating relationship of ImagePlus to the original image, as stored in ImageJ Calibration objectyOrigin- y-origin indicating relationship of ImagePlus to the original image, as stored in ImageJ Calibration objectdownsampleFactor- downsample factor at which the ImagePlus was extracted from the full-resolution image- Returns:
- the converted ROI, or null if no input ROI was provided
-
convertToROI
public static ROI convertToROI(Roi roi, double xOrigin, double yOrigin, double downsampleFactor, ImagePlane plane) Create a ROI from an ImageJ Roi.- Parameters:
roi- ImageJ RoixOrigin- x-origin, as stored in an ImageJ Calibration objectyOrigin- y-origin, as stored in an ImageJ Calibration objectdownsampleFactor-plane- plane defining c, z and t indices- Returns:
-
convertToOpticalDensitySum
public static FloatProcessor convertToOpticalDensitySum(ColorProcessor cp, double maxRed, double maxGreen, double maxBlue) Calculate optical density values for the red, green and blue channels, then add these all together.- Parameters:
cp-maxRed-maxGreen-maxBlue-- Returns:
-
colorDeconvolve
Apply color deconvolution to an RGB image, outputting 3 'stain' images in the same order as the stain vectors.- Parameters:
cp- input RGB color imagestains- color deconvolution stain vectors- Returns:
- array containing three
FloatProcessors, representing the deconvolved stains - See Also:
-
colorDeconvolve
public static FloatProcessor[] colorDeconvolve(ImageProcessor ipRed, ImageProcessor ipGreen, ImageProcessor ipBlue, ColorDeconvolutionStains stains) Apply color deconvolution to RGB channels, outputting 3 'stain' images in the same order as the stain vectors. This exists to support color deconvolution of non-RGB images.- Parameters:
ipRed- input red channelipGreen- input green channelipBlue- input blue channelstains- color deconvolution stain vectors- Returns:
- array containing three
FloatProcessors, representing the deconvolved stains - See Also:
-
convertToAnnotation(Roi, double, double, double, ImagePlane)