Last active
September 6, 2021 08:31
-
-
Save chsasank/4ee2f802cd48f3260f81798d304fb057 to your computer and use it in GitHub Desktop.
Elastic transformation/deformation of an image in Torch
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is based on https://gist.github.com/fmder/e28813c1e8721830ff9c