Class ImageUtils

java.lang.Object
deepnetts.util.ImageUtils

public final class ImageUtils extends Object
Utility methods to work with images. https://machinelearningmastery.com/best-practices-for-preparing-and-augmenting-image-data-for-convolutional-neural-networks/ random rescaling, horizontal flips, perturbations to brightness, contrast, and color, as well as random cropping. https://mxnet.apache.org/versions/1.5.0/tutorials/python/types_of_data_augmentation.html
  • Method Details

    • translateImage

      public static List<BufferedImage> translateImage(BufferedImage img)
      Generates 4 translated variations of the given image: translates it for one third of an image width and height in each direction. Uses black color to fill background extra space. TODO: add param for background color or pattern to use to fill the empty space.
      Parameters:
      img - image to translate
      Returns:
    • scaleImage

      public static BufferedImage scaleImage(BufferedImage img, int newWidth, int newHeight)
      Scales specified image to given size and returns new image with specified width and height. Original image is not changed..
      Parameters:
      img - image to sclae
      newWidth - width of scaled image
      newHeight - height of scaled image
      Returns:
    • scaleAndCenter

      public static BufferedImage scaleAndCenter(BufferedImage img, int targetWidth, int targetHeight, int padding, Color bgColor)
      Scales input image to specified target width or height, centers and returns resulting image. Scaling factor is calculated using larger dimension (width or height). Keeps aspect ratio and image type, and bgColor parameter to fill background.
      Parameters:
      img - image to scale
      targetWidth - witdth to scale to
      targetHeight - height to scale to
      padding - minimalno rastojanje od ivica slike koja se smanjuje i centrira
      bgColor - , umesto bgColor bolje da bude neki pattern koji popinjava tipa random pixels
      Returns:
      scaled and centered image
    • scaleBySmallerAndCrop

      public static BufferedImage scaleBySmallerAndCrop(BufferedImage img, int targetWidth, int targetHeight)
      Scales input image to specified target width or height, crops and returns resulting image. Scaling factor is calculated using smaller dimension (width or height). Keeps aspect ratio and image type, and bgColor parameter to fill background.
      Parameters:
      img - image to scale
      targetWidth - target image width
      targetHeight - target image height
      Returns:
      scaled and cropped image
    • scaleBySmallerTarget

      public static BufferedImage scaleBySmallerTarget(BufferedImage img, int targetWidth, int targetHeight)
    • flipHorizontal

      public static BufferedImage flipHorizontal(BufferedImage img)
    • flipVertical

      public static BufferedImage flipVertical(BufferedImage img)
    • loadImagesFromDirectory

      public static List<BufferedImage> loadImagesFromDirectory(File dir) throws IOException
      Loads all images from the specified directory, and returns them as a list.
      Parameters:
      dir -
      Returns:
      list of images as BufferedImage instances
      Throws:
      IOException
    • loadFileImageMapFromDirectory

      public static HashMap<File, BufferedImage> loadFileImageMapFromDirectory(File dir) throws IOException
      Loads JPG, JPEG or PNG images from specified directory and returns them as a map with File object as a key and BufferedImage object as a value. Skips all subdirectories.
      Parameters:
      dir - directory to load
      Returns:
      map of files and images.
      Throws:
      IOException
    • getImageType

      public static String getImageType(File file)
      Returns the extension of the given image file.
      Parameters:
      file -
      Returns:
    • grayscale

      public static BufferedImage grayscale(BufferedImage img)
      Returns grayscale version of the given image. Original image is not changed.
      Parameters:
      img -
      Returns:
    • createIndexFile

      public static void createIndexFile(HashMap<File, BufferedImage> imageMap, String imageFile, boolean useAbsPath) throws IOException
      Parameters:
      imageMap -
      imageFile -
      useAbsPath -
      Throws:
      IOException
    • randomTranslateImage

      public static BufferedImage[] randomTranslateImage(BufferedImage img, int stepCount, int stepSize)
      Returns an array of images created by translating specified input image by specified number of count with specified step size.
      Parameters:
      img - image to translate
      stepCount - number of
      stepSize -
      Returns:
    • randomCrop

      public static List<BufferedImage> randomCrop(BufferedImage img, int cropWidth, int cropHeight, int cropNum, Random random)
      Crops specified number of random sub-images with specified dimensions.
      Parameters:
      img - image to crop
      cropWidth - width of the cropped image
      cropHeight - height of the cropped image
      cropNum - number of images to crop
      random - random number generator used to generate random positions
      Returns:
      list of randomly cropped images
    • cropAtCornersAndCenter

      public static List<BufferedImage> cropAtCornersAndCenter(BufferedImage img, int cropWidth, int cropHeight)
      Returns five crops of a given image: four from each corner and one at center
      Parameters:
      img - image to crop
      cropWidth - width of the cropped area
      cropHeight - height of the cropped area
      Returns:
      five crops from the corners and center of the image
    • randomCrop

      public static BufferedImage randomCrop(BufferedImage img, int cropWidth, int cropHeight)
      Randomly crop image with specified width and height. Returns cropped image, while original image is unchanged.
      Parameters:
      img -
      cropWidth -
      cropHeight -
      Returns:
      cropped image
    • randomTintAndBrightness

      public static List<BufferedImage> randomTintAndBrightness(BufferedImage img, float maxTint, int maxBrightness, int num, Random random)
      Still not working as it should
      Parameters:
      img - input image
      maxTint - maximum tint value
      maxBrightness - maximum brightness value
      num - number of variations
      random - random generator
      Returns:
      list of augmented images
      See Also:
    • randomTintAndBrightness

      public static BufferedImage randomTintAndBrightness(BufferedImage img)
    • writeImages

      public static void writeImages(List<BufferedImage> images, String targetPath, String fileNamePrefix, String fileType) throws IOException
      Writes list of images to specified file path.
      Parameters:
      images - images to write
      targetPath - path to write to
      fileNamePrefix - prefix of image file name to use (eg. for prefix pattern "someImage" it will create file someImage_1.jpg )
      fileType - type of file/images to write
      Throws:
      IOException
    • generateRandomColoredImages

      public static void generateRandomColoredImages(int width, int height, int numberOfImages, String destPath) throws IOException
      Generates a specified number of randomly full colored images of a specified size. Images are saved at specified destination path named using negative_x.jpg name pattern
      Parameters:
      width - image width
      height - image height
      numberOfImages - number of images to generate
      destPath - destination path to save images
      Throws:
      IOException
    • generateRandomFilledRectImages

      public static void generateRandomFilledRectImages(int width, int height, int numberOfImages, String destPath) throws IOException
      Throws:
      IOException
    • generateNoisyImage

      public static void generateNoisyImage(int width, int height, int numberOfImages, String destPath) throws IOException
      Throws:
      IOException