Package com.itextpdf.pdfocr.onnx.util
Class BufferedImageUtil
java.lang.Object
com.itextpdf.pdfocr.onnx.util.BufferedImageUtil
Additional algorithms for working with
BufferedImage.
-
Method Summary
Modifier and TypeMethodDescriptionstatic Dimensions2DcalcOutputDimensions(BufferedImage image, ImageResizeOptions resizeOptions) Based on the provided ImageResizeOptions, calculates the dimensions of the output image, to where there original image should be scaled and placed with padding.static Dimensions2DcalcOutputDimensions(Collection<BufferedImage> images, ImageResizeOptions resizeOptions) Based on the provided ImageResizeOptions, calculates the dimensions to which a batch of images should be scaled and padded.static List<BufferedImage>extractBoxes(BufferedImage image, Collection boxes) Extracts sub-images from an image, based on provided rotated 4-point boxes.static BufferedImagerotate(BufferedImage image, TextOrientation orientation) Rotates image based on text orientation.static FloatBufferMdArraytoBchwInput(Collection<BufferedImage> images, OnnxInputProperties properties) Converts a collection of images to a batched ML model input in a BCHW format with 1 or 3 channels.static BufferedImagetruncateToRatio(BufferedImage image, double ratioLimit) Truncates the input image, so that neither width/height, nor height/width ratios exceed the limit.
-
Method Details
-
toBchwInput
public static FloatBufferMdArray toBchwInput(Collection<BufferedImage> images, OnnxInputProperties properties) Converts a collection of images to a batched ML model input in a BCHW format with 1 or 3 channels. This does aspect-preserving image resizing to fit the input shape.- Parameters:
-
images- collection of images to convert to model input -
properties- model input properties - Returns:
- batched BCHW model input MD-array
-
rotate
Rotates image based on text orientation. If no rotation necessary, same image is returned.- Parameters:
-
image- image to rotate -
orientation- text orientation used to rotate the image - Returns:
- new rotated image, or same image, if no rotation is required
-
extractBoxes
Extracts sub-images from an image, based on provided rotated 4-point boxes. Sub-images are transformed to fit the whole image without (in our use cases it is just rotation).- Parameters:
-
image- original image to be used for extraction -
boxes- list of 4-point boxes. Points should be in the following order: BL, TL, TR, BR - Returns:
- list of extracted image boxes
-
calcOutputDimensions
public static Dimensions2D calcOutputDimensions(Collection<BufferedImage> images, ImageResizeOptions resizeOptions) Based on the provided ImageResizeOptions, calculates the dimensions to which a batch of images should be scaled and padded.- Parameters:
-
images- batch of images to scale/pad -
resizeOptions- resize options to take into consideration for scaling/padding - Returns:
- the calculated dimensions
-
calcOutputDimensions
public static Dimensions2D calcOutputDimensions(BufferedImage image, ImageResizeOptions resizeOptions) Based on the provided ImageResizeOptions, calculates the dimensions of the output image, to where there original image should be scaled and placed with padding. The returned dimensions will always satisfy the minimum constraints. Maximum constraints will also be satisfied, if the dimension multiple is 1, but if it is greater, it may round up to be higher than maximum.- Parameters:
-
image- image, that will be scaled/padded -
resizeOptions- resize options to take into consideration for scaling/padding - Returns:
- the calculated dimensions
-
truncateToRatio
Truncates the input image, so that neither width/height, nor height/width ratios exceed the limit.If width/height ratio exceeds the limit, the image will be truncated on left and right equally.
If height/width ratio exceeds the limit, the image will be truncated on top and bottom equally.
- Parameters:
-
image- input image to truncate -
ratioLimit- target ratio limit - Returns:
- the truncated image
-