iproc.lua 877 B

1234567891011121314151617181920212223242526
  1. local gm = require 'graphicsmagick'
  2. local image = require 'image'
  3. local iproc = {}
  4. function iproc.scale(src, width, height, filter)
  5. local t = "float"
  6. if src:type() == "torch.ByteTensor" then
  7. t = "byte"
  8. end
  9. filter = filter or "Box"
  10. local im = gm.Image(src, "RGB", "DHW")
  11. im:size(math.ceil(width), math.ceil(height), filter)
  12. return im:toTensor(t, "RGB", "DHW")
  13. end
  14. function iproc.padding(img, w1, w2, h1, h2)
  15. local dst_height = img:size(2) + h1 + h2
  16. local dst_width = img:size(3) + w1 + w2
  17. local flow = torch.Tensor(2, dst_height, dst_width)
  18. flow[1] = torch.ger(torch.linspace(0, dst_height -1, dst_height), torch.ones(dst_width))
  19. flow[2] = torch.ger(torch.ones(dst_height), torch.linspace(0, dst_width - 1, dst_width))
  20. flow[1]:add(-h1)
  21. flow[2]:add(-w1)
  22. return image.warp(img, flow, "simple", false, "clamp")
  23. end
  24. return iproc