Package deepnetts.util
Class ImageUtils
java.lang.Object
deepnetts.util.ImageUtils
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 Summary
Modifier and TypeMethodDescriptionstatic void
createIndexFile
(HashMap<File, BufferedImage> imageMap, String imageFile, boolean useAbsPath) static List
<BufferedImage> cropAtCornersAndCenter
(BufferedImage img, int cropWidth, int cropHeight) Returns five crops of a given image: four from each corner and one at centerstatic BufferedImage
static BufferedImage
static void
generateNoisyImage
(int width, int height, int numberOfImages, String destPath) static void
generateRandomColoredImages
(int width, int height, int numberOfImages, String destPath) Generates a specified number of randomly full colored images of a specified size.static void
generateRandomFilledRectImages
(int width, int height, int numberOfImages, String destPath) static String
getImageType
(File file) Returns the extension of the given image file.static BufferedImage
grayscale
(BufferedImage img) Returns grayscale version of the given image.static HashMap
<File, BufferedImage> 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.static List
<BufferedImage> Loads all images from the specified directory, and returns them as a list.static BufferedImage
randomCrop
(BufferedImage img, int cropWidth, int cropHeight) Randomly crop image with specified width and height.static List
<BufferedImage> randomCrop
(BufferedImage img, int cropWidth, int cropHeight, int cropNum, Random random) Crops specified number of random sub-images with specified dimensions.static BufferedImage
static List
<BufferedImage> randomTintAndBrightness
(BufferedImage img, float maxTint, int maxBrightness, int num, Random random) Still not working as it shouldstatic 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.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.static BufferedImage
scaleBySmallerAndCrop
(BufferedImage img, int targetWidth, int targetHeight) Scales input image to specified target width or height, crops and returns resulting image.static BufferedImage
scaleBySmallerTarget
(BufferedImage img, int targetWidth, int targetHeight) static BufferedImage
scaleImage
(BufferedImage img, int newWidth, int newHeight) Scales specified image to given size and returns new image with specified width and height.static List
<BufferedImage> Generates 4 translated variations of the given image: translates it for one third of an image width and height in each direction.static void
writeImages
(List<BufferedImage> images, String targetPath, String fileNamePrefix, String fileType) Writes list of images to specified file path.
-
Method Details
-
translateImage
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
Scales specified image to given size and returns new image with specified width and height. Original image is not changed..- Parameters:
img
- image to sclaenewWidth
- width of scaled imagenewHeight
- 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 scaletargetWidth
- witdth to scale totargetHeight
- height to scale topadding
- minimalno rastojanje od ivica slike koja se smanjuje i centrirabgColor
- , 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 scaletargetWidth
- target image widthtargetHeight
- target image height- Returns:
- scaled and cropped image
-
scaleBySmallerTarget
public static BufferedImage scaleBySmallerTarget(BufferedImage img, int targetWidth, int targetHeight) -
flipHorizontal
-
flipVertical
-
loadImagesFromDirectory
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
-
grayscale
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
Returns an array of images created by translating specified input image by specified number of count with specified step size.- Parameters:
img
- image to translatestepCount
- number ofstepSize
-- 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 cropcropWidth
- width of the cropped imagecropHeight
- height of the cropped imagecropNum
- number of images to croprandom
- 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 cropcropWidth
- width of the cropped areacropHeight
- height of the cropped area- Returns:
- five crops from the corners and center of the image
-
randomCrop
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
-tint
-brightness
-- Returns:
- https://en.wikipedia.org/wiki/Normalization_(image_processing)
-
randomTintAndBrightness
-
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 writetargetPath
- path to write tofileNamePrefix
- 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 widthheight
- image heightnumberOfImages
- number of images to generatedestPath
- 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
-