iproc.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.scale_with_gamma22(src, width, height, filter)
  59. local conversion
  60. src, conversion = iproc.byte2float(src)
  61. filter = filter or "Box"
  62. local im = gm.Image(src, "RGB", "DHW")
  63. im:gammaCorrection(1.0 / 2.2):
  64. size(math.ceil(width), math.ceil(height), filter):
  65. gammaCorrection(2.2)
  66. local dest = im:toTensor("float", "RGB", "DHW")
  67. if conversion then
  68. dest = iproc.float2byte(dest)
  69. end
  70. return dest
  71. end
  72. function iproc.padding(img, w1, w2, h1, h2)
  73. local dst_height = img:size(2) + h1 + h2
  74. local dst_width = img:size(3) + w1 + w2
  75. local flow = torch.Tensor(2, dst_height, dst_width)
  76. flow[1] = torch.ger(torch.linspace(0, dst_height -1, dst_height), torch.ones(dst_width))
  77. flow[2] = torch.ger(torch.ones(dst_height), torch.linspace(0, dst_width - 1, dst_width))
  78. flow[1]:add(-h1)
  79. flow[2]:add(-w1)
  80. return image.warp(img, flow, "simple", false, "clamp")
  81. end
  82. local function test_conversion()
  83. 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}}})
  84. local im = gm.Image():fromTensor(x, "RGB", "DHW")
  85. local a, b
  86. a = iproc.float2byte(x):float()
  87. b = im:toTensor("byte", "RGB", "DHW"):float()
  88. assert((a - b):abs():sum() == 0)
  89. a = iproc.byte2float(iproc.float2byte(x))
  90. b = gm.Image():fromTensor(im:toTensor("byte", "RGB", "DHW"), "RGB", "DHW"):toTensor("float", "RGB", "DHW")
  91. assert((a - b):abs():sum() == 0)
  92. end
  93. --test_conversion()
  94. return iproc