Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/stitching/src/warpers_cuda.cpp
16337 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
26
//
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
29
//
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
40
//
41
//M*/
42
43
#include "precomp.hpp"
44
#include "opencv2/core/private.cuda.hpp"
45
46
using namespace cv;
47
using namespace cv::cuda;
48
49
#ifdef HAVE_CUDA
50
51
namespace cv { namespace cuda { namespace device
52
{
53
namespace imgproc
54
{
55
void buildWarpPlaneMaps(int tl_u, int tl_v, PtrStepSzf map_x, PtrStepSzf map_y,
56
const float k_rinv[9], const float r_kinv[9], const float t[3], float scale,
57
cudaStream_t stream);
58
59
void buildWarpSphericalMaps(int tl_u, int tl_v, PtrStepSzf map_x, PtrStepSzf map_y,
60
const float k_rinv[9], const float r_kinv[9], float scale,
61
cudaStream_t stream);
62
63
void buildWarpCylindricalMaps(int tl_u, int tl_v, PtrStepSzf map_x, PtrStepSzf map_y,
64
const float k_rinv[9], const float r_kinv[9], float scale,
65
cudaStream_t stream);
66
}
67
}}}
68
69
static void buildWarpPlaneMaps(Size src_size, Rect dst_roi, InputArray _K, InputArray _R, InputArray _T,
70
float scale, OutputArray _map_x, OutputArray _map_y, Stream& stream = Stream::Null())
71
{
72
CV_UNUSED(src_size);
73
74
Mat K = _K.getMat();
75
Mat R = _R.getMat();
76
Mat T = _T.getMat();
77
78
CV_Assert( K.size() == Size(3,3) && K.type() == CV_32FC1 );
79
CV_Assert( R.size() == Size(3,3) && R.type() == CV_32FC1 );
80
CV_Assert( (T.size() == Size(3,1) || T.size() == Size(1,3)) && T.type() == CV_32FC1 && T.isContinuous() );
81
82
Mat K_Rinv = K * R.t();
83
Mat R_Kinv = R * K.inv();
84
CV_Assert( K_Rinv.isContinuous() );
85
CV_Assert( R_Kinv.isContinuous() );
86
87
_map_x.create(dst_roi.size(), CV_32FC1);
88
_map_y.create(dst_roi.size(), CV_32FC1);
89
90
GpuMat map_x = _map_x.getGpuMat();
91
GpuMat map_y = _map_y.getGpuMat();
92
93
device::imgproc::buildWarpPlaneMaps(dst_roi.tl().x, dst_roi.tl().y, map_x, map_y, K_Rinv.ptr<float>(), R_Kinv.ptr<float>(),
94
T.ptr<float>(), scale, StreamAccessor::getStream(stream));
95
}
96
97
static void buildWarpSphericalMaps(Size src_size, Rect dst_roi, InputArray _K, InputArray _R, float scale,
98
OutputArray _map_x, OutputArray _map_y, Stream& stream = Stream::Null())
99
{
100
CV_UNUSED(src_size);
101
102
Mat K = _K.getMat();
103
Mat R = _R.getMat();
104
105
CV_Assert( K.size() == Size(3,3) && K.type() == CV_32FC1 );
106
CV_Assert( R.size() == Size(3,3) && R.type() == CV_32FC1 );
107
108
Mat K_Rinv = K * R.t();
109
Mat R_Kinv = R * K.inv();
110
CV_Assert( K_Rinv.isContinuous() );
111
CV_Assert( R_Kinv.isContinuous() );
112
113
_map_x.create(dst_roi.size(), CV_32FC1);
114
_map_y.create(dst_roi.size(), CV_32FC1);
115
116
GpuMat map_x = _map_x.getGpuMat();
117
GpuMat map_y = _map_y.getGpuMat();
118
119
device::imgproc::buildWarpSphericalMaps(dst_roi.tl().x, dst_roi.tl().y, map_x, map_y, K_Rinv.ptr<float>(), R_Kinv.ptr<float>(), scale, StreamAccessor::getStream(stream));
120
}
121
122
static void buildWarpCylindricalMaps(Size src_size, Rect dst_roi, InputArray _K, InputArray _R, float scale,
123
OutputArray _map_x, OutputArray _map_y, Stream& stream = Stream::Null())
124
{
125
CV_UNUSED(src_size);
126
127
Mat K = _K.getMat();
128
Mat R = _R.getMat();
129
130
CV_Assert( K.size() == Size(3,3) && K.type() == CV_32FC1 );
131
CV_Assert( R.size() == Size(3,3) && R.type() == CV_32FC1 );
132
133
Mat K_Rinv = K * R.t();
134
Mat R_Kinv = R * K.inv();
135
CV_Assert( K_Rinv.isContinuous() );
136
CV_Assert( R_Kinv.isContinuous() );
137
138
_map_x.create(dst_roi.size(), CV_32FC1);
139
_map_y.create(dst_roi.size(), CV_32FC1);
140
141
GpuMat map_x = _map_x.getGpuMat();
142
GpuMat map_y = _map_y.getGpuMat();
143
144
device::imgproc::buildWarpCylindricalMaps(dst_roi.tl().x, dst_roi.tl().y, map_x, map_y, K_Rinv.ptr<float>(), R_Kinv.ptr<float>(), scale, StreamAccessor::getStream(stream));
145
}
146
147
#endif
148
149
Rect cv::detail::PlaneWarperGpu::buildMaps(Size src_size, InputArray K, InputArray R,
150
cuda::GpuMat & xmap, cuda::GpuMat & ymap)
151
{
152
return buildMaps(src_size, K, R, Mat::zeros(3, 1, CV_32F), xmap, ymap);
153
}
154
155
Rect cv::detail::PlaneWarperGpu::buildMaps(Size src_size, InputArray K, InputArray R, InputArray T,
156
cuda::GpuMat & xmap, cuda::GpuMat & ymap)
157
{
158
#ifndef HAVE_CUDA
159
CV_UNUSED(src_size);
160
CV_UNUSED(K);
161
CV_UNUSED(R);
162
CV_UNUSED(T);
163
CV_UNUSED(xmap);
164
CV_UNUSED(ymap);
165
throw_no_cuda();
166
#else
167
projector_.setCameraParams(K, R, T);
168
169
Point dst_tl, dst_br;
170
detectResultRoi(src_size, dst_tl, dst_br);
171
172
::buildWarpPlaneMaps(src_size, Rect(dst_tl, Point(dst_br.x + 1, dst_br.y + 1)),
173
K, R, T, projector_.scale, xmap, ymap);
174
175
return Rect(dst_tl, dst_br);
176
#endif
177
}
178
179
Point cv::detail::PlaneWarperGpu::warp(const cuda::GpuMat & src, InputArray K, InputArray R,
180
int interp_mode, int border_mode,
181
cuda::GpuMat & dst)
182
{
183
return warp(src, K, R, Mat::zeros(3, 1, CV_32F), interp_mode, border_mode, dst);
184
}
185
186
187
Point cv::detail::PlaneWarperGpu::warp(const cuda::GpuMat & src, InputArray K, InputArray R, InputArray T,
188
int interp_mode, int border_mode,
189
cuda::GpuMat & dst)
190
{
191
#ifndef HAVE_OPENCV_CUDAWARPING
192
CV_UNUSED(src);
193
CV_UNUSED(K);
194
CV_UNUSED(R);
195
CV_UNUSED(T);
196
CV_UNUSED(interp_mode);
197
CV_UNUSED(border_mode);
198
CV_UNUSED(dst);
199
throw_no_cuda();
200
#else
201
Rect dst_roi = buildMaps(src.size(), K, R, T, d_xmap_, d_ymap_);
202
dst.create(dst_roi.height + 1, dst_roi.width + 1, src.type());
203
cuda::remap(src, dst, d_xmap_, d_ymap_, interp_mode, border_mode);
204
return dst_roi.tl();
205
#endif
206
}
207
208
Rect cv::detail::SphericalWarperGpu::buildMaps(Size src_size, InputArray K, InputArray R, cuda::GpuMat & xmap, cuda::GpuMat & ymap)
209
{
210
#ifndef HAVE_CUDA
211
CV_UNUSED(src_size);
212
CV_UNUSED(K);
213
CV_UNUSED(R);
214
CV_UNUSED(xmap);
215
CV_UNUSED(ymap);
216
throw_no_cuda();
217
#else
218
projector_.setCameraParams(K, R);
219
220
Point dst_tl, dst_br;
221
detectResultRoi(src_size, dst_tl, dst_br);
222
223
::buildWarpSphericalMaps(src_size, Rect(dst_tl, Point(dst_br.x + 1, dst_br.y + 1)),
224
K, R, projector_.scale, xmap, ymap);
225
226
return Rect(dst_tl, dst_br);
227
#endif
228
}
229
230
Point cv::detail::SphericalWarperGpu::warp(const cuda::GpuMat & src, InputArray K, InputArray R,
231
int interp_mode, int border_mode,
232
cuda::GpuMat & dst)
233
{
234
#ifndef HAVE_OPENCV_CUDAWARPING
235
CV_UNUSED(src);
236
CV_UNUSED(K);
237
CV_UNUSED(R);
238
CV_UNUSED(interp_mode);
239
CV_UNUSED(border_mode);
240
CV_UNUSED(dst);
241
throw_no_cuda();
242
#else
243
Rect dst_roi = buildMaps(src.size(), K, R, d_xmap_, d_ymap_);
244
dst.create(dst_roi.height + 1, dst_roi.width + 1, src.type());
245
cuda::remap(src, dst, d_xmap_, d_ymap_, interp_mode, border_mode);
246
return dst_roi.tl();
247
#endif
248
}
249
250
251
Rect cv::detail::CylindricalWarperGpu::buildMaps(Size src_size, InputArray K, InputArray R,
252
cuda::GpuMat & xmap, cuda::GpuMat & ymap)
253
{
254
#ifndef HAVE_CUDA
255
CV_UNUSED(src_size);
256
CV_UNUSED(K);
257
CV_UNUSED(R);
258
CV_UNUSED(xmap);
259
CV_UNUSED(ymap);
260
throw_no_cuda();
261
#else
262
projector_.setCameraParams(K, R);
263
264
Point dst_tl, dst_br;
265
detectResultRoi(src_size, dst_tl, dst_br);
266
267
::buildWarpCylindricalMaps(src_size, Rect(dst_tl, Point(dst_br.x + 1, dst_br.y + 1)),
268
K, R, projector_.scale, xmap, ymap);
269
270
return Rect(dst_tl, dst_br);
271
#endif
272
}
273
274
Point cv::detail::CylindricalWarperGpu::warp(const cuda::GpuMat & src, InputArray K, InputArray R,
275
int interp_mode, int border_mode,
276
cuda::GpuMat & dst)
277
{
278
#ifndef HAVE_OPENCV_CUDAWARPING
279
CV_UNUSED(src);
280
CV_UNUSED(K);
281
CV_UNUSED(R);
282
CV_UNUSED(interp_mode);
283
CV_UNUSED(border_mode);
284
CV_UNUSED(dst);
285
throw_no_cuda();
286
#else
287
Rect dst_roi = buildMaps(src.size(), K, R, d_xmap_, d_ymap_);
288
dst.create(dst_roi.height + 1, dst_roi.width + 1, src.type());
289
cuda::remap(src, dst, d_xmap_, d_ymap_, interp_mode, border_mode);
290
return dst_roi.tl();
291
#endif
292
}
293
294