-
Notifications
You must be signed in to change notification settings - Fork 3
/
iproc.lua
250 lines (229 loc) · 8.28 KB
/
iproc.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
require 'image'
-- image processing functions
local iproc = {}
local function rotate_with_warp(src, dst, theta, mode)
local height
local width
if src:dim() == 2 then
height = src:size(1)
width = src:size(2)
elseif src:dim() == 3 then
height = src:size(2)
width = src:size(3)
else
dok.error('src image must be 2D or 3D', 'image.rotate')
end
local flow = torch.Tensor(2, height, width)
local kernel = torch.Tensor({{math.cos(-theta), -math.sin(-theta)},
{math.sin(-theta), math.cos(-theta)}})
flow[1] = torch.ger(torch.linspace(0, 1, height), torch.ones(width))
flow[1]:mul(-(height -1)):add(math.floor(height / 2 + 0.5))
flow[2] = torch.ger(torch.ones(height), torch.linspace(0, 1, width))
flow[2]:mul(-(width -1)):add(math.floor(width / 2 + 0.5))
flow:add(-1, torch.mm(kernel, flow:view(2, height * width)))
dst:resizeAs(src)
return image.warp(dst, src, flow, mode, true, 'pad')
end
local function scale_with_warp(src, dst, mode)
local src_height, src_width, dst_height, dst_width
if src:dim() == 2 then
src_height = src:size(1)
src_width = src:size(2)
elseif src:dim() == 3 then
src_height = src:size(2)
src_width = src:size(3)
else
dok.error('src image must be 2D or 3D', 'image.rotate')
end
if dst:dim() == 2 then
dst_height = dst:size(1)
dst_width = dst:size(2)
elseif dst:dim() == 3 then
dst_height = dst:size(2)
dst_width = dst:size(3)
else
dok.error('src image must be 2D or 3D', 'image.rotate')
end
local flow = torch.Tensor(2, dst_height, dst_width)
flow[1] = torch.ger(torch.linspace(0, src_height - 1, dst_height), torch.ones(dst_width))
flow[2] = torch.ger(torch.ones(dst_height), torch.linspace(0, src_width - 1, dst_width))
return image.warp(dst, src, flow, mode, false)
end
local function scale_and_rotate_with_warp(src, dst, theta, mode)
local src_height, src_width, dst_height, dst_width, width, height
if src:dim() == 2 then
src_height = src:size(1)
src_width = src:size(2)
elseif src:dim() == 3 then
src_height = src:size(2)
src_width = src:size(3)
else
dok.error('src image must be 2D or 3D', 'image.rotate')
end
if dst:dim() == 2 then
dst_height = dst:size(1)
dst_width = dst:size(2)
elseif dst:dim() == 3 then
dst_height = dst:size(2)
dst_width = dst:size(3)
else
dok.error('src image must be 2D or 3D', 'image.rotate')
end
local flow = torch.Tensor(2, dst_height, dst_width)
local flow_rot = torch.Tensor(2, dst_height, dst_width)
local kernel = torch.Tensor({{math.cos(-theta), -math.sin(-theta)}, {math.sin(-theta), math.cos(-theta)}})
local grid_y = torch.ger(torch.linspace(-1, 1, dst_height), torch.ones(dst_width))
local grid_x = torch.ger(torch.ones(dst_height), torch.linspace(-1, 1, dst_width))
flow[1]:copy(grid_y):add(1):mul(0.5):mul(src_height - 1)
flow[2]:copy(grid_x):add(1):mul(0.5):mul(src_width - 1)
flow_rot[1]:copy(grid_y):mul((math.floor((src_height - 1) / 2 + 0.5)) * -1)
flow_rot[2]:copy(grid_x):mul((math.floor((src_width - 1) / 2 + 0.5)) * -1)
flow_rot:add(-1, torch.mm(kernel, flow_rot:view(2, dst_height * dst_width)))
flow:add(flow_rot)
return image.warp(dst, src, flow, mode, false, 'pad')
end
function iproc.nega(img)
return -img + 1
end
function iproc.scale(src, width, height, filter)
filter = filter or 'bicubic'
local dst = torch.Tensor(src:size(1), height, width)
scale_with_warp(src, dst, filter)
local min = src:min()
local max = src:max()
dst[torch.lt(dst, min)] = min
dst[torch.gt(dst, max)] = max
return dst
end
function iproc.rotate(src, theta, filter)
filter = filter or 'bicubic'
local dst = torch.Tensor():resizeAs(src)
rotate_with_warp(src, dst, theta, filter)
local min = src:min()
local max = src:max()
dst[torch.lt(dst, min)] = min
dst[torch.gt(dst, max)] = max
return dst
end
function iproc.scale_rotate(src, width, height, theta, filter)
filter = filter or 'bicubic'
local dst = torch.Tensor(src:size(1), height, width)
scale_and_rotate_with_warp(src, dst, theta, filter)
local min = src:min()
local max = src:max()
dst[torch.lt(dst, min)] = min
dst[torch.gt(dst, max)] = max
return dst
end
function iproc.scale_contrast(img, scale)
img = img:clone()
img:mul(scale)
img[torch.gt(img, 1.0)] = 1.0
return img
end
function iproc.zero_padding(img, w1, w2, h1, h2)
local dst_height = img:size(2) + h1 + h2
local dst_width = img:size(3) + w1 + w2
local flow = torch.Tensor(2, dst_height, dst_width)
flow[1] = torch.ger(torch.linspace(0, dst_height -1, dst_height), torch.ones(dst_width))
flow[2] = torch.ger(torch.ones(dst_height), torch.linspace(0, dst_width - 1, dst_width))
flow[1]:add(-h1)
flow[2]:add(-w1)
return image.warp(img, flow, "simple", false, "pad", 0)
end
function iproc.hflip(img)
local dst_height = img:size(2)
local dst_width = img:size(3)
local flow = torch.Tensor(2, dst_height, dst_width)
flow[1] = torch.ger(torch.linspace(0, dst_height -1, dst_height), torch.ones(dst_width))
flow[2] = torch.ger(torch.ones(dst_height), torch.linspace(0, dst_width - 1, dst_width))
flow[2]:add(-(dst_width - 1)):mul(-1)
return image.warp(img, flow, "simple", false, "pad", 0)
end
function iproc.vflip(img)
local dst_height = img:size(2)
local dst_width = img:size(3)
local flow = torch.Tensor(2, dst_height, dst_width)
flow[1] = torch.ger(torch.linspace(0, dst_height -1, dst_height), torch.ones(dst_width))
flow[2] = torch.ger(torch.ones(dst_height), torch.linspace(0, dst_width - 1, dst_width))
flow[1]:add(-(dst_height - 1)):mul(-1)
return image.warp(img, flow, "simple", false, "pad", 0)
end
function iproc.crop(src, w1, h1, w2, h2)
local dest
if src:dim() == 3 then
dest = src[{{}, { h1 + 1, h2 }, { w1 + 1, w2 }}]:clone()
else -- dim == 2
dest = src[{{ h1 + 1, h2 }, { w1 + 1, w2 }}]:clone()
end
return dest
end
function iproc.gcn(img)
local mean = img:mean()
local std = img:std() + 1.0e-16
return (img - mean):div(std)
end
local function get_perspective_param(p)
local params = torch.Tensor(8)
local sx = p[1].x - p[2].x + p[3].x - p[4].x
local sy = p[1].y - p[2].y + p[3].y - p[4].y
local dx1 = p[2].x - p[3].x
local dy1 = p[2].y - p[3].y
local dx2 = p[4].x - p[3].x
local dy2 = p[4].y - p[3].y
local z = dx1 * dy2 - dy1 * dx2
local g = (sx * dy2 - sy * dx2) / z
local h = (sy * dx1 - sx * dy1) / z
params[1] = p[2].x - p[1].x + g * p[2].x
params[2] = p[4].x - p[1].x + h * p[4].x
params[3] = p[1].x
params[4] = p[2].y - p[1].y + g * p[2].y
params[5] = p[4].y - p[1].y + h * p[4].y
params[6] = p[1].y
params[7] = g
params[8] = h
return params
end
function iproc.perspective_crop(src, x1, y1, x2, y2, x3, y3, x4, y4, w, h)
if src:dim() ~= 3 then
error("expected 3d tensor")
end
local height = h or src:size(2)
local width = w or src:size(3)
local flow = torch.Tensor(2, height, width)
local p = get_perspective_param({{x = x1, y = y1}, -- top left
{x = x2, y = y2}, -- top right
{x = x3, y = y3}, -- bottom right
{x = x4, y = y4} -- bottom left
})
local v = torch.ger(torch.linspace(-1,1, height), torch.ones(width)):add(1):mul(0.5)
local u = torch.ger(torch.ones(height), torch.linspace(-1,1, width)):add(1):mul(0.5)
local t = (u * p[7]) + (v * p[8]) + 1
flow[1]:copy(u * p[4]):add(v * p[5]):add(p[6]):cdiv(t)
flow[2]:copy(u * p[1]):add(v * p[2]):add(p[3]):cdiv(t)
dst = torch.Tensor(1, h, w)
image.warp(dst, src, flow, 'bicubic', false, 'pad')
dst[torch.lt(dst, 0)] = 0
return dst
end
local function test_perspective_crop()
torch.manualSeed(10)
local src = image.lena():narrow(1, 1, 1)
local imgs = torch.Tensor(25, 1, 512, 512)
for i = 1, 25 do
imgs[i]:copy(
iproc.perspective_crop(src,
1 + torch.random(0, 40), 1 + torch.random(0, 40),
512 - torch.random(0, 40), 1 + torch.random(0, 40),
512 - torch.random(0, 40), 512 - torch.random(0, 40),
1 + torch.random(0, 40), 512 - torch.random(0, 40))
)
end
imgs[10] = src
local view = image.toDisplayTensor({input = imgs,
padding = 2,
nrow = 5})
image.save("lena.png", image.scale(view, 800, 800))
end
--test_perspective_crop()
return iproc