-
Notifications
You must be signed in to change notification settings - Fork 4
/
img_trans.py
72 lines (54 loc) · 1.85 KB
/
img_trans.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import numpy as np
import random
from skimage import draw
from skimage import morphology as mp
from skimage import exposure as ep
from keras.preprocessing import image
def light(img):
# adjust_gamma(x, gamma)
# gamma > 1 --- darker
# gamma < 1 --- brighter
return ep.adjust_gamma(img, random.uniform(0.8, 1.05))
def shift(img):
# random_shift(x, wrg, hrg, row_axis=1, col_axis=2, channel_axis=0, fill_mode='nearest', cval=0.0)
img = img.reshape(1, 28, 28)
wrg = 0.2
hrg = 0.2
return image.random_shift(img, wrg, hrg).reshape(28, 28)
def rotate(img):
# random_rotation(x, rg, row_axis=1, col_axis=2, channel_axis=0, fill_mode='nearest', cval=0.0)
img = img.reshape(1, 28, 28)
rg = 30
return image.random_rotation(img, rg).reshape(28, 28)
def flip(img):
# flip_axis(x, axis)
# horizontal
return image.flip_axis(img, 1)
def zoom(img):
# random_zoom(x, zoom_range, row_axis=1, col_axis=2, channel_axis=0, fill_mode='nearest', cval=0.0)
img = img.reshape(1, 28, 28)
zoom_range = (0.6, 1.3)
return image.random_zoom(img, zoom_range).reshape(28, 28)
def dilation(img):
# return greyscale morphological dilation of an image
return mp.dilation(img, mp.square(2, dtype=np.uint64))
def erosion(img):
# return greyscale morphological erosion of an image
return mp.erosion(img, mp.square(2, dtype=np.uint64))
def draw_line(img):
# draw a straight line across the digit
r0 = random.randint(0, 28)
c0 = random.randint(0, 4)
r1 = random.randint(0, 28)
c1 = random.randint(24, 28)
rr, cc = draw.line(r0, c0, r1, c1)
img[rr, cc] = 255
return img
def s_p_noise(img):
# salt and pepper noise
rows, cols = img.shape
for i in range(50):
x = np.random.randint(0, rows)
y = np.random.randint(0, cols)
img[x, y] = 255
return img