iproc.lua 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. local gm = {}
  2. gm.Image = require 'graphicsmagick.Image'
  3. require 'dok'
  4. local image = require 'image'
  5. local iproc = {}
  6. local clip_eps8 = (1.0 / 255.0) * 0.5 - (1.0e-7 * (1.0 / 255.0) * 0.5)
  7. function iproc.crop_mod4(src)
  8. local w = src:size(3) % 4
  9. local h = src:size(2) % 4
  10. return iproc.crop(src, 0, 0, src:size(3) - w, src:size(2) - h)
  11. end
  12. function iproc.crop(src, w1, h1, w2, h2)
  13. local dest
  14. if src:dim() == 3 then
  15. dest = src[{{}, { h1 + 1, h2 }, { w1 + 1, w2 }}]:clone()
  16. else -- dim == 2
  17. dest = src[{{ h1 + 1, h2 }, { w1 + 1, w2 }}]:clone()
  18. end
  19. return dest
  20. end
  21. function iproc.crop_nocopy(src, w1, h1, w2, h2)
  22. local dest
  23. if src:dim() == 3 then
  24. dest = src[{{}, { h1 + 1, h2 }, { w1 + 1, w2 }}]
  25. else -- dim == 2
  26. dest = src[{{ h1 + 1, h2 }, { w1 + 1, w2 }}]
  27. end
  28. return dest
  29. end
  30. function iproc.byte2float(src)
  31. local conversion = false
  32. local dest = src
  33. if src:type() == "torch.ByteTensor" then
  34. conversion = true
  35. dest = src:float():div(255.0)
  36. end
  37. return dest, conversion
  38. end
  39. function iproc.float2byte(src)
  40. local conversion = false
  41. local dest = src
  42. if src:type() == "torch.FloatTensor" then
  43. conversion = true
  44. dest = (src + clip_eps8):mul(255.0)
  45. dest:clamp(0, 255.0)
  46. dest = dest:byte()
  47. end
  48. return dest, conversion
  49. end
  50. function iproc.scale(src, width, height, filter, blur)
  51. local conversion, color
  52. src, conversion = iproc.byte2float(src)
  53. filter = filter or "Box"
  54. if src:size(1) == 3 then
  55. color = "RGB"
  56. else
  57. color = "I"
  58. end
  59. local im = gm.Image(src, color, "DHW")
  60. im:size(math.ceil(width), math.ceil(height), filter, blur)
  61. local dest = im:toTensor("float", color, "DHW")
  62. if conversion then
  63. dest = iproc.float2byte(dest)
  64. end
  65. return dest
  66. end
  67. function iproc.scale_with_gamma22(src, width, height, filter, blur)
  68. local conversion
  69. src, conversion = iproc.byte2float(src)
  70. filter = filter or "Box"
  71. local im = gm.Image(src, "RGB", "DHW")
  72. im:gammaCorrection(1.0 / 2.2):
  73. size(math.ceil(width), math.ceil(height), filter, blur):
  74. gammaCorrection(2.2)
  75. local dest = im:toTensor("float", "RGB", "DHW"):clamp(0.0, 1.0)
  76. if conversion then
  77. dest = iproc.float2byte(dest)
  78. end
  79. return dest
  80. end
  81. function iproc.padding(img, w1, w2, h1, h2)
  82. image = image or require 'image'
  83. local dst_height = img:size(2) + h1 + h2
  84. local dst_width = img:size(3) + w1 + w2
  85. local flow = torch.Tensor(2, dst_height, dst_width)
  86. flow[1] = torch.ger(torch.linspace(0, dst_height -1, dst_height), torch.ones(dst_width))
  87. flow[2] = torch.ger(torch.ones(dst_height), torch.linspace(0, dst_width - 1, dst_width))
  88. flow[1]:add(-h1)
  89. flow[2]:add(-w1)
  90. return image.warp(img, flow, "simple", false, "clamp")
  91. end
  92. function iproc.zero_padding(img, w1, w2, h1, h2)
  93. image = image or require 'image'
  94. local dst_height = img:size(2) + h1 + h2
  95. local dst_width = img:size(3) + w1 + w2
  96. local flow = torch.Tensor(2, dst_height, dst_width)
  97. flow[1] = torch.ger(torch.linspace(0, dst_height -1, dst_height), torch.ones(dst_width))
  98. flow[2] = torch.ger(torch.ones(dst_height), torch.linspace(0, dst_width - 1, dst_width))
  99. flow[1]:add(-h1)
  100. flow[2]:add(-w1)
  101. return image.warp(img, flow, "simple", false, "pad", 0)
  102. end
  103. function iproc.white_noise(src, std, rgb_weights, gamma)
  104. gamma = gamma or 0.454545
  105. local conversion
  106. src, conversion = iproc.byte2float(src)
  107. std = std or 0.01
  108. local noise = torch.Tensor():resizeAs(src):normal(0, std)
  109. if rgb_weights then
  110. noise[1]:mul(rgb_weights[1])
  111. noise[2]:mul(rgb_weights[2])
  112. noise[3]:mul(rgb_weights[3])
  113. end
  114. local dest
  115. if gamma ~= 0 then
  116. dest = src:clone():pow(gamma):add(noise)
  117. dest:clamp(0.0, 1.0)
  118. dest:pow(1.0 / gamma)
  119. else
  120. dest = src + noise
  121. end
  122. if conversion then
  123. dest = iproc.float2byte(dest)
  124. end
  125. return dest
  126. end
  127. function iproc.hflip(src)
  128. local t
  129. if src:type() == "torch.ByteTensor" then
  130. t = "byte"
  131. else
  132. t = "float"
  133. end
  134. if src:size(1) == 3 then
  135. color = "RGB"
  136. else
  137. color = "I"
  138. end
  139. local im = gm.Image(src, color, "DHW")
  140. return im:flop():toTensor(t, color, "DHW")
  141. end
  142. function iproc.vflip(src)
  143. local t
  144. if src:type() == "torch.ByteTensor" then
  145. t = "byte"
  146. else
  147. t = "float"
  148. end
  149. if src:size(1) == 3 then
  150. color = "RGB"
  151. else
  152. color = "I"
  153. end
  154. local im = gm.Image(src, color, "DHW")
  155. return im:flip():toTensor(t, color, "DHW")
  156. end
  157. local function rotate_with_warp(src, dst, theta, mode)
  158. local height
  159. local width
  160. if src:dim() == 2 then
  161. height = src:size(1)
  162. width = src:size(2)
  163. elseif src:dim() == 3 then
  164. height = src:size(2)
  165. width = src:size(3)
  166. else
  167. dok.error('src image must be 2D or 3D', 'image.rotate')
  168. end
  169. local flow = torch.Tensor(2, height, width)
  170. local kernel = torch.Tensor({{math.cos(-theta), -math.sin(-theta)},
  171. {math.sin(-theta), math.cos(-theta)}})
  172. flow[1] = torch.ger(torch.linspace(0, 1, height), torch.ones(width))
  173. flow[1]:mul(-(height -1)):add(math.floor(height / 2 + 0.5))
  174. flow[2] = torch.ger(torch.ones(height), torch.linspace(0, 1, width))
  175. flow[2]:mul(-(width -1)):add(math.floor(width / 2 + 0.5))
  176. flow:add(-1, torch.mm(kernel, flow:view(2, height * width)))
  177. dst:resizeAs(src)
  178. return image.warp(dst, src, flow, mode, true, 'clamp')
  179. end
  180. function iproc.rotate(src, theta)
  181. local conversion
  182. src, conversion = iproc.byte2float(src)
  183. local dest = torch.Tensor():typeAs(src):resizeAs(src)
  184. rotate_with_warp(src, dest, theta, 'bilinear')
  185. dest:clamp(0, 1)
  186. if conversion then
  187. dest = iproc.float2byte(dest)
  188. end
  189. return dest
  190. end
  191. function iproc.negate(src)
  192. if src:type() == "torch.ByteTensor" then
  193. return -src + 255
  194. else
  195. return -src + 1
  196. end
  197. end
  198. function iproc.gaussian2d(kernel_size, sigma)
  199. sigma = sigma or 1
  200. local kernel = torch.Tensor(kernel_size, kernel_size)
  201. local u = math.floor(kernel_size / 2) + 1
  202. local amp = (1 / math.sqrt(2 * math.pi * sigma^2))
  203. for x = 1, kernel_size do
  204. for y = 1, kernel_size do
  205. kernel[x][y] = amp * math.exp(-((x - u)^2 + (y - u)^2) / (2 * sigma^2))
  206. end
  207. end
  208. kernel:div(kernel:sum())
  209. return kernel
  210. end
  211. function iproc.rgb2y(src)
  212. local conversion
  213. src, conversion = iproc.byte2float(src)
  214. local dest = torch.FloatTensor(1, src:size(2), src:size(3)):zero()
  215. dest:add(0.299, src[1]):add(0.587, src[2]):add(0.114, src[3])
  216. if conversion then
  217. dest = iproc.float2byte(dest)
  218. end
  219. return dest
  220. end
  221. local function test_conversion()
  222. local a = torch.linspace(0, 255, 256):float():div(255.0)
  223. local b = iproc.float2byte(a)
  224. local c = iproc.byte2float(a)
  225. local d = torch.linspace(0, 255, 256)
  226. assert((a - c):abs():sum() == 0)
  227. assert((d:float() - b:float()):abs():sum() == 0)
  228. a = torch.FloatTensor({256.0, 255.0, 254.999}):div(255.0)
  229. b = iproc.float2byte(a)
  230. assert(b:float():sum() == 255.0 * 3)
  231. a = torch.FloatTensor({254.0, 254.499, 253.50001}):div(255.0)
  232. b = iproc.float2byte(a)
  233. print(b)
  234. assert(b:float():sum() == 254.0 * 3)
  235. end
  236. local function test_flip()
  237. require 'sys'
  238. require 'torch'
  239. torch.setdefaulttensortype("torch.FloatTensor")
  240. image = require 'image'
  241. local src = image.lena()
  242. local src_byte = src:clone():mul(255):byte()
  243. print(src:size())
  244. print((image.hflip(src) - iproc.hflip(src)):sum())
  245. print((image.hflip(src_byte) - iproc.hflip(src_byte)):sum())
  246. print((image.vflip(src) - iproc.vflip(src)):sum())
  247. print((image.vflip(src_byte) - iproc.vflip(src_byte)):sum())
  248. end
  249. local function test_gaussian2d()
  250. local t = {3, 5, 7}
  251. for i = 1, #t do
  252. local kp = iproc.gaussian2d(t[i], 0.5)
  253. print(kp)
  254. end
  255. end
  256. local function test_conv()
  257. local image = require 'image'
  258. local src = image.lena()
  259. local kernel = torch.Tensor(3, 3):fill(1)
  260. kernel:div(kernel:sum())
  261. --local blur = image.convolve(iproc.padding(src, 1, 1, 1, 1), kernel, 'valid')
  262. local blur = image.convolve(src, kernel, 'same')
  263. print(src:size(), blur:size())
  264. local diff = (blur - src):abs()
  265. image.save("diff.png", diff)
  266. image.display({image = blur, min=0, max=1})
  267. image.display({image = diff, min=0, max=1})
  268. end
  269. --test_conversion()
  270. --test_flip()
  271. --test_gaussian2d()
  272. --test_conv()
  273. return iproc