Skip to main content

Elastic deformations for N-D images.

Project description

Elastic deformations for N-dimensional images (Python, SciPy, NumPy, TensorFlow, PyTorch)

Documentation Status Test Build DOI

This library implements elastic grid-based deformations for N-dimensional images.

The elastic deformation approach is described in

The procedure generates a coarse displacement grid with a random displacement for each grid point. This grid is then interpolated to compute a displacement for each pixel in the input image. The input image is then deformed using the displacement vectors and a spline interpolation.

In addition to the normal, forward deformation, this package also provides a function that can backpropagate the gradient through the deformation. This makes it possible to use the deformation as a layer in a convolutional neural network. For convenience, TensorFlow and PyTorch wrappers are provided in elasticdeform.tf and elasticdeform.torch.

Installation

pip install elasticdeform
or
pip install git+https://github.com/gvtulder/elasticdeform

This library requires Python 3 and NumPy development headers.

On Windows, try to install the precompiled binaries directly using pip install elasticdeform. If that does not work, these precompiled packages might be an alternative option.

Examples

This basic example deforms an image with a random 3 x 3 deformation grid:

import numpy, imageio, elasticdeform
X = numpy.zeros((200, 300))
X[::10, ::10] = 1

# apply deformation with a random 3 x 3 grid
X_deformed = elasticdeform.deform_random_grid(X, sigma=25, points=3)

imageio.imsave('test_X.png', X)
imageio.imsave('test_X_deformed.png', X_deformed)

Multiple inputs

If you have multiple images, e.g., an image and a segmentation image, you can deform both simultaneously by providing a list of inputs. You can specify a different spline order for each input.

# apply deformation to inputs X and Y
[X_deformed, Y_deformed] = elasticdeform.deform_random_grid([X, Y])

# apply deformation to inputs X and Y,
# with a different interpolation for each input
[X_deformed, Y_deformed] = elasticdeform.deform_random_grid([X, Y], order=[3, 0])

Multi-channel images

By default, a deformation will be applied to every dimension of the input. If you have multi-channel images, you can use the axis parameter to specify which axes should be deformed. The same deformation will be applied for each channel.

For example, to deform an RGB image across the first two dimensions, run:

X_deformed = elasticdeform.deform_random_grid(X, axis=(0, 1))

When deforming multiple inputs, you can provide a tuple of axes for each input:

X = numpy.random.rand(3, 200, 300)
Y = numpy.random.rand(200, 300)
[X_deformed, Y_deformed] = elasticdeform.deform_random_grid([X, Y], axis=[(1, 2), (0, 1)])

Cropping

If you intend to crop a small subpatch from the deformed image, you can provide the crop dimensions to the deform function. It will then compute only the cropped output pixels, while still computing the deformation grid based on the full image dimensions. This saves computation time.

X = numpy.random.rand(200, 300)

# define a crop region
crop = (slice(50, 150), slice(0, 100))

# generate a deformation grid
displacement = numpy.random.randn(2, 3, 3) * 25

# deform full image
X_deformed = elasticdeform.deform_grid(X, displacement)
# compute only the cropped region
X_deformed_crop = elasticdeform.deform_grid(X, displacement, crop=crop)

# the deformation is the same
numpy.testing.assert_equal(X_deformed[crop], X_deformed_crop)

Rotate and zoom

The deformation functions accept rotate and zoom parameters, which allows you to combine the elastic deformation with rotation and scaling. This can be useful as data augmentation step. The rotation and zoom are applied to the output coordinates, using the center pixel of the output patch as the origin.

# apply deformation with a random 3 x 3 grid,
# rotate by 30 degrees and rescale with a factor 1.5
X_deformed = elasticdeform.deform_random_grid(X, sigma=25, points=3,
                                              rotate=30, zoom=1.5)

Note that the output shape remains the same. The mapping of the input to the output is rotated within the given output frame.

Rotate and zoom can be combined with the crop argument. In that case, the scaling and rotation is performed relative to the center of the cropped output.

For more advanced transformations, it is also possible to provide an affine transformation matrix directly.

Gradient

The deform_grid_gradient function can be used to backpropagate the gradient of the output with respect to the input. Call deform_grid_gradient with the parameters that were used for the forward step.

X = numpy.random.rand(200, 300)

# generate a deformation grid
displacement = numpy.random.randn(2, 3, 3) * 25

# perform forward deformation
X_deformed = elasticdeform.deform_grid(X, displacement)

# obtain the gradient w.r.t. X_deformed (e.g., with backpropagation)
dX_deformed = numpy.random.randn(*X_deformed.shape)

# compute the gradient w.r.t. X
dX = elasticdeform.deform_grid_gradient(dX_deformed, displacement)

