Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def shear_and_rotate(image_path, img_w, img_h):
- from skimage.io import imread, imsave
- from skimage.transform import resize
- from skimage import transform as transf
- import numpy as np
- from skimage.filters import threshold_otsu
- from scipy.ndimage.interpolation import rotate
- import math
- image = imread(image_path)
- augmented_images = []
- # Resize the image
- resized_ori = resize(image, (img_h, img_w))
- # Binarize the image using Otsu thresholding
- thresh = threshold_otsu(resized_ori)
- resized_ori = resized_ori > thresh
- resized_ori = resized_ori.astype(int)
- #resized_ori = np.transpose(resized_ori)
- augmented_images.append(resized_ori)
- for rotate_angle in np.arange(-5, 7, 2):
- rotated = rotate(image, rotate_angle, reshape=True, mode='constant', cval=1.)
- for shear_angle in np.arange(-0.5, 0.7, 0.2):
- affine_tf = transf.AffineTransform(shear=math.radians(shear_angle))
- sheared = transf.warp(rotated, affine_tf, mode='constant', cval=1.)
- resized = resize(sheared, (img_h, img_w))
- thresh = threshold_otsu(resized)
- resized = resized > thresh
- resized = resized.astype(int)
- #resized = np.transpose(resized) # For CNN-RNN only
- augmented_images.append(resized)
- augmented_images = np.array(augmented_images) # 36 additional images generated for each input image
- return augmented_images
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement