image——Data Augmentation的代码

来源:互联网 发布:王作强鱼线淘宝店 编辑:程序博客网 时间:2024/06/06 09:24


切片(crop):

def crop(image, random_crop, image_size):    if image.shape[1]>image_size:        sz1 = int(image.shape[1]//2)        sz2 = int(image_size//2)        if random_crop:            diff = sz1-sz2            (h, v) = (np.random.randint(-diff, diff+1), np.random.randint(-diff, diff+1))        else:            (h, v) = (0,0)        image = image[(sz1-sz2+v):(sz1+sz2+v),(sz1-sz2+h):(sz1+sz2+h),:]    return image


############################################################################# 函数:crop# 描述:随机裁剪图像## 输入:图像image, crop_size# 返回:图像image############################################################################def crop(image, crop_size, random_crop=True):    if random_crop:  # 若随机裁剪        if image.shape[1] > crop_size:            sz1 = image.shape[1] // 2            sz2 = crop_size // 2            diff = sz1 - sz2            (h, v) = (np.random.randint(0, diff + 1), np.random.randint(0, diff + 1))            image = image[v:(v + crop_size), h:(h + crop_size), :]    return image





# 左右上下翻转

def flip(image, random_flip=True):    if random_flip and np.random.choice([True, False]):        image = np.fliplr(image)    if random_flip and np.random.choice([True, False]):        image = np.flipud(image)    return image



#图像旋转

def random_rotate_image(image):    angle = np.random.uniform(low=-10.0, high=10.0)    return misc.imrotate(image, angle, 'bicubic')


############################################################################# 函数:rotation# 描述:随机旋转图片,增强数据,用图像边缘进行填充。## 输入:图像image# 返回:图像image############################################################################def rotation(image, random_flip=True):    if random_flip and np.random.choice([True, False]):        w,h = image.shape[1], image.shape[0]        # 0-180随机产生旋转角度。        angle = np.random.randint(0,180)        RotateMatrix = cv2.getRotationMatrix2D(center=(image.shape[1]/2, image.shape[0]/2), angle=angle, scale=0.7)        # image = cv2.warpAffine(image, RotateMatrix, (w,h), borderValue=(129,137,130))        #image = cv2.warpAffine(image, RotateMatrix, (w,h),borderValue=(129,137,130))        image = cv2.warpAffine(image, RotateMatrix, (w,h),borderMode=cv2.BORDER_REPLICATE)    return image



图像归一化处理:

def prewhiten(x):    mean = np.mean(x)    std = np.std(x)    std_adj = np.maximum(std, 1.0/np.sqrt(x.size))    y = np.multiply(np.subtract(x, mean), 1/std_adj)    return y



图像平移:

############################################################################# 函数:translation# 描述:随机平移图片,增强数据,用图像边缘进行填充。## 输入:图像image# 返回:图像image############################################################################def translation(image, random_flip=True):    if random_flip and np.random.choice([True, False]):        w,h = 1920, 1080        H1 = np.float32([[1,0],[0,1]])        H2 = np.random.uniform(50,500, [2,1])        H = np.hstack([H1, H2])        # H = np.float32([[1,0,408],[0,1,431]])        print (H)        image = cv2.warpAffine(image, H, (w,h), borderMode=cv2.BORDER_REPLICATE)    return image




调整光照

from skimage import exposureimport numpy as npdef gen_exposure(image, random_xp=True):    if random_xp and np.random.choice([True, False]):        image = exposure.adjust_gamma(image, 1.2) # 调暗    if random_xp and np.random.choice([True, False]):        image = exposure.adjust_gamma(image, 1.5) # 调暗    if random_xp and np.random.choice([True, False]):        image = exposure.adjust_gamma(image, 0.9) # 调亮    if random_xp and np.random.choice([True, False]):        image = exposure.adjust_gamma(image, 0.8) # 调亮    if random_xp and np.random.choice([True, False]):        image = exposure.adjust_gamma(image, 0.7) # 调暗    return image









原创粉丝点击