gvtulder / elasticdeform

Differentiable elastic deformations for N-dimensional images (Python, SciPy, NumPy, TensorFlow, PyTorch).
Other
186 stars 25 forks source link

Is this method suitable for multi-channel images? #1

Closed joe1chief closed 6 years ago

joe1chief commented 6 years ago

input image Run following codes on the above image, I get an odd result. result


X = imageio.imread('test_X.jpg')

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)```
gvtulder commented 6 years ago

By default, the deformation is applied to every dimension of the input. So in your RGB image, there's also a deformation between the colour channels. That looks odd.

I've pushed an update that adds an axis parameter that you can use to specify the axes that you want to deform. For your example, that would look like this:

X = imageio.imread('testgrid.jpg')
X_deformed = elasticdeform.deform_random_grid(X.astype('float64'),
                                              sigma=25, points=3, axis=(0, 1))
imageio.imsave('test_X.png', X)
imageio.imsave('test_X_deformed.png', X_deformed.clip(0, 255).astype('uint8'))

It might be useful to convert the image to a float before computing the deformation, since the uint8 will give you strange rounding and overflow errors. If you do the interpolation in floats the result looks much better:

test_x_deformed

joe1chief commented 6 years ago

Thanks a lot!