-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathMyWarper.cpp
318 lines (246 loc) · 10.8 KB
/
MyWarper.cpp
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include "MyWarper.h"
namespace cv {
namespace detail {
//////////////////////////////////////////////////////////////////////////
// MyWarperGpu
Point2f MyWarperGpu::warpPoint(const Point2f &pt, const Mat &K, const Mat &R, const Mat &T)
{
projector_.setCameraParams(K, R, T);
Point2f uv;
projector_.mapForward(pt.x, pt.y, uv.x, uv.y);
return uv;
}
Rect MyWarperGpu::warpRoi(Size src_size, const Mat &K, const Mat &R, const Mat &T)
{
projector_.setCameraParams(K, R, T);
Point dst_tl, dst_br;
detectResultRoi(src_size, dst_tl, dst_br);
return Rect(dst_tl, Point(dst_br.x + 1, dst_br.y + 1));
}
void MyWarperGpu::detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br)
{
float tl_uf = numeric_limits<float>::max();
float tl_vf = numeric_limits<float>::max();
float br_uf = -numeric_limits<float>::max();
float br_vf = -numeric_limits<float>::max();
float u, v;
projector_.mapForward(0, 0, u, v);
tl_uf = min(tl_uf, u); tl_vf = min(tl_vf, v);
br_uf = max(br_uf, u); br_vf = max(br_vf, v);
projector_.mapForward(0, static_cast<float>(src_size.height - 1), u, v);
tl_uf = min(tl_uf, u); tl_vf = min(tl_vf, v);
br_uf = max(br_uf, u); br_vf = max(br_vf, v);
projector_.mapForward(static_cast<float>(src_size.width - 1), 0, u, v);
tl_uf = min(tl_uf, u); tl_vf = min(tl_vf, v);
br_uf = max(br_uf, u); br_vf = max(br_vf, v);
projector_.mapForward(static_cast<float>(src_size.width - 1), static_cast<float>(src_size.height - 1), u, v);
tl_uf = min(tl_uf, u); tl_vf = min(tl_vf, v);
br_uf = max(br_uf, u); br_vf = max(br_vf, v);
dst_tl.x = static_cast<int>(tl_uf);
dst_tl.y = static_cast<int>(tl_vf);
dst_br.x = static_cast<int>(br_uf);
dst_br.y = static_cast<int>(br_vf);
}
Rect MyWarperGpu::buildMaps(Size src_size, const Mat &K, const Mat &R, const Mat &T, gpu::GpuMat &xmap, gpu::GpuMat &ymap)
{
projector_.setCameraParams(K, R, T);
Point dst_tl, dst_br;
detectResultRoi(src_size, dst_tl, dst_br);
gpu::buildWarpPlaneMaps(src_size, Rect(dst_tl, Point(dst_br.x + 1, dst_br.y + 1)),
K, R, T, projector_.scale, xmap, ymap);
return Rect(dst_tl, dst_br);
}
Point MyWarperGpu::warp_gpu(const gpu::GpuMat &src, const Mat &K, const Mat &R, const Mat &T, int interp_mode, int border_mode,
gpu::GpuMat &dst)
{
//Mat xmap, ymap;
Rect dst_roi = buildMaps(src.size(), K, R, T, d_xmap_, d_ymap_);
dst.create(dst_roi.height + 1, dst_roi.width + 1, src.type());
gpu::remap(src, dst, d_xmap_, d_ymap_, interp_mode, border_mode);
return dst_roi.tl();
}
//////////////////////////////////////////////////////////////////////////
// MyCylindricalWarperGpu
void MyCylindricalWarperGpu::detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br)
{
float tl_uf = std::numeric_limits<float>::max();
float tl_vf = std::numeric_limits<float>::max();
float br_uf = -std::numeric_limits<float>::max();
float br_vf = -std::numeric_limits<float>::max();
float u, v;
for (float x = 0; x < src_size.width; ++x)
{
projector_.mapForward(static_cast<float>(x), 0, u, v);
tl_uf = std::min(tl_uf, u); tl_vf = std::min(tl_vf, v);
br_uf = std::max(br_uf, u); br_vf = std::max(br_vf, v);
projector_.mapForward(static_cast<float>(x), static_cast<float>(src_size.height - 1), u, v);
tl_uf = std::min(tl_uf, u); tl_vf = std::min(tl_vf, v);
br_uf = std::max(br_uf, u); br_vf = std::max(br_vf, v);
}
for (int y = 0; y < src_size.height; ++y)
{
projector_.mapForward(0, static_cast<float>(y), u, v);
tl_uf = std::min(tl_uf, u); tl_vf = std::min(tl_vf, v);
br_uf = std::max(br_uf, u); br_vf = std::max(br_vf, v);
projector_.mapForward(static_cast<float>(src_size.width - 1), static_cast<float>(y), u, v);
tl_uf = std::min(tl_uf, u); tl_vf = std::min(tl_vf, v);
br_uf = std::max(br_uf, u); br_vf = std::max(br_vf, v);
}
dst_tl.x = static_cast<int>(tl_uf);
dst_tl.y = static_cast<int>(tl_vf);
dst_br.x = static_cast<int>(br_uf);
dst_br.y = static_cast<int>(br_vf);
}
Rect MyCylindricalWarperGpu::buildMaps(Size src_size, const Mat &K, const Mat &R, const Mat &T, gpu::GpuMat &xmap, gpu::GpuMat &ymap)
{
projector_.setCameraParams(K, R);
Point dst_tl, dst_br;
detectResultRoi(src_size, dst_tl, dst_br);
gpu::buildWarpCylindricalMaps(src_size, Rect(dst_tl, Point(dst_br.x + 1, dst_br.y + 1)),
K, R, projector_.scale, xmap, ymap);
return Rect(dst_tl, dst_br);
}
/*
Point MyCylindricalWarperGpu::warp(const gpu::GpuMat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode,
gpu::GpuMat &dst)
{
Rect dst_roi = buildMaps(src.size(), K, R, d_xmap_, d_ymap_);
dst.create(dst_roi.height + 1, dst_roi.width + 1, src.type());
gpu::remap(src, dst, d_xmap_, d_ymap_, interp_mode, border_mode);
return dst_roi.tl();
}
*/
Point MyCylindricalWarperGpu::warp_gpu(const gpu::GpuMat &src, const Mat &K, const Mat &R, const Mat &T, int interp_mode, int border_mode,
gpu::GpuMat &dst)
{
Rect dst_roi = buildMaps(src.size(), K, R, T, d_xmap_, d_ymap_);
dst.create(dst_roi.height + 1, dst_roi.width + 1, src.type());
gpu::remap(src, dst, d_xmap_, d_ymap_, interp_mode, border_mode);
return dst_roi.tl();
}
//////////////////////////////////////////////////////////////////////////
// MyPlaneWarper
Point2f MyPlaneWarper::warpPoint(const Point2f &pt, const Mat &K, const Mat &R, const Mat &T)
{
projector_.setCameraParams(K, R, T);
Point2f uv;
projector_.mapForward(pt.x, pt.y, uv.x, uv.y);
return uv;
}
Rect MyPlaneWarper::buildMaps(Size src_size, const Mat &K, const Mat &R, const Mat &T, Mat &xmap, Mat &ymap)
{
projector_.setCameraParams(K, R, T);
Point dst_tl, dst_br;
detectResultRoi(src_size, dst_tl, dst_br);
xmap.create(dst_br.y - dst_tl.y + 1, dst_br.x - dst_tl.x + 1, CV_32F);
ymap.create(dst_br.y - dst_tl.y + 1, dst_br.x - dst_tl.x + 1, CV_32F);
float x, y;
for (int v = dst_tl.y; v <= dst_br.y; ++v)
{
for (int u = dst_tl.x; u <= dst_br.x; ++u)
{
projector_.mapBackward(static_cast<float>(u), static_cast<float>(v), x, y);
xmap.at<float>(v - dst_tl.y, u - dst_tl.x) = x;
ymap.at<float>(v - dst_tl.y, u - dst_tl.x) = y;
}
}
return Rect(dst_tl, dst_br);
}
Rect MyPlaneWarper::buildMaps(Size src_size, const Mat &K, const Mat &R, const Mat &T, gpu::GpuMat &xmap, gpu::GpuMat &ymap)
{
projector_.setCameraParams(K, R, T);
Point dst_tl, dst_br;
detectResultRoi(src_size, dst_tl, dst_br);
xmap.create(dst_br.y - dst_tl.y + 1, dst_br.x - dst_tl.x + 1, CV_32F);
ymap.create(dst_br.y - dst_tl.y + 1, dst_br.x - dst_tl.x + 1, CV_32F);
float x, y;
for (int v = dst_tl.y; v <= dst_br.y; ++v)
{
for (int u = dst_tl.x; u <= dst_br.x; ++u)
{
projector_.mapBackward(static_cast<float>(u), static_cast<float>(v), x, y);
//xmap.at<float>(v - dst_tl.y, u - dst_tl.x) = x;
//ymap.at<float>(v - dst_tl.y, u - dst_tl.x) = y;
}
}
return Rect(dst_tl, dst_br);
}
Point MyPlaneWarper::warp(const Mat &src, const Mat &K, const Mat &R, const Mat &T, int interp_mode, int border_mode,
Mat &dst)
{
Mat xmap, ymap;
Rect dst_roi;// = buildMaps(src.size(), K, R, T, xmap, ymap);
dst.create(dst_roi.height + 1, dst_roi.width + 1, src.type());
remap(src, dst, xmap, ymap, interp_mode, border_mode);
return dst_roi.tl();
}
Point MyPlaneWarper::warp(const Mat &src, const Mat &K, const Mat &R, const Mat &T, int interp_mode, int border_mode,
gpu::GpuMat &dst)
{
Mat xmap, ymap;
Rect dst_roi;// = buildMaps(src.size(), K, R, T, xmap, ymap);
dst.create(dst_roi.height + 1, dst_roi.width + 1, src.type());
remap(src, dst, xmap, ymap, interp_mode, border_mode);
return dst_roi.tl();
}
Rect MyPlaneWarper::warpRoi(Size src_size, const Mat &K, const Mat &R, const Mat &T)
{
projector_.setCameraParams(K, R, T);
Point dst_tl, dst_br;
detectResultRoi(src_size, dst_tl, dst_br);
return Rect(dst_tl, Point(dst_br.x + 1, dst_br.y + 1));
}
void MyPlaneWarper::detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br)
{
float tl_uf = numeric_limits<float>::max();
float tl_vf = numeric_limits<float>::max();
float br_uf = -numeric_limits<float>::max();
float br_vf = -numeric_limits<float>::max();
float u, v;
projector_.mapForward(0, 0, u, v);
tl_uf = min(tl_uf, u); tl_vf = min(tl_vf, v);
br_uf = max(br_uf, u); br_vf = max(br_vf, v);
projector_.mapForward(0, static_cast<float>(src_size.height - 1), u, v);
tl_uf = min(tl_uf, u); tl_vf = min(tl_vf, v);
br_uf = max(br_uf, u); br_vf = max(br_vf, v);
projector_.mapForward(static_cast<float>(src_size.width - 1), 0, u, v);
tl_uf = min(tl_uf, u); tl_vf = min(tl_vf, v);
br_uf = max(br_uf, u); br_vf = max(br_vf, v);
projector_.mapForward(static_cast<float>(src_size.width - 1), static_cast<float>(src_size.height - 1), u, v);
tl_uf = min(tl_uf, u); tl_vf = min(tl_vf, v);
br_uf = max(br_uf, u); br_vf = max(br_vf, v);
dst_tl.x = static_cast<int>(tl_uf);
dst_tl.y = static_cast<int>(tl_vf);
dst_br.x = static_cast<int>(br_uf);
dst_br.y = static_cast<int>(br_vf);
}
//////////////////////////////////////////////////////////////////////////
// MyPlaneWarperGpu
Rect MyPlaneWarperGpu::buildMaps(Size src_size, const Mat &K, const Mat &R, gpu::GpuMat &xmap, gpu::GpuMat &ymap)
{
return buildMaps(src_size, K, R, Mat::zeros(3, 1, CV_32F), xmap, ymap);
}
Rect MyPlaneWarperGpu::buildMaps(Size src_size, const Mat &K, const Mat &R, const Mat &T, gpu::GpuMat &xmap, gpu::GpuMat &ymap)
{
projector_.setCameraParams(K, R, T);
Point dst_tl, dst_br;
detectResultRoi(src_size, dst_tl, dst_br);
gpu::buildWarpPlaneMaps(src_size, Rect(dst_tl, Point(dst_br.x + 1, dst_br.y + 1)),
K, R, T, projector_.scale, xmap, ymap);
return Rect(dst_tl, dst_br);
}
Point MyPlaneWarperGpu::warp(const gpu::GpuMat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode,
gpu::GpuMat &dst)
{
return warp(src, K, R, Mat::zeros(3, 1, CV_32F), interp_mode, border_mode, dst);
}
Point MyPlaneWarperGpu::warp(const gpu::GpuMat &src, const Mat &K, const Mat &R, const Mat &T, int interp_mode, int border_mode,
gpu::GpuMat &dst)
{
Rect dst_roi = buildMaps(src.size(), K, R, T, d_xmap_, d_ymap_);
dst.create(dst_roi.height + 1, dst_roi.width + 1, src.type());
gpu::remap(src, dst, d_xmap_, d_ymap_, interp_mode, border_mode);
return dst_roi.tl();
}
} // namespace detail
} // namespace cv