Note: The gradient function will assume that the input has the same size as the output. If you used the crop parameter in the forward phase, it is necessary to provide the gradient function with the original, uncropped input shape in the X_shape parameter.

TensorFlow wrapper

The elasticdeform.tf module provides a wrapper for deform_grid in TensorFlow. The function uses TensorFlow Tensors as input and output, but otherwise uses the same parameters.

import numpy
import elasticdeform.tf as etf

displacement_val = numpy.random.randn(2, 3, 3) * 5
X_val = numpy.random.rand(200, 300)
dY_val = numpy.random.rand(200, 300)

# construct TensorFlow input and top gradient
displacement = tf.Variable(displacement_val)
X = tf.Variable(X_val)
dY = tf.Variable(dY_val)

# the deform_grid function is similar to the plain Python equivalent,
# but it accepts and returns TensorFlow Tensors
X_deformed = etf.deform_grid(X, displacement, order=3)

# the gradient w.r.t. X can be computed in the normal TensorFlow manner
[dX] = tf.gradients(X_deformed, X, dY)

PyTorch wrapper

The elasticdeform.torch module provides a wrapper for deform_grid in PyTorch. The function uses PyTorch Tensors as input and output, but otherwise uses the same parameters.

import numpy
import elasticdeform.torch as etorch

displacement_val = numpy.random.randn(2, 3, 3) * 5
X_val = numpy.random.rand(200, 300)
dY_val = numpy.random.rand(200, 300)

# construct PyTorch input and top gradient
displacement = torch.tensor(displacement_val)
X = torch.tensor(X_val, requires_grad=True)
dY = torch.tensor(dY_val)

# the deform_grid function is similar to the plain Python equivalent,
# but it accepts and returns PyTorch Tensors
X_deformed = etorch.deform_grid(X, displacement, order=3)

# the gradient w.r.t. X can be computed in the normal PyTorch manner
X_deformed.backward(dY)
print(X.grad)

License information

This library was written by Gijs van Tulder at the Biomedical Imaging Group Rotterdam, Erasmus MC, Rotterdam, the Netherlands

It is inspired by a similar, Python-based implementation by Florian Calvet. This C-based implementation gives the same results, but is faster and has a gradient implementation.

This C implementation includes a modified version of the NI_GeometricTransform from SciPy's ndimage library.

This code is made available under the BSD license. See LICENSE.txt for details.

If you want to cite this library, please see DOI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

elasticdeform-0.5.1.tar.gz (33.7 kB view details)

Uploaded Source

Built Distributions

elasticdeform-0.5.1-cp312-cp312-win_amd64.whl (32.1 kB view details)

Uploaded CPython 3.12 Windows x86-64

