In this blog, we will discuss ImageDataGenerator “apply_transform” method. Using this method, you can apply any desired transformations to an image. You can find its use in the ImageDataGenerator “flow” method. First of all, let’s discuss its Keras API.
Keras API
1 |
apply_transform(x, transform_parameters) |
This applies transformations to x (3D tensor) according to the transform parameters specified.
The “transform_parameters” is a dictionary specifying the set of transformations to be applied. Only the following transformations are available
1 |
theta, tx, ty, shear, zx, zy, flip_horizontal, flip_vertical, channel_shift_intensity, brightness |
Let’s discuss these in detail.
theta: Rotation angle in degrees. Below is an example that rotates the image by 40 degrees.
tx and ty: These are the shifts in the vertical and the horizontal directions respectively. For instance, tx=20 will shift the image vertically by 20 pixels.
In this, first of all, the translation matrix is calculated. Then affine transformation is applied using the “scipy.ndimage” affine_transformation method.
zx and zy: This zooms the image in the vertical and horizontal directions respectively. If less than 1, the image is zoomed in otherwise zoomed out.
Note: -ve values of zx and zy results in flipping the image in vertical and horizontal directions respectively. For instance, zx=-1 will flip the image vertically.
flip_horizontal and flip_vertical: This flips the image horizontally and vertically. For instance, below is the code for flipping the image horizontally,
1 2 3 |
b = img.swapaxes(1,0) c = b[::-1, ...] flip_img = c.swapaxes(0,1) |
channel_shift_intensity: This shifts the channel values by the amount specified. The following code sums up how it works
1 |
[np.clip(x_channel + amount, min_img, max_img) for x_channel in img] |
brightness: This controls the brightness of the image. An enhancement factor of 0.0 gives a black image. A factor of 1.0 gives the original image.
Hope you understand all the arguments. Now, let’s see how to use this.
How to use this?
Because “apply_transform” is a method inside the ImageDataGenerator class. Thus, one first need to create the instance of this class and then apply this method as shown below
1 2 3 4 5 |
from keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array img = load_img('D:/downloads/opencv_logo.PNG') img = img_to_array(img) datagen = ImageDataGenerator() rotate = datagen.apply_transform(x=img, transform_parameters={'theta':40, 'brightness':0.8, 'zx':0.9, 'zy':0.9}) |
Hope you enjoy reading.
If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.