Skip to content

Instantly share code, notes, and snippets.

@AlexanderSavochkin
Forked from chsasank/elastic.lua
Created August 31, 2016 05:00
Show Gist options
  • Save AlexanderSavochkin/5b317e36d39a9b10d40e2230ec7110bd to your computer and use it in GitHub Desktop.
Save AlexanderSavochkin/5b317e36d39a9b10d40e2230ec7110bd 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment