iproc.lua 1.1 KB

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