Package ai.djl.modality.cv.util
Class NDImageUtils
java.lang.Object
ai.djl.modality.cv.util.NDImageUtils
NDImageUtils
is an image processing utility to load, reshape, and convert images using
NDArray
images.-
Method Summary
Modifier and TypeMethodDescriptionstatic 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
Crops an image with a given location and size.static boolean
Check if the shape of the image follows CHW/NCHW.static NDArray
Normalizes an image NDArray of shape CHW or NCHW with mean and standard deviation.static NDArray
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
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
Resizes an image to the given width and height.static NDArray
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
Rotate an image NDArray counter-clockwise 90 degree.static NDArray
Converts an image NDArray from preprocessing format to Neural Network format.
-
Method Details
-
resize
Resizes an image to the given width and height.- Parameters:
image
- the image to resizesize
- the desired size- Returns:
- the resized NDArray
-
resize
Resizes an image to the given width and height.- Parameters:
image
- the image to resizewidth
- the desired widthheight
- 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 resizewidth
- the desired widthheight
- the desired heightinterpolation
- the desired interpolation- Returns:
- the resized NDArray
-
rotate90
Rotate an image NDArray counter-clockwise 90 degree.- Parameters:
image
- the image to rotatetimes
- the image to rotate- Returns:
- the rotated Image
-
normalize
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
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
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
Crops an image to a square of sizemin(width, height)
.- Parameters:
image
- the image to crop- Returns:
- the cropped image
- See Also:
-
centerCrop
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
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
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
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
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
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
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
-