Class NDImageUtils

java.lang.Object
ai.djl.modality.cv.util.NDImageUtils

public final class NDImageUtils extends Object
NDImageUtils is an image processing utility to load, reshape, and convert images using NDArray images.
  • Method Summary

    Modifier and Type
    Method
    Description
    static NDArray
    Crops an image to a square of size min(width, height).
    static NDArray
    centerCrop(NDArray image, int width, int height)
    Crops an image to a given width and height from the center of the image.
    static NDArray
    crop(NDArray image, int x, int y, int width, int height)
    Crops an image with a given location and size.
    static boolean
    isCHW(Shape shape)
    Check if the shape of the image follows CHW/NCHW.
    static NDArray
    normalize(NDArray input, float[] mean, float[] std)
    Normalizes an image NDArray of shape CHW or NCHW with mean and standard deviation.
    static NDArray
    normalize(NDArray input, float mean, float std)
    Normalizes an image NDArray of shape CHW or NCHW with a single mean and standard deviation to apply to all channels.
    static NDArray
    randomBrightness(NDArray image, float brightness)
    Randomly jitters image brightness with a factor chosen from [max(0, 1 - brightness), 1 + brightness].
    static NDArray
    randomColorJitter(NDArray image, float brightness, float contrast, float saturation, float hue)
    Randomly jitters the brightness, contrast, saturation, and hue of an image.
    static NDArray
    Randomly flip the input image left to right with a probability of 0.5.
    static NDArray
    Randomly flip the input image top to bottom with a probability of 0.5.
    static NDArray
    randomHue(NDArray image, float hue)
    Randomly jitters image hue with a factor chosen from [max(0, 1 - hue), 1 + hue].
    static NDArray
    randomResizedCrop(NDArray image, int width, int height, double minAreaScale, double maxAreaScale, double minAspectRatio, double maxAspectRatio)
    Crop the input image with random scale and aspect ratio.
    static NDArray
    resize(NDArray image, int size)
    Resizes an image to the given width and height.
    static NDArray
    resize(NDArray image, int width, int height)
    Resizes an image to the given width and height.
    static NDArray
    resize(NDArray image, int width, int height, Image.Interpolation interpolation)
    Resizes an image to the given width and height with given interpolation.
    static NDArray
    rotate90(NDArray image, int times)
    Rotate an image NDArray counter-clockwise 90 degree.
    static NDArray
    Converts an image NDArray from preprocessing format to Neural Network format.

    Methods inherited from class java.lang.Object

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

    • resize

      public static NDArray resize(NDArray image, int size)
      Resizes an image to the given width and height.
      Parameters:
      image - the image to resize
      size - the desired size
      Returns:
      the resized NDArray
    • resize

      public static NDArray resize(NDArray image, int width, int height)
      Resizes an image to the given width and height.
      Parameters:
      image - the image to resize
      width - the desired width
      height - the desired height
      Returns:
      the resized NDArray
    • resize

      public static NDArray resize(NDArray image, int width, int height, Image.Interpolation interpolation)
      Resizes an image to the given width and height with given interpolation.
      Parameters:
      image - the image to resize
      width - the desired width
      height - the desired height
      interpolation - the desired interpolation
      Returns:
      the resized NDArray
    • rotate90

      public static NDArray rotate90(NDArray image, int times)
      Rotate an image NDArray counter-clockwise 90 degree.
      Parameters:
      image - the image to rotate
      times - the image to rotate
      Returns:
      the rotated Image
    • normalize

      public static NDArray normalize(NDArray input, float mean, float std)
      Normalizes an image NDArray of shape CHW or NCHW with a single mean and standard deviation to apply to all channels. TensorFlow enforce HWC instead.
      Parameters:
      input - the image to normalize
      mean - the mean to normalize with (for all channels)
      std - the standard deviation to normalize with (for all channels)
      Returns:
      the normalized NDArray
      See Also:
    • normalize

      public static NDArray normalize(NDArray input, float[] mean, float[] std)
      Normalizes an image NDArray of shape CHW or NCHW with mean and standard deviation. TensorFlow enforce HWC instead.

      Given mean (m1, ..., mn) and standard deviation (s1, ..., sn for n channels, this transform normalizes each channel of the input tensor with: output[i] = (input[i] - m1) / (s1).

      Parameters:
      input - the image to normalize
      mean - the mean to normalize with for each channel
      std - the standard deviation to normalize with for each channel
      Returns:
      the normalized NDArray
    • toTensor

      public static NDArray toTensor(NDArray image)
      Converts an image NDArray from preprocessing format to Neural Network format.

      Converts an image NDArray of shape HWC in the range [0, 255] to a DataType.FLOAT32 tensor NDArray of shape CHW in the range [0, 1].

      Parameters:
      image - the image to convert
      Returns:
      the converted image
    • centerCrop

      public static NDArray centerCrop(NDArray image)
      Crops an image to a square of size min(width, height).
      Parameters:
      image - the image to crop
      Returns:
      the cropped image
      See Also:
    • centerCrop

      public static NDArray centerCrop(NDArray image, int width, int height)
      Crops an image to a given width and height from the center of the image.
      Parameters:
      image - the image to crop
      width - the desired width of the cropped image
      height - the desired height of the cropped image
      Returns:
      the cropped image
    • crop

      public static NDArray crop(NDArray image, int x, int y, int width, int height)
      Crops an image with a given location and size.
      Parameters:
      image - the image to crop
      x - the x coordinate of the top-left corner of the crop
      y - the y coordinate of the top-left corner of the crop
      width - the width of the cropped image
      height - the height of the cropped image
      Returns:
      the cropped image
    • randomFlipLeftRight

      public static NDArray randomFlipLeftRight(NDArray image)
      Randomly flip the input image left to right with a probability of 0.5.
      Parameters:
      image - the image with HWC format
      Returns:
      the flipped image
    • randomFlipTopBottom

      public static NDArray randomFlipTopBottom(NDArray image)
      Randomly flip the input image top to bottom with a probability of 0.5.
      Parameters:
      image - the image with HWC format
      Returns:
      the flipped image
    • randomResizedCrop

      public static NDArray randomResizedCrop(NDArray image, int width, int height, double minAreaScale, double maxAreaScale, double minAspectRatio, double maxAspectRatio)
      Crop the input image with random scale and aspect ratio.
      Parameters:
      image - the image with HWC format
      width - the output width of the image
      height - the output height of the image
      minAreaScale - minimum targetArea/srcArea value
      maxAreaScale - maximum targetArea/srcArea value
      minAspectRatio - minimum aspect ratio
      maxAspectRatio - maximum aspect ratio
      Returns:
      the cropped image
    • randomBrightness

      public static NDArray randomBrightness(NDArray image, float brightness)
      Randomly jitters image brightness with a factor chosen from [max(0, 1 - brightness), 1 + brightness].
      Parameters:
      image - the image with HWC format
      brightness - the brightness factor from 0 to 1
      Returns:
      the transformed image
    • randomHue

      public static NDArray randomHue(NDArray image, float hue)
      Randomly jitters image hue with a factor chosen from [max(0, 1 - hue), 1 + hue].
      Parameters:
      image - the image with HWC format
      hue - the hue factor from 0 to 1
      Returns:
      the transformed image
    • randomColorJitter

      public static NDArray randomColorJitter(NDArray image, float brightness, float contrast, float saturation, float hue)
      Randomly jitters the brightness, contrast, saturation, and hue of an image.
      Parameters:
      image - the image with HWC format
      brightness - the brightness factor from 0 to 1
      contrast - the contrast factor from 0 to 1
      saturation - the saturation factor from 0 to 1
      hue - the hue factor from 0 to 1
      Returns:
      the transformed image
    • isCHW

      public static boolean isCHW(Shape shape)
      Check if the shape of the image follows CHW/NCHW.
      Parameters:
      shape - the shape of the image
      Returns:
      true for (N)CHW, false for (N)HWC