elasticdeform-0.5.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (93.6 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

elasticdeform-0.5.1-cp312-cp312-macosx_11_0_arm64.whl (34.7 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

elasticdeform-0.5.1-cp312-cp312-macosx_10_9_x86_64.whl (37.3 kB view details)

Uploaded CPython 3.12 macOS 10.9+ x86-64

elasticdeform-0.5.1-cp311-cp311-win_amd64.whl (32.0 kB view details)

Uploaded CPython 3.11 Windows x86-64

elasticdeform-0.5.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (93.1 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

elasticdeform-0.5.1-cp311-cp311-macosx_11_0_arm64.whl (34.7 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

elasticdeform-0.5.1-cp311-cp311-macosx_10_9_x86_64.whl (37.3 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

elasticdeform-0.5.1-cp310-cp310-win_amd64.whl (32.0 kB view details)

Uploaded CPython 3.10 Windows x86-64

elasticdeform-0.5.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (91.6 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

elasticdeform-0.5.1-cp310-cp310-macosx_11_0_arm64.whl (34.7 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

elasticdeform-0.5.1-cp310-cp310-macosx_10_9_x86_64.whl (37.3 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

elasticdeform-0.5.1-cp39-cp39-win_amd64.whl (32.0 kB view details)

Uploaded CPython 3.9 Windows x86-64

elasticdeform-0.5.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (91.3 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

elasticdeform-0.5.1-cp39-cp39-macosx_11_0_arm64.whl (34.7 kB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

elasticdeform-0.5.1-cp39-cp39-macosx_10_9_x86_64.whl (37.3 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

elasticdeform-0.5.1-cp38-cp38-win_amd64.whl (32.1 kB view details)

Uploaded CPython 3.8 Windows x86-64

elasticdeform-0.5.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (91.5 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

elasticdeform-0.5.1-cp38-cp38-macosx_11_0_arm64.whl (34.8 kB view details)

Uploaded CPython 3.8 macOS 11.0+ ARM64

elasticdeform-0.5.1-cp38-cp38-macosx_10_9_x86_64.whl (37.3 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

File details

Details for the file elasticdeform-0.5.1.tar.gz.

File metadata

  • Download URL: elasticdeform-0.5.1.tar.gz
  • Upload date:
  • Size: 33.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.19

File hashes

Hashes for elasticdeform-0.5.1.tar.gz
Algorithm Hash digest
SHA256 5f7145d018b6fa1d5d352a2a6fd6140bb845e2a1df1a53a6604cfbd104d12a81
MD5 00e9120183c5662584b8c3aba719cc09
BLAKE2b-256 a8e397001ed966c4d3c91a1e0389ab2686fd01fa4c43e6aed0ec2c24aa8bf11d

See more details on using hashes here.

File details

Details for the file elasticdeform-0.5.1-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for elasticdeform-0.5.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 7e3a4671116984393e60c9373340944accd81a0f34b69b9063320e766b88d0cb
MD5 25f17e6680b63cdb455fa4a6c6763e75
BLAKE2b-256 39700681c8d322ff6061fd1978844f4504df9737bb7ef444e2cf9c12bafb2626

See more details on using hashes here.

File details

Details for the file elasticdeform-0.5.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for elasticdeform-0.5.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 995de482d022519f449980755d649a7f820795a858b43ca7f17437254e64fbfd
MD5 85a0e7ec129cbefdb6ae89a09f1c7e09
BLAKE2b-256 acd83519db9885bd148aa2a108c2e5d3a1341df2198e1805385de40f74f584ed

See more details on using hashes here.

File details

Details for the file elasticdeform-0.5.1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for elasticdeform-0.5.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4a226c1c552c7cf60ea05675d956c60347ac5ffe706e6f40984c446433b41659
MD5 2676dde2cf471eec40425dab2b70d166
BLAKE2b-256 8dd269cf0a819efd6b6db326c896592658e6abe2cace4937ce3561232b9f8c27

See more details on using hashes here.

File details

Details for the file elasticdeform-0.5.1-cp312-cp312-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for elasticdeform-0.5.1-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 27d98f213033f4b22fcd4c5b411154ae1d5d19ae33e3b2497c3198201b36716d
MD5 dea83b11dde87adc16e80b926325bb01
BLAKE2b-256 cb239f259872967e835a3b4d40f6fdb5ac8ba42e144d76dfbb74443f38909568

See more details on using hashes here.

File details

Details for the file elasticdeform-0.5.1-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for elasticdeform-0.5.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 f7caea0c897c2953ae810361f41df4f554b104168d945365e8d26e8955afeb3f
MD5 b19238d1fd4db8798561eff6886ce454
BLAKE2b-256 cb1ad00503e042b55525b3c4814eb3d0935b7c3baf4160d11628d2e7dced2e58

See more details on using hashes here.

File details

Details for the file elasticdeform-0.5.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for elasticdeform-0.5.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ebb3ff5223909c5b16d978a64ee2e39694d4417f1e8cc98d6c254fe2fab54094
MD5 2cfdcf7d81e15ad9066a5fa68ffc868b
BLAKE2b-256 0507b880b88558a2bcb062027aac7ae548b20dc8a3935a983a519a32c1786276

See more details on using hashes here.

File details

Details for the file elasticdeform-0.5.1-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for elasticdeform-0.5.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 151daf857245e952f602a39c1b169f7d65d1064e0b239b46ea9cceda814f22ad
MD5 673a83338135e4503afa01fe4f246a7a
BLAKE2b-256 14beabfc353749d02ced833839f9c94f28693889543a0ed9e9c6c310c59aba1d

See more details on using hashes here.

File details

Details for the file elasticdeform-0.5.1-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for elasticdeform-0.5.1-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 62f9e4077d4973c4975583bcc921c4599906951c3e042f7f55a5fb2437310cd7
MD5 277cb30e683145e2e65328ff4f4a3ad8
BLAKE2b-256 1552fd326b130b8cfb76069b52e8e14e3c3736667554fbcd10795474300d0868

See more details on using hashes here.

File details

Details for the file elasticdeform-0.5.1-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for elasticdeform-0.5.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 8a1e45c3a5f8e949ff36187288699642617cf9d35f8d92a6c45be9a718e2ac04
MD5 cfd14cd86b05d5ac85fc50ead8a19937
BLAKE2b-256 8ad1ac205d621ea4d0a1124428e02de0dfef14e03562d63905c473a91ff79295

See more details on using hashes here.

File details

Details for the file elasticdeform-0.5.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for elasticdeform-0.5.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 daf4419135929c3afb0d9ad8d01f84be58ed2406e7b7ea57bc91eed7b76be022
MD5 c063dd61564bebbf9f2f9e41c693a173
BLAKE2b-256 fc191895a0e7ed4a672a78a7dd02af52d0d84cb54fb504ddb252cdf3d9cb51fa

See more details on using hashes here.

File details

Details for the file elasticdeform-0.5.1-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for elasticdeform-0.5.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c25ae877a0bb9a5b85baad5c0aa61e5a0c3d7e882ac6abd622d318b8da10ef2d
MD5 ae49ec9d86d12acc8f3272f273b55e70
BLAKE2b-256 ba7582bc064b1379c54db7849457f1fb506b176baa0845d6ad1f049e2b5cc41d

See more details on using hashes here.

File details

Details for the file elasticdeform-0.5.1-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for elasticdeform-0.5.1-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 bed0c4d317984038917c39655214a0c51286922c773b97357f072fc26672acde
MD5 e9d04fa652128ba798aecbf1f8790e42
BLAKE2b-256 129d9e73b5318633e30141943281bd6ec8a047e0b1d36ed1e396a4ba06e42169

See more details on using hashes here.

File details

Details for the file elasticdeform-0.5.1-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for elasticdeform-0.5.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 928d57293a449a221e525f10e8f81ee42c2120725fa1dc91adeb7c2381961334
MD5 577f143c0e7632a3603f86dbe9a65b44
BLAKE2b-256 6b51449ba73936e8855da506b2beeb7d08e3cb11e983875c31d5713ce7ad37f5

See more details on using hashes here.

File details

Details for the file elasticdeform-0.5.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for elasticdeform-0.5.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1dc5031cce98c85fc8830254b1ae35a11a16e4ce65ee8a8eeadd49c5b5b392a7
MD5 377e391fa0de1d9f808c5b937bc90725
BLAKE2b-256 6f826e9e20457b90b3b4174191e9c2aab05d277bc4d12c173370ae97c353b346

See more details on using hashes here.

File details

Details for the file elasticdeform-0.5.1-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for elasticdeform-0.5.1-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 df2709a882fe036831a929ab5b64827e0898374cb8211318dac9ab87d8fdec9b
MD5 863624ab54e3507b4fafc3fea9f56a7d
BLAKE2b-256 a1d85b8d9125c59c805c1a47406d38ba675d1812231e484493fc7e7fcca90498

See more details on using hashes here.

File details

Details for the file elasticdeform-0.5.1-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for elasticdeform-0.5.1-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 14cebe9094b684808e7f72c0c73acae849c3cf0a534f809d35ad8aad29c71ffb
MD5 a0c1a6be12f8cf12e4eaa2a8e1cc6645
BLAKE2b-256 4d38349dafb7e8ab9e65d121b55d34dcb82377c0437013bfbf83f008271bfb87

See more details on using hashes here.

File details

Details for the file elasticdeform-0.5.1-cp38-cp38-win_amd64.whl.

File metadata

File hashes

Hashes for elasticdeform-0.5.1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 00e81ad072d62d45d68e12a4229098eec1ca28f896f39396ac0e0066848c54fb
MD5 53a6ad9480f5209a36c9df20b74639bf
BLAKE2b-256 685dc8fdb970f2ad1feba19aa5d6bf0c8e535f581e3c1246eff9510ef49052fd

See more details on using hashes here.

File details

Details for the file elasticdeform-0.5.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for elasticdeform-0.5.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 29443d52fac0b2694b5c51f4cdeaacc7d432dedb742861f56198734a0b483018
MD5 ee79662a6b0be45ed116d1fe7a95f26b
BLAKE2b-256 30a27dedda275c906aa3cff267bde28783e4777ba4c8831d1a358693a8d4164a

See more details on using hashes here.

File details

Details for the file elasticdeform-0.5.1-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for elasticdeform-0.5.1-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 27644bb99f69a6a946707376dec062c424b52c4cb547162b12662b2571b5372d
MD5 0e9d7ff99c345d1c0d71091e4ce3015e
BLAKE2b-256 3b890f5fb76ddd813908196a1fc624df0a0e376b364a9c677aa467fe744ebbfc

See more details on using hashes here.

File details

Details for the file elasticdeform-0.5.1-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for elasticdeform-0.5.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 0296789a3747a874aed56d8952845f238f3574ecf17efade9bcdca9714edc94a
MD5 7ffa7c9ecb5020ff3fe494bfd98bd376
BLAKE2b-256 e95ea63d1a635fef87c1df785a8c9bbbef8f7ea1cab2fc13e21fae740a7442a2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page