warpers.hpp
Go to the documentation of this file.
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 #ifndef __OPENCV_STITCHING_WARPERS_HPP__
44 #define __OPENCV_STITCHING_WARPERS_HPP__
45 
46 #include "opencv2/core/core.hpp"
47 #include "opencv2/core/gpumat.hpp"
49 
50 namespace cv {
51 namespace detail {
52 
53 class CV_EXPORTS RotationWarper
54 {
55 public:
56  virtual ~RotationWarper() {}
57 
58  virtual Point2f warpPoint(const Point2f &pt, const Mat &K, const Mat &R) = 0;
59 
60  virtual Rect buildMaps(Size src_size, const Mat &K, const Mat &R, Mat &xmap, Mat &ymap) = 0;
61 
62  virtual Point warp(const Mat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode,
63  Mat &dst) = 0;
64 
65  virtual void warpBackward(const Mat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode,
66  Size dst_size, Mat &dst) = 0;
67 
68  virtual Rect warpRoi(Size src_size, const Mat &K, const Mat &R) = 0;
69 
70  float getScale() const { return 1.f; }
71  void setScale(float) {}
72 };
73 
74 
75 struct CV_EXPORTS ProjectorBase
76 {
77  void setCameraParams(const Mat &K = Mat::eye(3, 3, CV_32F),
78  const Mat &R = Mat::eye(3, 3, CV_32F),
79  const Mat &T = Mat::zeros(3, 1, CV_32F));
80 
81  float scale;
82  float k[9];
83  float rinv[9];
84  float r_kinv[9];
85  float k_rinv[9];
86  float t[3];
87 };
88 
89 
90 template <class P>
91 class CV_EXPORTS RotationWarperBase : public RotationWarper
92 {
93 public:
94  Point2f warpPoint(const Point2f &pt, const Mat &K, const Mat &R);
95 
96  Rect buildMaps(Size src_size, const Mat &K, const Mat &R, Mat &xmap, Mat &ymap);
97 
98  Point warp(const Mat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode,
99  Mat &dst);
100 
101  void warpBackward(const Mat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode,
102  Size dst_size, Mat &dst);
103 
104  Rect warpRoi(Size src_size, const Mat &K, const Mat &R);
105 
106  float getScale() const { return projector_.scale; }
107  void setScale(float val) { projector_.scale = val; }
108 
109 protected:
110 
111  // Detects ROI of the destination image. It's correct for any projection.
112  virtual void detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br);
113 
114  // Detects ROI of the destination image by walking over image border.
115  // Correctness for any projection isn't guaranteed.
116  void detectResultRoiByBorder(Size src_size, Point &dst_tl, Point &dst_br);
117 
119 };
120 
121 
122 struct CV_EXPORTS PlaneProjector : ProjectorBase
123 {
124  void mapForward(float x, float y, float &u, float &v);
125  void mapBackward(float u, float v, float &x, float &y);
126 };
127 
128 
129 class CV_EXPORTS PlaneWarper : public RotationWarperBase<PlaneProjector>
130 {
131 public:
132  PlaneWarper(float scale = 1.f) { projector_.scale = scale; }
133 
134  void setScale(float scale) { projector_.scale = scale; }
135 
136  Point2f warpPoint(const Point2f &pt, const Mat &K, const Mat &R, const Mat &T);
137 
138  Rect buildMaps(Size src_size, const Mat &K, const Mat &R, const Mat &T, Mat &xmap, Mat &ymap);
139 
140  Point warp(const Mat &src, const Mat &K, const Mat &R, const Mat &T, int interp_mode, int border_mode,
141  Mat &dst);
142 
143  Rect warpRoi(Size src_size, const Mat &K, const Mat &R, const Mat &T);
144 
145 protected:
146  void detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br);
147 };
148 
149 
151 {
152  void mapForward(float x, float y, float &u, float &v);
153  void mapBackward(float u, float v, float &x, float &y);
154 };
155 
156 
157 // Projects image onto unit sphere with origin at (0, 0, 0).
158 // Poles are located at (0, -1, 0) and (0, 1, 0) points.
159 class CV_EXPORTS SphericalWarper : public RotationWarperBase<SphericalProjector>
160 {
161 public:
162  SphericalWarper(float scale) { projector_.scale = scale; }
163 
164 protected:
165  void detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br);
166 };
167 
168 
170 {
171  void mapForward(float x, float y, float &u, float &v);
172  void mapBackward(float u, float v, float &x, float &y);
173 };
174 
175 
176 // Projects image onto x * x + z * z = 1 cylinder
177 class CV_EXPORTS CylindricalWarper : public RotationWarperBase<CylindricalProjector>
178 {
179 public:
180  CylindricalWarper(float scale) { projector_.scale = scale; }
181 
182 protected:
183  void detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br)
184  {
186  }
187 };
188 
189 
190 struct CV_EXPORTS FisheyeProjector : ProjectorBase
191 {
192  void mapForward(float x, float y, float &u, float &v);
193  void mapBackward(float u, float v, float &x, float &y);
194 };
195 
196 
197 class CV_EXPORTS FisheyeWarper : public RotationWarperBase<FisheyeProjector>
198 {
199 public:
200  FisheyeWarper(float scale) { projector_.scale = scale; }
201 };
202 
203 
205 {
206  void mapForward(float x, float y, float &u, float &v);
207  void mapBackward(float u, float v, float &x, float &y);
208 };
209 
210 
211 class CV_EXPORTS StereographicWarper : public RotationWarperBase<StereographicProjector>
212 {
213 public:
214  StereographicWarper(float scale) { projector_.scale = scale; }
215 };
216 
217 
219 {
220  float a, b;
221 
222  void mapForward(float x, float y, float &u, float &v);
223  void mapBackward(float u, float v, float &x, float &y);
224 };
225 
226 
227 class CV_EXPORTS CompressedRectilinearWarper : public RotationWarperBase<CompressedRectilinearProjector>
228 {
229 public:
230  CompressedRectilinearWarper(float scale, float A = 1, float B = 1)
231  {
232  projector_.a = A;
233  projector_.b = B;
234  projector_.scale = scale;
235  }
236 };
237 
238 
240 {
241  float a, b;
242 
243  void mapForward(float x, float y, float &u, float &v);
244  void mapBackward(float u, float v, float &x, float &y);
245 };
246 
247 
248 class CV_EXPORTS CompressedRectilinearPortraitWarper : public RotationWarperBase<CompressedRectilinearPortraitProjector>
249 {
250 public:
251  CompressedRectilinearPortraitWarper(float scale, float A = 1, float B = 1)
252  {
253  projector_.a = A;
254  projector_.b = B;
255  projector_.scale = scale;
256  }
257 };
258 
259 
260 struct CV_EXPORTS PaniniProjector : ProjectorBase
261 {
262  float a, b;
263 
264  void mapForward(float x, float y, float &u, float &v);
265  void mapBackward(float u, float v, float &x, float &y);
266 };
267 
268 
269 class CV_EXPORTS PaniniWarper : public RotationWarperBase<PaniniProjector>
270 {
271 public:
272  PaniniWarper(float scale, float A = 1, float B = 1)
273  {
274  projector_.a = A;
275  projector_.b = B;
276  projector_.scale = scale;
277  }
278 };
279 
280 
282 {
283  float a, b;
284 
285  void mapForward(float x, float y, float &u, float &v);
286  void mapBackward(float u, float v, float &x, float &y);
287 };
288 
289 
290 class CV_EXPORTS PaniniPortraitWarper : public RotationWarperBase<PaniniPortraitProjector>
291 {
292 public:
293  PaniniPortraitWarper(float scale, float A = 1, float B = 1)
294  {
295  projector_.a = A;
296  projector_.b = B;
297  projector_.scale = scale;
298  }
299 
300 };
301 
302 
303 struct CV_EXPORTS MercatorProjector : ProjectorBase
304 {
305  void mapForward(float x, float y, float &u, float &v);
306  void mapBackward(float u, float v, float &x, float &y);
307 };
308 
309 
310 class CV_EXPORTS MercatorWarper : public RotationWarperBase<MercatorProjector>
311 {
312 public:
313  MercatorWarper(float scale) { projector_.scale = scale; }
314 };
315 
316 
318 {
319  void mapForward(float x, float y, float &u, float &v);
320  void mapBackward(float u, float v, float &x, float &y);
321 };
322 
323 
324 class CV_EXPORTS TransverseMercatorWarper : public RotationWarperBase<TransverseMercatorProjector>
325 {
326 public:
327  TransverseMercatorWarper(float scale) { projector_.scale = scale; }
328 };
329 
330 
331 class CV_EXPORTS PlaneWarperGpu : public PlaneWarper
332 {
333 public:
335 
336  Rect buildMaps(Size src_size, const Mat &K, const Mat &R, Mat &xmap, Mat &ymap)
337  {
338  Rect result = buildMaps(src_size, K, R, d_xmap_, d_ymap_);
339  d_xmap_.download(xmap);
340  d_ymap_.download(ymap);
341  return result;
342  }
343 
344  Rect buildMaps(Size src_size, const Mat &K, const Mat &R, const Mat &T, Mat &xmap, Mat &ymap)
345  {
346  Rect result = buildMaps(src_size, K, R, T, d_xmap_, d_ymap_);
347  d_xmap_.download(xmap);
348  d_ymap_.download(ymap);
349  return result;
350  }
351 
352  Point warp(const Mat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode,
353  Mat &dst)
354  {
355  d_src_.upload(src);
356  Point result = warp(d_src_, K, R, interp_mode, border_mode, d_dst_);
357  d_dst_.download(dst);
358  return result;
359  }
360 
361  Point warp(const Mat &src, const Mat &K, const Mat &R, const Mat &T, int interp_mode, int border_mode,
362  Mat &dst)
363  {
364  d_src_.upload(src);
365  Point result = warp(d_src_, K, R, T, interp_mode, border_mode, d_dst_);
366  d_dst_.download(dst);
367  return result;
368  }
369 
370  Rect buildMaps(Size src_size, const Mat &K, const Mat &R, gpu::GpuMat &xmap, gpu::GpuMat &ymap);
371 
372  Rect buildMaps(Size src_size, const Mat &K, const Mat &R, const Mat &T, gpu::GpuMat &xmap, gpu::GpuMat &ymap);
373 
374  Point warp(const gpu::GpuMat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode,
375  gpu::GpuMat &dst);
376 
377  Point warp(const gpu::GpuMat &src, const Mat &K, const Mat &R, const Mat &T, int interp_mode, int border_mode,
378  gpu::GpuMat &dst);
379 
380 private:
381  gpu::GpuMat d_xmap_, d_ymap_, d_src_, d_dst_;
382 };
383 
384 
385 class CV_EXPORTS SphericalWarperGpu : public SphericalWarper
386 {
387 public:
389 
390  Rect buildMaps(Size src_size, const Mat &K, const Mat &R, Mat &xmap, Mat &ymap)
391  {
392  Rect result = buildMaps(src_size, K, R, d_xmap_, d_ymap_);
393  d_xmap_.download(xmap);
394  d_ymap_.download(ymap);
395  return result;
396  }
397 
398  Point warp(const Mat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode,
399  Mat &dst)
400  {
401  d_src_.upload(src);
402  Point result = warp(d_src_, K, R, interp_mode, border_mode, d_dst_);
403  d_dst_.download(dst);
404  return result;
405  }
406 
407  Rect buildMaps(Size src_size, const Mat &K, const Mat &R, gpu::GpuMat &xmap, gpu::GpuMat &ymap);
408 
409  Point warp(const gpu::GpuMat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode,
410  gpu::GpuMat &dst);
411 
412 private:
413  gpu::GpuMat d_xmap_, d_ymap_, d_src_, d_dst_;
414 };
415 
416 
417 class CV_EXPORTS CylindricalWarperGpu : public CylindricalWarper
418 {
419 public:
421 
422  Rect buildMaps(Size src_size, const Mat &K, const Mat &R, Mat &xmap, Mat &ymap)
423  {
424  Rect result = buildMaps(src_size, K, R, d_xmap_, d_ymap_);
425  d_xmap_.download(xmap);
426  d_ymap_.download(ymap);
427  return result;
428  }
429 
430  Point warp(const Mat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode,
431  Mat &dst)
432  {
433  d_src_.upload(src);
434  Point result = warp(d_src_, K, R, interp_mode, border_mode, d_dst_);
435  d_dst_.download(dst);
436  return result;
437  }
438 
439  Rect buildMaps(Size src_size, const Mat &K, const Mat &R, gpu::GpuMat &xmap, gpu::GpuMat &ymap);
440 
441  Point warp(const gpu::GpuMat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode,
442  gpu::GpuMat &dst);
443 
444 private:
445  gpu::GpuMat d_xmap_, d_ymap_, d_src_, d_dst_;
446 };
447 
448 
450 {
451  void mapForward(float x, float y, float &u, float &v);
452  void mapBackward(float u, float v, float &x, float &y);
453 };
454 
455 
456 // Projects image onto unit sphere with origin at (0, 0, 0).
457 // Poles are located NOT at (0, -1, 0) and (0, 1, 0) points, BUT at (1, 0, 0) and (-1, 0, 0) points.
458 class CV_EXPORTS SphericalPortraitWarper : public RotationWarperBase<SphericalPortraitProjector>
459 {
460 public:
461  SphericalPortraitWarper(float scale) { projector_.scale = scale; }
462 
463 protected:
464  void detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br);
465 };
466 
468 {
469  void mapForward(float x, float y, float &u, float &v);
470  void mapBackward(float u, float v, float &x, float &y);
471 };
472 
473 
474 class CV_EXPORTS CylindricalPortraitWarper : public RotationWarperBase<CylindricalPortraitProjector>
475 {
476 public:
477  CylindricalPortraitWarper(float scale) { projector_.scale = scale; }
478 
479 protected:
480  void detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br)
481  {
483  }
484 };
485 
487 {
488  void mapForward(float x, float y, float &u, float &v);
489  void mapBackward(float u, float v, float &x, float &y);
490 };
491 
492 
493 class CV_EXPORTS PlanePortraitWarper : public RotationWarperBase<PlanePortraitProjector>
494 {
495 public:
496  PlanePortraitWarper(float scale) { projector_.scale = scale; }
497 
498 protected:
499  void detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br)
500  {
502  }
503 };
504 
505 } // namespace detail
506 } // namespace cv
507 
508 #include "warpers_inl.hpp"
509 
510 #endif // __OPENCV_STITCHING_WARPERS_HPP__
CylindricalPortraitWarper(float scale)
Definition: warpers.hpp:477
void mapForward(float x, float y, float &u, float &v)
Definition: warpers_inl.hpp:721
CvPoint2D32f pt[4]
Definition: imgproc_c.h:410
Definition: warpers.hpp:290
GLenum GLint GLint y
Definition: core_c.h:613
CvPoint2D32f CvPoint2D32f * warpPoint
Definition: legacy.hpp:547
Definition: warpers.hpp:75
virtual ~RotationWarper()
Definition: warpers.hpp:56
Rect buildMaps(Size src_size, const Mat &K, const Mat &R, Mat &xmap, Mat &ymap)
Definition: warpers.hpp:422
CompressedRectilinearPortraitWarper(float scale, float A=1, float B=1)
Definition: warpers.hpp:251
void mapForward(float x, float y, float &u, float &v)
Definition: warpers_inl.hpp:676
Definition: warpers.hpp:458
void detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br)
Definition: warpers.hpp:183
static MatExpr eye(int rows, int cols, int type)
P projector_
Definition: warpers.hpp:118
SphericalPortraitWarper(float scale)
Definition: warpers.hpp:461
Definition: warpers.hpp:211
GLuint src
Definition: core_c.h:1650
float b
Definition: warpers.hpp:262
Definition: warpers.hpp:204
Definition: warpers.hpp:129
Definition: warpers.hpp:190
const CvMat const CvMat const CvMat CvMat CvMat CvMat CvMat CvSize CvMat CvMat * T
Definition: calib3d.hpp:270
void mapBackward(float u, float v, float &x, float &y)
Definition: warpers_inl.hpp:649
void setScale(float val)
Definition: warpers.hpp:107
Definition: warpers.hpp:385
SphericalWarperGpu(float scale)
Definition: warpers.hpp:388
Definition: warpers.hpp:449
Rect buildMaps(Size src_size, const Mat &K, const Mat &R, Mat &xmap, Mat &ymap)
Definition: warpers.hpp:390
Definition: warpers.hpp:53
float b
Definition: warpers.hpp:283
PlanePortraitWarper(float scale)
Definition: warpers.hpp:496
The 2D size class.
Definition: core.hpp:81
const CvArr const CvArr CvArr * result
Definition: core_c.h:805
SphericalWarper(float scale)
Definition: warpers.hpp:162
Definition: warpers.hpp:281
Definition: warpers.hpp:474
FisheyeWarper(float scale)
Definition: warpers.hpp:200
static MatExpr zeros(int rows, int cols, int type)
Matlab-style matrix initialization.
Definition: warpers.hpp:150
GLuint GLfloat * val
StereographicWarper(float scale)
Definition: warpers.hpp:214
Point warp(const Mat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode, Mat &dst)
Definition: warpers.hpp:398
void mapBackward(float u, float v, float &x, float &y)
Definition: warpers_inl.hpp:743
PlaneWarperGpu(float scale=1.f)
Definition: warpers.hpp:334
MercatorWarper(float scale)
Definition: warpers.hpp:313
const CvMat const CvMat const CvMat CvMat CvMat CvMat CvMat CvSize CvMat * R
Definition: calib3d.hpp:270
Definition: warpers.hpp:177
Point warp(const Mat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode, Mat &dst)
Definition: warpers.hpp:430
void detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br)
Definition: warpers.hpp:480
PaniniPortraitWarper(float scale, float A=1, float B=1)
Definition: warpers.hpp:293
float getScale() const
Definition: warpers.hpp:70
Definition: warpers.hpp:227
const CvMat CvMat CvMat int k
Definition: legacy.hpp:3052
Smart pointer for GPU memory with reference counting. Its interface is mostly similar with cv::Mat...
Definition: gpumat.hpp:154
GLenum GLint x
Definition: core_c.h:632
Definition: warpers.hpp:467
PlaneWarper(float scale=1.f)
Definition: warpers.hpp:132
TransverseMercatorWarper(float scale)
Definition: warpers.hpp:327
Definition: warpers.hpp:486
The Image Processing.
Definition: warpers.hpp:91
const GLdouble * v
void mapForward(float x, float y, float &u, float &v)
Definition: warpers_inl.hpp:630
Definition: warpers.hpp:310
Point warp(const Mat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode, Mat &dst)
Definition: warpers.hpp:352
The Core Functionality.
The n-dimensional matrix class.
Definition: core.hpp:1688
void detectResultRoiByBorder(Size src_size, Point &dst_tl, Point &dst_br)
Definition: warpers_inl.hpp:170
void setScale(float)
Definition: warpers.hpp:71
void detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br)
Definition: warpers.hpp:499
float b
Definition: warpers.hpp:220
CvScalar scale
Definition: core_c.h:518
Rect buildMaps(Size src_size, const Mat &K, const Mat &R, const Mat &T, Mat &xmap, Mat &ymap)
Definition: warpers.hpp:344
template 2D point class.
Definition: core.hpp:82
Definition: warpers.hpp:331
Definition: warpers.hpp:417
Definition: warpers.hpp:197
void setScale(float scale)
Definition: warpers.hpp:134
Definition: warpers.hpp:260
Definition: warpers.hpp:317
GLuint dst
Definition: calib3d.hpp:134
CylindricalWarper(float scale)
Definition: warpers.hpp:180
Definition: warpers.hpp:493
void mapBackward(float u, float v, float &x, float &y)
Definition: warpers_inl.hpp:695
float scale
Definition: warpers.hpp:81
Definition: warpers.hpp:122
CvPoint2D32f float a
Definition: legacy.hpp:578
Definition: warpers.hpp:159
Definition: warpers.hpp:324
float getScale() const
Definition: warpers.hpp:106
const CvMat * B
Definition: calib3d.hpp:161
Rect buildMaps(Size src_size, const Mat &K, const Mat &R, Mat &xmap, Mat &ymap)
Definition: warpers.hpp:336
GLenum GLenum GLenum GLenum GLenum scale
GLdouble GLdouble t
Point warp(const Mat &src, const Mat &K, const Mat &R, const Mat &T, int interp_mode, int border_mode, Mat &dst)
Definition: warpers.hpp:361
CylindricalWarperGpu(float scale)
Definition: warpers.hpp:420
GLclampf f
Definition: warpers.hpp:269
CompressedRectilinearWarper(float scale, float A=1, float B=1)
Definition: warpers.hpp:230
PaniniWarper(float scale, float A=1, float B=1)
Definition: warpers.hpp:272
Definition: warpers.hpp:303
Definition: warpers.hpp:169