Class BufferedImageTools

java.lang.Object
qupath.lib.awt.common.BufferedImageTools

public final class BufferedImageTools extends Object
Static methods for working with BufferedImages and QuPath objects.
Author:
Pete Bankhead
  • Method Details

    • createROIMask

      public static BufferedImage createROIMask(int width, int height, ROI roi, RegionRequest request)
      Create a grayscale BufferedImage representing a mask for a specified ROI.

      Pixels inside the ROI will be 255, pixels outside will be 0.

      Parameters:
      width - width of the requested mask image
      height - height of the requested mask image
      roi - ROI for mask
      request - region that the mask should correspond to, including the origin (x & y) and downsample factor to use.
      Returns:
    • createROIMask

      public static BufferedImage createROIMask(ROI roi, double downsample)
      Create a ROI mask using the minimal bounding box for the ROI.
      Parameters:
      roi -
      downsample -
      Returns:
    • createROIMask

      public static BufferedImage createROIMask(int width, int height, ROI roi, double xOrigin, double yOrigin, double downsample)
      Create a grayscale BufferedImage representing a mask for a specified ROI.

      Pixels inside the ROI will be 255, pixels outside will be 0.

      Parameters:
      width - width of the requested mask image
      height - height of the requested mask image
      roi - ROI for mask
      xOrigin - pixel x coordinate of the top left of the region to include in the mask.
      yOrigin - pixel y coordinate of the top left of the region to include in the mask.
      downsample - downsample factor to use when generating the mask, i.e. the amount to scale.
      Returns:
    • createROIMask

      public static BufferedImage createROIMask(Shape shape, double downsample)
      Create a Shape mask using the minimal bounding box for the Shape.
      Parameters:
      shape -
      downsample -
      Returns:
    • createShapeMask

      public static BufferedImage createShapeMask(int width, int height, Shape shape, double xOrigin, double yOrigin, double downsample)
      Create a grayscale BufferedImage representing a mask for a specified ROI.

      Pixels inside the ROI will be 255, pixels outside will be 0.

      Parameters:
      width - width of the requested mask image
      height - height of the requested mask image
      shape - Shape for mask
      xOrigin - pixel x coordinate of the top left of the region to include in the mask.
      yOrigin - pixel y coordinate of the top left of the region to include in the mask.
      downsample - downsample factor to use when generating the mask, i.e. the amount to scale.
      Returns:
    • ensureBufferedImageType

      public static BufferedImage ensureBufferedImageType(BufferedImage img, int requestedType)
      Convert a BufferedImage to have a specified type.

      This makes use of Graphics2D.drawImage, which imposes limits on supported types (i.e. RGB or 8-bit grayscale), and is therefore not suitable for arbitrary type conversion.

      A possible use is to ensure that a BGR/RGB/ARGB image is converted to the same representation, for example to allow packed int arrays to be treated in the same way.

      Images that already have the same type are returned unchanged.

      Parameters:
      img - the input image
      requestedType - the type to which the image should be converted
      Returns:
      the (possibly-new) output image
      See Also:
    • ensureBufferedImage

      public static BufferedImage ensureBufferedImage(Image image)
      Convert an Image to a BufferedImage if necessary, or return the original image unchanged if it is already a BufferedImage.
      Parameters:
      image - the image to (possible convert)
      Returns:
      a BufferedImage
    • duplicate

      public static BufferedImage duplicate(BufferedImage img)
      Duplicate a BufferedImage. This retains the same color model, but copies the raster. Properties are copied if non-null.
      Parameters:
      img -
      Returns:
    • swapRGBOrder

      public static void swapRGBOrder(BufferedImage img, String order)
      Swap the order of pixels in an RGB image. Specify the order in which each channel should appear in the output image. The color model is unchanged; the purpose of this method is to 'fix' problems that may occur when an RGB image has channels wrongly interpreted (normally BGR rather than RGB).
      Parameters:
      img - input image
      order - a String that is one of "RGB", "RBG", "GRB", "GBR", "BRG", "BGR".
      Throws:
      IllegalArgumentException - if the image type is not TYPE_INT_ARGB or TYPE_INT_RGB.
    • setValues

      public static void setValues(DataBuffer buffer, double val)
      Set all values in a DataBuffer.
      Parameters:
      buffer - the buffer containing the banks whose values should be set.
      val - the requested value. This will be cast to an int or float if necessary.
    • is8bitColorType

      public static boolean is8bitColorType(int type)
      Returns true if a BufferedImage type represents an 8-bit color image. The precise representation (BGR, RGB, byte, int, with/without alpha) is not important.
      Parameters:
      type -
      Returns:
      See Also:
    • crop

      public static BufferedImage crop(BufferedImage img, int x, int y, int width, int height)
      Create a crop of the image using the specified bounding box.
      Parameters:
      img -
      x -
      y -
      width -
      height -
      Returns:
      cropped image, or a duplicate of the original image if no cropping is required
    • resize

      public static BufferedImage resize(BufferedImage img, int finalWidth, int finalHeight, boolean smoothInterpolate)
      Resize the image to have the requested width/height, using area averaging and bilinear interpolation.
      Parameters:
      img - input image to be resized
      finalWidth - target output width
      finalHeight - target output height
      smoothInterpolate - if true, the resize method is permitted to use a smooth interpolation method. If false, nearest-neighbor interpolation is used.
      Returns:
      resized image
    • computeUnsignedIntHistogram

      public static long[] computeUnsignedIntHistogram(WritableRaster raster, long[] counts, WritableRaster rasterMask)
      Compute the full histogram for a raster containing 8-bit or 16-bit unsigned integer values.
      Parameters:
      raster - the raster containing the data for the histogram; if not TYPE_BYTE or TYPE_USHORT an IllegalArgumentException will be thrown
      counts - histogram counts; if null, a new array will be created. Its must be sufficient for the data type, i.e. 256 or 65536. No size checking is performed, therefore if non-null it must be sufficiently large for the data type.
      rasterMask - optional single-channel mask; if not null, corresponding pixels with 0 values in the mask will be skipped
      Returns:
      the updated histogram counts
      See Also:
    • computeUnsignedIntHistogram

      public static long[] computeUnsignedIntHistogram(WritableRaster raster, long[] counts, WritableRaster rasterMask, Rectangle bounds)
      Compute the full histogram for a raster containing 8-bit or 16-bit unsigned integer values, optionally restricted to a bounding rectangle.
      Parameters:
      raster - the raster containing the data for the histogram; if not TYPE_BYTE or TYPE_USHORT an IllegalArgumentException will be thrown
      counts - histogram counts; if null, a new array will be created. Its must be sufficient for the data type, i.e. 256 or 65536. No size checking is performed, therefore if non-null it must be sufficiently large for the data type.
      rasterMask - optional single-channel mask; if not null, corresponding pixels with 0 values in the mask will be skipped.
      bounds - optional rectangle defining the bounds of the raster to search. If not null, this is used with the (also optional) mask to restrict the pixels that are used. This can give substantial performance improvements for small masked regions embedded in larger images.
      Returns:
      the updated histogram counts
      See Also:
    • computeArgMaxHistogram

      public static long[] computeArgMaxHistogram(WritableRaster raster, long[] counts, WritableRaster rasterMask)
      Create a histogram that identifies the channels (bands) of an image with the maximum values according to the argmax criterion.
      Parameters:
      raster - the multi-band raster containing values to check
      counts - existing histogram if it should be updated, or null if a new histogram should be created. The length should match the number of bands in the raster.
      rasterMask - optional single-channel mask; if not null, corresponding pixels with 0 values in the mask will be skipped
      Returns:
      See Also:
    • computeArgMaxHistogram

      public static long[] computeArgMaxHistogram(WritableRaster raster, long[] counts, WritableRaster rasterMask, Rectangle bounds)
      Create a histogram that identifies the channels (bands) of an image with the maximum values according to the argmax criterion, with an optional bounding rectangle.
      Parameters:
      raster - the multi-band raster containing values to check
      counts - existing histogram if it should be updated, or null if a new histogram should be created. The length should match the number of bands in the raster.
      rasterMask - optional single-channel mask; if not null, corresponding pixels with 0 values in the mask will be skipped
      bounds - optional rectangle defining the bounds of the raster to search. If not null, this is used with the (also optional) mask to restrict the pixels that are used. This can give substantial performance improvements for small masked regions embedded in larger images.
      Returns:
      See Also:
    • computeAboveThresholdCounts

      public static long computeAboveThresholdCounts(WritableRaster raster, int band, double threshold, WritableRaster rasterMask)
      Count the number of above-threshold pixels in a specified band of a raster, with optional mask.
      Parameters:
      raster - the multi-band raster containing values to check
      band - the band (channel) to consider
      threshold - threshold value; pixels with values > threshold this will be counted
      rasterMask - optional single-channel mask; if not null, corresponding pixels with 0 values in the mask will be skipped
      Returns:
      See Also:
    • computeAboveThresholdCounts

      public static long computeAboveThresholdCounts(WritableRaster raster, int band, double threshold, WritableRaster rasterMask, Rectangle bounds)
      Count the number of above-threshold pixels in a specified band of a raster, with optional mask and/or bounding rectangle.
      Parameters:
      raster - the multi-band raster containing values to check
      band - the band (channel) to consider
      threshold - threshold value; pixels with values > threshold this will be counted
      rasterMask - optional single-channel mask; if not null, corresponding pixels with 0 values in the mask will be skipped
      bounds - optional rectangle defining the bounds of the raster to search. If not null, this is used with the (also optional) mask to restrict the pixels that are used. This can give substantial performance improvements for small masked regions embedded in larger images.
      Returns:
      See Also: