Module NewOCR

Class 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 a BufferedImage 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 ints
      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
      static 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 it
      static 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`.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • OCRUtils

        public OCRUtils()
    • 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 number
        two - 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 number
        two - 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 array
        input2 - 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 check
        upperBound - The upper bound to check
        value - 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 a BufferedImage with the same dimensions as the image.
        Parameters:
        bufferedImage - The input BufferedImage
        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 image
        values - 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 values
        y - 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 image
        x - The X coordinate to check
        y - 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 image
        path - 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 on
        color - The color to use
        y - The Y value of the horizontal line
        x - The X start position of the line
        width - 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 on
        color - The color to use
        y - The Y start position of the line
        x - The X value of the vertical line
        height - 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