-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata-augumentation.py
83 lines (69 loc) · 2.35 KB
/
data-augumentation.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
73
74
75
76
77
78
79
80
81
82
83
from __future__ import print_function
import matplotlib.pyplot as plt
import numpy as np
from skimage.io import imread
from skimage import exposure, color
from skimage.transform import resize
import keras
from keras import backend as K
from keras.datasets import cifar10
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.preprocessing.image import ImageDataGenerator
# Define functions for contrast adjustment
# Contrast stretching
def contrast_stretching(img):
p2, p98 = np.percentile(img, (2, 98))
img_rescale = exposure.rescale_intensity(img, in_range=(p2, p98))
return img_rescale
# Histogram equalization
def HE(img):
img_eq = exposure.equalize_hist(img)
return img_eq
# Adaptive histogram equalization
def AHE(img):
img_adapteq = exposure.equalize_adapthist(img, clip_limit=0.03)
return img_adapteq
def imgGen(img, zca=False, rotation=0., w_shift=0., h_shift=0., shear=0., zoom=0., h_flip=False, v_flip=False, preprocess_fcn=None, batch_size=9):
datagen = ImageDataGenerator(
zca_whitening=zca,
rotation_range=rotation,
width_shift_range=w_shift,
height_shift_range=h_shift,
shear_range=shear,
zoom_range=zoom,
fill_mode='nearest',
horizontal_flip=h_flip,
vertical_flip=v_flip,
preprocessing_function=preprocess_fcn,
data_format=K.image_data_format())
datagen.fit(img)
i=0
for img_batch in datagen.flow(img, batch_size=9, shuffle=False):
for img in img_batch:
plt.subplot(330 + 1 + i)
plt.imshow(img)
i=i+1
if i >= batch_size:
break
plt.show()
# visualize the image
img = imread("001.png")
plt.imshow(img)
plt.show()
# reshape it to prepare for data generator
img = img.astype('float32')
img /= 255
h_dim = np.shape(img)[0]
w_dim = np.shape(img)[1]
print(h_dim)
print(w_dim)
num_channel = np.shape(img)[2]
img = img.reshape(1, h_dim, w_dim, num_channel)
print(img.shape)
# generate images using function imgGen
#imgGen(img, rotation=30, h_shift=0.1)
imgGen(img, rotation=30, h_shift=0.1, preprocess_fcn = contrast_stretching)
imgGen(img, rotation=30, h_shift=0.1, preprocess_fcn = HE)
imgGen(img, rotation=30, h_shift=0.1, preprocess_fcn = AHE)