import numpy as np
import tensorflow as tf
import cv2 as cv
img = cv.imread(img_path)
作用: 如字面意思
img = tf.image.flip_up_down(img).numpy()
作用: 如字面意思
img = tf.image.flip_left_right(img).numpy()
作用: 对原始影像放大1.2-1.5倍,然后随机选择一个区域作为处理后的影像数据,区域大小与原始输入一致。
IMG_SIZE = (512,512) # 原始影像尺寸
def up_scaling(img):
'SCALE:1.2~1.5'
scale = np.random.randint(20,50) / 100.0 + 1
img_h = IMG_SIZE[0]
img_w = IMG_SIZE[1]
img_h_new = int(img_h * scale)
img_w_new = int(img_w * scale)
start_h = np.random.randint(0, img_h_new - img_h - 1)
start_w = np.random.randint(0, img_w_new - img_w - 1)
new_img = cv.resize(img, dsize=(img_h_new, img_w_new))
img = new_img[start_h:start_h + IMG_SIZE[0], start_w:start_w+IMG_SIZE[1],:]
return img
作用: 对原始影像缩小0.5-0.8倍,然后随机选择一个区域作为处理后的影像数据,区域大小与原始输入一致。
IMG_SIZE = (512,512) # 原始影像尺寸
def down_scaling(img):
'SCALE:0.5~0.8'
scale = np.random.randint(50, 80) / 100.0
img_h = IMG_SIZE[0]
img_w = IMG_SIZE[1]
img_h_new = int(img_h * scale)
img_w_new = int(img_w * scale)
start_h = np.random.randint(0, img_h - img_h_new - 1)
start_w = np.random.randint(0, img_w - img_w_new - 1)
new_img = np.ones(shape=IMG_SIZE, dtype=np.int8) * 128
img1 = cv.resize(img, dsize=(img_w_new, img_h_new))
new_img[start_h:start_h + img_h_new, start_w:start_w+img_w_new,:] = img1
return new_img
作用: 将随机的两张样本按比例混合,分类的结果按比例分配
def mixup(img1, img2, alpha):
img = cv.addWeighted(src1=img1, alpha=alpha, src2=img2, beta=1-alpha, gamma=0)
return img
作用: 随机的将样本中的部分区域裁剪掉,并且填充0像素值
def cutout(img):
h, w, c = img.shape
random_x = np.random.randint(int(w * 0.75))
random_y = np.random.randint(int(h * 0.75))
random_w = np.random.randint(int(w * 0.25))
random_h = np.random.randint(int(h * 0.25))
img[random_y:random_y + random_h, random_x:random_x + random_w, :] = [0,0,0]
return img