iproc.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. local gm = require 'graphicsmagick'
  2. local image = require 'image'
  3. local iproc = {}
  4. local clip_eps8 = (1.0 / 255.0) * 0.5 - (1.0e-7 * (1.0 / 255.0) * 0.5)
  5. function iproc.crop_mod4(src)
  6. local w = src:size(3) % 4
  7. local h = src:size(2) % 4
  8. return iproc.crop(src, 0, 0, src:size(3) - w, src:size(2) - h)
  9. end
  10. function iproc.crop(src, w1, h1, w2, h2)
  11. local dest
  12. if src:dim() == 3 then
  13. dest = src[{{}, { h1 + 1, h2 }, { w1 + 1, w2 }}]:clone()
  14. else -- dim == 2
  15. dest = src[{{ h1 + 1, h2 }, { w1 + 1, w2 }}]:clone()
  16. end
  17. return dest
  18. end
  19. function iproc.crop_nocopy(src, w1, h1, w2, h2)
  20. local dest
  21. if src:dim() == 3 then
  22. dest = src[{{}, { h1 + 1, h2 }, { w1 + 1, w2 }}]
  23. else -- dim == 2
  24. dest = src[{{ h1 + 1, h2 }, { w1 + 1, w2 }}]
  25. end
  26. return dest
  27. end
  28. function iproc.byte2float(src)
  29. local conversion = false
  30. local dest = src
  31. if src:type() == "torch.ByteTensor" then
  32. conversion = true
  33. dest = src:float():div(255.0)
  34. end
  35. return dest, conversion
  36. end
  37. function iproc.float2byte(src)
  38. local conversion = false
  39. local dest = src
  40. if src:type() == "torch.FloatTensor" then
  41. conversion = true
  42. dest = (src + clip_eps8):mul(255.0)
  43. dest[torch.lt(dest, 0.0)] = 0
  44. dest[torch.gt(dest, 255.0)] = 255.0
  45. dest = dest:byte()
  46. end
  47. return dest, conversion
  48. end
  49. function iproc.scale(src, width, height, filter)
  50. local conversion
  51. src, conversion = iproc.byte2float(src)
  52. filter = filter or "Box"
  53. local im = gm.Image(src, "RGB", "DHW")
  54. im:size(math.ceil(width), math.ceil(height), filter)
  55. local dest = im:toTensor("float", "RGB", "DHW")
  56. if conversion then
  57. dest = iproc.float2byte(dest)
  58. end
  59. return dest
  60. end
  61. function iproc.scale_with_gamma22(src, width, height, filter)
  62. local conversion
  63. src, conversion = iproc.byte2float(src)
  64. filter = filter or "Box"
  65. local im = gm.Image(src, "RGB", "DHW")
  66. im:gammaCorrection(1.0 / 2.2):
  67. size(math.ceil(width), math.ceil(height), filter):
  68. gammaCorrection(2.2)
  69. local dest = im:toTensor("float", "RGB", "DHW")
  70. if conversion then
  71. dest = iproc.float2byte(dest)
  72. end
  73. return dest
  74. end
  75. function iproc.padding(img, w1, w2, h1, h2)
  76. local dst_height = img:size(2) + h1 + h2
  77. local dst_width = img:size(3) + w1 + w2
  78. local flow = torch.Tensor(2, dst_height, dst_width)
  79. flow[1] = torch.ger(torch.linspace(0, dst_height -1, dst_height), torch.ones(dst_width))
  80. flow[2] = torch.ger(torch.ones(dst_height), torch.linspace(0, dst_width - 1, dst_width))
  81. flow[1]:add(-h1)
  82. flow[2]:add(-w1)
  83. return image.warp(img, flow, "simple", false, "clamp")
  84. end
  85. local function test_conversion()
  86. local a = torch.linspace(0, 255, 256):float():div(255.0)
  87. local b = iproc.float2byte(a)
  88. local c = iproc.byte2float(a)
  89. local d = torch.linspace(0, 255, 256)
  90. assert((a - c):abs():sum() == 0)
  91. assert((d:float() - b:float()):abs():sum() == 0)
  92. a = torch.FloatTensor({256.0, 255.0, 254.999}):div(255.0)
  93. b = iproc.float2byte(a)
  94. assert(b:float():sum() == 255.0 * 3)
  95. a = torch.FloatTensor({254.0, 254.499, 253.50001}):div(255.0)
  96. b = iproc.float2byte(a)
  97. print(b)
  98. assert(b:float():sum() == 254.0 * 3)
  99. end
  100. --test_conversion()
  101. return iproc