import numpy as np | |
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 = 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[0]), np.arange(shape[1]), 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 distored_image.reshape(image.shape) |
This comment has been minimized.
This comment has been minimized.
jdelange
commented
Sep 2, 2016
Nice! What are good values for alpha and sigma? I assume the alpha from the original paper (a=8) cannot be directly translated to this implementation? |
This comment has been minimized.
This comment has been minimized.
mamrehn
commented
Nov 24, 2016
Thanks! |
This comment has been minimized.
This comment has been minimized.
lgy1425
commented
Apr 17, 2017
Thank you. But should Input Image be square? |
This comment has been minimized.
This comment has been minimized.
iliya-hajjar
commented
Nov 19, 2017
How can I save this distored image ? |
This comment has been minimized.
This comment has been minimized.
l770943527
commented
Feb 26, 2018
•
Hi, I have load a RGB img whose shape is (400, 248, 3), but I have got an error
can anyone help ? THX!!! |
This comment has been minimized.
This comment has been minimized.
iver56
commented
Oct 15, 2018
•
This comment has been minimized.
This comment has been minimized.
bigfred76
commented
Jan 17, 2019
you need to invert the shapes in the resolution of x,y,z : |
This comment has been minimized.
This comment has been minimized.
bigfred76
commented
Jan 17, 2019
•
This comment has been minimized.
This comment has been minimized.
Rsalganik1123
commented
Mar 4, 2019
Hello I get an error: tuple index out of range |
This comment has been minimized.
This comment has been minimized.
noorulhasan06
commented
Apr 13, 2019
try to print the shape and you will find out that the shape is something like [x,y] not [x,y,z]. |
This comment has been minimized.
erniejunior commentedJan 11, 2016
This version also supports color images (3 RGB channels).