图像弹性变换算法的实现

原文: 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)

 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值