Joe Verbout / Mbed 2 deprecated main

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers warpers.hpp Source File

warpers.hpp

00001  /*M///////////////////////////////////////////////////////////////////////////////////////
00002 //
00003 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
00004 //
00005 //  By downloading, copying, installing or using the software you agree to this license.
00006 //  If you do not agree to this license, do not download, install,
00007 //  copy or use the software.
00008 //
00009 //
00010 //                          License Agreement
00011 //                For Open Source Computer Vision Library
00012 //
00013 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
00014 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
00015 // Third party copyrights are property of their respective owners.
00016 //
00017 // Redistribution and use in source and binary forms, with or without modification,
00018 // are permitted provided that the following conditions are met:
00019 //
00020 //   * Redistribution's of source code must retain the above copyright notice,
00021 //     this list of conditions and the following disclaimer.
00022 //
00023 //   * Redistribution's in binary form must reproduce the above copyright notice,
00024 //     this list of conditions and the following disclaimer in the documentation
00025 //     and/or other materials provided with the distribution.
00026 //
00027 //   * The name of the copyright holders may not be used to endorse or promote products
00028 //     derived from this software without specific prior written permission.
00029 //
00030 // This software is provided by the copyright holders and contributors "as is" and
00031 // any express or implied warranties, including, but not limited to, the implied
00032 // warranties of merchantability and fitness for a particular purpose are disclaimed.
00033 // In no event shall the Intel Corporation or contributors be liable for any direct,
00034 // indirect, incidental, special, exemplary, or consequential damages
00035 // (including, but not limited to, procurement of substitute goods or services;
00036 // loss of use, data, or profits; or business interruption) however caused
00037 // and on any theory of liability, whether in contract, strict liability,
00038 // or tort (including negligence or otherwise) arising in any way out of
00039 // the use of this software, even if advised of the possibility of such damage.
00040 //
00041 //M*/
00042 
00043 #ifndef __OPENCV_STITCHING_WARPERS_HPP__
00044 #define __OPENCV_STITCHING_WARPERS_HPP__
00045 
00046 #include "opencv2/core.hpp"
00047 #include "opencv2/core/cuda.hpp"
00048 #include "opencv2/imgproc.hpp"
00049 #include "opencv2/opencv_modules.hpp"
00050 
00051 namespace cv {
00052 namespace detail {
00053 
00054 //! @addtogroup stitching_warp
00055 //! @{
00056 
00057 /** @brief Rotation-only model image warper interface.
00058  */
00059 class CV_EXPORTS RotationWarper
00060 {
00061 public:
00062     virtual ~RotationWarper() {}
00063 
00064     /** @brief Projects the image point.
00065 
00066     @param pt Source point
00067     @param K Camera intrinsic parameters
00068     @param R Camera rotation matrix
00069     @return Projected point
00070      */
00071     virtual Point2f  warpPoint(const Point2f  &pt, InputArray K, InputArray R) = 0;
00072 
00073     /** @brief Builds the projection maps according to the given camera data.
00074 
00075     @param src_size Source image size
00076     @param K Camera intrinsic parameters
00077     @param R Camera rotation matrix
00078     @param xmap Projection map for the x axis
00079     @param ymap Projection map for the y axis
00080     @return Projected image minimum bounding box
00081      */
00082     virtual Rect buildMaps(Size src_size, InputArray K, InputArray R, OutputArray xmap, OutputArray ymap) = 0;
00083 
00084     /** @brief Projects the image.
00085 
00086     @param src Source image
00087     @param K Camera intrinsic parameters
00088     @param R Camera rotation matrix
00089     @param interp_mode Interpolation mode
00090     @param border_mode Border extrapolation mode
00091     @param dst Projected image
00092     @return Project image top-left corner
00093      */
00094     virtual Point warp(InputArray src, InputArray K, InputArray R, int interp_mode, int border_mode,
00095                        OutputArray dst) = 0;
00096 
00097     /** @brief Projects the image backward.
00098 
00099     @param src Projected image
00100     @param K Camera intrinsic parameters
00101     @param R Camera rotation matrix
00102     @param interp_mode Interpolation mode
00103     @param border_mode Border extrapolation mode
00104     @param dst_size Backward-projected image size
00105     @param dst Backward-projected image
00106      */
00107     virtual void warpBackward(InputArray src, InputArray K, InputArray R, int interp_mode, int border_mode,
00108                               Size dst_size, OutputArray dst) = 0;
00109 
00110     /**
00111     @param src_size Source image bounding box
00112     @param K Camera intrinsic parameters
00113     @param R Camera rotation matrix
00114     @return Projected image minimum bounding box
00115      */
00116     virtual Rect warpRoi(Size src_size, InputArray K, InputArray R) = 0;
00117 
00118     virtual float getScale() const { return 1.f; }
00119     virtual void setScale(float) {}
00120 };
00121 
00122 /** @brief Base class for warping logic implementation.
00123  */
00124 struct CV_EXPORTS ProjectorBase
00125 {
00126     void setCameraParams(InputArray K = Mat::eye(3, 3, CV_32F),
00127                          InputArray R = Mat::eye(3, 3, CV_32F),
00128                          InputArray T = Mat::zeros(3, 1, CV_32F));
00129 
00130     float scale;
00131     float k[9];
00132     float rinv[9];
00133     float r_kinv[9];
00134     float k_rinv[9];
00135     float t[3];
00136 };
00137 
00138 /** @brief Base class for rotation-based warper using a detail::ProjectorBase_ derived class.
00139  */
00140 template <class P>
00141 class CV_EXPORTS RotationWarperBase : public RotationWarper
00142 {
00143 public:
00144     Point2f  warpPoint(const Point2f  &pt, InputArray K, InputArray R);
00145 
00146     Rect buildMaps(Size src_size, InputArray K, InputArray R, OutputArray xmap, OutputArray ymap);
00147 
00148     Point warp(InputArray src, InputArray K, InputArray R, int interp_mode, int border_mode,
00149                OutputArray dst);
00150 
00151     void warpBackward(InputArray src, InputArray K, InputArray R, int interp_mode, int border_mode,
00152                       Size dst_size, OutputArray dst);
00153 
00154     Rect warpRoi(Size src_size, InputArray K, InputArray R);
00155 
00156     float getScale() const { return projector_.scale; }
00157     void setScale(float val) { projector_.scale = val; }
00158 
00159 protected:
00160 
00161     // Detects ROI of the destination image. It's correct for any projection.
00162     virtual void detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br);
00163 
00164     // Detects ROI of the destination image by walking over image border.
00165     // Correctness for any projection isn't guaranteed.
00166     void detectResultRoiByBorder(Size src_size, Point &dst_tl, Point &dst_br);
00167 
00168     P projector_;
00169 };
00170 
00171 
00172 struct CV_EXPORTS PlaneProjector : ProjectorBase
00173 {
00174     void mapForward(float x, float y, float &u, float &v);
00175     void mapBackward(float u, float v, float &x, float &y);
00176 };
00177 
00178 /** @brief Warper that maps an image onto the z = 1 plane.
00179  */
00180 class CV_EXPORTS PlaneWarper : public RotationWarperBase<PlaneProjector>
00181 {
00182 public:
00183     /** @brief Construct an instance of the plane warper class.
00184 
00185     @param scale Projected image scale multiplier
00186      */
00187     PlaneWarper(float scale = 1.f) { projector_.scale = scale; }
00188 
00189     Point2f  warpPoint(const Point2f  &pt, InputArray K, InputArray R);
00190     Point2f  warpPoint(const Point2f  &pt, InputArray K, InputArray R, InputArray T);
00191 
00192     virtual Rect buildMaps(Size src_size, InputArray K, InputArray R, InputArray T, OutputArray xmap, OutputArray ymap);
00193     Rect buildMaps(Size src_size, InputArray K, InputArray R, OutputArray xmap, OutputArray ymap);
00194 
00195     Point warp(InputArray src, InputArray K, InputArray R,
00196                int interp_mode, int border_mode, OutputArray dst);
00197     virtual Point warp(InputArray src, InputArray K, InputArray R, InputArray T, int interp_mode, int border_mode,
00198                OutputArray dst);
00199 
00200     Rect warpRoi(Size src_size, InputArray K, InputArray R);
00201     Rect warpRoi(Size src_size, InputArray K, InputArray R, InputArray T);
00202 
00203 protected:
00204     void detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br);
00205 };
00206 
00207 
00208 struct CV_EXPORTS SphericalProjector : ProjectorBase
00209 {
00210     void mapForward(float x, float y, float &u, float &v);
00211     void mapBackward(float u, float v, float &x, float &y);
00212 };
00213 
00214 
00215 /** @brief Warper that maps an image onto the unit sphere located at the origin.
00216 
00217  Projects image onto unit sphere with origin at (0, 0, 0).
00218  Poles are located at (0, -1, 0) and (0, 1, 0) points.
00219 */
00220 class CV_EXPORTS SphericalWarper : public RotationWarperBase<SphericalProjector>
00221 {
00222 public:
00223     /** @brief Construct an instance of the spherical warper class.
00224 
00225     @param scale Projected image scale multiplier
00226      */
00227     SphericalWarper(float scale) { projector_.scale = scale; }
00228 
00229     Rect buildMaps(Size src_size, InputArray K, InputArray R, OutputArray xmap, OutputArray ymap);
00230     Point warp(InputArray src, InputArray K, InputArray R, int interp_mode, int border_mode, OutputArray dst);
00231 protected:
00232     void detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br);
00233 };
00234 
00235 
00236 struct CV_EXPORTS CylindricalProjector : ProjectorBase
00237 {
00238     void mapForward(float x, float y, float &u, float &v);
00239     void mapBackward(float u, float v, float &x, float &y);
00240 };
00241 
00242 
00243 /** @brief Warper that maps an image onto the x\*x + z\*z = 1 cylinder.
00244  */
00245 class CV_EXPORTS CylindricalWarper : public RotationWarperBase<CylindricalProjector>
00246 {
00247 public:
00248     /** @brief Construct an instance of the cylindrical warper class.
00249 
00250     @param scale Projected image scale multiplier
00251      */
00252     CylindricalWarper(float scale) { projector_.scale = scale; }
00253 
00254     Rect buildMaps(Size src_size, InputArray K, InputArray R, OutputArray xmap, OutputArray ymap);
00255     Point warp(InputArray src, InputArray K, InputArray R, int interp_mode, int border_mode, OutputArray dst);
00256 protected:
00257     void detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br)
00258     {
00259         RotationWarperBase<CylindricalProjector>::detectResultRoiByBorder(src_size, dst_tl, dst_br);
00260     }
00261 };
00262 
00263 
00264 struct CV_EXPORTS FisheyeProjector : ProjectorBase
00265 {
00266     void mapForward(float x, float y, float &u, float &v);
00267     void mapBackward(float u, float v, float &x, float &y);
00268 };
00269 
00270 
00271 class CV_EXPORTS FisheyeWarper : public RotationWarperBase<FisheyeProjector>
00272 {
00273 public:
00274     FisheyeWarper(float scale) { projector_.scale = scale; }
00275 };
00276 
00277 
00278 struct CV_EXPORTS StereographicProjector : ProjectorBase
00279 {
00280     void mapForward(float x, float y, float &u, float &v);
00281     void mapBackward(float u, float v, float &x, float &y);
00282 };
00283 
00284 
00285 class CV_EXPORTS StereographicWarper : public RotationWarperBase<StereographicProjector>
00286 {
00287 public:
00288     StereographicWarper(float scale) { projector_.scale = scale; }
00289 };
00290 
00291 
00292 struct CV_EXPORTS CompressedRectilinearProjector : ProjectorBase
00293 {
00294     float a, b;
00295 
00296     void mapForward(float x, float y, float &u, float &v);
00297     void mapBackward(float u, float v, float &x, float &y);
00298 };
00299 
00300 
00301 class CV_EXPORTS CompressedRectilinearWarper : public RotationWarperBase<CompressedRectilinearProjector>
00302 {
00303 public:
00304     CompressedRectilinearWarper(float scale, float A = 1, float B = 1)
00305     {
00306         projector_.a = A;
00307         projector_.b = B;
00308         projector_.scale = scale;
00309     }
00310 };
00311 
00312 
00313 struct CV_EXPORTS CompressedRectilinearPortraitProjector : ProjectorBase
00314 {
00315     float a, b;
00316 
00317     void mapForward(float x, float y, float &u, float &v);
00318     void mapBackward(float u, float v, float &x, float &y);
00319 };
00320 
00321 
00322 class CV_EXPORTS CompressedRectilinearPortraitWarper : public RotationWarperBase<CompressedRectilinearPortraitProjector>
00323 {
00324 public:
00325    CompressedRectilinearPortraitWarper(float scale, float A = 1, float B = 1)
00326    {
00327        projector_.a = A;
00328        projector_.b = B;
00329        projector_.scale = scale;
00330    }
00331 };
00332 
00333 
00334 struct CV_EXPORTS PaniniProjector : ProjectorBase
00335 {
00336     float a, b;
00337 
00338     void mapForward(float x, float y, float &u, float &v);
00339     void mapBackward(float u, float v, float &x, float &y);
00340 };
00341 
00342 
00343 class CV_EXPORTS PaniniWarper : public RotationWarperBase<PaniniProjector>
00344 {
00345 public:
00346    PaniniWarper(float scale, float A = 1, float B = 1)
00347    {
00348        projector_.a = A;
00349        projector_.b = B;
00350        projector_.scale = scale;
00351    }
00352 };
00353 
00354 
00355 struct CV_EXPORTS PaniniPortraitProjector : ProjectorBase
00356 {
00357     float a, b;
00358 
00359     void mapForward(float x, float y, float &u, float &v);
00360     void mapBackward(float u, float v, float &x, float &y);
00361 };
00362 
00363 
00364 class CV_EXPORTS PaniniPortraitWarper : public RotationWarperBase<PaniniPortraitProjector>
00365 {
00366 public:
00367    PaniniPortraitWarper(float scale, float A = 1, float B = 1)
00368    {
00369        projector_.a = A;
00370        projector_.b = B;
00371        projector_.scale = scale;
00372    }
00373 
00374 };
00375 
00376 
00377 struct CV_EXPORTS MercatorProjector : ProjectorBase
00378 {
00379     void mapForward(float x, float y, float &u, float &v);
00380     void mapBackward(float u, float v, float &x, float &y);
00381 };
00382 
00383 
00384 class CV_EXPORTS MercatorWarper : public RotationWarperBase<MercatorProjector>
00385 {
00386 public:
00387     MercatorWarper(float scale) { projector_.scale = scale; }
00388 };
00389 
00390 
00391 struct CV_EXPORTS TransverseMercatorProjector : ProjectorBase
00392 {
00393     void mapForward(float x, float y, float &u, float &v);
00394     void mapBackward(float u, float v, float &x, float &y);
00395 };
00396 
00397 
00398 class CV_EXPORTS TransverseMercatorWarper : public RotationWarperBase<TransverseMercatorProjector>
00399 {
00400 public:
00401     TransverseMercatorWarper(float scale) { projector_.scale = scale; }
00402 };
00403 
00404 
00405 class CV_EXPORTS PlaneWarperGpu : public PlaneWarper
00406 {
00407 public:
00408     PlaneWarperGpu(float scale = 1.f) : PlaneWarper(scale) {}
00409 
00410     Rect buildMaps(Size src_size, InputArray K, InputArray R, OutputArray xmap, OutputArray ymap)
00411     {
00412         Rect result = buildMaps(src_size, K, R, d_xmap_, d_ymap_);
00413         d_xmap_.download(xmap);
00414         d_ymap_.download(ymap);
00415         return result;
00416     }
00417 
00418     Rect buildMaps(Size src_size, InputArray K, InputArray R, InputArray T, OutputArray xmap, OutputArray ymap)
00419     {
00420         Rect result = buildMaps(src_size, K, R, T, d_xmap_, d_ymap_);
00421         d_xmap_.download(xmap);
00422         d_ymap_.download(ymap);
00423         return result;
00424     }
00425 
00426     Point warp(InputArray src, InputArray K, InputArray R, int interp_mode, int border_mode,
00427                OutputArray dst)
00428     {
00429         d_src_.upload(src);
00430         Point result = warp(d_src_, K, R, interp_mode, border_mode, d_dst_);
00431         d_dst_.download(dst);
00432         return result;
00433     }
00434 
00435     Point warp(InputArray src, InputArray K, InputArray R, InputArray T, int interp_mode, int border_mode,
00436                OutputArray dst)
00437     {
00438         d_src_.upload(src);
00439         Point result = warp(d_src_, K, R, T, interp_mode, border_mode, d_dst_);
00440         d_dst_.download(dst);
00441         return result;
00442     }
00443 
00444     Rect buildMaps(Size src_size, InputArray K, InputArray R, cuda::GpuMat & xmap, cuda::GpuMat & ymap);
00445 
00446     Rect buildMaps(Size src_size, InputArray K, InputArray R, InputArray T, cuda::GpuMat & xmap, cuda::GpuMat & ymap);
00447 
00448     Point warp(const cuda::GpuMat & src, InputArray K, InputArray R, int interp_mode, int border_mode,
00449                cuda::GpuMat & dst);
00450 
00451     Point warp(const cuda::GpuMat & src, InputArray K, InputArray R, InputArray T, int interp_mode, int border_mode,
00452                cuda::GpuMat & dst);
00453 
00454 private:
00455     cuda::GpuMat d_xmap_, d_ymap_, d_src_, d_dst_;
00456 };
00457 
00458 
00459 class CV_EXPORTS SphericalWarperGpu : public SphericalWarper
00460 {
00461 public:
00462     SphericalWarperGpu(float scale) : SphericalWarper(scale) {}
00463 
00464     Rect buildMaps(Size src_size, InputArray K, InputArray R, OutputArray xmap, OutputArray ymap)
00465     {
00466         Rect result = buildMaps(src_size, K, R, d_xmap_, d_ymap_);
00467         d_xmap_.download(xmap);
00468         d_ymap_.download(ymap);
00469         return result;
00470     }
00471 
00472     Point warp(InputArray src, InputArray K, InputArray R, int interp_mode, int border_mode,
00473                OutputArray dst)
00474     {
00475         d_src_.upload(src);
00476         Point result = warp(d_src_, K, R, interp_mode, border_mode, d_dst_);
00477         d_dst_.download(dst);
00478         return result;
00479     }
00480 
00481     Rect buildMaps(Size src_size, InputArray K, InputArray R, cuda::GpuMat & xmap, cuda::GpuMat & ymap);
00482 
00483     Point warp(const cuda::GpuMat & src, InputArray K, InputArray R, int interp_mode, int border_mode,
00484                cuda::GpuMat & dst);
00485 
00486 private:
00487     cuda::GpuMat d_xmap_, d_ymap_, d_src_, d_dst_;
00488 };
00489 
00490 
00491 class CV_EXPORTS CylindricalWarperGpu : public CylindricalWarper
00492 {
00493 public:
00494     CylindricalWarperGpu(float scale) : CylindricalWarper(scale) {}
00495 
00496     Rect buildMaps(Size src_size, InputArray K, InputArray R, OutputArray xmap, OutputArray ymap)
00497     {
00498         Rect result = buildMaps(src_size, K, R, d_xmap_, d_ymap_);
00499         d_xmap_.download(xmap);
00500         d_ymap_.download(ymap);
00501         return result;
00502     }
00503 
00504     Point warp(InputArray src, InputArray K, InputArray R, int interp_mode, int border_mode,
00505                OutputArray dst)
00506     {
00507         d_src_.upload(src);
00508         Point result = warp(d_src_, K, R, interp_mode, border_mode, d_dst_);
00509         d_dst_.download(dst);
00510         return result;
00511     }
00512 
00513     Rect buildMaps(Size src_size, InputArray K, InputArray R, cuda::GpuMat & xmap, cuda::GpuMat & ymap);
00514 
00515     Point warp(const cuda::GpuMat & src, InputArray K, InputArray R, int interp_mode, int border_mode,
00516                cuda::GpuMat & dst);
00517 
00518 private:
00519     cuda::GpuMat d_xmap_, d_ymap_, d_src_, d_dst_;
00520 };
00521 
00522 
00523 struct SphericalPortraitProjector : ProjectorBase
00524 {
00525     void mapForward(float x, float y, float &u, float &v);
00526     void mapBackward(float u, float v, float &x, float &y);
00527 };
00528 
00529 
00530 // Projects image onto unit sphere with origin at (0, 0, 0).
00531 // Poles are located NOT at (0, -1, 0) and (0, 1, 0) points, BUT at (1, 0, 0) and (-1, 0, 0) points.
00532 class CV_EXPORTS SphericalPortraitWarper : public RotationWarperBase<SphericalPortraitProjector>
00533 {
00534 public:
00535     SphericalPortraitWarper(float scale) { projector_.scale = scale; }
00536 
00537 protected:
00538     void detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br);
00539 };
00540 
00541 struct CylindricalPortraitProjector : ProjectorBase
00542 {
00543     void mapForward(float x, float y, float &u, float &v);
00544     void mapBackward(float u, float v, float &x, float &y);
00545 };
00546 
00547 
00548 class CV_EXPORTS CylindricalPortraitWarper : public RotationWarperBase<CylindricalPortraitProjector>
00549 {
00550 public:
00551     CylindricalPortraitWarper(float scale) { projector_.scale = scale; }
00552 
00553 protected:
00554     void detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br)
00555     {
00556         RotationWarperBase<CylindricalPortraitProjector>::detectResultRoiByBorder(src_size, dst_tl, dst_br);
00557     }
00558 };
00559 
00560 struct PlanePortraitProjector : ProjectorBase
00561 {
00562     void mapForward(float x, float y, float &u, float &v);
00563     void mapBackward(float u, float v, float &x, float &y);
00564 };
00565 
00566 
00567 class CV_EXPORTS PlanePortraitWarper : public RotationWarperBase<PlanePortraitProjector>
00568 {
00569 public:
00570     PlanePortraitWarper(float scale) { projector_.scale = scale; }
00571 
00572 protected:
00573     void detectResultRoi(Size src_size, Point &dst_tl, Point &dst_br)
00574     {
00575         RotationWarperBase<PlanePortraitProjector>::detectResultRoiByBorder(src_size, dst_tl, dst_br);
00576     }
00577 };
00578 
00579 //! @} stitching_warp
00580 
00581 } // namespace detail
00582 } // namespace cv
00583 
00584 #include "warpers_inl.hpp"
00585 
00586 #endif // __OPENCV_STITCHING_WARPERS_HPP__
00587