# 干货 | 图像数据增强实战

【磐创AI导读】：本文讲解了图像数据增强实战。想要获取更多的机器学习、深度学习资源，欢迎大家点击上方蓝字关注我们的公众号：磐创AI。

``# 加载图像from PIL import Imageimport numpy as npimport matplotlib.pyplot as pltimg = Image.open('./NIKE.png')img = np.array(img)plt.imshow(img)plt.show()``

``# 用 Numpy 翻转flipped_img = np.fliplr(img)plt.imshow(flipped_img)plt.show()``

``# 向左平移for i in range(HEIGHT, 1, -1):  for j in range(WIDTH):    if (i < HEIGHT-20):      img[j][i] = img[j][i-20]    elif (i < HEIGHT-1):      img[j][i] = 0plt.imshow(img)plt.show()``

``# 向右平移for j in range(WIDTH): for i in range(HEIGHT):  if (i < HEIGHT-20):    img[j][i] = img[j][i+20]plt.imshow(img)plt.show()``

``# 向上平移for j in range(WIDTH): for i in range(HEIGHT):  if (j < WIDTH - 20 and j > 20):    img[j][i] = img[j+20][i]  else:    img[j][i] = 0plt.imshow(img)plt.show()``

``# 向下平移for j in range(WIDTH, 1, -1): for i in range(278):  if (j < 144 and j > 20):    img[j][i] = img[j-20][i]plt.imshow(img)plt.show()``

``# 添加噪声noise = np.random.randint(5, size = (164,278, 4), dtype = 'uint8')for i in range(WIDTH):   for j in range(HEIGHT):        for k in range(DEPTH):           if (img[i][j][k] != 255):                img[i][j][k] += noise[i][j][k]plt.imshow(img)plt.show()``

翻译自：https://towardsdatascience.com/image-augmentation-examples-in-python-d552c26f2873

点击下方 |  | 了解更多