Class RoiLabeling

java.lang.Object
qupath.imagej.processing.RoiLabeling

public class RoiLabeling extends Object
Collection of static methods to help work with ROIs, binary & labelled images in ImageJ.

This enables switching between different methods of representing regions as required.

Author:
Pete Bankhead
  • Constructor Details

    • RoiLabeling

      public RoiLabeling()
  • Method Details

    • findDirectionalMaxima

      public static ByteProcessor findDirectionalMaxima(ImageProcessor ip)
      Create a binary image for pixels that have a higher value than their neighbors. Comparisons are made horizontally, vertically and diagonally. Pixels meeting the criterion have the value 255, all others are 0.
      Parameters:
      ip -
      Returns:
    • findDirectionalMinima

      public static ByteProcessor findDirectionalMinima(ImageProcessor ip)
      Create a binary image for pixels that have a lower value than their neighbors. Comparisons are made horizontally, vertically and diagonally. Pixels meeting the criterion have the value 255, all others are 0.
      Parameters:
      ip -
      Returns:
    • getFilledPolygonROIsExperimental

      public static List<PolygonRoi> getFilledPolygonROIsExperimental(ImageProcessor ip)
      Experimental 8-connected ROI creation; non-zero pixels considered within objects

      TODO: Improve experimental getFillPolygonROIs!!! Consider efficiency (i.e. no min filter) & standard ImageJ compatibility Using standard ImageJ draw/fill, the original binary image is NOT reconstructed.

      Parameters:
      ip -
      Returns:
    • labelsToFilledROIs

      public static PolygonRoi[] labelsToFilledROIs(ImageProcessor ipLabels, int n)
      Convert a labelled image into a list of PolygonRois by tracing.

      Note that labels are assumed not to contain any holes or nested ROIs; ROIs produced by this command will not contain holes.

      Some entries in the resulting array may be null if this is not the case, or if not all labels are found. Otherwise, pixels with the integer label L will belong to the Roi in the output array at entry L-1

      Parameters:
      ipLabels -
      n - maximum number of labels
      Returns:
    • labelsToFilledRoiList

      public static List<PolygonRoi> labelsToFilledRoiList(ImageProcessor ipLabels, boolean conn8)
      Convert a labelled image into a list of PolygonRois by tracing.

      Unlike labelsToFilledROIs, the order in which ROIs are returned is arbitrary.

      Also, the multiple Rois may be created for the same label, if unconnected regions are used.

      Parameters:
      ipLabels -
      conn8 -
      Returns:
    • labelsToConnectedROIs

      public static Roi[] labelsToConnectedROIs(ImageProcessor ipLabels, int n)
      Create ROIs from labels in an image.
      Parameters:
      ipLabels - the labeled image; generally this should be a ByteProcessor or ShortProcessor
      n - the total number of labels; often this is equal to the maximum value in the image
      Returns:
      an array of length n; output[i] is the ROI for label i+1, or null if no Roi is found with that label.
    • getFilledPolygonROIsFromLabels

      public static Map<Float,PolygonRoi> getFilledPolygonROIsFromLabels(ImageProcessor ip, int wandMode)
      Get filled Polygon ROIs using distinct labels, creating a map between labels and ROIs.

      Note that discontinuous ROIs are not supported; if labelled regions are discontinuous, then ROIs detected earlier will be discarded from the map.

      Parameters:
      ip -
      wandMode -
      Returns:
    • getFilledPolygonROIs

      public static List<PolygonRoi> getFilledPolygonROIs(ImageProcessor ip, int wandMode)
      Get filled PolygonRois for connected pixels with the same value in an image. Because this uses ImageJ's Wand tool, holes will be filled.

      Note that this command applies any thresholds that were set in the ImageProcessor, returning only Rois for values within these limits. Therefore to identify only non-zero pixels in a labelled image you may need to first call ip.setThreshold(0.5, Double.POSITIVE_INFINITY, ImageProcessor.NO_LUT_UPDATE);

      Parameters:
      ip -
      wandMode -
      Returns:
    • wandToRoi

      public static PolygonRoi wandToRoi(Wand wand)
      Convert a traced outline from the ImageJ Wand into a PolygonRoi.
      Parameters:
      wand -
      Returns:
    • fillHoles

      public static void fillHoles(ByteProcessor bp)
      Fill holes in a binary image.

      Assumes 255 is foreground, 0 is background.

      Based on code in ImageJ's Binary class.

      Parameters:
      bp -
    • labelROIs

      public static ImageProcessor labelROIs(ImageProcessor ipLabels, List<? extends Roi> rois)
      Label ROIs by filling each pixel with an integer value corresponding to the index of the Roi in the list + 1.
      Parameters:
      ipLabels -
      rois -
      Returns:
    • labelImage

      public static ImageProcessor labelImage(ImageProcessor ip, float threshold, boolean conn8)
      Create a labelled image from above-threshold pixels for an image.
      Parameters:
      ip -
      threshold -
      conn8 -
      Returns:
      labelled image, as a ShortProcessor (if possible) or FloatProcessor (if necessary)
    • removeSmallAreas

      public static void removeSmallAreas(ByteProcessor bp, double minPixels, boolean conn8)
      Remove objects having small areas, defined in terms of pixels
      Parameters:
      bp -
      minPixels - minimum number of pixels in an object that should be kept
      conn8 -
    • removeByAreas

      public static int removeByAreas(ByteProcessor bp, double minPixels, double maxPixels, boolean conn8)
      Remove objects containing < minPixels or > maxPixels.
      Parameters:
      bp -
      minPixels -
      maxPixels -
      conn8 -
      Returns:
      the number of connected objects remaining.
    • fillOutside

      public static void fillOutside(ImageProcessor ip, Roi roi, double value)
      Fill in a region outside a specified ROI
      Parameters:
      ip -
      roi -
      value -
    • clearOutside

      public static void clearOutside(ImageProcessor ip, Roi roi)
      Clear (i.e. set pixels to zero) in a region outside a specified ROI
      Parameters:
      ip -
      roi -
    • clearBoundary

      public static void clearBoundary(ByteProcessor bp, Roi roi, double clearValue)
      Starting from all white pixels (value = 255) on a ROI's boundary, fill the pixels with black
      Parameters:
      bp -
      roi -
      clearValue -