iproc.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. function iproc.white_noise(src, std, rgb_weights, gamma)
  86. gamma = gamma or 0.454545
  87. local conversion
  88. src, conversion = iproc.byte2float(src)
  89. std = std or 0.01
  90. local noise = torch.Tensor():resizeAs(src):normal(0, std)
  91. if rgb_weights then
  92. noise[1]:mul(rgb_weights[1])
  93. noise[2]:mul(rgb_weights[2])
  94. noise[3]:mul(rgb_weights[3])
  95. end
  96. local dest
  97. if gamma ~= 0 then
  98. dest = src:clone():pow(gamma):add(noise):pow(1.0 / gamma)
  99. else
  100. dest = src + noise
  101. end
  102. if conversion then
  103. dest = iproc.float2byte(dest)
  104. end
  105. return dest
  106. end
  107. local function test_conversion()
  108. local a = torch.linspace(0, 255, 256):float():div(255.0)
  109. local b = iproc.float2byte(a)
  110. local c = iproc.byte2float(a)
  111. local d = torch.linspace(0, 255, 256)
  112. assert((a - c):abs():sum() == 0)
  113. assert((d:float() - b:float()):abs():sum() == 0)
  114. a = torch.FloatTensor({256.0, 255.0, 254.999}):div(255.0)
  115. b = iproc.float2byte(a)
  116. assert(b:float():sum() == 255.0 * 3)
  117. a = torch.FloatTensor({254.0, 254.499, 253.50001}):div(255.0)
  118. b = iproc.float2byte(a)
  119. print(b)
  120. assert(b:float():sum() == 254.0 * 3)
  121. end
  122. --test_conversion()
  123. return iproc