forked from DmitryUlyanov/texture_nets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmask.lua
86 lines (70 loc) · 2.28 KB
/
mask.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
require 'image'
local cmd = torch.CmdLine()
cmd:option('-backs', '', 'path to static images of background.')
cmd:option('-input', '', 'path to input folder.')
cmd:option('-output', '', 'path to output folder.')
cmd:option('-threshold', 1, 'threshold for pixels')
cmd:option('-scale', 1, 'scale images.')
cmd:option('-start_from', 0, 'Place to start from.')
cmd:option('-start_backindex', 1, 'Back index to start from.')
local params = cmd:parse(arg)
-- back: image.
-- source, dest: filenames.
local function mask(back, threshold, source, dest)
local fore = image.load(source, 3):float()
if params.scale ~= 1 then
fore = image.scale(fore, fore:size(3)*params.scale, fore:size(2)*params.scale)
end
if back == nil then
back = torch.FloatTensor():resizeAs(fore)
back:zero()
end
local delta = torch.add(fore, -1, back)
delta = torch.abs(delta)
-- normalized greyscale
local grey_delta = image.rgb2y(delta)
-- threshold
local function run_threshold(x)
if x < threshold then
return 0
else
return x
end
end
grey_delta:apply(run_threshold)
image.save(dest, grey_delta)
end
local function run(params)
-- Load background. Start out with totally black.
local back = nil
-- Sort backs. File names match when to switch.
local back_index = params.start_backindex
local backs = {}
for file in paths.iterfiles(params.backs) do table.insert(backs, file) end
table.sort(backs)
print(backs)
-- Sort files in the input path.
local files = {}
for file in paths.iterfiles(params.input) do table.insert(files, file) end
table.sort(files)
-- Iterate through sorted files, apply the mask.
local counter = params.start_from
for _,file in pairs(files) do
if counter > 0 then
counter = counter-1
else
if file == backs[back_index] then
print('loaded back ' .. backs[back_index])
back = image.load(paths.concat(params.backs, backs[back_index]), 3):float()
if params.scale ~= 1 then
back = image.scale(back, back:size(3)*params.scale, back:size(2)*params.scale)
end
back_index = back_index+1
end
local source = paths.concat(params.input, file)
local dest = paths.concat(params.output, file)
mask(back, params.threshold, source, dest)
end
end
end
run(params)