Skip to content

Instantly share code, notes, and snippets.

@chsasank
Last active September 6, 2021 08:31
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chsasank/4ee2f802cd48f3260f81798d304fb057 to your computer and use it in GitHub Desktop.
Save chsasank/4ee2f802cd48f3260f81798d304fb057 to your computer and use it in GitHub Desktop.
Elastic transformation/deformation of an image in Torch
require 'image'
function ElasticTransform(img, alpha, sigma)
--[[
Parameters
----------
img: Tensor of size KxHxW
Image on which elastic transformation have to be applied
alpha: number
Intensity of the transformation
sigma: number
Sigma for smoothing the transformation. Larger alphas require larger sigmas
Returns
-------
img: Tensor of size KxHxW
Image with elastic deformations appiled
--]]
H = img:size(2)
W = img:size(3)
filterSize = math.max(5,math.ceil(3*sigma))
flow = torch.rand(2, H, W)*2 - 1
kernel = image.gaussian(filterSize, sigma, 1, true)
flow = image.convolve(flow, kernel, 'same')*alpha
img = image.warp(img, flow)
return img
end
@chsasank
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment