Package qupath.opencv.ml.pixel
Class PixelClassifierTools
java.lang.Object
qupath.opencv.ml.pixel.PixelClassifierTools
Helper methods for working with pixel classification.
- Author:
- Pete Bankhead
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
Options when creating objects from a pixel classifier. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
addMeasurements
(Collection<? extends PathObject> objectsToMeasure, PixelClassificationMeasurementManager manager, String measurementID) Add measurements to specified objects from aPixelClassificationMeasurementManager
.static boolean
addMeasurementsToSelectedObjects
(ImageData<BufferedImage> imageData, PixelClassifier classifier, String measurementID) Add measurements to selected objects based upon the output of aPixelClassifier
.static void
classifyCellsByCentroid
(ImageData<BufferedImage> imageData, PixelClassifier classifier, boolean preferNucleusROI) Classify cells according to the prediction of the pixel corresponding to the cell centroid using aPixelClassifier
.static void
classifyDetectionsByCentroid
(ImageData<BufferedImage> imageData, PixelClassifier classifier) Classify detections according to the prediction of the pixel corresponding to the detection centroid using aPixelClassifier
.static void
classifyObjectsByCentroid
(ImageData<BufferedImage> imageData, PixelClassifier classifier, Collection<PathObject> pathObjects, boolean preferNucleusROI) Classify objects according to the prediction of the pixel corresponding to the object's ROI centroid using aPixelClassifier
.static void
classifyObjectsByCentroid
(ImageServer<BufferedImage> classifierServer, Collection<PathObject> pathObjects, boolean preferNucleusROI) Apply classification from a server to a collection of objects.static boolean
createAnnotationsFromPixelClassifier
(ImageData<BufferedImage> imageData, PixelClassifier classifier, double minArea, double minHoleArea, PixelClassifierTools.CreateObjectOptions... options) Create annotation objects based upon the output of a pixel classifier, applied to selected objects.static boolean
createAnnotationsFromPixelClassifier
(PathObjectHierarchy hierarchy, ImageServer<BufferedImage> classifierServer, double minArea, double minHoleArea, PixelClassifierTools.CreateObjectOptions... options) Create annotation objects based upon anImageServer
that provides classification or probability output, applied to selected objects.static boolean
createDetectionsFromPixelClassifier
(ImageData<BufferedImage> imageData, PixelClassifier classifier, double minArea, double minHoleArea, PixelClassifierTools.CreateObjectOptions... options) Create detection objects based upon the output of a pixel classifier, applied to selected objects.static boolean
createDetectionsFromPixelClassifier
(PathObjectHierarchy hierarchy, ImageServer<BufferedImage> classifierServer, double minArea, double minHoleArea, PixelClassifierTools.CreateObjectOptions... options) Create detection objects based upon anImageServer
that provides classification or probability output, applied to selected objects.createMeasurementManager
(ImageData<BufferedImage> imageData, PixelClassifier classifier) Create aPixelClassificationMeasurementManager
that can be used to generate measurements from applying a pixel classifier to an image.createMeasurementManager
(ImageServer<BufferedImage> classifierServer) Create aPixelClassificationMeasurementManager
that can be used to generate measurements from anImageServer
where pixels provide classification or probability information.static Collection
<PathObject> createObjectsFromPixelClassifier
(ImageServer<BufferedImage> server, Map<Integer, PathClass> labels, ROI roi, Function<ROI, ? extends PathObject> creator, double minArea, double minHoleArea, boolean doSplit) Create objects based upon anImageServer
that provides classification or probability output.static boolean
createObjectsFromPredictions
(ImageServer<BufferedImage> server, PathObjectHierarchy hierarchy, Collection<PathObject> selectedObjects, Function<ROI, ? extends PathObject> creator, double minArea, double minHoleArea, PixelClassifierTools.CreateObjectOptions... options) Create objects from an image (usually created with a pixel classifier) where values represent classifications or predictions.static ImageServer
<BufferedImage> createPixelClassificationServer
(ImageData<BufferedImage> imageData, PixelClassifier classifier) Create anImageServer
that displays the results of applying aPixelClassifier
to an image.static ImageServer
<BufferedImage> createPixelClassificationServer
(ImageData<BufferedImage> imageData, PixelClassifier classifier, String id, ColorModel colorModel, boolean cacheAllTiles) Create anImageServer
that displays the results of applying aPixelClassifier
to an image.static ImageServer
<BufferedImage> createThresholdServer
(ImageServer<BufferedImage> server, int channel, double threshold, PathClass below, PathClass aboveEquals) Create a newImageServer
by applying a threshold to one channel of another server.static ImageServer
<BufferedImage> createThresholdServer
(ImageServer<BufferedImage> server, Map<Integer, ? extends Number> thresholds, PathClass below, PathClass aboveEquals) Create a newImageServer
by applying a threshold to one or more channels of another server.static int
getClassification
(ImageServer<BufferedImage> server, int x, int y, int z, int t) Request the classification for a specific pixel.
-
Constructor Details
-
PixelClassifierTools
public PixelClassifierTools()
-
-
Method Details
-
createDetectionsFromPixelClassifier
public static boolean createDetectionsFromPixelClassifier(PathObjectHierarchy hierarchy, ImageServer<BufferedImage> classifierServer, double minArea, double minHoleArea, PixelClassifierTools.CreateObjectOptions... options) throws IOException Create detection objects based upon anImageServer
that provides classification or probability output, applied to selected objects. If no objects are selected, objects are created across the entire image.- Parameters:
hierarchy
-classifierServer
-minArea
- the minimum area of connected regions to retainminHoleArea
- the minimum area of connected 'hole' regions to retainoptions
- additional options to control how objects are created- Returns:
- true if changes were made to the hierarchy, false otherwise
- Throws:
IOException
-
createDetectionsFromPixelClassifier
public static boolean createDetectionsFromPixelClassifier(ImageData<BufferedImage> imageData, PixelClassifier classifier, double minArea, double minHoleArea, PixelClassifierTools.CreateObjectOptions... options) throws IOException Create detection objects based upon the output of a pixel classifier, applied to selected objects. If no objects are selected, objects are created across the entire image.- Parameters:
imageData
- the original image data, which will be the input to the pixel classifierclassifier
- the pixel classifierminArea
- the minimum area of connected regions to retainminHoleArea
- the minimum area of connected 'hole' regions to retainoptions
- additional options to control how objects are created- Returns:
- true if changes were made to the hierarchy, false otherwise
- Throws:
IOException
-
createObjectsFromPredictions
public static boolean createObjectsFromPredictions(ImageServer<BufferedImage> server, PathObjectHierarchy hierarchy, Collection<PathObject> selectedObjects, Function<ROI, ? extends PathObject> creator, double minArea, double minHoleArea, PixelClassifierTools.CreateObjectOptions... options) throws IOExceptionCreate objects from an image (usually created with a pixel classifier) where values represent classifications or predictions.- Parameters:
server
- the image to thresholdhierarchy
- the hierarchy to which the objects should be addedselectedObjects
- the selected objects, if the classification should be constrained to thesecreator
- function to create an object of the required typeminArea
- the minimum size of a connected region to retain, in calibrated unitsminHoleArea
- the minimum size of a hole to retain, in calibrated unitsoptions
- additional options to control how objects are created- Returns:
- true if the command ran successfully to completion, false otherwise.
- Throws:
IOException
- See Also:
-
createAnnotationsFromPixelClassifier
public static boolean createAnnotationsFromPixelClassifier(ImageData<BufferedImage> imageData, PixelClassifier classifier, double minArea, double minHoleArea, PixelClassifierTools.CreateObjectOptions... options) throws IOException Create annotation objects based upon the output of a pixel classifier, applied to selected objects. If no objects are selected, objects are created across the entire image.- Parameters:
imageData
- the original image data, which will be the input to the pixel classifierclassifier
- the pixel classifierminArea
- the minimum area of connected regions to retainminHoleArea
- the minimum area of connected 'hole' regions to retainoptions
- additional options to control how objects are created- Returns:
- true if changes were made to the hierarchy, false otherwise
- Throws:
IOException
-
createAnnotationsFromPixelClassifier
public static boolean createAnnotationsFromPixelClassifier(PathObjectHierarchy hierarchy, ImageServer<BufferedImage> classifierServer, double minArea, double minHoleArea, PixelClassifierTools.CreateObjectOptions... options) throws IOException Create annotation objects based upon anImageServer
that provides classification or probability output, applied to selected objects. If no objects are selected, objects are created across the entire image.- Parameters:
hierarchy
-classifierServer
-minArea
- the minimum area of connected regions to retainminHoleArea
- the minimum area of connected 'hole' regions to retainoptions
- additional options to control how objects are created- Returns:
- true if changes were made to the hierarchy, false otherwise
- Throws:
IOException
-
createObjectsFromPixelClassifier
public static Collection<PathObject> createObjectsFromPixelClassifier(ImageServer<BufferedImage> server, Map<Integer, PathClass> labels, ROI roi, Function<ROI, throws IOException? extends PathObject> creator, double minArea, double minHoleArea, boolean doSplit) Create objects based upon anImageServer
that provides classification or probability output.- Parameters:
server
- image server providing pixels from which objects should be createdlabels
- classification labels; if null, these will be taken from ImageServer#getMetadata() and all non-ignored classifications will be used. Providing a map makes it possible to explicitly exclude some classifications.roi
- region of interest in which objects should be created (optional; if null, the entire image is used)creator
- function to create an object from a ROI (e.g. annotation or detection)minArea
- minimum area for an object fragment to retain, in calibrated units based on the pixel calibrationminHoleArea
- minimum area for a hole to fill, in calibrated units based on the pixel calibrationdoSplit
- if true, split connected regions into separate objects- Returns:
- the objects created within the ROI
- Throws:
IOException
-
createPixelClassificationServer
public static ImageServer<BufferedImage> createPixelClassificationServer(ImageData<BufferedImage> imageData, PixelClassifier classifier) Create anImageServer
that displays the results of applying aPixelClassifier
to an image.- Parameters:
imageData
- the image to which the classifier should applyclassifier
- the pixel classifier- Returns:
- the classification
ImageServer
-
createPixelClassificationServer
public static ImageServer<BufferedImage> createPixelClassificationServer(ImageData<BufferedImage> imageData, PixelClassifier classifier, String id, ColorModel colorModel, boolean cacheAllTiles) Create anImageServer
that displays the results of applying aPixelClassifier
to an image.- Parameters:
imageData
- the image to which the classifier should applyclassifier
- the pixel classifierid
- an ID to use for theImageServer
; this may be null, in which case an ID will be derived (if possible from a JSON representation of the classifier)colorModel
- optional colormodel for the classifier (may be null to use the default)cacheAllTiles
- optionally request that all tiles are computed immediately as the classifier is created. This is useful for images that are 'small' and where the classification can comfortably be held in RAM.- Returns:
- the classification
ImageServer
-
createThresholdServer
public static ImageServer<BufferedImage> createThresholdServer(ImageServer<BufferedImage> server, Map<Integer, ? extends Number> thresholds, PathClass below, PathClass aboveEquals) Create a newImageServer
by applying a threshold to one or more channels of another server. This is particularly useful where one channel represents intensities to threshold, and one channel should be used as a mask.- Parameters:
server
- the server to thresholdthresholds
- map between channel numbers (zero-based) and thresholdsbelow
- the classification for pixels whose values are below the threshold in any channelaboveEquals
- the classification for pixels whose values are greater than or equal to the threshold in all channels- Returns:
- the thresholded server
-
createThresholdServer
public static ImageServer<BufferedImage> createThresholdServer(ImageServer<BufferedImage> server, int channel, double threshold, PathClass below, PathClass aboveEquals) Create a newImageServer
by applying a threshold to one channel of another server.- Parameters:
server
- the server to thresholdchannel
- the channel to threshold (zero-based)threshold
- the threshold value to applybelow
- the classification for pixels below the threshold (must not be null)aboveEquals
- the classification for pixels greater than or equal to the threshold (must not be null)- Returns:
- the thresholded server
-
createMeasurementManager
public static PixelClassificationMeasurementManager createMeasurementManager(ImageData<BufferedImage> imageData, PixelClassifier classifier) Create aPixelClassificationMeasurementManager
that can be used to generate measurements from applying a pixel classifier to an image.- Parameters:
imageData
- the image to which the classifier should be appliedclassifier
- the pixel classifier- Returns:
- the
PixelClassificationMeasurementManager
-
createMeasurementManager
public static PixelClassificationMeasurementManager createMeasurementManager(ImageServer<BufferedImage> classifierServer) Create aPixelClassificationMeasurementManager
that can be used to generate measurements from anImageServer
where pixels provide classification or probability information.- Parameters:
classifierServer
- the classification image server- Returns:
- the
PixelClassificationMeasurementManager
-
addMeasurementsToSelectedObjects
public static boolean addMeasurementsToSelectedObjects(ImageData<BufferedImage> imageData, PixelClassifier classifier, String measurementID) Add measurements to selected objects based upon the output of aPixelClassifier
.- Parameters:
imageData
- the image data, which will be input to the classifier and which contains the selected objects to measure. If no objects are selected, measurements will be applied to the entire image.classifier
- the pixel classifiermeasurementID
- identifier that is prepended to measurement names, to make these identifiable later (optional; may be null)- Returns:
- true if measurements were added, false otherwise
-
addMeasurements
public static boolean addMeasurements(Collection<? extends PathObject> objectsToMeasure, PixelClassificationMeasurementManager manager, String measurementID) Add measurements to specified objects from aPixelClassificationMeasurementManager
.- Parameters:
objectsToMeasure
- the objects to measure.manager
- the manager used to generate measurementsmeasurementID
- identifier that is prepended to measurement names, to make these identifiable later (optional; may be null)- Returns:
- true if measurements were added, false otherwise
-
classifyObjectsByCentroid
public static void classifyObjectsByCentroid(ImageServer<BufferedImage> classifierServer, Collection<PathObject> pathObjects, boolean preferNucleusROI) Apply classification from a server to a collection of objects.- Parameters:
classifierServer
- anImageServer
with output typepathObjects
-preferNucleusROI
-
-
getClassification
public static int getClassification(ImageServer<BufferedImage> server, int x, int y, int z, int t) throws IOException Request the classification for a specific pixel.If the output for the server is
ImageServerMetadata.ChannelType.PROBABILITY
and only one channel is available, the return value will be -1 if the probability is less than 0.5 (or 127.5 if 8-bit).- Parameters:
server
-x
-y
-z
-t
-- Returns:
- Throws:
IOException
-
classifyCellsByCentroid
public static void classifyCellsByCentroid(ImageData<BufferedImage> imageData, PixelClassifier classifier, boolean preferNucleusROI) Classify cells according to the prediction of the pixel corresponding to the cell centroid using aPixelClassifier
.- Parameters:
imageData
- theImageData
containing the cellsclassifier
- the classifierpreferNucleusROI
- whether to use the nucleus ROI (if available) rather than the cell ROI
-
classifyDetectionsByCentroid
public static void classifyDetectionsByCentroid(ImageData<BufferedImage> imageData, PixelClassifier classifier) Classify detections according to the prediction of the pixel corresponding to the detection centroid using aPixelClassifier
. If the detections are cells, the nucleus ROI is used where possible.- Parameters:
imageData
- theImageData
containing the cellsclassifier
- the classifier
-
classifyObjectsByCentroid
public static void classifyObjectsByCentroid(ImageData<BufferedImage> imageData, PixelClassifier classifier, Collection<PathObject> pathObjects, boolean preferNucleusROI) Classify objects according to the prediction of the pixel corresponding to the object's ROI centroid using aPixelClassifier
.- Parameters:
imageData
- theImageData
containing the cellsclassifier
- the classifierpathObjects
- the objects to classifypreferNucleusROI
- use the nucleus ROI in the case of cells; ignored for all other object types
-