iproc.lua 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. local gm = require 'graphicsmagick'
  2. local image = require 'image'
  3. local iproc = {}
  4. function iproc.crop_mod4(src)
  5. local w = src:size(3) % 4
  6. local h = src:size(2) % 4
  7. return image.crop(src, 0, 0, src:size(3) - w, src:size(2) - h)
  8. end
  9. function iproc.crop(src, w1, h1, w2, h2)
  10. local dest
  11. if src:dim() == 3 then
  12. dest = src[{{}, { h1 + 1, h2 }, { w1 + 1, w2 }}]:clone()
  13. else -- dim == 2
  14. dest = src[{{ h1 + 1, h2 }, { w1 + 1, w2 }}]:clone()
  15. end
  16. return dest
  17. end
  18. function iproc.crop_nocopy(src, w1, h1, w2, h2)
  19. local dest
  20. if src:dim() == 3 then
  21. dest = src[{{}, { h1 + 1, h2 }, { w1 + 1, w2 }}]
  22. else -- dim == 2
  23. dest = src[{{ h1 + 1, h2 }, { w1 + 1, w2 }}]
  24. end
  25. return dest
  26. end
  27. function iproc.byte2float(src)
  28. local conversion = false
  29. local dest = src
  30. if src:type() == "torch.ByteTensor" then
  31. conversion = true
  32. dest = src:float():div(255.0)
  33. end
  34. return dest, conversion
  35. end
  36. function iproc.float2byte(src)
  37. local conversion = false
  38. local dest = src
  39. if src:type() == "torch.FloatTensor" then
  40. conversion = true
  41. dest = (src * 255.0)
  42. dest[torch.lt(dest, 0.0)] = 0
  43. dest[torch.gt(dest, 255.0)] = 255.0
  44. dest = dest:byte()
  45. end
  46. return dest, conversion
  47. end
  48. function iproc.scale(src, width, height, filter)
  49. local t = "float"
  50. if src:type() == "torch.ByteTensor" then
  51. t = "byte"
  52. end
  53. filter = filter or "Box"
  54. local im = gm.Image(src, "RGB", "DHW")
  55. im:size(math.ceil(width), math.ceil(height), filter)
  56. return im:toTensor(t, "RGB", "DHW")
  57. end
  58. function iproc.padding(img, w1, w2, h1, h2)
  59. local dst_height = img:size(2) + h1 + h2
  60. local dst_width = img:size(3) + w1 + w2
  61. local flow = torch.Tensor(2, dst_height, dst_width)
  62. flow[1] = torch.ger(torch.linspace(0, dst_height -1, dst_height), torch.ones(dst_width))
  63. flow[2] = torch.ger(torch.ones(dst_height), torch.linspace(0, dst_width - 1, dst_width))
  64. flow[1]:add(-h1)
  65. flow[2]:add(-w1)
  66. return image.warp(img, flow, "simple", false, "clamp")
  67. end
  68. local function test_conversion()
  69. local x = torch.FloatTensor({{{0, 0.1}, {-0.1, 1.0}}, {{0.1234, 0.5}, {0.85, 1.2}}, {{0, 0.1}, {0.5, 0.8}}})
  70. local im = gm.Image():fromTensor(x, "RGB", "DHW")
  71. local a, b
  72. a = iproc.float2byte(x):float()
  73. b = im:toTensor("byte", "RGB", "DHW"):float()
  74. assert((a - b):abs():sum() == 0)
  75. a = iproc.byte2float(iproc.float2byte(x))
  76. b = gm.Image():fromTensor(im:toTensor("byte", "RGB", "DHW"), "RGB", "DHW"):toTensor("float", "RGB", "DHW")
  77. assert((a - b):abs():sum() == 0)
  78. end
  79. --test_conversion()
  80. return iproc