iproc.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. local gm = {}
  2. gm.Image = require 'graphicsmagick.Image'
  3. local image = nil
  4. require 'dok'
  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[torch.lt(dest, 0.0)] = 0
  46. dest[torch.gt(dest, 255.0)] = 255.0
  47. dest = dest:byte()
  48. end
  49. return dest, conversion
  50. end
  51. function iproc.scale(src, width, height, filter, blur)
  52. local conversion, color
  53. src, conversion = iproc.byte2float(src)
  54. filter = filter or "Box"
  55. if src:size(1) == 3 then
  56. color = "RGB"
  57. else
  58. color = "I"
  59. end
  60. local im = gm.Image(src, color, "DHW")
  61. im:size(math.ceil(width), math.ceil(height), filter, blur)
  62. local dest = im:toTensor("float", color, "DHW")
  63. if conversion then
  64. dest = iproc.float2byte(dest)
  65. end
  66. return dest
  67. end
  68. function iproc.scale_with_gamma22(src, width, height, filter, blur)
  69. local conversion
  70. src, conversion = iproc.byte2float(src)
  71. filter = filter or "Box"
  72. local im = gm.Image(src, "RGB", "DHW")
  73. im:gammaCorrection(1.0 / 2.2):
  74. size(math.ceil(width), math.ceil(height), filter, blur):
  75. gammaCorrection(2.2)
  76. local dest = im:toTensor("float", "RGB", "DHW"):clamp(0.0, 1.0)
  77. if conversion then
  78. dest = iproc.float2byte(dest)
  79. end
  80. return dest
  81. end
  82. function iproc.padding(img, w1, w2, h1, h2)
  83. image = image or require 'image'
  84. local dst_height = img:size(2) + h1 + h2
  85. local dst_width = img:size(3) + w1 + w2
  86. local flow = torch.Tensor(2, dst_height, dst_width)
  87. flow[1] = torch.ger(torch.linspace(0, dst_height -1, dst_height), torch.ones(dst_width))
  88. flow[2] = torch.ger(torch.ones(dst_height), torch.linspace(0, dst_width - 1, dst_width))
  89. flow[1]:add(-h1)
  90. flow[2]:add(-w1)
  91. return image.warp(img, flow, "simple", false, "clamp")
  92. end
  93. function iproc.zero_padding(img, w1, w2, h1, h2)
  94. image = image or require 'image'
  95. local dst_height = img:size(2) + h1 + h2
  96. local dst_width = img:size(3) + w1 + w2
  97. local flow = torch.Tensor(2, dst_height, dst_width)
  98. flow[1] = torch.ger(torch.linspace(0, dst_height -1, dst_height), torch.ones(dst_width))
  99. flow[2] = torch.ger(torch.ones(dst_height), torch.linspace(0, dst_width - 1, dst_width))
  100. flow[1]:add(-h1)
  101. flow[2]:add(-w1)
  102. return image.warp(img, flow, "simple", false, "pad", 0)
  103. end
  104. function iproc.white_noise(src, std, rgb_weights, gamma)
  105. gamma = gamma or 0.454545
  106. local conversion
  107. src, conversion = iproc.byte2float(src)
  108. std = std or 0.01
  109. local noise = torch.Tensor():resizeAs(src):normal(0, std)
  110. if rgb_weights then
  111. noise[1]:mul(rgb_weights[1])
  112. noise[2]:mul(rgb_weights[2])
  113. noise[3]:mul(rgb_weights[3])
  114. end
  115. local dest
  116. if gamma ~= 0 then
  117. dest = src:clone():pow(gamma):add(noise)
  118. dest[torch.lt(dest, 0.0)] = 0.0
  119. dest[torch.gt(dest, 1.0)] = 1.0
  120. dest:pow(1.0 / gamma)
  121. else
  122. dest = src + noise
  123. end
  124. if conversion then
  125. dest = iproc.float2byte(dest)
  126. end
  127. return dest
  128. end
  129. function iproc.hflip(src)
  130. local t
  131. if src:type() == "torch.ByteTensor" then
  132. t = "byte"
  133. else
  134. t = "float"
  135. end
  136. if src:size(1) == 3 then
  137. color = "RGB"
  138. else
  139. color = "I"
  140. end
  141. local im = gm.Image(src, color, "DHW")
  142. return im:flop():toTensor(t, color, "DHW")
  143. end
  144. function iproc.vflip(src)
  145. local t
  146. if src:type() == "torch.ByteTensor" then
  147. t = "byte"
  148. else
  149. t = "float"
  150. end
  151. if src:size(1) == 3 then
  152. color = "RGB"
  153. else
  154. color = "I"
  155. end
  156. local im = gm.Image(src, color, "DHW")
  157. return im:flip():toTensor(t, color, "DHW")
  158. end
  159. -- from torch/image
  160. ----------------------------------------------------------------------
  161. -- image.rgb2yuv(image)
  162. -- converts a RGB image to YUV
  163. --
  164. function iproc.rgb2yuv(...)
  165. -- arg check
  166. local output,input
  167. local args = {...}
  168. if select('#',...) == 2 then
  169. output = args[1]
  170. input = args[2]
  171. elseif select('#',...) == 1 then
  172. input = args[1]
  173. else
  174. print(dok.usage('image.rgb2yuv',
  175. 'transforms an image from RGB to YUV', nil,
  176. {type='torch.Tensor', help='input image', req=true},
  177. '',
  178. {type='torch.Tensor', help='output image', req=true},
  179. {type='torch.Tensor', help='input image', req=true}
  180. ))
  181. dok.error('missing input', 'image.rgb2yuv')
  182. end
  183. -- resize
  184. output = output or input.new()
  185. output:resizeAs(input)
  186. -- input chanels
  187. local inputRed = input[1]
  188. local inputGreen = input[2]
  189. local inputBlue = input[3]
  190. -- output chanels
  191. local outputY = output[1]
  192. local outputU = output[2]
  193. local outputV = output[3]
  194. -- convert
  195. outputY:zero():add(0.299, inputRed):add(0.587, inputGreen):add(0.114, inputBlue)
  196. outputU:zero():add(-0.14713, inputRed):add(-0.28886, inputGreen):add(0.436, inputBlue)
  197. outputV:zero():add(0.615, inputRed):add(-0.51499, inputGreen):add(-0.10001, inputBlue)
  198. -- return YUV image
  199. return output
  200. end
  201. ----------------------------------------------------------------------
  202. -- image.yuv2rgb(image)
  203. -- converts a YUV image to RGB
  204. --
  205. function iproc.yuv2rgb(...)
  206. -- arg check
  207. local output,input
  208. local args = {...}
  209. if select('#',...) == 2 then
  210. output = args[1]
  211. input = args[2]
  212. elseif select('#',...) == 1 then
  213. input = args[1]
  214. else
  215. print(dok.usage('image.yuv2rgb',
  216. 'transforms an image from YUV to RGB', nil,
  217. {type='torch.Tensor', help='input image', req=true},
  218. '',
  219. {type='torch.Tensor', help='output image', req=true},
  220. {type='torch.Tensor', help='input image', req=true}
  221. ))
  222. dok.error('missing input', 'image.yuv2rgb')
  223. end
  224. -- resize
  225. output = output or input.new()
  226. output:resizeAs(input)
  227. -- input chanels
  228. local inputY = input[1]
  229. local inputU = input[2]
  230. local inputV = input[3]
  231. -- output chanels
  232. local outputRed = output[1]
  233. local outputGreen = output[2]
  234. local outputBlue = output[3]
  235. -- convert
  236. outputRed:copy(inputY):add(1.13983, inputV)
  237. outputGreen:copy(inputY):add(-0.39465, inputU):add(-0.58060, inputV)
  238. outputBlue:copy(inputY):add(2.03211, inputU)
  239. -- return RGB image
  240. return output
  241. end
  242. function iproc.gaussian2d(kernel_size, sigma)
  243. sigma = sigma or 1
  244. local kernel = torch.Tensor(kernel_size, kernel_size)
  245. local u = math.floor(kernel_size / 2) + 1
  246. local amp = (1 / math.sqrt(2 * math.pi * sigma^2))
  247. for x = 1, kernel_size do
  248. for y = 1, kernel_size do
  249. kernel[x][y] = amp * math.exp(-((x - u)^2 + (y - u)^2) / (2 * sigma^2))
  250. end
  251. end
  252. kernel:div(kernel:sum())
  253. return kernel
  254. end
  255. -- from image.convolve
  256. function iproc.convolve(...)
  257. local dst,src,kernel,mode
  258. local args = {...}
  259. if select('#',...) == 4 then
  260. dst = args[1]
  261. src = args[2]
  262. kernel = args[3]
  263. mode = args[4]
  264. elseif select('#',...) == 3 then
  265. if type(args[3]) == 'string' then
  266. src = args[1]
  267. kernel = args[2]
  268. mode = args[3]
  269. else
  270. dst = args[1]
  271. src = args[2]
  272. kernel = args[3]
  273. end
  274. elseif select('#',...) == 2 then
  275. src = args[1]
  276. kernel = args[2]
  277. else
  278. print(dok.usage('iproc.convolve',
  279. 'convolves an input image with a kernel, returns the result', nil,
  280. {type='torch.Tensor', help='input image', req=true},
  281. {type='torch.Tensor', help='kernel', req=true},
  282. {type='string', help='type: full | valid | same', default='valid'},
  283. '',
  284. {type='torch.Tensor', help='destination', req=true},
  285. {type='torch.Tensor', help='input image', req=true},
  286. {type='torch.Tensor', help='kernel', req=true},
  287. {type='string', help='type: full | valid | same', default='valid'}))
  288. dok.error('incorrect arguments', 'image.convolve')
  289. end
  290. if mode and mode ~= 'valid' and mode ~= 'full' and mode ~= 'same' then
  291. dok.error('mode has to be one of: full | valid | same', 'image.convolve')
  292. end
  293. local md = (((mode == 'full') or (mode == 'same')) and 'F') or 'V'
  294. if kernel:nDimension() == 2 and src:nDimension() == 3 then
  295. local k3d = src.new(src:size(1), kernel:size(1), kernel:size(2))
  296. for i = 1,src:size(1) do
  297. k3d[i]:copy(kernel)
  298. end
  299. kernel = k3d
  300. end
  301. if dst then
  302. torch.conv2(dst,src,kernel,md)
  303. else
  304. dst = torch.conv2(src,kernel,md)
  305. end
  306. if mode == 'same' then
  307. local cx = dst:dim()
  308. local cy = cx-1
  309. local ofy = math.ceil(kernel:size(cy)/2)
  310. local ofx = math.ceil(kernel:size(cx)/2)
  311. dst = dst:narrow(cy, ofy, src:size(cy)):narrow(cx, ofx, src:size(cx))
  312. end
  313. return dst
  314. end
  315. local function test_conversion()
  316. local a = torch.linspace(0, 255, 256):float():div(255.0)
  317. local b = iproc.float2byte(a)
  318. local c = iproc.byte2float(a)
  319. local d = torch.linspace(0, 255, 256)
  320. assert((a - c):abs():sum() == 0)
  321. assert((d:float() - b:float()):abs():sum() == 0)
  322. a = torch.FloatTensor({256.0, 255.0, 254.999}):div(255.0)
  323. b = iproc.float2byte(a)
  324. assert(b:float():sum() == 255.0 * 3)
  325. a = torch.FloatTensor({254.0, 254.499, 253.50001}):div(255.0)
  326. b = iproc.float2byte(a)
  327. print(b)
  328. assert(b:float():sum() == 254.0 * 3)
  329. end
  330. local function test_flip()
  331. require 'sys'
  332. require 'torch'
  333. torch.setdefaulttensortype("torch.FloatTensor")
  334. image = require 'image'
  335. local src = image.lena()
  336. local src_byte = src:clone():mul(255):byte()
  337. print(src:size())
  338. print((image.hflip(src) - iproc.hflip(src)):sum())
  339. print((image.hflip(src_byte) - iproc.hflip(src_byte)):sum())
  340. print((image.vflip(src) - iproc.vflip(src)):sum())
  341. print((image.vflip(src_byte) - iproc.vflip(src_byte)):sum())
  342. end
  343. local function test_gaussian2d()
  344. local t = {3, 5, 7}
  345. for i = 1, #t do
  346. local kp = iproc.gaussian2d(t[i], 0.5)
  347. print(kp)
  348. end
  349. end
  350. local function test_conv()
  351. local image = require 'image'
  352. local src = image.lena()
  353. local kernel = torch.Tensor(3, 3):fill(1)
  354. kernel:div(kernel:sum())
  355. --local blur = iproc.convolve(iproc.padding(src, 1, 1, 1, 1), kernel, 'valid')
  356. local blur = iproc.convolve(src, kernel, 'same')
  357. print(src:size(), blur:size())
  358. local diff = (blur - src):abs()
  359. image.save("diff.png", diff)
  360. image.display({image = blur, min=0, max=1})
  361. image.display({image = diff, min=0, max=1})
  362. end
  363. --test_conversion()
  364. --test_flip()
  365. --test_gaussian2d()
  366. --test_conv()
  367. return iproc