Package ai.djl.modality.cv.util
Class NDImageUtils
- java.lang.Object
-
- ai.djl.modality.cv.util.NDImageUtils
-
public final class NDImageUtils extends java.lang.Object
NDImageUtils
is an image processing utility to load, reshape, and convert images usingNDArray
images.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static NDArray
centerCrop(NDArray image)
Crops an image to a square of sizemin(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
randomFlipLeftRight(NDArray image)
Randomly flip the input image left to right with a probability of 0.5.static NDArray
randomFlipTopBottom(NDArray image)
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
toTensor(NDArray image)
Converts an image NDArray from preprocessing format to Neural Network format.
-
-
-
Method Detail
-
resize
public static NDArray resize(NDArray image, int size)
Resizes an image to the given width and height.- Parameters:
image
- the image to resizesize
- the desired size- Returns:
- the resized NDList
-
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 resizewidth
- the desired widthheight
- the desired height- Returns:
- the resized NDList
-
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 resizewidth
- the desired widthheight
- the desired heightinterpolation
- the desired interpolation- Returns:
- the resized NDList
-
rotate90
public static NDArray rotate90(NDArray image, int times)
Rotate an image NDArray counter-clockwise 90 degree.- Parameters:
image
- the image to rotatetimes
- 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 normalizemean
- 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(NDArray, float[], float[])
-
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
forn
channels, this transform normalizes each channel of the input tensor with:output[i] = (input[i] - m1) / (s1)
.- Parameters:
input
- the image to normalizemean
- the mean to normalize with for each channelstd
- 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 aDataType.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 sizemin(width, height)
.- Parameters:
image
- the image to crop- Returns:
- the cropped image
- See Also:
centerCrop(NDArray, int, int)
-
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 cropwidth
- the desired width of the cropped imageheight
- 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 cropx
- the x coordinate of the top-left corner of the cropy
- the y coordinate of the top-left corner of the cropwidth
- the width of the cropped imageheight
- 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 formatwidth
- the output width of the imageheight
- the output height of the imageminAreaScale
- minimum targetArea/srcArea valuemaxAreaScale
- maximum targetArea/srcArea valueminAspectRatio
- minimum aspect ratiomaxAspectRatio
- 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 formatbrightness
- 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 formathue
- 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 formatbrightness
- the brightness factor from 0 to 1contrast
- the contrast factor from 0 to 1saturation
- the saturation factor from 0 to 1hue
- 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
-
-