Search code examples
imageimage-processingluatorch

How to skew an image in Torch


I'm trying to skew an image for input into an NN, but I can't seem to figure out how to do it. There aren't too many libraries which seem to provide what I'm looking for.

Preferably it would work on the type torch.FloatTensor

edit: Actually, the function image.warp looks promising, but the warp_test.lua is not particularly helpful. I am just looking to skew the image in x by a varying amount


Solution

  • Here is a simplistic version:

    require 'torch'
    require 'image'
    
    local function skew(input, factor)
      local w, h  = input:size(3), input:size(2)
      local y     = torch.range(0, h - 1):view(h, 1):expand(h, w)
      local x     = torch.range(0, w - 1):view(1, w):expand(h, w)
      local field = torch.Tensor(2, h, w)
      field[1]    = y
      field[2]    = torch.add(x, factor or 0, y)
      return image.warp(input, field, "bilinear", false, "pad", 0)
    end
    
    local output = skew(image.lena(), 0.25)