Class BinaryImages

java.lang.Object
inra.ijpb.binary.BinaryImages

public class BinaryImages
extends java.lang.Object
A collection of static methods for operating on binary images (2D/3D). Some of the methods need the LabelImages class.
Author:
David Legland
See Also:
LabelImages
  • Method Summary

    Modifier and Type Method Description
    static ij.process.ImageProcessor areaOpening​(ij.process.ImageProcessor image, int nPixelMin)
    Applies area opening on a binary image: creates a new binary image that contains only particles with at least the specified number of pixels.
    static ij.ImagePlus binarize​(ij.ImagePlus imagePlus)
    Converts a grayscale 2D or 3D image into a binary image by setting non-zero elements to 255.
    static ij.ImageStack binarize​(ij.ImageStack image)
    Converts a grayscale 3D image into a binary 3D image by setting non-zero voxels to 255.
    static ij.process.ImageProcessor binarize​(ij.process.ImageProcessor image)
    Converts a grayscale 2D image into a binary 2D image by setting non-zero pixels to 255.
    static ij.ImagePlus binarizeBackground​(ij.ImagePlus imagePlus)
    Converts a grayscale 2D or 3D image into a binary image by setting zero elements to 255, and non zero ones to 0.
    static ij.ImageStack binarizeBackground​(ij.ImageStack image)
    Converts a grayscale 3D image into a binary 3D image by setting zero elements to 255, and non zero ones to 0.
    static ij.process.ImageProcessor binarizeBackground​(ij.process.ImageProcessor image)
    Converts a grayscale 2D image into a binary 2D image by setting zero elements to 255, and non zero ones to 0.
    static ij.ImagePlus componentsLabeling​(ij.ImagePlus imagePlus, int conn, int bitDepth)
    Computes the labels in the binary 2D or 3D image contained in the given ImagePlus, and computes the maximum label to set up the display range of the resulting ImagePlus.
    static ij.ImageStack componentsLabeling​(ij.ImageStack image, int conn, int bitDepth)
    Computes the labels of the connected components in the given 3D binary image.
    static ij.process.ImageProcessor componentsLabeling​(ij.process.ImageProcessor image, int conn, int bitDepth)
    Computes the labels of the connected components in the given planar binary image.
    static int countForegroundPixels​(ij.process.ImageProcessor image)
    Counts the number of foreground pixels.
    static int countForegroundVoxels​(ij.ImageStack image)
    Counts the number of foreground voxels.
    static ij.ImagePlus distanceMap​(ij.ImagePlus imagePlus)
    Computes the distance map (or distance transform) from a binary image processor.
    static ij.ImageStack distanceMap​(ij.ImageStack image)
    Computes the distance map from a binary 3D image.
    static ij.ImageStack distanceMap​(ij.ImageStack image, float[] weights, boolean normalize)
    Computes the distance map from a binary 3D image.
    static ij.ImageStack distanceMap​(ij.ImageStack image, short[] weights, boolean normalize)
    Computes the distance map from a binary 3D image.
    static ij.ImageStack distanceMap​(ij.ImageStack image, ChamferMask3D chamferMask, boolean floatingPoint, boolean normalize)
    Computes the distance map from a binary 3D image.
    static ij.process.ImageProcessor distanceMap​(ij.process.ImageProcessor image)
    Computes the distance map (or distance transform) from a binary image processor.
    static ij.process.FloatProcessor distanceMap​(ij.process.ImageProcessor image, float[] weights, boolean normalize)
    Computes the distance map from a binary image processor, by specifying weights and normalization.
    static ij.process.ShortProcessor distanceMap​(ij.process.ImageProcessor image, short[] weights, boolean normalize)
    Computes the distance map from a binary image processor, by specifying weights and normalization.
    static ij.process.ImageProcessor distanceMap​(ij.process.ImageProcessor image, ChamferMask2D mask, boolean floatingPoint, boolean normalize)
    Computes the distance map from a binary image processor, by specifying weights and normalization.
    static ij.ImagePlus geodesicDistanceMap​(ij.ImagePlus markerPlus, ij.ImagePlus maskPlus)
    Computes the geodesic distance transform (or geodesic distance map) of a binary image of marker, constrained to a binary mask.
    static ij.process.ImageProcessor geodesicDistanceMap​(ij.process.ImageProcessor marker, ij.process.ImageProcessor mask)
    Computes the geodesic distance transform (or geodesic distance map) of a binary image of marker, constrained to a binary mask.
    static ij.process.ImageProcessor geodesicDistanceMap​(ij.process.ImageProcessor marker, ij.process.ImageProcessor mask, float[] weights, boolean normalize)
    Computes the geodesic distance transform (or geodesic distance map) of a binary image of marker, constrained to a binary mask.
    static ij.process.ImageProcessor geodesicDistanceMap​(ij.process.ImageProcessor marker, ij.process.ImageProcessor mask, short[] weights, boolean normalize)
    Computes the geodesic distance transform (or geodesic distance map) of a binary image of marker, constrained to a binary mask.
    static boolean isBinaryImage​(ij.ImagePlus image)
    Check if input image is binary (8-bit with only 0 or 255 values)
    static boolean isBinaryImage​(ij.process.ImageProcessor image)
    Check if input image is binary (8-bit with only 0 or 255 values)
    static ij.ImagePlus keepLargestRegion​(ij.ImagePlus imagePlus)
    Returns a binary image that contains only the largest region.
    static ij.ImageStack keepLargestRegion​(ij.ImageStack image)
    Returns a binary image that contains only the largest label.
    static ij.process.ImageProcessor keepLargestRegion​(ij.process.ImageProcessor image)
    Returns a binary image that contains only the largest region.
    static void removeLargestRegion​(ij.ImagePlus imagePlus)
    Returns a binary image in which the largest region has been replaced by background.
    static ij.ImageStack removeLargestRegion​(ij.ImageStack image)
    Returns a binary image in which the largest region has been replaced by the background value.
    static ij.process.ImageProcessor removeLargestRegion​(ij.process.ImageProcessor image)
    Returns a binary image in which the largest region has been replaced by the background value.
    static ij.ImagePlus sizeOpening​(ij.ImagePlus imagePlus, int minElementCount)
    Applies size opening on a binary 2D or 3D image.
    static ij.process.ImageProcessor skeleton​(ij.process.ImageProcessor image)
    Computes the skeleton of a binary image, and returns another binary image.
    static ij.ImageStack volumeOpening​(ij.ImageStack image, int nVoxelMin)
    Applies area opening on a binary image: creates a new binary image that contains only particle with at least the specified number of voxels.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • isBinaryImage

      public static final boolean isBinaryImage​(ij.ImagePlus image)
      Check if input image is binary (8-bit with only 0 or 255 values)
      Parameters:
      image - input image
      Returns:
      true if image is binary
    • isBinaryImage

      public static final boolean isBinaryImage​(ij.process.ImageProcessor image)
      Check if input image is binary (8-bit with only 0 or 255 values)
      Parameters:
      image - input image
      Returns:
      true if image is binary
    • countForegroundPixels

      public static final int countForegroundPixels​(ij.process.ImageProcessor image)
      Counts the number of foreground pixels. The foreground pixels are the pixels with a value greater than zero.
      Parameters:
      image - a binary image with zero values for background
      Returns:
      the number of pixels with value greater than zero
    • countForegroundVoxels

      public static final int countForegroundVoxels​(ij.ImageStack image)
      Counts the number of foreground voxels. The foreground voxels are the voxels with a value greater than zero.
      Parameters:
      image - a binary 3D image with zero values for background
      Returns:
      the number of voxels with value greater than zero
    • componentsLabeling

      public static final ij.ImagePlus componentsLabeling​(ij.ImagePlus imagePlus, int conn, int bitDepth)
      Computes the labels in the binary 2D or 3D image contained in the given ImagePlus, and computes the maximum label to set up the display range of the resulting ImagePlus.
      Parameters:
      imagePlus - contains the 3D binary image stack
      conn - the connectivity, either 4 or 8 for planar images, or 6 or 26 for 3D images
      bitDepth - the number of bits used to create the result image (8, 16 or 32)
      Returns:
      an ImagePlus containing the label of each connected component.
      Throws:
      java.lang.RuntimeException - if the number of labels reaches the maximum number that can be represented with this bitDepth
      See Also:
      ConnectedComponentsLabeling, ConnectedComponentsLabeling3D, FloodFill
    • componentsLabeling

      public static final ij.process.ImageProcessor componentsLabeling​(ij.process.ImageProcessor image, int conn, int bitDepth)
      Computes the labels of the connected components in the given planar binary image. The type of result is controlled by the bitDepth option. Uses a Flood-fill type algorithm.
      Parameters:
      image - contains the binary image (any type is accepted)
      conn - the connectivity, either 4 or 8
      bitDepth - the number of bits used to create the result image (8, 16 or 32)
      Returns:
      a new instance of ImageProcessor containing the label of each connected component.
      Throws:
      java.lang.RuntimeException - if the number of labels reaches the maximum number that can be represented with this bitDepth
      See Also:
      ConnectedComponentsLabeling
    • componentsLabeling

      public static final ij.ImageStack componentsLabeling​(ij.ImageStack image, int conn, int bitDepth)
      Computes the labels of the connected components in the given 3D binary image. The type of result is controlled by the bitDepth option. Uses a Flood-fill type algorithm.
      Parameters:
      image - contains the 3D binary image (any type is accepted)
      conn - the connectivity, either 6 or 26
      bitDepth - the number of bits used to create the result stack (8, 16 or 32)
      Returns:
      a new instance of ImageStack containing the label of each connected component.
      Throws:
      java.lang.RuntimeException - if the number of labels reaches the maximum number that can be represented with this bitDepth
      See Also:
      ConnectedComponentsLabeling3D
    • distanceMap

      public static final ij.ImagePlus distanceMap​(ij.ImagePlus imagePlus)
      Computes the distance map (or distance transform) from a binary image processor. Distance is computed for each foreground (white) pixel or voxel, as the chamfer distance to the nearest background (black) pixel or voxel.
      Parameters:
      imagePlus - an ImagePlus object containing a binary or label image
      Returns:
      a new ImagePlus containing the distance map
      See Also:
      DistanceTransform, DistanceTransform3D
    • distanceMap

      public static final ij.process.ImageProcessor distanceMap​(ij.process.ImageProcessor image)

      Computes the distance map (or distance transform) from a binary image processor. Distance is computed for each foreground (white) pixel, as the chamfer distance to the nearest background (black) pixel.

      This method uses default 5x5 weights, and normalizes the resulting map. Result is given in a new instance of ShortProcessor.

      Parameters:
      image - the input binary or label image
      Returns:
      a new ImageProcessor containing the distance map result
    • distanceMap

      public static final ij.process.ImageProcessor distanceMap​(ij.process.ImageProcessor image, ChamferMask2D mask, boolean floatingPoint, boolean normalize)

      Computes the distance map from a binary image processor, by specifying weights and normalization.

      Distance is computed for each foreground (white) pixel, as the chamfer distance to the nearest background (black) pixel. Result is given in a new instance of FloatProcessor.

      Parameters:
      image - the input binary or label image
      mask - the chamfer mask used to propagate distances
      floatingPoint - indicates if the computation should be performed using floating point computation
      normalize - indicates whether the resulting distance map should be normalized (divide distances by the first chamfer weight)
      Returns:
      the distance map obtained after applying the distance transform
    • distanceMap

      public static final ij.process.ShortProcessor distanceMap​(ij.process.ImageProcessor image, short[] weights, boolean normalize)

      Computes the distance map from a binary image processor, by specifying weights and normalization.

      Distance is computed for each foreground (white) pixel, as the chamfer distance to the nearest background (black) pixel. Result is given as a new instance of ShortProcessor.

      Parameters:
      image - the input binary or label image
      weights - an array of chamfer weights, with at least two values
      normalize - indicates whether the resulting distance map should be normalized (divide distances by the first chamfer weight)
      Returns:
      the distance map obtained after applying the distance transform
    • distanceMap

      public static final ij.process.FloatProcessor distanceMap​(ij.process.ImageProcessor image, float[] weights, boolean normalize)

      Computes the distance map from a binary image processor, by specifying weights and normalization.

      Distance is computed for each foreground (white) pixel, as the chamfer distance to the nearest background (black) pixel. Result is given in a new instance of FloatProcessor.

      Parameters:
      image - the input binary or label image
      weights - an array of chamfer weights, with at least two values
      normalize - indicates whether the resulting distance map should be normalized (divide distances by the first chamfer weight)
      Returns:
      the distance map obtained after applying the distance transform
    • distanceMap

      public static final ij.ImageStack distanceMap​(ij.ImageStack image)
      Computes the distance map from a binary 3D image. Distance is computed for each foreground (white) pixel, as the chamfer distance to the nearest background (black) pixel.
      Parameters:
      image - the input binary image
      Returns:
      the distance map obtained after applying the distance transform
    • distanceMap

      public static final ij.ImageStack distanceMap​(ij.ImageStack image, ChamferMask3D chamferMask, boolean floatingPoint, boolean normalize)
      Computes the distance map from a binary 3D image. Distance is computed for each foreground (white) pixel, as the chamfer distance to the nearest background (black) pixel.
      Parameters:
      image - the input binary image
      chamferMask - the chamfer mask used for propagating distances
      floatingPoint - indicates if the distane propagation should use floating point computation
      normalize - indicates whether the resulting distance map should be normalized (divide distances by the first chamfer weight)
      Returns:
      the distance map obtained after applying the distance transform
    • distanceMap

      public static final ij.ImageStack distanceMap​(ij.ImageStack image, short[] weights, boolean normalize)
      Computes the distance map from a binary 3D image. Distance is computed for each foreground (white) pixel, as the chamfer distance to the nearest background (black) pixel.
      Parameters:
      image - the input binary image
      weights - an array of chamfer weights, with at least three values
      normalize - indicates whether the resulting distance map should be normalized (divide distances by the first chamfer weight)
      Returns:
      the distance map obtained after applying the distance transform
    • distanceMap

      public static final ij.ImageStack distanceMap​(ij.ImageStack image, float[] weights, boolean normalize)
      Computes the distance map from a binary 3D image. Distance is computed for each foreground (white) pixel, as the chamfer distance to the nearest background (black) pixel.
      Parameters:
      image - the input 3D binary image
      weights - an array of chamfer weights, with at least three values
      normalize - indicates whether the resulting distance map should be normalized (divide distances by the first chamfer weight)
      Returns:
      the distance map obtained after applying the distance transform
    • geodesicDistanceMap

      public static final ij.ImagePlus geodesicDistanceMap​(ij.ImagePlus markerPlus, ij.ImagePlus maskPlus)
      Computes the geodesic distance transform (or geodesic distance map) of a binary image of marker, constrained to a binary mask. Returns the result in a new instance of ImagePlus.
      Parameters:
      markerPlus - the image containing the marker
      maskPlus - the image containing the marker
      Returns:
      the geodesic distance map in a new ImagePlus
    • geodesicDistanceMap

      public static final ij.process.ImageProcessor geodesicDistanceMap​(ij.process.ImageProcessor marker, ij.process.ImageProcessor mask)
      Computes the geodesic distance transform (or geodesic distance map) of a binary image of marker, constrained to a binary mask. Returns the result in a new instance of ShortProcessor.
      Parameters:
      marker - the binary image of marker
      mask - the binary image of mask
      Returns:
      the geodesic distance map in a new ImageProcessor
    • geodesicDistanceMap

      public static final ij.process.ImageProcessor geodesicDistanceMap​(ij.process.ImageProcessor marker, ij.process.ImageProcessor mask, short[] weights, boolean normalize)
      Computes the geodesic distance transform (or geodesic distance map) of a binary image of marker, constrained to a binary mask. Returns the result in a new instance of ShortProcessor.
      Parameters:
      marker - the binary image of marker
      mask - the binary image of mask
      weights - an array of chamfer weights, with at least two values
      normalize - indicates whether the resulting distance map should be normalized (divide distances by the first chamfer weight)
      Returns:
      the geodesic distance map in a new ImageProcessor
    • geodesicDistanceMap

      public static final ij.process.ImageProcessor geodesicDistanceMap​(ij.process.ImageProcessor marker, ij.process.ImageProcessor mask, float[] weights, boolean normalize)
      Computes the geodesic distance transform (or geodesic distance map) of a binary image of marker, constrained to a binary mask. Returns the result in a new instance of FloatProcessor.
      Parameters:
      marker - the binary image of marker
      mask - the binary image of mask
      weights - an array of chamfer weights, with at least two values
      normalize - indicates whether the resulting distance map should be normalized (divide distances by the first chamfer weight)
      Returns:
      the geodesic distance map in a new ImageProcessor
    • sizeOpening

      public static final ij.ImagePlus sizeOpening​(ij.ImagePlus imagePlus, int minElementCount)
      Applies size opening on a binary 2D or 3D image. The method creates a new binary image that contains only particles with at least the specified number of pixels.
      Parameters:
      imagePlus - the binary 2D or 3D image containing individual particles
      minElementCount - the minimum number of pixels or voxels required to keep a particle
      Returns:
      a new binary image containing only the selected particles
      See Also:
      LabelImages.sizeOpening(ImagePlus, int)
    • areaOpening

      public static final ij.process.ImageProcessor areaOpening​(ij.process.ImageProcessor image, int nPixelMin)
      Applies area opening on a binary image: creates a new binary image that contains only particles with at least the specified number of pixels.
      Parameters:
      image - the binary image containing individual particles
      nPixelMin - the minimum number of pixels required to keep a particle
      Returns:
      a new binary image containing only the selected particles
      See Also:
      LabelImages.areaOpening(ImageProcessor, int)
    • volumeOpening

      public static final ij.ImageStack volumeOpening​(ij.ImageStack image, int nVoxelMin)
      Applies area opening on a binary image: creates a new binary image that contains only particle with at least the specified number of voxels.
      Parameters:
      image - the 3D binary image containing individual particles
      nVoxelMin - the minimum number of voxels required to keep a particle
      Returns:
      a new binary image containing only the selected particles
      See Also:
      LabelImages.volumeOpening(ImageStack, int)
    • keepLargestRegion

      public static final ij.ImagePlus keepLargestRegion​(ij.ImagePlus imagePlus)
      Returns a binary image that contains only the largest region.
      Parameters:
      imagePlus - an instance of imagePlus that contains a binary image
      Returns:
      a new ImagePlus containing only the largest region from original image
    • keepLargestRegion

      public static final ij.process.ImageProcessor keepLargestRegion​(ij.process.ImageProcessor image)
      Returns a binary image that contains only the largest region.
      Parameters:
      image - a binary image containing several individual regions
      Returns:
      a new binary image containing only the largest region from original image
    • keepLargestRegion

      public static final ij.ImageStack keepLargestRegion​(ij.ImageStack image)
      Returns a binary image that contains only the largest label.
      Parameters:
      image - a binary 3D image containing several individual regions
      Returns:
      a new binary image containing only the largest region from original image
    • removeLargestRegion

      public static final void removeLargestRegion​(ij.ImagePlus imagePlus)
      Returns a binary image in which the largest region has been replaced by background. Works for both 2D and 3D images.
      Parameters:
      imagePlus - a binary image containing several individual particles
    • removeLargestRegion

      public static final ij.process.ImageProcessor removeLargestRegion​(ij.process.ImageProcessor image)
      Returns a binary image in which the largest region has been replaced by the background value.
      Parameters:
      image - a binary image containing several individual particles
      Returns:
      a new binary image containing all the regions from original image but the largest one
    • removeLargestRegion

      public static final ij.ImageStack removeLargestRegion​(ij.ImageStack image)
      Returns a binary image in which the largest region has been replaced by the background value.
      Parameters:
      image - a binary 3D image containing several individual particles
      Returns:
      a new binary image containing all the regions from original image but the largest one
    • binarize

      public static final ij.ImagePlus binarize​(ij.ImagePlus imagePlus)
      Converts a grayscale 2D or 3D image into a binary image by setting non-zero elements to 255.
      Parameters:
      imagePlus - an ImagePlus object containing a gray scale image
      Returns:
      a binary image containing 255 for all non-zero elements of original image
    • binarize

      public static final ij.process.ImageProcessor binarize​(ij.process.ImageProcessor image)
      Converts a grayscale 2D image into a binary 2D image by setting non-zero pixels to 255.
      Parameters:
      image - a gray scale image
      Returns:
      a binary image containing 255 for all non-zero elements of original image
    • binarize

      public static final ij.ImageStack binarize​(ij.ImageStack image)
      Converts a grayscale 3D image into a binary 3D image by setting non-zero voxels to 255.
      Parameters:
      image - a gray scale 3D image
      Returns:
      a binary image containing 255 for all non-zero elements of original image
    • binarizeBackground

      public static final ij.ImagePlus binarizeBackground​(ij.ImagePlus imagePlus)
      Converts a grayscale 2D or 3D image into a binary image by setting zero elements to 255, and non zero ones to 0.
      Parameters:
      imagePlus - an ImagePlus object containing a gray scale image
      Returns:
      a binary image containing 255 for all non-zero elements of original image
    • binarizeBackground

      public static final ij.process.ImageProcessor binarizeBackground​(ij.process.ImageProcessor image)
      Converts a grayscale 2D image into a binary 2D image by setting zero elements to 255, and non zero ones to 0.
      Parameters:
      image - a gray scale image
      Returns:
      a binary image containing 255 for all non-zero elements of original image
    • binarizeBackground

      public static final ij.ImageStack binarizeBackground​(ij.ImageStack image)
      Converts a grayscale 3D image into a binary 3D image by setting zero elements to 255, and non zero ones to 0.
      Parameters:
      image - a gray scale 3D image
      Returns:
      a binary image containing 255 for all non-zero elements of original image
    • skeleton

      public static final ij.process.ImageProcessor skeleton​(ij.process.ImageProcessor image)
      Computes the skeleton of a binary image, and returns another binary image. Uses an adaptation of the algorithm from ImageJ.
      Parameters:
      image - a binary image
      Returns:
      a binary image containing 255 for skeleton elements