import numpy as np
import matplotlib.pyplot as plt
from keras.preprocessing.image import ImageDataGenerator
from keras.utils import load_img, img_to_array
# Preview of the image we will work on
img = load_img('/content/drive/MyDrive/Sample/Cat.jpg')
# Resizing the image
resized_img = img.resize((400, 500))
resized_img
# Saving the resized image into local disk
resized_img.save('/content/drive/MyDrive/Sample/resized_cat.jpg')
# Image plotting function
def img_plot(images):
fig, axes = plt.subplots(1, 5, figsize=(30, 40))
for i in range(5):
aug_img = next(images)
aug_results = aug_img[0].astype('uint8')
axes[i].imshow(aug_results)
axes[i].axis('off')
# Shifting width
pic = img_to_array(resized_img)
pic_sample = np.expand_dims(pic, axis=0)
data = ImageDataGenerator(width_shift_range=[10, 50])
aug_data = data.flow(pic_sample, batch_size=1)
img_plot(aug_data)
# Height shift
pic = img_to_array(resized_img)
pic_sample = np.expand_dims(pic, axis=0)
data = ImageDataGenerator(height_shift_range=[20, 70])
aug_data = data.flow(pic_sample, batch_size=1)
img_plot(aug_data)
# Rotation
pic = img_to_array(resized_img)
pic_sample = np.expand_dims(pic, axis=0)
data = ImageDataGenerator(rotation_range=50)
aug_data = data.flow(pic_sample, batch_size=1)
img_plot(aug_data)
# Horizontal Flip
pic = img_to_array(resized_img)
pic_sample = np.expand_dims(pic, axis=0)
data = ImageDataGenerator(horizontal_flip=True)
aug_data = data.flow(pic_sample, batch_size=1)
img_plot(aug_data)
# Random Flip
pic = img_to_array(resized_img)
pic_sample = np.expand_dims(pic, axis=0)
data = ImageDataGenerator(horizontal_flip=True, vertical_flip=True)
aug_data = data.flow(pic_sample, batch_size=1)
img_plot(aug_data)
# Random Brightness
pic = img_to_array(resized_img)
pic_sample = np.expand_dims(pic, axis=0)
data = ImageDataGenerator(brightness_range=[0.5, 1.5])
aug_data = data.flow(pic_sample, batch_size=1)
img_plot(aug_data)
# Random Zoom
pic = img_to_array(resized_img)
pic_sample = np.expand_dims(pic, axis=0)
data = ImageDataGenerator(zoom_range=0.5)
aug_data = data.flow(pic_sample, batch_size=1)
img_plot(aug_data)
# Shear
pic = img_to_array(resized_img)
pic_sample = np.expand_dims(pic, axis=0)
data = ImageDataGenerator(shear_range=50.5)
aug_data = data.flow(pic_sample, batch_size=1)
img_plot(aug_data)
# Shifting Channels
pic = img_to_array(resized_img)
pic_sample = np.expand_dims(pic, axis=0)
data = ImageDataGenerator(channel_shift_range=55.0, fill_mode='constant')
aug_data = data.flow(pic_sample, batch_size=1)
img_plot(aug_data)
# RGB to Grayscale
import cv2
import numpy as np
from PIL import Image
def rgb_to_gray(image):
img = np.array(image)
# converting the image from rgb to gray
gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# creating the image and reshaping it
return gray_img.reshape(500, 400, 1)
# RGB to Gray Images
pic = img_to_array(resized_img)
pic_sample = np.expand_dims(pic, axis=0)
data = ImageDataGenerator(preprocessing_function=rgb_to_gray)
aug_data = data.flow(pic_sample, batch_size=1)
img_plot(aug_data)
# RGB to HSV (Hue and Saturation Value)
import cv2
import numpy as np
def rgb_to_hsv(image):
img = np.array(image)
# converting the image from rgb to hsv
hsv_img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
# returning the image
return hsv_img
# RGB to HSV Images
pic = img_to_array(resized_img)
pic_sample = np.expand_dims(pic, axis=0)
data = ImageDataGenerator(preprocessing_function = rgb_to_hsv)
aug_data = data.flow(pic_sample, batch_size=1)
img_plot(aug_data)
# Adding Contrast
import cv2
import numpy as np
def scale_img(image):
img = np.array(image)
# scaling the image
scale_img = cv2.convertScaleAbs(img, alpha=1.8, beta=10)
# returning the image
return scale_img
# Contrasting Images
pic = img_to_array(resized_img)
pic_sample = np.expand_dims(pic, axis=0)
data = ImageDataGenerator(preprocessing_function = scale_img)
aug_data = data.flow(pic_sample, batch_size=1)
img_plot(aug_data)
# Decreasing Contrast
import cv2
import numpy as np
def scale_img(image):
img = np.array(image)
# scaling the image
scale_img = cv2.convertScaleAbs(img, alpha=0.2, beta=0.4)
# returning the image
return scale_img
# decontrasting Images
pic = img_to_array(resized_img)
pic_sample = np.expand_dims(pic, axis=0)
data = ImageDataGenerator(preprocessing_function = scale_img)
aug_data = data.flow(pic_sample, batch_size=1)
img_plot(aug_data)
# Adding Noise
import cv2
import numpy as np
def add_noise(image):
mean = 0
var = 10
sigma = var ** 1.5
gaussian = np.random.normal(mean, sigma, (500, 400, 3))
noisy_image = image + gaussian
return noisy_image
pic = img_to_array(resized_img)
pic_sample = np.expand_dims(pic, axis=0)
data = ImageDataGenerator(preprocessing_function = add_noise)
aug_data = data.flow(pic_sample, batch_size=1)
img_plot(aug_data)