方法一

原理:

该过程为每个网格点生成一个随机位移的粗位移网格。然后对这个网格进行插值,以计算输入图像中每个像素的位移。然后使用位移矢量和样条插值对输入图像进行变形。

文章

方法二

详见文章

原理:

文章

Elastic deformation of images as described in [Simard2003]_.
[Simard2003] Simard, Steinkraus and Platt, "Best Practices for
Convolutional Neural Networks applied to Visual Document Analysis", in
Proc. of the International Conference on Document Analysis and
Recognition, 2003.

实现

https://gist.github.com/erniejunior/601cdf56d2b424757de5

评测

效果图

原图 实现一 实现二
无参数 sigma=5, points=3 alpha=256, sigma=5

时间效率

实现一

In [2]: %timeit X_deformed = Image.fromarray(elasticdeform.deform_random_grid(np.array(X, dtype='float32'), sigma=5, points=3, a
   ...: xis=(0, 1)).clip(0, 255).astype('uint8'))
   ...:
5.98 ms ± 34.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

实现二

In [5]: %timeit elastic_transform(X)
8.22 ms ± 129 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

附录:测试脚本

import elasticdeform
import numpy as np
from scipy.ndimage.interpolation import map_coordinates
from scipy.ndimage.filters import gaussian_filter
from PIL import Image
X = Image.open('D:\\for_test.jpg')

# apply deformation with a random 3 x 3 grid
X_deformed = Image.fromarray(elasticdeform.deform_random_grid(np.array(X, dtype='float32'), sigma=5, points=3, axis=(0, 1)).clip(0, 255).astype('uint8'))

def elastic_transform(image, alpha=256, sigma=5, random_state=None):
    """Elastic deformation of images as described in [Simard2003]_.
    .. [Simard2003] Simard, Steinkraus and Platt, "Best Practices for
       Convolutional Neural Networks applied to Visual Document Analysis", in
       Proc. of the International Conference on Document Analysis and
       Recognition, 2003.
    """
    image = np.array(image, dtype='uint8')
    if random_state is None:
        random_state = np.random.RandomState(None)

    shape = image.shape
    dx = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma, mode="constant", cval=0) * alpha
    dy = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma, mode="constant", cval=0) * alpha
    dz = np.zeros_like(dx)

    x, y, z = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]), np.arange(shape[2]))
    # print(x.shape)
    indices = np.reshape(y+dy, (-1, 1)), np.reshape(x+dx, (-1, 1)), np.reshape(z, (-1, 1))

    distored_image = map_coordinates(image, indices, order=1, mode='reflect')
    return Image.fromarray(distored_image.reshape(image.shape))

X.save('test_X.png')
X_deformed.save('test_X_deformed.png')

X_deformed_2 = elastic_transform(X)
X_deformed_2.save('test_X_deformed_2.png')
最后修改日期: 2020年2月29日

作者

留言

撰写回覆或留言

发布留言必须填写的电子邮件地址不会公开。