Last active
August 22, 2021 14:54
-
-
Save fmder/e28813c1e8721830ff9c to your computer and use it in GitHub Desktop.
Elastic transformation of an image in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
thanks for sharing.
Is it code working with dicom images?
A c++ implementation can be found here Implementation of elastic distortion algorithm in C++
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing this.
Any possibility you could use a license so I know if and how I can use it?