Skip to content

Commit f66b832

Browse files
committed
data augument
1 parent d7e1129 commit f66b832

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

utils/data_augument.py

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
import cv2
2+
import numpy as np
3+
import os
4+
import random
5+
from matplotlib import pyplot as plt
6+
7+
# create by Feng, edit by Feng 2017 / 08 / 14
8+
# this project is doing image data augmentation for any DL/ML algorithm
9+
#
10+
11+
# avg blur minimum filter size is 3
12+
13+
def avg_blur(img, max_filiter_size = 3) :
14+
img = img.astype(np.uint8)
15+
if max_filiter_size >= 3 :
16+
filter_size = random.randint(3, max_filiter_size)
17+
if filter_size % 2 == 0 :
18+
filter_size += 1
19+
out = cv2.blur(img, (filter_size, filter_size))
20+
return out
21+
22+
# gaussain blur minimum filter size is 3
23+
# when sigma = 0 gaussain blur weight will compute by program
24+
# when the sigma is more large the blur effect more obvious
25+
26+
def gaussain_blur(img, max_filiter_size = 3, sigma = 0) :
27+
img = img.astype(np.uint8)
28+
if max_filiter_size >= 3 :
29+
filter_size = random.randint(3, max_filiter_size)
30+
if filter_size % 2 == 0 :
31+
filter_size += 1
32+
#print ('size = %d'% filter_size)
33+
out = cv2.GaussianBlur(img, (filter_size, filter_size), sigma)
34+
return out
35+
36+
def gaussain_noise(img, mean = 0, var = 0.1) :
37+
img = img.astype(np.uint8)
38+
h, w, c = img.shape
39+
sigma = var ** 0.5
40+
gauss = np.random.normal(mean, sigma, (h, w, c))
41+
gauss = gauss.reshape(h, w, c).astype(np.uint8)
42+
noisy = img + gauss
43+
return noisy
44+
45+
# fill_pixel is 0(black) or 255(white)
46+
47+
def img_shift(img, x_min_shift_piexl = -1, x_max_shift_piexl = 1, y_min_shift_piexl = -1, y_max_shift_piexl = 1, fill_pixel = 0) :
48+
img = img.astype(np.uint8)
49+
h, w, c = img.shape
50+
out = np.zeros(img.shape)
51+
if fill_pixel == 255 :
52+
out[:, :] = 255
53+
out = out.astype(np.uint8)
54+
move_x = random.randint(x_min_shift_piexl, x_max_shift_piexl)
55+
move_y = random.randint(y_min_shift_piexl, y_max_shift_piexl)
56+
#print (('move_x = %d')% (move_x))
57+
#print (('move_y = %d')% (move_y))
58+
if move_x >= 0 and move_y >= 0 :
59+
out[move_y:, move_x: ] = img[0: (h - move_y), 0: (w - move_x)]
60+
elif move_x < 0 and move_y < 0 :
61+
out[0: (h + move_y), 0: (w + move_x)] = img[ - move_y:, - move_x:]
62+
elif move_x >= 0 and move_y < 0 :
63+
out[0: (h + move_y), move_x:] = img[ - move_y:, 0: (w - move_x)]
64+
elif move_x < 0 and move_y >= 0 :
65+
out[move_y:, 0: (w + move_x)] = img[0 : (h - move_y), - move_x:]
66+
return out
67+
68+
# In img_rotation func. rotation center is image center
69+
70+
def img_rotation(img, min_angel = 0, max_angel = 0, min_scale = 1, max_scale = 1, fill_pixel = 0) :
71+
img = img.astype(np.uint8)
72+
h, w, c = img.shape
73+
_angel = random.randint(min_angel, max_angel)
74+
_scale = random.uniform(min_scale, max_scale)
75+
rotation_matrix = cv2.getRotationMatrix2D((w / 2, h / 2), _angel, _scale)
76+
out = cv2.warpAffine(img, rotation_matrix, (w, h))
77+
if fill_pixel == 255 :
78+
mask = np.zeros(img.shape)
79+
mask[:, :, :] = 255
80+
mask = mask.astype(np.uint8)
81+
mask = cv2.warpAffine(mask, rotation_matrix, (w, h))
82+
for i in range (h) :
83+
for j in range(w) :
84+
if mask[i, j, 0] == 0 and mask[i, j, 1] == 0 and mask[i, j, 2] == 0 :
85+
out[i, j, :] = 255
86+
return out
87+
88+
# In img_flip func. it will random filp image
89+
# when flip factor is 1 it will do hor. flip (Horizontal)
90+
# 0 ver. flip (Vertical)
91+
# -1 hor. + ver flip
92+
93+
def img_flip(img) :
94+
img = img.astype(np.uint8)
95+
flip_factor = random.randint(-1, 1)
96+
out = cv2.flip(img, flip_factor)
97+
return out
98+
99+
# Zoom image by scale
100+
101+
def img_zoom(img, min_scale = 1, max_scale = 1) :
102+
img = img.astype(np.uint8)
103+
h, w, c = img.shape
104+
scale = random.uniform(min_scale, max_scale)
105+
h = int(h * scale)
106+
w = int(w * scale)
107+
out = cv2.resize(img, (w, h), interpolation=cv2.INTER_CUBIC)
108+
return out
109+
110+
# change image contrast by hsv
111+
112+
def img_contrast(img, min_s, max_s, min_v, max_v) :
113+
img = img.astype(np.uint8)
114+
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
115+
_s = random.randint(min_s, max_s)
116+
_v = random.randint(min_v, max_v)
117+
if _s >= 0 :
118+
hsv_img[:, :, 1] += _s
119+
else :
120+
_s = - _s
121+
hsv_img[:, :, 1] -= _s
122+
if _v >= 0 :
123+
hsv_img[:, :, 2] += _v
124+
else :
125+
_v = - _v
126+
hsv_img[:, :, 2] += _v
127+
out = cv2.cvtColor(hsv_img, cv2.COLOR_HSV2BGR)
128+
return out
129+
130+
# change image color by hsv
131+
132+
def img_color(img, min_h, max_h) :
133+
img = img.astype(np.uint8)
134+
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
135+
_h = random.randint(min_h, max_h)
136+
hsv_img[:, :, 0] += _h
137+
out = cv2.cvtColor(hsv_img, cv2.COLOR_HSV2BGR)
138+
return out
139+
140+
141+
142+
143+
144+
145+
146+
147+
148+
149+
150+
151+
152+
153+
154+
155+
156+
157+
158+
159+
160+
161+
162+
163+
164+
165+
166+

0 commit comments

Comments
 (0)