- java.lang.Object
-
- com.uddernetworks.newocr.utils.OCRUtils
-
public class OCRUtils extends java.lang.Object
Some various utility methods used by the OCR that may assist others using the library.- Since:
- April 25, 2019
-
-
Constructor Summary
Constructors Constructor Description OCRUtils()
-
Method Summary
Modifier and Type Method Description static void
colorColumn(java.awt.image.BufferedImage image, java.awt.Color color, int x, int y, int height)
Colors in a vertical line of an image.static void
colorRow(java.awt.image.BufferedImage image, java.awt.Color color, int y, int x, int width)
Colors in a horizontal line of an image.static boolean[][]
createGrid(java.awt.image.BufferedImage bufferedImage)
Creates a grid of booleans from aBufferedImage
with the same dimensions as the image.static double
diff(double one, double two)
Gets the difference between two doubles.static int
diff(int one, int two)
Gets the difference between two intsstatic java.util.Optional<java.awt.image.BufferedImage>
filter(java.awt.image.BufferedImage input)
Binarizes the input image, making all pixels wither black or white with an alpha of 255static java.util.OptionalDouble
getDifferencesFrom(double[] input1, double[] input2)
Gets the difference of two arrays' values.static boolean
isBlack(java.awt.image.BufferedImage image, int x, int y)
Gets if a pixel should be considered black.static boolean
isRowPopulated(boolean[][] values, int y)
Gets if the row has any `true` (Black) values in itstatic boolean
isWithin(double lowerBound, double upperBound, double value)
Gets if a given number is within two bounds.static void
makeImage(boolean[][] values, java.lang.String path)
Creates an image from a grid of booleans, `true` being black and `false` being white.static void
printOut(boolean[][] values)
Prints a grid of booleans to console using full width characters so it will appear proportional and not skewed with spaces and the filling character.static java.awt.image.BufferedImage
readImage(java.io.File input)
An ImageIO.read() replacement, which in tests can be up to 15x faster.static java.lang.String
removeLeadingSpaces(java.lang.String string)
Removes all common spaces between all newlines, useful if the OCR say adds an extra 2 spaces before all lines of text, this will remove the 2 spaces.static void
toGrid(java.awt.image.BufferedImage input, boolean[][] values)
Populates a boolean 2D array with the same dimensions as the input image where each pixel is represented by a boolean value, black being `true`, white being `false`.
-
-
-
Method Detail
-
readImage
public static java.awt.image.BufferedImage readImage(java.io.File input) throws java.io.IOException
An ImageIO.read() replacement, which in tests can be up to 15x faster. This has shown to significantly improve the OCR's performance both in training and actual usage.- Parameters:
input
- The file to read- Returns:
- The BufferedImage of the file
- Throws:
java.io.IOException
- If there are issues reading the file
-
removeLeadingSpaces
public static java.lang.String removeLeadingSpaces(java.lang.String string)
Removes all common spaces between all newlines, useful if the OCR say adds an extra 2 spaces before all lines of text, this will remove the 2 spaces.- Parameters:
string
- The input string- Returns:
- The input string trimmed properly
-
diff
public static double diff(double one, double two)
Gets the difference between two doubles.- Parameters:
one
- The first numbertwo
- The second number- Returns:
- The difference
-
diff
public static int diff(int one, int two)
Gets the difference between two ints- Parameters:
one
- The first numbertwo
- The second number- Returns:
- The difference
-
getDifferencesFrom
public static java.util.OptionalDouble getDifferencesFrom(double[] input1, double[] input2)
Gets the difference of two arrays' values.- Parameters:
input1
- The first arrayinput2
- The second array- Returns:
- An array with the same length as the inputs containing the difference of both arrays' respective values
-
isWithin
public static boolean isWithin(double lowerBound, double upperBound, double value)
Gets if a given number is within two bounds.- Parameters:
lowerBound
- The lower bound to checkupperBound
- The upper bound to checkvalue
- The value to check- Returns:
- If the two values are within the given bounds
-
createGrid
public static boolean[][] createGrid(java.awt.image.BufferedImage bufferedImage)
Creates a grid of booleans from aBufferedImage
with the same dimensions as the image.- Parameters:
bufferedImage
- The inputBufferedImage
- Returns:
- The created grid
-
toGrid
public static void toGrid(java.awt.image.BufferedImage input, boolean[][] values)
Populates a boolean 2D array with the same dimensions as the input image where each pixel is represented by a boolean value, black being `true`, white being `false`.- Parameters:
input
- The input imagevalues
- The mutable empty grid
-
isRowPopulated
public static boolean isRowPopulated(boolean[][] values, int y)
Gets if the row has any `true` (Black) values in it- Parameters:
values
- The grid of image valuesy
- The Y coordinate of the row to check- Returns:
- If the row has anything in it
-
filter
public static java.util.Optional<java.awt.image.BufferedImage> filter(java.awt.image.BufferedImage input)
Binarizes the input image, making all pixels wither black or white with an alpha of 255- Parameters:
input
- The input image to be filtered- Returns:
- The filtered image
-
isBlack
public static boolean isBlack(java.awt.image.BufferedImage image, int x, int y)
Gets if a pixel should be considered black.- Parameters:
image
- The input imagex
- The X coordinate to checky
- The Y coordinate to check- Returns:
- If the pixel should be considered black
-
makeImage
public static void makeImage(boolean[][] values, java.lang.String path)
Creates an image from a grid of booleans, `true` being black and `false` being white.- Parameters:
values
- The values to convert into an imagepath
- The path of the file
-
colorRow
public static void colorRow(java.awt.image.BufferedImage image, java.awt.Color color, int y, int x, int width)
Colors in a horizontal line of an image.- Parameters:
image
- The image to color oncolor
- The color to usey
- The Y value of the horizontal linex
- The X start position of the linewidth
- The width of the line to draw
-
colorColumn
public static void colorColumn(java.awt.image.BufferedImage image, java.awt.Color color, int x, int y, int height)
Colors in a vertical line of an image.- Parameters:
image
- The image to color oncolor
- The color to usey
- The Y start position of the linex
- The X value of the vertical lineheight
- The height of the line to draw
-
printOut
public static void printOut(boolean[][] values)
Prints a grid of booleans to console using full width characters so it will appear proportional and not skewed with spaces and the filling character.- Parameters:
values
- The values to print out
-
-