Class ColorDeconvolutionStains

java.lang.Object
qupath.lib.color.ColorDeconvolutionStains
All Implemented Interfaces:
Externalizable, Serializable

public class ColorDeconvolutionStains extends Object implements Externalizable
Helper class for storing stain vectors and maximum channel values to be used for color deconvolution.

The maximum channel values are the RGB values corresponding to pixels that have no staining of any kind (i.e. all light has passed through... the pixel should look close to white). By default, these are 255.

See Also:
  • Field Details

  • Constructor Details

    • ColorDeconvolutionStains

      public ColorDeconvolutionStains(String name, StainVector stain1, StainVector stain2, StainVector stain3, double maxRed, double maxGreen, double maxBlue)
      Constructor for ColorDeconvolutionStains with three stain vectors.
      Parameters:
      name -
      stain1 -
      stain2 -
      stain3 -
      maxRed - background (white) value for red channel
      maxGreen - background (white) value for green channel
      maxBlue - background (white) value for blue channel
    • ColorDeconvolutionStains

      public ColorDeconvolutionStains(String name, StainVector stain1, StainVector stain2, double maxRed, double maxGreen, double maxBlue)
      Constructor for ColorDeconvolutionStains with two stain vectors.
      Parameters:
      name -
      stain1 -
      stain2 -
      maxRed - background (white) value for red channel
      maxGreen - background (white) value for green channel
      maxBlue - background (white) value for blue channel
    • ColorDeconvolutionStains

      public ColorDeconvolutionStains()
      Default constructor, for Externalizable. Should not be used directly.
  • Method Details

    • makeDefaultColorDeconvolutionStains

      public static ColorDeconvolutionStains makeDefaultColorDeconvolutionStains(ColorDeconvolutionStains.DefaultColorDeconvolutionStains stains)
      Create a ColorDeconvolutionStains for a default stain combination, and default max values (255 for all channels).
      Parameters:
      stains -
      Returns:
    • isHematoxylin

      public static boolean isHematoxylin(StainVector stain)
      Check if the name of a StainVector is "haematoxylin", "hematoxylin", "haem" or "h" (ignoring case)
      Parameters:
      stain -
      Returns:
    • isEosin

      public static boolean isEosin(StainVector stain)
      Check if the name of a StainVector is "eosin", "eos" or "e" (ignoring case)
      Parameters:
      stain -
      Returns:
    • isDAB

      public static boolean isDAB(StainVector stain)
      Check if the name of a StainVector is "dab" or "d" (ignoring case)
      Parameters:
      stain -
      Returns:
    • isH_E

      public boolean isH_E()
      Check if we have H&E staining, by checking the names of the first two stains and confirming that the third stain is a residual. Note the order of the stains must be 1-Hematoxylin, 2-Eosin, 3-residual (missing)
      Returns:
    • isH_DAB

      public boolean isH_DAB()
      Check if we have H-DAB staining, by checking the names of the first two stains and confirming that the third stain is a residual. Note the order of the stains must be 1-Hematoxylin, 2-DAB, 3-residual (missing)
      Returns:
    • changeStain

      public ColorDeconvolutionStains changeStain(StainVector stainNew, int stainNumber)
      Create a new stains object, identical to this one but for one StainVector having been changed.
      Parameters:
      stainNew -
      stainNumber -
      Returns:
    • changeName

      public ColorDeconvolutionStains changeName(String name)
      Create a new stains object with the same StainVectors but a new name.
      Parameters:
      name -
      Returns:
    • changeMaxValues

      public ColorDeconvolutionStains changeMaxValues(double maxRed, double maxGreen, double maxBlue)
      Create a new stains object with the same StainVectors but new max (background) values.
      Parameters:
      maxRed -
      maxGreen -
      maxBlue -
      Returns:
    • getStain

      public StainVector getStain(int n)
      Get a specified color deconvolution stain vector, where n should be 1, 2 or 3 (because color deconvolution on a 3-channel image can recover [to some extent] up to 3 stains).

      If only 2 stains are set (i.e. stain1 and stain2), then a third stain is computed orthogonal to the first two, with the name 'residual'.

      Parameters:
      n -
      Returns:
      The requested stain vector, or null if n is out of range or the stain was not set.
    • getStains

      public List<StainVector> getStains()
      Get an unmodifiable list containing the 3 stains. This allows 0-based indexing (while getStain(int) uses 1-based indexing).
      Returns:
      a 3-element list of stains
    • getStains

      public Collection<StainVector> getStains(boolean includeResidual)
      Get a collection of all the stains.
      Parameters:
      includeResidual - if true, include residual stains in the list. If false, only include non-residual stains.
      Returns:
      a collection of stains.
    • getName

      public String getName()
      Get the stains name.
      Returns:
    • getStainNumber

      public int getStainNumber(StainVector stain)
      Get the number for a specific stain (1, 2 or 3).
      Parameters:
      stain -
      Returns:
    • getMaxRed

      public double getMaxRed()
      Get the maximum value for the red channel (default = 255).
      Returns:
    • getMaxGreen

      public double getMaxGreen()
      Get the maximum value for the green channel (default = 255).
      Returns:
    • getMaxBlue

      public double getMaxBlue()
      Get the maximum value for the blue channel (default = 255).
      Returns:
    • getMatrixInverse

      public double[][] getMatrixInverse()
      Get matrix inverse, as useful for color deconvolution. See static ColorDeconvolution classes for usage.
      Returns:
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • getColorDeconvolutionStainsAsString

      public static String getColorDeconvolutionStainsAsString(ColorDeconvolutionStains stains, int nDecimalPlaces)
      Get a String representation of the stains and background.
      Parameters:
      stains -
      nDecimalPlaces -
      Returns:
      See Also:
    • parseColorDeconvolutionStainsArg

      public static ColorDeconvolutionStains parseColorDeconvolutionStainsArg(String s)
      Parse color deconvolution stains from a JSON String, with keys 'Stain 1', 'Stain 2', (optionally) 'Stain 3' and 'Background'.
      Parameters:
      s -
      Returns:
      See Also:
    • parseStainValues

      public static double[] parseStainValues(Locale locale, String s)
      Parses 3 values, suitable for use as a stain vector
      Parameters:
      locale -
      s -
      Returns:
    • parseColorDeconvolutionStains

      public static ColorDeconvolutionStains parseColorDeconvolutionStains(String name, Map<String,List<Number>> stains)
      Create color deconvolution stains from the provided name and map.

      This is the inverse operation of getColorDeconvolutionStainsAsMap().

      Parameters:
      name - the name of the color deconvolution stains
      stains - a map of stain name to stain values. Each stain value must be a list containing at least three elements (otherwise the value is skipped). A stain must be provided with the name defined by BACKGROUND_KEY. A stain with the name defined by RESIDUAL_KEY will be set as residual, others won't. The order of the map matters: the first entry will be the first stain (unless it's the background), and so on.
      Returns:
      color deconvolution stains representing the provided map
      Throws:
      IllegalArgumentException - if the provided stains do not contain a stain with the name defined by BACKGROUND_KEY and with at least three values, or if the provided stains do not contain at least two non-BACKGROUND_KEY stains with at least three values
      NullPointerException - if one of the parameter is null
    • getColorDeconvolutionStainsAsMap

      public Map<String,List<Number>> getColorDeconvolutionStainsAsMap()
      Create a map containing information on the stain vectors and maximum RGB values of this object.

      This is the inverse operation of parseColorDeconvolutionStains(String,Map) (without the name information).

      Returns:
      a map of stain name to stain values. Each stain value is a list of three elements. A stain with the name defined by BACKGROUND_KEY represents the maximum RGB values. The returned map is ordered: the first entry represents the first stain, and so on (the BACKGROUND_KEY stain will be the last element)
    • writeExternal

      public void writeExternal(ObjectOutput out) throws IOException
      Specified by:
      writeExternal in interface Externalizable
      Throws:
      IOException
    • readExternal

      public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
      Specified by:
      readExternal in interface Externalizable
      Throws:
      IOException
      ClassNotFoundException