Class PixelClassifierTools

java.lang.Object
qupath.opencv.ml.pixel.PixelClassifierTools

public class PixelClassifierTools extends Object
Helper methods for working with pixel classification.
Author:
Pete Bankhead
  • 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 an ImageServer 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 retain
      minHoleArea - the minimum area of connected 'hole' regions to retain
      options - 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 classifier
      classifier - the pixel classifier
      minArea - the minimum area of connected regions to retain
      minHoleArea - the minimum area of connected 'hole' regions to retain
      options - 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 IOException
      Create objects from an image (usually created with a pixel classifier) where values represent classifications or predictions.
      Parameters:
      server - the image to threshold
      hierarchy - the hierarchy to which the objects should be added
      selectedObjects - the selected objects, if the classification should be constrained to these
      creator - function to create an object of the required type
      minArea - the minimum size of a connected region to retain, in calibrated units
      minHoleArea - the minimum size of a hole to retain, in calibrated units
      options - 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 classifier
      classifier - the pixel classifier
      minArea - the minimum area of connected regions to retain
      minHoleArea - the minimum area of connected 'hole' regions to retain
      options - 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 an ImageServer 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 retain
      minHoleArea - the minimum area of connected 'hole' regions to retain
      options - 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,? extends PathObject> creator, double minArea, double minHoleArea, boolean doSplit) throws IOException
      Create objects based upon an ImageServer that provides classification or probability output.
      Parameters:
      server - image server providing pixels from which objects should be created
      labels - 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 calibration
      minHoleArea - minimum area for a hole to fill, in calibrated units based on the pixel calibration
      doSplit - 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 an ImageServer that displays the results of applying a PixelClassifier to an image.
      Parameters:
      imageData - the image to which the classifier should apply
      classifier - 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 an ImageServer that displays the results of applying a PixelClassifier to an image.
      Parameters:
      imageData - the image to which the classifier should apply
      classifier - the pixel classifier
      id - an ID to use for the ImageServer; 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 new ImageServer 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 threshold
      thresholds - map between channel numbers (zero-based) and thresholds
      below - the classification for pixels whose values are below the threshold in any channel
      aboveEquals - 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 new ImageServer by applying a threshold to one channel of another server.
      Parameters:
      server - the server to threshold
      channel - the channel to threshold (zero-based)
      threshold - the threshold value to apply
      below - 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 a PixelClassificationMeasurementManager 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 applied
      classifier - the pixel classifier
      Returns:
      the PixelClassificationMeasurementManager
    • createMeasurementManager

      public static PixelClassificationMeasurementManager createMeasurementManager(ImageServer<BufferedImage> classifierServer)
      Create a PixelClassificationMeasurementManager that can be used to generate measurements from an ImageServer 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 a PixelClassifier.
      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 classifier
      measurementID - 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 a PixelClassificationMeasurementManager.
      Parameters:
      objectsToMeasure - the objects to measure.
      manager - the manager used to generate measurements
      measurementID - 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 - an ImageServer with output type
      pathObjects -
      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 a PixelClassifier.
      Parameters:
      imageData - the ImageData containing the cells
      classifier - the classifier
      preferNucleusROI - 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 a PixelClassifier. If the detections are cells, the nucleus ROI is used where possible.
      Parameters:
      imageData - the ImageData containing the cells
      classifier - 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 a PixelClassifier.
      Parameters:
      imageData - the ImageData containing the cells
      classifier - the classifier
      pathObjects - the objects to classify
      preferNucleusROI - use the nucleus ROI in the case of cells; ignored for all other object types