Skip to content

Instantly share code, notes, and snippets.

@kevin369ml
Forked from chsasank/elastic_transform.py
Created September 19, 2017 20:40
Show Gist options
  • Select an option

  • Save kevin369ml/05dfb3a66655756316da3412ee8c7a4d to your computer and use it in GitHub Desktop.

Select an option

Save kevin369ml/05dfb3a66655756316da3412ee8c7a4d to your computer and use it in GitHub Desktop.

Revisions

  1. @chsasank chsasank revised this gist Jul 27, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion elastic_transform.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    import numpy
    import numpy as np
    from scipy.ndimage.interpolation import map_coordinates
    from scipy.ndimage.filters import gaussian_filter

  2. @chsasank chsasank revised this gist Jul 27, 2016. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions elastic_transform.py
    Original file line number Diff line number Diff line change
    @@ -11,14 +11,17 @@ def elastic_transform(image, alpha, sigma, random_state=None):
    Proc. of the International Conference on Document Analysis and
    Recognition, 2003.
    """
    assert len(image.shape)==2

    if random_state is None:
    random_state = numpy.random.RandomState(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

    x, y = numpy.meshgrid(numpy.arange(shape[0]), numpy.arange(shape[1]))
    indices = numpy.reshape(y+dy, (-1, 1)), numpy.reshape(x+dx, (-1, 1))

    x, y = np.meshgrid(np.arange(shape[0]), np.arange(shape[1]), indexing='ij')
    indices = np.reshape(x+dx, (-1, 1)), np.reshape(y+dy, (-1, 1))
    return map_coordinates(image, indices, order=1).reshape(shape)
  3. @fmder fmder revised this gist Aug 21, 2015. No changes.
  4. @fmder fmder created this gist Aug 21, 2015.
    24 changes: 24 additions & 0 deletions elastic_transform.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    import numpy
    from scipy.ndimage.interpolation import map_coordinates
    from scipy.ndimage.filters import gaussian_filter

    def elastic_transform(image, alpha, sigma, 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.
    """
    if random_state is None:
    random_state = numpy.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

    x, y = numpy.meshgrid(numpy.arange(shape[0]), numpy.arange(shape[1]))
    indices = numpy.reshape(y+dy, (-1, 1)), numpy.reshape(x+dx, (-1, 1))

    return map_coordinates(image, indices, order=1).reshape(shape)