Class TransformedServerBuilder

java.lang.Object
qupath.lib.images.servers.TransformedServerBuilder

public class TransformedServerBuilder extends Object
Helper class for creating an ImageServer that applies one or more transforms to another (wrapped) ImageServer.

Note: This is an early-stage experimental class, which may well change!

  • Constructor Details

    • TransformedServerBuilder

      public TransformedServerBuilder(ImageServer<BufferedImage> baseServer)
      Create a transformed ImageServer.
      Parameters:
      baseServer - the initial server that will be transformed.
  • Method Details

    • crop

      public TransformedServerBuilder crop(ImageRegion region)
      Crop a specified region based on a bounding box.
      Parameters:
      region - the region to crop
      Returns:
      this builder
    • slice

      public TransformedServerBuilder slice(int zStart, int zEnd, int tStart, int tEnd)
      Slice a specific region along the z or the t axis.
      Parameters:
      zStart - the inclusive 0-based index of the first slice to consider
      zEnd - the exclusive 0-based index of the last slide to consider
      tStart - the inclusive 0-based index of the first timepoint to consider
      tEnd - the exclusive 0-based index of the last timepoint to consider
      Returns:
      this builder
      Throws:
      IllegalArgumentException - when a start index is greater than its corresponding end index
    • slice

      public TransformedServerBuilder slice(int zStart, int zEnd, int zStep, int tStart, int tEnd, int tStep)
      Slice a specific region along the z or the t axis with a step.
      Parameters:
      zStart - the inclusive 0-based index of the first slice to consider
      zEnd - the exclusive 0-based index of the last slide to consider
      zStep - a step to indicate which slides to consider
      tStart - the inclusive 0-based index of the first timepoint to consider
      tEnd - the exclusive 0-based index of the last timepoint to consider
      tStep - a step to indicate which timepoints to consider
      Returns:
      this builder
      Throws:
      IllegalArgumentException - when a start index is greater than its corresponding end index, or when a step is less than or equal to 0
    • zProjectMean

      public TransformedServerBuilder zProjectMean()
      Apply a mean Z-projection to the image. The projection will be calculated from all z-slices, and the resulting image will have a single z-slice.
      Returns:
      this builder
    • zProjectMin

      public TransformedServerBuilder zProjectMin()
      Apply a minimum Z-projection to the image. The projection will be calculated from all z-slices, and the resulting image will have a single z-slice.
      Returns:
      this builder
    • zProjectMax

      public TransformedServerBuilder zProjectMax()
      Apply a maximum Z-projection to the image. The projection will be calculated from all z-slices, and the resulting image will have a single z-slice.
      Returns:
      this builder
    • zProjectSum

      public TransformedServerBuilder zProjectSum()
      Apply a sum Z-projection to the image. The projection will be calculated from all z-slices, and the resulting image will have a single z-slice.
      Returns:
      this builder
    • zProjectStandardDeviation

      public TransformedServerBuilder zProjectStandardDeviation()
      Apply a standard deviation Z-projection to the image. The projection will be calculated from all z-slices, and the resulting image will have a single z-slice.
      Returns:
      this builder
    • zProjectMedian

      public TransformedServerBuilder zProjectMedian()
      Apply a median Z-projection to the image. The projection will be calculated from all z-slices, and the resulting image will have a single z-slice.
      Returns:
      this builder
    • zProject

      Apply a Z-projection. The projection will be calculated from all z-slices, and the resulting image will have a single z-slice.
      Parameters:
      projection - a type of projection to convert the multiple z-stacks into one
      Returns:
      this builder
    • zProject

      public TransformedServerBuilder zProject(ZProjectedImageServer.Projection projection, int offset)
      Apply a Z-projection, either from all slices or using a running projection from adjacent slices.

      If offset is ZProjectedImageServer.NO_RUNNING_OFFSET, this is equivalent to zProject(ZProjectedImageServer.Projection) and the resulting image will have a single z-slice.

      Otherwise, if offset is greater than 0, the resulting image will have the same number of z-slices as the input, where each slice is a projection using up to offset slices both above and below the current slice. This means that offset x 2 + 1 slices will be used for each projection, except at the edges where fewer slices will be used.

      Parameters:
      projection - a type of projection to convert the multiple z-stacks into one
      offset - the number of slices to use for the running projection (ignored for non-running projections).
      Returns:
      this builder
    • zProjectMax

      public TransformedServerBuilder zProjectMax(int offset)
      Apply a running maximum Z-projection to the image.
      Parameters:
      offset - the number of slices to use for the running projection, or ZProjectedImageServer.NO_RUNNING_OFFSET to create a single projection from all slices.
      Returns:
      this builder
      See Also:
    • zProjectMin

      public TransformedServerBuilder zProjectMin(int offset)
      Apply a running minimum Z-projection to the image.
      Parameters:
      offset - the number of slices to use for the running projection, or ZProjectedImageServer.NO_RUNNING_OFFSET to create a single projection from all slices.
      Returns:
      this builder
      See Also:
    • zProjectMean

      public TransformedServerBuilder zProjectMean(int offset)
      Apply a running mean Z-projection to the image.
      Parameters:
      offset - the number of slices to use for the running projection, or ZProjectedImageServer.NO_RUNNING_OFFSET to create a single projection from all slices.
      Returns:
      this builder
      See Also:
    • zProjectMedian

      public TransformedServerBuilder zProjectMedian(int offset)
      Apply a running median Z-projection to the image.
      Parameters:
      offset - the number of slices to use for the running projection, or ZProjectedImageServer.NO_RUNNING_OFFSET to create a single projection from all slices.
      Returns:
      this builder
      See Also:
    • zProjectStandardDeviation

      public TransformedServerBuilder zProjectStandardDeviation(int offset)
      Apply a running standard deviation Z-projection to the image.
      Parameters:
      offset - the number of slices to use for the running projection, or ZProjectedImageServer.NO_RUNNING_OFFSET to create a single projection from all slices.
      Returns:
      this builder
      See Also:
    • concatToZStack

      public TransformedServerBuilder concatToZStack(List<ImageServer<BufferedImage>> servers)
      Concatenate a list of additional servers along the 'z' dimension (iteration order is used).
      Parameters:
      servers - the servers to concatenate
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the provided images are not similar (see ZConcatenatedImageServer(List, Number)), or if one of them have more than one z-stack
    • concatToZStack

      public TransformedServerBuilder concatToZStack(List<ImageServer<BufferedImage>> servers, Number zSpacingMicrons)
      Concatenate a list of additional servers along the 'z' dimension (iteration order is used).
      Parameters:
      servers - the servers to concatenate
      zSpacingMicrons - the spacing in microns there should be between the combined images. Can be null to use the default value
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the provided images are not similar (see ZConcatenatedImageServer(List, Number)), or if one of them have more than one z-stack
    • transform

      public TransformedServerBuilder transform(AffineTransform transform)
      Apply an AffineTransform to the server. Note that the transform must be invertible, otherwise and IllegalArgumentException will be thrown.
      Parameters:
      transform - the transform to apply to the image
      Returns:
      this builder
    • deconvolveStains

      public TransformedServerBuilder deconvolveStains(ColorDeconvolutionStains stains, int... stainNumbers)
      Apply color deconvolution to the brightfield image, so that deconvolved stains behave as separate channels.
      Parameters:
      stains - the stains to apply for color deconvolution
      stainNumbers - the indices of the stains that should be use (an array compressing values that are 1, 2 or 3); if not specified, all 3 stains will be used.
      Returns:
      this builder
    • reorderRGB

      public TransformedServerBuilder reorderRGB(String order)
      Rearrange the channel order of an RGB image. This is intended for cases where an image has wrongly been interpreted as RGB or BGR.
      Parameters:
      order - a text containing the letters R, G, and B in any order
      Returns:
      this builder
    • rotate

      Rotate the image, using an increment of 90 degrees.
      Parameters:
      rotation - the rotation to apply
      Returns:
      this image
    • extractChannels

      public TransformedServerBuilder extractChannels(int... channels)
      Extract specified channels for an image.
      Parameters:
      channels - indices (0-based) of channels to extract.
      Returns:
      this builder
    • extractChannels

      public TransformedServerBuilder extractChannels(String... names)
      Extract specified channels for an image.
      Parameters:
      names - names of channels to extract.
      Returns:
      this builder
    • maxChannelProject

      public TransformedServerBuilder maxChannelProject()
      Perform a maximum projection of the channels.
      Returns:
      this builder
    • averageChannelProject

      public TransformedServerBuilder averageChannelProject()
      Perform an average (mean) projection of the channels.
      Returns:
      this builder
    • minChannelProject

      public TransformedServerBuilder minChannelProject()
      Perform a minimum projection of the channels.
      Returns:
      this builder
    • concatChannels

      public TransformedServerBuilder concatChannels(Collection<ImageServer<BufferedImage>> additionalChannels)
      Concatenate a collection of additional servers along the 'channels' dimension (iteration order is used).
      Parameters:
      additionalChannels - additional servers that will be applied as channels; note that these should be of an appropriate type and dimension for concatenation.
      Returns:
      this builder
    • concatChannels

      public TransformedServerBuilder concatChannels(ImageServer<BufferedImage>... additionalChannels)
      Concatenate additional servers along the 'channels' dimension.
      Parameters:
      additionalChannels - additional servers from which channels will be added; note that the servers should be of an appropriate type and dimension for concatenation.
      Returns:
      this builder
    • subtractOffset

      public TransformedServerBuilder subtractOffset(double... offsets)
      Subtract a constant offset from all channels, without clipping.
      Parameters:
      offsets - a single offset to subtract from all channels, or an array of offsets to subtract from each channel.
      Returns:
      Since:
      v0.6.0
    • subtractOffsetAndClipZero

      public TransformedServerBuilder subtractOffsetAndClipZero(double... offsets)
      Subtract a constant offset from all channels, clipping the result to be ≥ 0.
      Parameters:
      offsets - a single offset to subtract from all channels, or an array of offsets to subtract from each channel.
      Returns:
      Since:
      v0.6.0
    • subtractOffsetAndScale

      public TransformedServerBuilder subtractOffsetAndScale(double[] offsets, double[] scales)
      Subtract a constant offset from all channels, then multiply the result by a scale factor.
      Parameters:
      offsets - a single offset to subtract from all channels, or an array of offsets to subtract from each channel.
      scales - a single scale factor to apply to all channels, or an array of scale factors to apply to each channel.
      Returns:
      Since:
      v0.6.0
    • scaleChannels

      public TransformedServerBuilder scaleChannels(double... scales)
      Scale all channels by a constant factor.
      Parameters:
      scales - a single scale factor to apply to all channels, or an array of scale factors to apply to each channel.
      Returns:
      Since:
      v0.6.0
    • stainNormalize

      public TransformedServerBuilder stainNormalize(ColorDeconvolutionStains stainsInput, ColorDeconvolutionStains stainsOutput, double... scales)
      Normalize stains using color deconvolution and reconvolution.
      Parameters:
      stainsInput - stain vectors to apply to deconvolve the input image, which should relate to the original colors
      stainsOutput - stain vectors to apply for reconvolution, determining the output colors
      scales - optional array of scale factors to apply to each deconvolved channel. A scale factor of 1.0 will leave the channel unchanged, while a scale of 0.0 will suppress the channel.
      Returns:
      Since:
      v0.6.0
    • normalize

      public TransformedServerBuilder normalize(BufferedImageNormalizer normalizer)
      Normalize the image using the provided normalizer.
      Parameters:
      normalizer -
      Returns:
      Since:
      v0.6.0
      Implementation Note
      To use this method to create an image that can be added to a project, the normalizers must be JSON-serializable and registered under ImageServers.getNormalizerFactory().
    • applyColorTransforms

      public TransformedServerBuilder applyColorTransforms(Collection<? extends ColorTransforms.ColorTransform> transforms)
      Apply color transforms to the image.
      Parameters:
      transforms - the transforms to apply
      Returns:
      this builder
      Since:
      v0.6.0
    • applyColorTransforms

      public TransformedServerBuilder applyColorTransforms(ColorTransforms.ColorTransform... transforms)
      Apply color transforms to the image.
      Parameters:
      transforms - the transforms to apply
      Returns:
      this builder
      Since:
      v0.6.0
    • convertType

      public TransformedServerBuilder convertType(PixelType pixelType)
      Convert to the specified pixel type.
      Parameters:
      pixelType - the target pixel type
      Returns:
      this builder
      Since:
      v0.6.0
    • build

      public ImageServer<BufferedImage> build()
      Get the ImageServer that applies the requested transforms sequentially.