alpha_util.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. local w2nn = require 'w2nn'
  2. local reconstruct = require 'reconstruct'
  3. local image = require 'image'
  4. local iproc = require 'iproc'
  5. local gm = require 'graphicsmagick'
  6. alpha_util = {}
  7. function alpha_util.make_border(rgb, alpha, offset)
  8. if not alpha then
  9. return rgb
  10. end
  11. local sum2d = nn.SpatialConvolutionMM(1, 1, 3, 3, 1, 1, 1, 1):cuda()
  12. sum2d.weight:fill(1)
  13. sum2d.bias:zero()
  14. local mask = alpha:clone()
  15. mask[torch.gt(mask, 0.0)] = 1
  16. mask[torch.eq(mask, 0.0)] = 0
  17. local mask_nega = (mask - 1):abs():byte()
  18. local eps = 1.0e-7
  19. rgb = rgb:clone()
  20. rgb[1][mask_nega] = 0
  21. rgb[2][mask_nega] = 0
  22. rgb[3][mask_nega] = 0
  23. for i = 1, offset do
  24. local mask_weight = sum2d:forward(mask:cuda()):float()
  25. local border = rgb:clone()
  26. for j = 1, 3 do
  27. border[j]:copy(sum2d:forward(rgb[j]:reshape(1, rgb:size(2), rgb:size(3)):cuda()))
  28. border[j]:cdiv((mask_weight + eps))
  29. rgb[j][mask_nega] = border[j][mask_nega]
  30. end
  31. mask = mask_weight:clone()
  32. mask[torch.gt(mask_weight, 0.0)] = 1
  33. mask_nega = (mask - 1):abs():byte()
  34. if border:size(2) * border:size(3) > 1024*1024 then
  35. collectgarbage()
  36. end
  37. end
  38. rgb:clamp(0.0, 1.0)
  39. return rgb
  40. end
  41. function alpha_util.composite(rgb, alpha, model2x)
  42. if not alpha then
  43. return rgb
  44. end
  45. if not (alpha:size(2) == rgb:size(2) and alpha:size(3) == rgb:size(3)) then
  46. if model2x then
  47. alpha = reconstruct.scale(model2x, 2.0, alpha)
  48. else
  49. alpha = gm.Image(alpha, "I", "DHW"):size(rgb:size(3), rgb:size(2), "Sinc"):toTensor("float", "I", "DHW")
  50. end
  51. end
  52. local out = torch.Tensor(4, rgb:size(2), rgb:size(3))
  53. out[1]:copy(rgb[1])
  54. out[2]:copy(rgb[2])
  55. out[3]:copy(rgb[3])
  56. out[4]:copy(alpha)
  57. return out
  58. end
  59. function alpha_util.fill(fg, alpha, val)
  60. assert(fg:size(2) == alpha:size(2) and fg:size(3) == alpha:size(3))
  61. local conversion = false
  62. fg, conversion = iproc.byte2float(fg)
  63. val = val or 0
  64. fg = fg:clone()
  65. bg = fg:clone():fill(val)
  66. bg[1]:cmul(1-alpha)
  67. bg[2]:cmul(1-alpha)
  68. bg[3]:cmul(1-alpha)
  69. fg[1]:cmul(alpha)
  70. fg[2]:cmul(alpha)
  71. fg[3]:cmul(alpha)
  72. local ret = bg:add(fg)
  73. if conversion then
  74. ret = iproc.float2byte(ret)
  75. end
  76. return ret
  77. end
  78. local function test()
  79. require 'sys'
  80. require 'trepl'
  81. torch.setdefaulttensortype("torch.FloatTensor")
  82. local image_loader = require 'image_loader'
  83. local rgb, alpha = image_loader.load_float("alpha.png")
  84. local t = sys.clock()
  85. rgb = alpha_util.make_border(rgb, alpha, 7)
  86. print(sys.clock() - t)
  87. print(rgb:min(), rgb:max())
  88. image.display({image = rgb, min = 0, max = 1})
  89. image.save("out.png", rgb)
  90. end
  91. --test()
  92. return alpha_util