knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
A common application of supervised machine learning is identifying the object of an image. One issue that users encounter is a model misclassifying a new image because the object is rotated or translated in some way that was not captured in the training images. The purpose of this package is to create a more robust set of images for users to train their model with. The package will accept an image as an input, apply a series of transformations to it, and return an array of transformed pixel values. Transformations include: rotating, mirroring, and translating (shifting the object's location in the frame).
Rotates an image the user-specified number of times. The image will be rotated to a random degree of roation in the clockwise direction, with the maximum rotation angle specified by the user. The pixel values of the original and rotated images is returned.
Function:
rotate(image_path, num_images, max_rotation)
Arguments:
image_path
: file path of the input image (string)num_images
: number of randomly rotated images to be returned (integer)max_rotation
: maximum allowable degrees of rotation of the images (integer) between 1 and 360Output:
num_images
+ 1 images (original plus all rotated images)Example:
rotate("../tests/testthat/img/milad.jpg", 10, 280)
Mirrors an image in the horizontal and/or vertical direction and returns the pixel values of the original and mirrored image(s).
Function:
mirror(image_path, direction)
Arguments:
image_path
: file path of the input image (string)direction
: direction of mirroring (string, optional) 'horizontal', 'vertical', or 'all'. If not specified, defaults to 'all'Output:
direction = 'all'
(original, horizontally mirrored, vertically mirrored) or 2 images if direction is horizontal or vertical (original image, mirrored image)Example:
mirror("../tests/testthat/img/milad.jpg", "horizontal")
Translate will move an image within its frame, so that the topic of the image will be shifted to a new location in the frame. The distance and direction of translation will be chosen randomly, but the user specifies the maximum distance of the translation and the number of images they want generated. It returns the pixel values of the original and translated images.
Function:
translate(image_path, num_images, max_translation)
Arguments:
image_path
: file path of the input image (string)num_images
: number of randomly translated images to be returned (integer)max_translation
: maximum distance in pixels that the image can be translated (integer)Output
num_images
+ 1 images (original plus all translated images)Example:
translate("../tests/testthat/img/milad.jpg", 5, 80)
Perform transformations on the image using the mirror, rotate, and translate functions:
library(AutoTransformR) # mirror function with horizontal and vertical mirroring m <- mirror("../tests/testthat/img/milad.jpg") # rotation with 8 rotated images returned and a maximum rotation of 360 degrees r <- rotate("../tests/testthat/img/milad.jpg", 8, 360) # translation with 8 translated images returned and a maximum translation of 60 pixels t <- translate("../tests/testthat/img/milad.jpg", 5, 60)
OUTPUT:
Use dim()
to see the dimensions of the returned array.
dim(m) # mirror function
dim(r) # rotate function
dim(t) # translate function
VIEW TRANSFORMED IMAGES:
To access individual images in the returned array, you must use the last dimension of the array to index to the image in question. The first image in the array is the original, the transformed images follow. Below are examples of how to view the images using the the function imageshow
from the package OpenImageR
.
Original Image:
library(OpenImageR) # View original image imageShow(m[,,,1])
Mirrored Image:
If the direction of mirroring is "all", the array will have the horizontally mirrored image at index 2, and the vertically mirrored image at index 3.
# view horizontal mirroring imageShow(m[,,,2])
Rotated Image:
# view one of the rotated images imageShow(r[,,,3])
Translated Image:
# view one of the translated images imageShow(t[,,,2])
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.