Data enhanced policy:
an online mode - Training
randomly cut (completely random, the four corners of the center +) Crop Allows you
DEF random_crop (IMG, Scale = [0.8, 1.0], ratio = [. 3 / 4., 4. /. 3. .], resize_w = 100, resize_h = 100):
"" "
randomly crop
: param IMG:
: param scale: scale
: param ratio:
: param resize_w:
: param resize_h:
: return:
" ""
an aspect_ratio Math.sqrt = ( np.random.uniform (* ratio))
W = an aspect_ratio 1. *
H = 1. / an aspect_ratio
src_h, src_w and img.shape = [: 2]
bound = min ((a float (src_w and) / src_h) / (W ** 2),
(a float (src_h) / src_w and) / (H ** 2))
scale_max = min (Scale [. 1], bound)
scale_min = min (Scale [0], bound)
src_w and src_h * * = target_area np.random.uniform (scale_min,
scale_max)
target_size of Math.sqrt = (target_area)
W = int (* target_size of W)
H = int (target_size of H *)
I = np.random.randint (0, src_w and - W +. 1)
J = np.random.randint (0, src_h - H +. 1)
IMG = IMG [J: J + H, I: I + W]
IMG = cv2.resize (IMG, (resize_w, resize_h ))
return IMG
DEF rule_crop (IMG, box_ratio = (. 3 /. 4, 3. /. 4), location_type = 'LT', resize_w = 100, resize_h = 100):.
"" "
crop according to certain rules, directly in the original operation size, no picture for
: param IMG:
: param box_ratio: shear ratio: (the ratio of the width, the height ratio)
: param location_type: = the specific position in which: one of the following:
the LR: left corner
RT: top right
LB : 左下角
RB : 右下角
CC : 中心
:param resize_w: 输出图的width
:param resize_h: 输出图的height
:return:
"""
assert location_type in ('LT', 'RT', 'LB', 'RB', 'CC'), 'must have a location .'
is_gray = False
if len(img.shape) == 3:
h, w, c = img.shape
elif len(img.shape) == 2:
h, w = img.shape
is_gray = True
crop_w, crop_h = int(w * box_ratio[0]), int(h * box_ratio[1])
crop_img = np.zeros([10, 10])
if location_type == 'LT':
crop_img = img[:crop_h, :crop_w, :] if not is_gray else img[:crop_h, :crop_w]
elif location_type == 'RT':
crop_img = img[:crop_h:, w - crop_w:, :] if not is_gray else img[:crop_h:, w - crop_w:]
elif location_type == 'LB':
crop_img = img[h - crop_h:, :crop_w, :] if not is_gray else img[h - crop_h:, :crop_w]
elif location_type == 'RB':
crop_img = img[h - crop_h:, w - crop_w:, :] if not is_gray else img[h - crop_h:, w - crop_w:]
elif location_type == 'CC':
start_h = (h - crop_h) // 2
start_w = (w - crop_w) // 2
crop_img = img[start_h:start_h + crop_h, start_w:start_w + crop_w, :] if not is_gray else img[
start_h:start_h + crop_h,
start_w:start_w + crop_w]
resize = cv2.resize(crop_img, (resize_w, resize_h))
return resize
horizontal flip Flip
DEF random_flip (IMG, MODE =. 1):
"" "
randomly flipping
: param IMG:
: param Model: horizontal flip. 1 = / 0 = vertical / horizontal and vertical = -1
: return:
" ""
Assert MODE in (0,. 1, -1), "MODE IS Not right"
Flip np.random.choice = (2) * 2 -. 1 # -1 /. 1
IF MODE ==. 1:
IMG IMG = [Flip :, :: ,:]
elif MODE == 0:
IMG IMG = [Flip ::,:,:]
elif MODE == -1:
IMG IMG = [:: Flip, Flip ::,:]
return IMG
DEF Flip (IMG, MODE = 1):
"" "
flip
: param IMG:
: param MODE: = 1 inverted horizontal / vertical = 0 / -1 = horizontal vertical
: return:
"""
MODE in Assert (0,. 1, -1), "MODE IS Not right"
return cv2.flip (IMG, MODE = flipCode)
2 offline mode
2.1 random perturbation
noise (Gaussian, custom) Noise
DEF random_noise (IMG, rand_range = (3, 20)):
"" "
random noise
: param IMG:
: param rand_range: (min, max)
: return:
" ""
IMG = np.asarray (IMG, np.float)
Sigma = the random.randint (* rand_range)
NOSIE np.random.normal = (0, Sigma, size = img.shape)
IMG = + NOSIE
IMG = np.uint8 (np.clip (IMG, 0,255))
return img
Filter (Gaussian smoothing, mean, median, minimum and maximum values, bilateral, guided motion)
# describes various filtering principle: HTTPS: //blog.csdn.net/hellocsz/article/details/80727972
DEF gaussianBlue (IMG, = KS (. 7,. 7), for 1.5 STDEV =):
"" "
Gaussian blur, the image may be smoothed to remove sharp noise
: param IMG:
: param KS: convolution kernel
: param stdev: standard deviation
: return:
" ""
return cv2.GaussianBlur (IMG, (. 7,. 7), for 1.5)
2.2 convert
rotational rorate
DEF Rotate (IMG, angle, Scale = 1.0):
"" "
rotation
: param IMG:
: param angle: rotation angle> 0 counter-clockwise,
: param Scale:
:return:
"" "
height, width = img.shape [: 2] # image acquired width and height of
center = (width / 2, height / 2) # image taken midpoint
M = cv2.getRotationMatrix2D (center, angle, scale) # obtain an image around a point of rotation matrix
the second argument # cv2.warpAffine () is a transformation matrix, the third parameter is the size of an output image
rotated = cv2.warpAffine (img, M , (height, width))
return Rotated
DEF random_rotate (IMG, angle_range = (- 10, 10)):
"" "
random rotation
: param IMG:
: param angle_range: rotational angle range (min, max)> 0 indicates counterclockwise ,
: return:
" ""
height, width = img.shape [: 2] # Get the height and width of the image
center = (width / 2, height / 2) # midpoint image taking
angle = random.randrange (* angle_range 1)
M = cv2.getRotationMatrix2D (center, angle, 1.0) # obtain an image around a point of rotation matrix
# cv2.warpAffine () of the second parameter is a transformation matrix, the third parameter is the size of an output image
rotated = cv2. warpAffine (IMG, M, (height, width))
return Rotated
offset Shift
DEF Shift (IMG, x_offset, y_offset):
"" "
shift, right, and down
: param IMG:
: param x_offset:> 0 to the right side indicates shift px, <0 represents a leftward
: param y_offset:> 0 represents an offset downwardly px <0 indicates an upward
: return:
"" "
H, W, _ = img.shape
M = np.array ([[. 1, 0, x_offset], [0, . 1, y_offset]], DTYPE = np.float)
return cv2.warpAffine (IMG, M, (W, H))
twisted skew
...
zoom scale
DEF resize_img (IMG, resize_w, resize_h):
height, width = IMG. shape [: 2] # acquired image height and width
return cv2.resize (IMG, (resize_w, resize_h), interpolation = cv2.INTER_CUBIC)
the RGB / BGR-> the HSV
DEF rgb2hsv_py (R & lt, G, B):
# from HTTPS : //blog.csdn.net/weixin_43360384/article/details/84871521
r, g, b = r/255.0, g/255.0, b/255.0
mx = max(r, g, b)
mn = min(r, g, b)
m = mx-mn
if mx == mn:
h = 0
elif mx == r:
if g >= b:
h = ((g-b)/m)*60
else:
h = ((g-b)/m)*60 + 360
elif mx == g:
h = ((b-r)/m)*60 + 120
elif mx == b:
h = ((r-g)/m)*60 + 240
if mx == 0:
s = 0
else:
s = m/mx
v = mx
return h, s, v
def rgb2hsv_cv(img):
# from https://blog.csdn.net/qq_38332453/article/details/89258058
h = img.shape[0]
w = img.shape[1]
H = np.zeros((h,w),np.float32)
S = np.zeros((h, w), np.float32)
V = np.zeros((h, w), np.float32)
r,g,b = cv2.split(img)
r, g, b = r/255.0, g/255.0, b/255.0
for i in range(0, h):
for j in range(0, w):
mx = max((b[i, j], g[i, j], r[i, j]))
mn = min((b[i, j], g[i, j], r[i, j]))
V[i, j] = mx
if V[i, j] == 0:
S[i, j] = 0
else:
S[i, j] = (V[i, j] - mn) / V[i, j]
if mx == mn:
H[i, j] = 0
elif V[i, j] == r[i, j]:
if g[i, j] >= b[i, j]:
H [I, J] = (60 * ((G [I, J]) - B [I, J]) / (V [I, J] - Mn))
the else:
H [I, J] = (60 * ((G [I, J]) - B [I, J]) / (V [I, J] - Mn)) + 360
elif V [I, J] == G [I, J]:
H [ I, J] = 60 * ((B [I, J]) - R & lt [I, J]) / (V [I, J] - Mn) + 120
elif V [I, J] == B [I, J]:
H [I, J] = 60 * ((R & lt [I, J]) - G [I, J]) / (V [I, J] - Mn) + 240
"" "
G (X) = (. 1 - [alpha]) F0 (X) + αf1 (X) a # → (0,1) of a different value You may achieve different effects
H[i,j] = H[i,j] / 2
return H, S, V
image overlay fusion
DEF addWeight (srcl, Alpha, SRC2, Beta, Gamma):
DST = srcl * Alpha + SRC2 * Beta + Gamma
: param srcl: IMG1
: param Alpha:
: param SRC2: IMG2
: Beta param:
: param Gamma:
: return:
"" "
Assert src1.shap == src2.shape
return cv2.addWeighted (srcl, Alpha, SRC2, Beta, Gamma)
dithering (luminance \ color \ saturation \ contrast) Jitter Color
DEF adjust_contrast_bright (IMG, Contrast = 1.2, Brightness = 100):
"" "
adjust the brightness and contrast
dst = img * Brightness contrast +
: param IMG:
: param contrast: contrast the larger brighter
: param brightness: brightness 100 ~ 0
: return:
"" "
# 0-255 exceeds the pixel value, so that it interrupts
return np.uint8 (np.clip ((+ Brightness Contrast * IMG), 0, 255))
DEF pytorch_color_jitter (IMG):
return torchvision.transforms.ColorJitter (Brightness = 0, Contrast = 0, = 0 Saturation, Hue = 0)
3D geometric transformation