Created
April 27, 2017 07:09
-
-
Save yshean/4922e31658edba348429deb6f1c9de48 to your computer and use it in GitHub Desktop.
Data augmentation using shear and rotate within a range.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment