1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
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.
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.
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.
43 #ifndef __OPENCV_STITCHING_WARPERS_HPP__
44 #define __OPENCV_STITCHING_WARPERS_HPP__
46 #include "opencv2/core/core.hpp"
47 #include "opencv2/imgproc/imgproc.hpp"
48 #include "opencv2/opencv_modules.hpp"
49 #ifdef HAVE_OPENCV_GPU
50 # include "opencv2/gpu/gpu.hpp"
56 class CV_EXPORTS RotationWarper
59 virtual ~RotationWarper() {}
61 virtual Point2f warpPoint(const Point2f &pt, const Mat &K, const Mat &R) = 0;
63 virtual Rect buildMaps(Size src_size, const Mat &K, const Mat &R, Mat &xmap, Mat &ymap) = 0;
65 virtual Point warp(const Mat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode,
68 virtual void warpBackward(const Mat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode,
69 Size dst_size, Mat &dst) = 0;
71 virtual Rect warpRoi(Size src_size, const Mat &K, const Mat &R) = 0;
73 virtual float getScale() const { return 1.f; }
74 virtual void setScale(float) {}
78 struct CV_EXPORTS ProjectorBase
80 void setCameraParams(const Mat &K = Mat::eye(3, 3, CV_32F),
81 const Mat &R = Mat::eye(3, 3, CV_32F),
82 const Mat &T = Mat::zeros(3, 1, CV_32F));
94 class CV_EXPORTS RotationWarperBase : public RotationWarper
97 Point2f warpPoint(const Point2f &pt, const Mat &K, const Mat &R);
99 Rect buildMaps(Size src_size, const Mat &K, const Mat &R, Mat &xmap, Mat &ymap);
101 Point warp(const Mat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode,
104 void warpBackward(const Mat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode,
105 Size dst_size, Mat &dst);
107 Rect warpRoi(Size src_size, const Mat &K, const Mat &R);
109 float getScale() const { return projector_.scale; }
110 void setScale(float val) { projector_.scale = val; }
114 // Detects ROI of the destination image. It's correct for any projection.
115 virtual void detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br);
117 // Detects ROI of the destination image by walking over image border.
118 // Correctness for any projection isn't guaranteed.
119 void detectResultRoiByBorder(Size src_size, Point &dst_tl, Point &dst_br);
125 struct CV_EXPORTS PlaneProjector : ProjectorBase
127 void mapForward(float x, float y, float &u, float &v);
128 void mapBackward(float u, float v, float &x, float &y);
132 class CV_EXPORTS PlaneWarper : public RotationWarperBase<PlaneProjector>
135 PlaneWarper(float scale = 1.f) { projector_.scale = scale; }
137 Point2f warpPoint(const Point2f &pt, const Mat &K, const Mat &R, const Mat &T);
139 Rect buildMaps(Size src_size, const Mat &K, const Mat &R, const Mat &T, Mat &xmap, Mat &ymap);
141 Point warp(const Mat &src, const Mat &K, const Mat &R, const Mat &T, int interp_mode, int border_mode,
144 Rect warpRoi(Size src_size, const Mat &K, const Mat &R, const Mat &T);
147 void detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br);
151 struct CV_EXPORTS SphericalProjector : ProjectorBase
153 void mapForward(float x, float y, float &u, float &v);
154 void mapBackward(float u, float v, float &x, float &y);
158 // Projects image onto unit sphere with origin at (0, 0, 0).
159 // Poles are located at (0, -1, 0) and (0, 1, 0) points.
160 class CV_EXPORTS SphericalWarper : public RotationWarperBase<SphericalProjector>
163 SphericalWarper(float scale) { projector_.scale = scale; }
166 void detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br);
170 struct CV_EXPORTS CylindricalProjector : ProjectorBase
172 void mapForward(float x, float y, float &u, float &v);
173 void mapBackward(float u, float v, float &x, float &y);
177 // Projects image onto x * x + z * z = 1 cylinder
178 class CV_EXPORTS CylindricalWarper : public RotationWarperBase<CylindricalProjector>
181 CylindricalWarper(float scale) { projector_.scale = scale; }
184 void detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br)
186 RotationWarperBase<CylindricalProjector>::detectResultRoiByBorder(src_size, dst_tl, dst_br);
191 struct CV_EXPORTS FisheyeProjector : ProjectorBase
193 void mapForward(float x, float y, float &u, float &v);
194 void mapBackward(float u, float v, float &x, float &y);
198 class CV_EXPORTS FisheyeWarper : public RotationWarperBase<FisheyeProjector>
201 FisheyeWarper(float scale) { projector_.scale = scale; }
205 struct CV_EXPORTS StereographicProjector : ProjectorBase
207 void mapForward(float x, float y, float &u, float &v);
208 void mapBackward(float u, float v, float &x, float &y);
212 class CV_EXPORTS StereographicWarper : public RotationWarperBase<StereographicProjector>
215 StereographicWarper(float scale) { projector_.scale = scale; }
219 struct CV_EXPORTS CompressedRectilinearProjector : ProjectorBase
223 void mapForward(float x, float y, float &u, float &v);
224 void mapBackward(float u, float v, float &x, float &y);
228 class CV_EXPORTS CompressedRectilinearWarper : public RotationWarperBase<CompressedRectilinearProjector>
231 CompressedRectilinearWarper(float scale, float A = 1, float B = 1)
235 projector_.scale = scale;
240 struct CV_EXPORTS CompressedRectilinearPortraitProjector : ProjectorBase
244 void mapForward(float x, float y, float &u, float &v);
245 void mapBackward(float u, float v, float &x, float &y);
249 class CV_EXPORTS CompressedRectilinearPortraitWarper : public RotationWarperBase<CompressedRectilinearPortraitProjector>
252 CompressedRectilinearPortraitWarper(float scale, float A = 1, float B = 1)
256 projector_.scale = scale;
261 struct CV_EXPORTS PaniniProjector : ProjectorBase
265 void mapForward(float x, float y, float &u, float &v);
266 void mapBackward(float u, float v, float &x, float &y);
270 class CV_EXPORTS PaniniWarper : public RotationWarperBase<PaniniProjector>
273 PaniniWarper(float scale, float A = 1, float B = 1)
277 projector_.scale = scale;
282 struct CV_EXPORTS PaniniPortraitProjector : ProjectorBase
286 void mapForward(float x, float y, float &u, float &v);
287 void mapBackward(float u, float v, float &x, float &y);
291 class CV_EXPORTS PaniniPortraitWarper : public RotationWarperBase<PaniniPortraitProjector>
294 PaniniPortraitWarper(float scale, float A = 1, float B = 1)
298 projector_.scale = scale;
304 struct CV_EXPORTS MercatorProjector : ProjectorBase
306 void mapForward(float x, float y, float &u, float &v);
307 void mapBackward(float u, float v, float &x, float &y);
311 class CV_EXPORTS MercatorWarper : public RotationWarperBase<MercatorProjector>
314 MercatorWarper(float scale) { projector_.scale = scale; }
318 struct CV_EXPORTS TransverseMercatorProjector : ProjectorBase
320 void mapForward(float x, float y, float &u, float &v);
321 void mapBackward(float u, float v, float &x, float &y);
325 class CV_EXPORTS TransverseMercatorWarper : public RotationWarperBase<TransverseMercatorProjector>
328 TransverseMercatorWarper(float scale) { projector_.scale = scale; }
332 #ifdef HAVE_OPENCV_GPU
333 class CV_EXPORTS PlaneWarperGpu : public PlaneWarper
336 PlaneWarperGpu(float scale = 1.f) : PlaneWarper(scale) {}
338 Rect buildMaps(Size src_size, const Mat &K, const Mat &R, Mat &xmap, Mat &ymap)
340 Rect result = buildMaps(src_size, K, R, d_xmap_, d_ymap_);
341 d_xmap_.download(xmap);
342 d_ymap_.download(ymap);
346 Rect buildMaps(Size src_size, const Mat &K, const Mat &R, const Mat &T, Mat &xmap, Mat &ymap)
348 Rect result = buildMaps(src_size, K, R, T, d_xmap_, d_ymap_);
349 d_xmap_.download(xmap);
350 d_ymap_.download(ymap);
354 Point warp(const Mat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode,
358 Point result = warp(d_src_, K, R, interp_mode, border_mode, d_dst_);
359 d_dst_.download(dst);
363 Point warp(const Mat &src, const Mat &K, const Mat &R, const Mat &T, int interp_mode, int border_mode,
367 Point result = warp(d_src_, K, R, T, interp_mode, border_mode, d_dst_);
368 d_dst_.download(dst);
372 Rect buildMaps(Size src_size, const Mat &K, const Mat &R, gpu::GpuMat &xmap, gpu::GpuMat &ymap);
374 Rect buildMaps(Size src_size, const Mat &K, const Mat &R, const Mat &T, gpu::GpuMat &xmap, gpu::GpuMat &ymap);
376 Point warp(const gpu::GpuMat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode,
379 Point warp(const gpu::GpuMat &src, const Mat &K, const Mat &R, const Mat &T, int interp_mode, int border_mode,
383 gpu::GpuMat d_xmap_, d_ymap_, d_src_, d_dst_;
387 class CV_EXPORTS SphericalWarperGpu : public SphericalWarper
390 SphericalWarperGpu(float scale) : SphericalWarper(scale) {}
392 Rect buildMaps(Size src_size, const Mat &K, const Mat &R, Mat &xmap, Mat &ymap)
394 Rect result = buildMaps(src_size, K, R, d_xmap_, d_ymap_);
395 d_xmap_.download(xmap);
396 d_ymap_.download(ymap);
400 Point warp(const Mat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode,
404 Point result = warp(d_src_, K, R, interp_mode, border_mode, d_dst_);
405 d_dst_.download(dst);
409 Rect buildMaps(Size src_size, const Mat &K, const Mat &R, gpu::GpuMat &xmap, gpu::GpuMat &ymap);
411 Point warp(const gpu::GpuMat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode,
415 gpu::GpuMat d_xmap_, d_ymap_, d_src_, d_dst_;
419 class CV_EXPORTS CylindricalWarperGpu : public CylindricalWarper
422 CylindricalWarperGpu(float scale) : CylindricalWarper(scale) {}
424 Rect buildMaps(Size src_size, const Mat &K, const Mat &R, Mat &xmap, Mat &ymap)
426 Rect result = buildMaps(src_size, K, R, d_xmap_, d_ymap_);
427 d_xmap_.download(xmap);
428 d_ymap_.download(ymap);
432 Point warp(const Mat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode,
436 Point result = warp(d_src_, K, R, interp_mode, border_mode, d_dst_);
437 d_dst_.download(dst);
441 Rect buildMaps(Size src_size, const Mat &K, const Mat &R, gpu::GpuMat &xmap, gpu::GpuMat &ymap);
443 Point warp(const gpu::GpuMat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode,
447 gpu::GpuMat d_xmap_, d_ymap_, d_src_, d_dst_;
452 struct SphericalPortraitProjector : ProjectorBase
454 void mapForward(float x, float y, float &u, float &v);
455 void mapBackward(float u, float v, float &x, float &y);
459 // Projects image onto unit sphere with origin at (0, 0, 0).
460 // Poles are located NOT at (0, -1, 0) and (0, 1, 0) points, BUT at (1, 0, 0) and (-1, 0, 0) points.
461 class SphericalPortraitWarper : public RotationWarperBase<SphericalPortraitProjector>
464 SphericalPortraitWarper(float scale) { projector_.scale = scale; }
467 void detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br);
470 struct CylindricalPortraitProjector : ProjectorBase
472 void mapForward(float x, float y, float &u, float &v);
473 void mapBackward(float u, float v, float &x, float &y);
477 class CylindricalPortraitWarper : public RotationWarperBase<CylindricalPortraitProjector>
480 CylindricalPortraitWarper(float scale) { projector_.scale = scale; }
483 void detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br)
485 RotationWarperBase<CylindricalPortraitProjector>::detectResultRoiByBorder(src_size, dst_tl, dst_br);
489 struct PlanePortraitProjector : ProjectorBase
491 void mapForward(float x, float y, float &u, float &v);
492 void mapBackward(float u, float v, float &x, float &y);
496 class PlanePortraitWarper : public RotationWarperBase<PlanePortraitProjector>
499 PlanePortraitWarper(float scale) { projector_.scale = scale; }
502 void detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br)
504 RotationWarperBase<PlanePortraitProjector>::detectResultRoiByBorder(src_size, dst_tl, dst_br);
508 } // namespace detail
511 #include "warpers_inl.hpp"
513 #endif // __OPENCV_STITCHING_WARPERS_HPP__