原文: https://blog.csdn.net/lhanchao/article/details/54234490
我只是实现了Python版本的该算法
from PIL import Image, ImageFilter
import numpy as np
import math
import os
def augment(old_file, new_file):
# 打开图片
# image = Image.open(r'F:\TJ AI-Food\test.png')
image = Image.open(old_file)
# 创建一个相等大小的空白图作为结果
result = Image.new('RGB', (image.width, image.height), (255, 255, 255))
# 创建一个跟图片大小相等的空白图片(1通道)
arr = np.random.rand(image.height, image.width) * 200
delta_x = Image.fromarray(arr, mode='RGB')
arr = np.random.rand(image.height, image.width) * 200
delta_y = Image.fromarray(arr, mode='RGB')
# 对delta_x和delta_y进行高斯模糊
delta_x = delta_x.filter(ImageFilter.GaussianBlur(radius=4))
delta_y = delta_y.filter(ImageFilter.GaussianBlur(radius=4))
# 像素平移
for i in range(image.width):
for j in range(image.height):
x_low = i + math.floor((delta_x.getpixel((i, j))[0] / 256 * 2 - 1) * 20)
x_high = i + math.ceil((delta_x.getpixel((i, j))[0] / 256 * 2 - 1) * 20)
y_low = j + math.floor((delta_y.getpixel((i, j))[0] / 256 * 2 - 1) * 20)
y_high = j + math.ceil((delta_y.getpixel((i, j))[0] / 256 * 2 - 1) * 20)
def bound(value, max_value):
if value < 0: return 0
if value >= max_value: return max_value - 1
return value
x_low = bound(x_low, image.width)
x_high = bound(x_high, image.width)
y_low = bound(y_low, image.height)
y_high = bound(y_high, image.height)
new_pixel = (int((image.getpixel((x_low, y_low))[0] + image.getpixel((x_low, y_high))[0] + image.getpixel((x_high, y_low))[0] + image.getpixel((x_high, y_high))[0]) / 4),
int((image.getpixel((x_low, y_low))[1] + image.getpixel((x_low, y_high))[1] + image.getpixel((x_high, y_low))[1] + image.getpixel((x_high, y_high))[1]) / 4),
int((image.getpixel((x_low, y_low))[2] + image.getpixel((x_low, y_high))[2] + image.getpixel((x_high, y_low))[2] + image.getpixel((x_high, y_high))[2]) / 4))
sum_val = (bound(new_pixel[0], 256), bound(new_pixel[1], 256), bound(new_pixel[2], 256))
# put pixel at (i, j)
result.putpixel((i, j), sum_val)
result.save(new_file)