iproc.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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: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. -- from torch/image
  158. ----------------------------------------------------------------------
  159. -- image.rgb2yuv(image)
  160. -- converts a RGB image to YUV
  161. --
  162. function iproc.rgb2yuv(...)
  163. -- arg check
  164. local output,input
  165. local args = {...}
  166. if select('#',...) == 2 then
  167. output = args[1]
  168. input = args[2]
  169. elseif select('#',...) == 1 then
  170. input = args[1]
  171. else
  172. print(dok.usage('image.rgb2yuv',
  173. 'transforms an image from RGB to YUV', nil,
  174. {type='torch.Tensor', help='input image', req=true},
  175. '',
  176. {type='torch.Tensor', help='output image', req=true},
  177. {type='torch.Tensor', help='input image', req=true}
  178. ))
  179. dok.error('missing input', 'image.rgb2yuv')
  180. end
  181. -- resize
  182. output = output or input.new()
  183. output:resizeAs(input)
  184. -- input chanels
  185. local inputRed = input[1]
  186. local inputGreen = input[2]
  187. local inputBlue = input[3]
  188. -- output chanels
  189. local outputY = output[1]
  190. local outputU = output[2]
  191. local outputV = output[3]
  192. -- convert
  193. outputY:zero():add(0.299, inputRed):add(0.587, inputGreen):add(0.114, inputBlue)
  194. outputU:zero():add(-0.14713, inputRed):add(-0.28886, inputGreen):add(0.436, inputBlue)
  195. outputV:zero():add(0.615, inputRed):add(-0.51499, inputGreen):add(-0.10001, inputBlue)
  196. -- return YUV image
  197. return output
  198. end
  199. ----------------------------------------------------------------------
  200. -- image.yuv2rgb(image)
  201. -- converts a YUV image to RGB
  202. --
  203. function iproc.yuv2rgb(...)
  204. -- arg check
  205. local output,input
  206. local args = {...}
  207. if select('#',...) == 2 then
  208. output = args[1]
  209. input = args[2]
  210. elseif select('#',...) == 1 then
  211. input = args[1]
  212. else
  213. print(dok.usage('image.yuv2rgb',
  214. 'transforms an image from YUV to RGB', nil,
  215. {type='torch.Tensor', help='input image', req=true},
  216. '',
  217. {type='torch.Tensor', help='output image', req=true},
  218. {type='torch.Tensor', help='input image', req=true}
  219. ))
  220. dok.error('missing input', 'image.yuv2rgb')
  221. end
  222. -- resize
  223. output = output or input.new()
  224. output:resizeAs(input)
  225. -- input chanels
  226. local inputY = input[1]
  227. local inputU = input[2]
  228. local inputV = input[3]
  229. -- output chanels
  230. local outputRed = output[1]
  231. local outputGreen = output[2]
  232. local outputBlue = output[3]
  233. -- convert
  234. outputRed:copy(inputY):add(1.13983, inputV)
  235. outputGreen:copy(inputY):add(-0.39465, inputU):add(-0.58060, inputV)
  236. outputBlue:copy(inputY):add(2.03211, inputU)
  237. -- return RGB image
  238. return output
  239. end
  240. function iproc.gaussian2d(kernel_size, sigma)
  241. sigma = sigma or 1
  242. local kernel = torch.Tensor(kernel_size, kernel_size)
  243. local u = math.floor(kernel_size / 2) + 1
  244. local amp = (1 / math.sqrt(2 * math.pi * sigma^2))
  245. for x = 1, kernel_size do
  246. for y = 1, kernel_size do
  247. kernel[x][y] = amp * math.exp(-((x - u)^2 + (y - u)^2) / (2 * sigma^2))
  248. end
  249. end
  250. kernel:div(kernel:sum())
  251. return kernel
  252. end
  253. -- from image.convolve
  254. function iproc.convolve(...)
  255. local dst,src,kernel,mode
  256. local args = {...}
  257. if select('#',...) == 4 then
  258. dst = args[1]
  259. src = args[2]
  260. kernel = args[3]
  261. mode = args[4]
  262. elseif select('#',...) == 3 then
  263. if type(args[3]) == 'string' then
  264. src = args[1]
  265. kernel = args[2]
  266. mode = args[3]
  267. else
  268. dst = args[1]
  269. src = args[2]
  270. kernel = args[3]
  271. end
  272. elseif select('#',...) == 2 then
  273. src = args[1]
  274. kernel = args[2]
  275. else
  276. print(dok.usage('iproc.convolve',
  277. 'convolves an input image with a kernel, returns the result', nil,
  278. {type='torch.Tensor', help='input image', req=true},
  279. {type='torch.Tensor', help='kernel', req=true},
  280. {type='string', help='type: full | valid | same', default='valid'},
  281. '',
  282. {type='torch.Tensor', help='destination', req=true},
  283. {type='torch.Tensor', help='input image', req=true},
  284. {type='torch.Tensor', help='kernel', req=true},
  285. {type='string', help='type: full | valid | same', default='valid'}))
  286. dok.error('incorrect arguments', 'image.convolve')
  287. end
  288. if mode and mode ~= 'valid' and mode ~= 'full' and mode ~= 'same' then
  289. dok.error('mode has to be one of: full | valid | same', 'image.convolve')
  290. end
  291. local md = (((mode == 'full') or (mode == 'same')) and 'F') or 'V'
  292. if kernel:nDimension() == 2 and src:nDimension() == 3 then
  293. local k3d = src.new(src:size(1), kernel:size(1), kernel:size(2))
  294. for i = 1,src:size(1) do
  295. k3d[i]:copy(kernel)
  296. end
  297. kernel = k3d
  298. end
  299. if dst then
  300. torch.conv2(dst,src,kernel,md)
  301. else
  302. dst = torch.conv2(src,kernel,md)
  303. end
  304. if mode == 'same' then
  305. local cx = dst:dim()
  306. local cy = cx-1
  307. local ofy = math.ceil(kernel:size(cy)/2)
  308. local ofx = math.ceil(kernel:size(cx)/2)
  309. dst = dst:narrow(cy, ofy, src:size(cy)):narrow(cx, ofx, src:size(cx))
  310. end
  311. return dst
  312. end
  313. local function test_conversion()
  314. local a = torch.linspace(0, 255, 256):float():div(255.0)
  315. local b = iproc.float2byte(a)
  316. local c = iproc.byte2float(a)
  317. local d = torch.linspace(0, 255, 256)
  318. assert((a - c):abs():sum() == 0)
  319. assert((d:float() - b:float()):abs():sum() == 0)
  320. a = torch.FloatTensor({256.0, 255.0, 254.999}):div(255.0)
  321. b = iproc.float2byte(a)
  322. assert(b:float():sum() == 255.0 * 3)
  323. a = torch.FloatTensor({254.0, 254.499, 253.50001}):div(255.0)
  324. b = iproc.float2byte(a)
  325. print(b)
  326. assert(b:float():sum() == 254.0 * 3)
  327. end
  328. local function test_flip()
  329. require 'sys'
  330. require 'torch'
  331. torch.setdefaulttensortype("torch.FloatTensor")
  332. image = require 'image'
  333. local src = image.lena()
  334. local src_byte = src:clone():mul(255):byte()
  335. print(src:size())
  336. print((image.hflip(src) - iproc.hflip(src)):sum())
  337. print((image.hflip(src_byte) - iproc.hflip(src_byte)):sum())
  338. print((image.vflip(src) - iproc.vflip(src)):sum())
  339. print((image.vflip(src_byte) - iproc.vflip(src_byte)):sum())
  340. end
  341. local function test_gaussian2d()
  342. local t = {3, 5, 7}
  343. for i = 1, #t do
  344. local kp = iproc.gaussian2d(t[i], 0.5)
  345. print(kp)
  346. end
  347. end
  348. local function test_conv()
  349. local image = require 'image'
  350. local src = image.lena()
  351. local kernel = torch.Tensor(3, 3):fill(1)
  352. kernel:div(kernel:sum())
  353. --local blur = iproc.convolve(iproc.padding(src, 1, 1, 1, 1), kernel, 'valid')
  354. local blur = iproc.convolve(src, kernel, 'same')
  355. print(src:size(), blur:size())
  356. local diff = (blur - src):abs()
  357. image.save("diff.png", diff)
  358. image.display({image = blur, min=0, max=1})
  359. image.display({image = diff, min=0, max=1})
  360. end
  361. --test_conversion()
  362. --test_flip()
  363. --test_gaussian2d()
  364. --test_conv()
  365. return iproc