From 95a3ffd0c5b1edb5390d1288e9c3f1c4fda9320c Mon Sep 17 00:00:00 2001
From: Alexey Spizhevoy <no@email>
Date: Wed, 7 Sep 2011 06:34:22 +0000
Subject: [PATCH] put all old stitching API into detail namespace

---
 OpenCVModule.cmake                            |   2 +-
 .../stitching/{ => detail}/autocalib.hpp      |   5 +-
 .../stitching/{ => detail}/blenders.hpp       |   5 +-
 .../opencv2/stitching/{ => detail}/camera.hpp |   5 +-
 .../{ => detail}/exposure_compensate.hpp      |   5 +-
 .../stitching/{ => detail}/matchers.hpp       |   5 +-
 .../{ => detail}/motion_estimators.hpp        |   5 +-
 .../stitching/{ => detail}/seam_finders.hpp   |   5 +-
 .../opencv2/stitching/{ => detail}/util.hpp   |   5 +-
 .../stitching/{ => detail}/util_inl.hpp       |   5 +-
 .../stitching/{ => detail}/warpers.hpp        |   5 +-
 .../stitching/{ => detail}/warpers_inl.hpp    |   5 +-
 .../include/opencv2/stitching/stitching.hpp   |  54 --
 modules/stitching/src/autocalib.cpp           |  36 +-
 modules/stitching/src/blenders.cpp            |  42 +-
 modules/stitching/src/camera.cpp              |  55 +-
 modules/stitching/src/exposure_compensate.cpp |  26 +-
 modules/stitching/src/matchers.cpp            | 558 +++++++++---------
 modules/stitching/src/motion_estimators.cpp   | 105 ++--
 modules/stitching/src/precomp.hpp             |  18 +-
 modules/stitching/src/seam_finders.cpp        |  29 +-
 modules/stitching/src/util.cpp                |  25 +-
 modules/stitching/src/warpers.cpp             |  27 +-
 samples/cpp/stitching.cpp                     |  11 +-
 24 files changed, 552 insertions(+), 491 deletions(-)
 rename modules/stitching/include/opencv2/stitching/{ => detail}/autocalib.hpp (96%)
 rename modules/stitching/include/opencv2/stitching/{ => detail}/blenders.hpp (96%)
 rename modules/stitching/include/opencv2/stitching/{ => detail}/camera.hpp (97%)
 rename modules/stitching/include/opencv2/stitching/{ => detail}/exposure_compensate.hpp (96%)
 rename modules/stitching/include/opencv2/stitching/{ => detail}/matchers.hpp (96%)
 rename modules/stitching/include/opencv2/stitching/{ => detail}/motion_estimators.hpp (96%)
 rename modules/stitching/include/opencv2/stitching/{ => detail}/seam_finders.hpp (95%)
 rename modules/stitching/include/opencv2/stitching/{ => detail}/util.hpp (96%)
 rename modules/stitching/include/opencv2/stitching/{ => detail}/util_inl.hpp (95%)
 rename modules/stitching/include/opencv2/stitching/{ => detail}/warpers.hpp (96%)
 rename modules/stitching/include/opencv2/stitching/{ => detail}/warpers_inl.hpp (96%)
 delete mode 100644 modules/stitching/include/opencv2/stitching/stitching.hpp

diff --git a/OpenCVModule.cmake b/OpenCVModule.cmake
index aa523423cb..5ef2170047 100644
--- a/OpenCVModule.cmake
+++ b/OpenCVModule.cmake
@@ -144,7 +144,7 @@ macro(define_opencv_module name)
 
     file(GLOB lib_srcs "src/*.cpp")
     file(GLOB lib_int_hdrs "src/*.h*")
-    file(GLOB lib_hdrs "include/opencv2/${name}/*.h*")
+    file(GLOB lib_hdrs "include/opencv2/${name}/*.h*" "include/opencv2/${name}/detail/*.h*")
 
     if(COMMAND get_module_external_sources)
        get_module_external_sources(${name})
diff --git a/modules/stitching/include/opencv2/stitching/autocalib.hpp b/modules/stitching/include/opencv2/stitching/detail/autocalib.hpp
similarity index 96%
rename from modules/stitching/include/opencv2/stitching/autocalib.hpp
rename to modules/stitching/include/opencv2/stitching/detail/autocalib.hpp
index 3d9869016d..9d1b075547 100644
--- a/modules/stitching/include/opencv2/stitching/autocalib.hpp
+++ b/modules/stitching/include/opencv2/stitching/detail/autocalib.hpp
@@ -45,8 +45,8 @@
 #include "opencv2/core/core.hpp"
 #include "matchers.hpp"
 
-namespace cv
-{
+namespace cv {
+namespace detail {
 
 // See "Construction of Panoramic Image Mosaics with Global and Local Alignment"
 // by Heung-Yeung Shum and Richard Szeliski.
@@ -58,6 +58,7 @@ void CV_EXPORTS estimateFocal(const std::vector<ImageFeatures> &features,
 
 bool CV_EXPORTS calibrateRotatingCamera(const std::vector<Mat> &Hs, Mat &K);
 
+} // namespace detail
 } // namespace cv
 
 #endif // __OPENCV_STITCHING_AUTOCALIB_HPP__
diff --git a/modules/stitching/include/opencv2/stitching/blenders.hpp b/modules/stitching/include/opencv2/stitching/detail/blenders.hpp
similarity index 96%
rename from modules/stitching/include/opencv2/stitching/blenders.hpp
rename to modules/stitching/include/opencv2/stitching/detail/blenders.hpp
index 15b7769984..1c4878502e 100644
--- a/modules/stitching/include/opencv2/stitching/blenders.hpp
+++ b/modules/stitching/include/opencv2/stitching/detail/blenders.hpp
@@ -44,8 +44,8 @@
 
 #include "opencv2/core/core.hpp"
 
-namespace cv
-{
+namespace cv {
+namespace detail {
 
 // Simple blender which puts one image over another
 class CV_EXPORTS Blender
@@ -119,6 +119,7 @@ void CV_EXPORTS createLaplacePyrGpu(const Mat &img, int num_levels, std::vector<
 // Restores source image
 void CV_EXPORTS restoreImageFromLaplacePyr(std::vector<Mat>& pyr);
 
+} // namespace detail
 } // namespace cv
 
 #endif // __OPENCV_STITCHING_BLENDERS_HPP__
diff --git a/modules/stitching/include/opencv2/stitching/camera.hpp b/modules/stitching/include/opencv2/stitching/detail/camera.hpp
similarity index 97%
rename from modules/stitching/include/opencv2/stitching/camera.hpp
rename to modules/stitching/include/opencv2/stitching/detail/camera.hpp
index 012e6c01e2..507305b4d7 100644
--- a/modules/stitching/include/opencv2/stitching/camera.hpp
+++ b/modules/stitching/include/opencv2/stitching/detail/camera.hpp
@@ -44,8 +44,8 @@
 
 #include "opencv2/core/core.hpp"
 
-namespace cv
-{
+namespace cv {
+namespace detail {
 
 struct CV_EXPORTS CameraParams
 {
@@ -58,6 +58,7 @@ struct CV_EXPORTS CameraParams
     Mat t; // Translation
 };
 
+} // namespace detail
 } // namespace cv
 
 #endif // #ifndef __OPENCV_STITCHING_CAMERA_HPP__
diff --git a/modules/stitching/include/opencv2/stitching/exposure_compensate.hpp b/modules/stitching/include/opencv2/stitching/detail/exposure_compensate.hpp
similarity index 96%
rename from modules/stitching/include/opencv2/stitching/exposure_compensate.hpp
rename to modules/stitching/include/opencv2/stitching/detail/exposure_compensate.hpp
index 4a4324bd2c..2bf922ba20 100644
--- a/modules/stitching/include/opencv2/stitching/exposure_compensate.hpp
+++ b/modules/stitching/include/opencv2/stitching/detail/exposure_compensate.hpp
@@ -44,8 +44,8 @@
 
 #include "opencv2/core/core.hpp"
 
-namespace cv
-{
+namespace cv {
+namespace detail {
 
 class CV_EXPORTS ExposureCompensator
 {
@@ -99,6 +99,7 @@ private:
     std::vector<Mat_<float> > gain_maps_;
 };
 
+} // namespace detail
 } // namespace cv
 
 #endif // __OPENCV_STITCHING_EXPOSURE_COMPENSATE_HPP__
diff --git a/modules/stitching/include/opencv2/stitching/matchers.hpp b/modules/stitching/include/opencv2/stitching/detail/matchers.hpp
similarity index 96%
rename from modules/stitching/include/opencv2/stitching/matchers.hpp
rename to modules/stitching/include/opencv2/stitching/detail/matchers.hpp
index d45e9fc4b6..20abd78344 100644
--- a/modules/stitching/include/opencv2/stitching/matchers.hpp
+++ b/modules/stitching/include/opencv2/stitching/detail/matchers.hpp
@@ -45,8 +45,8 @@
 #include "opencv2/core/core.hpp"
 #include "opencv2/features2d/features2d.hpp"
 
-namespace cv
-{
+namespace cv {
+namespace detail {
 
 struct CV_EXPORTS ImageFeatures
 {
@@ -140,6 +140,7 @@ protected:
     cv::Ptr<FeaturesMatcher> impl_;
 };
 
+} // namespace detail
 } // namespace cv
 
 #endif // __OPENCV_STITCHING_MATCHERS_HPP__
diff --git a/modules/stitching/include/opencv2/stitching/motion_estimators.hpp b/modules/stitching/include/opencv2/stitching/detail/motion_estimators.hpp
similarity index 96%
rename from modules/stitching/include/opencv2/stitching/motion_estimators.hpp
rename to modules/stitching/include/opencv2/stitching/detail/motion_estimators.hpp
index 78c822b534..bc7ae3cd9c 100644
--- a/modules/stitching/include/opencv2/stitching/motion_estimators.hpp
+++ b/modules/stitching/include/opencv2/stitching/detail/motion_estimators.hpp
@@ -47,8 +47,8 @@
 #include "util.hpp"
 #include "camera.hpp"
 
-namespace cv
-{
+namespace cv {
+namespace detail {
 
 class CV_EXPORTS Estimator
 {
@@ -126,6 +126,7 @@ std::vector<int> CV_EXPORTS leaveBiggestComponent(std::vector<ImageFeatures> &fe
 void CV_EXPORTS findMaxSpanningTree(int num_images, const std::vector<MatchesInfo> &pairwise_matches, 
                                     Graph &span_tree, std::vector<int> &centers);
 
+} // namespace detail
 } // namespace cv
 
 #endif // __OPENCV_STITCHING_MOTION_ESTIMATORS_HPP__
diff --git a/modules/stitching/include/opencv2/stitching/seam_finders.hpp b/modules/stitching/include/opencv2/stitching/detail/seam_finders.hpp
similarity index 95%
rename from modules/stitching/include/opencv2/stitching/seam_finders.hpp
rename to modules/stitching/include/opencv2/stitching/detail/seam_finders.hpp
index c383cce884..017baf8564 100644
--- a/modules/stitching/include/opencv2/stitching/seam_finders.hpp
+++ b/modules/stitching/include/opencv2/stitching/detail/seam_finders.hpp
@@ -44,8 +44,8 @@
 
 #include "opencv2/core/core.hpp"
 
-namespace cv
-{
+namespace cv {
+namespace detail {
 
 class CV_EXPORTS SeamFinder
 {
@@ -103,6 +103,7 @@ private:
     Ptr<Impl> impl_;
 };
 
+} // namespace detail
 } // namespace cv
 
 #endif // __OPENCV_STITCHING_SEAM_FINDERS_HPP__
diff --git a/modules/stitching/include/opencv2/stitching/util.hpp b/modules/stitching/include/opencv2/stitching/detail/util.hpp
similarity index 96%
rename from modules/stitching/include/opencv2/stitching/util.hpp
rename to modules/stitching/include/opencv2/stitching/detail/util.hpp
index 2f90b6abe8..e21fcfdca8 100644
--- a/modules/stitching/include/opencv2/stitching/util.hpp
+++ b/modules/stitching/include/opencv2/stitching/detail/util.hpp
@@ -56,8 +56,8 @@
 
 #define LOGLN(msg) LOG(msg << std::endl)
 
-namespace cv
-{
+namespace cv {
+namespace detail {
 
 class CV_EXPORTS DisjointSets
 {
@@ -114,6 +114,7 @@ Point CV_EXPORTS resultTl(const std::vector<Point> &corners);
 // Returns random 'count' element subset of the {0,1,...,size-1} set
 void CV_EXPORTS selectRandomSubset(int count, int size, std::vector<int> &subset);
 
+} // namespace detail
 } // namespace cv
 
 #include "util_inl.hpp"
diff --git a/modules/stitching/include/opencv2/stitching/util_inl.hpp b/modules/stitching/include/opencv2/stitching/detail/util_inl.hpp
similarity index 95%
rename from modules/stitching/include/opencv2/stitching/util_inl.hpp
rename to modules/stitching/include/opencv2/stitching/detail/util_inl.hpp
index 7500002429..3c69cc58bc 100644
--- a/modules/stitching/include/opencv2/stitching/util_inl.hpp
+++ b/modules/stitching/include/opencv2/stitching/detail/util_inl.hpp
@@ -46,8 +46,8 @@
 #include "opencv2/core/core.hpp"
 #include "util.hpp" // Make your IDE see declarations
 
-namespace cv
-{
+namespace cv {
+namespace detail {
 
 template <typename B>
 B Graph::forEach(B body) const
@@ -120,6 +120,7 @@ static inline int sqr(int x) { return x * x; }
 static inline float sqr(float x) { return x * x; }
 static inline double sqr(double x) { return x * x; }
 
+} // namespace detail
 } // namespace cv
 
 #endif // __OPENCV_STITCHING_UTIL_INL_HPP__
diff --git a/modules/stitching/include/opencv2/stitching/warpers.hpp b/modules/stitching/include/opencv2/stitching/detail/warpers.hpp
similarity index 96%
rename from modules/stitching/include/opencv2/stitching/warpers.hpp
rename to modules/stitching/include/opencv2/stitching/detail/warpers.hpp
index 45344b0c12..1c33100b04 100644
--- a/modules/stitching/include/opencv2/stitching/warpers.hpp
+++ b/modules/stitching/include/opencv2/stitching/detail/warpers.hpp
@@ -46,8 +46,8 @@
 #include "opencv2/imgproc/imgproc.hpp"
 #include "opencv2/gpu/gpu.hpp"
 
-namespace cv
-{
+namespace cv {
+namespace detail {
 
 class CV_EXPORTS Warper
 {
@@ -194,6 +194,7 @@ private:
     gpu::GpuMat d_xmap_, d_ymap_, d_dst_, d_src_;
 };
 
+} // namespace detail
 } // namespace cv
 
 #include "warpers_inl.hpp"
diff --git a/modules/stitching/include/opencv2/stitching/warpers_inl.hpp b/modules/stitching/include/opencv2/stitching/detail/warpers_inl.hpp
similarity index 96%
rename from modules/stitching/include/opencv2/stitching/warpers_inl.hpp
rename to modules/stitching/include/opencv2/stitching/detail/warpers_inl.hpp
index 2fd7862a75..169155b1b6 100644
--- a/modules/stitching/include/opencv2/stitching/warpers_inl.hpp
+++ b/modules/stitching/include/opencv2/stitching/detail/warpers_inl.hpp
@@ -45,8 +45,8 @@
 #include "opencv2/core/core.hpp"
 #include "warpers.hpp" // Make your IDE see declarations
 
-namespace cv
-{
+namespace cv {
+namespace detail {
 
 template <class P>
 Point WarperBase<P>::warp(const Mat &src, float focal, const Mat &R, Mat &dst,
@@ -256,6 +256,7 @@ void CylindricalProjector::mapBackward(float u, float v, float &x, float &y)
     y = focal * y / z + size.height * 0.5f;
 }
 
+} // namespace detail
 } // namespace cv
 
 #endif // __OPENCV_STITCHING_WARPERS_INL_HPP__
diff --git a/modules/stitching/include/opencv2/stitching/stitching.hpp b/modules/stitching/include/opencv2/stitching/stitching.hpp
deleted file mode 100644
index 0751d0db41..0000000000
--- a/modules/stitching/include/opencv2/stitching/stitching.hpp
+++ /dev/null
@@ -1,54 +0,0 @@
-/*M///////////////////////////////////////////////////////////////////////////////////////
-//
-//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
-//
-//  By downloading, copying, installing or using the software you agree to this license.
-//  If you do not agree to this license, do not download, install,
-//  copy or use the software.
-//
-//
-//                        Intel License Agreement
-//
-// Copyright (C) 2000, Intel Corporation, all rights reserved.
-// Third party copyrights are property of their respective owners.
-//
-// Redistribution and use in source and binary forms, with or without modification,
-// are permitted provided that the following conditions are met:
-//
-//   * Redistribution's of source code must retain the above copyright notice,
-//     this list of conditions and the following disclaimer.
-//
-//   * Redistribution's in binary form must reproduce the above copyright notice,
-//     this list of conditions and the following disclaimer in the documentation
-//     and/or other materials provided with the distribution.
-//
-//   * The name of Intel Corporation may not be used to endorse or promote products
-//     derived from this software without specific prior written permission.
-//
-// This software is provided by the copyright holders and contributors "as is" and
-// any express or implied warranties, including, but not limited to, the implied
-// warranties of merchantability and fitness for a particular purpose are disclaimed.
-// In no event shall the Intel Corporation or contributors be liable for any direct,
-// indirect, incidental, special, exemplary, or consequential damages
-// (including, but not limited to, procurement of substitute goods or services;
-// loss of use, data, or profits; or business interruption) however caused
-// and on any theory of liability, whether in contract, strict liability,
-// or tort (including negligence or otherwise) arising in any way out of
-// the use of this software, even if advised of the possibility of such damage.
-//
-//M*/
-
-#ifndef __OPENCV_STITCHING_HPP__
-#define __OPENCV_STITCHING_HPP__
-
-#include "opencv2/stitching/autocalib.hpp"
-#include "opencv2/stitching/blenders.hpp"
-#include "opencv2/stitching/camera.hpp"
-#include "opencv2/stitching/exposure_compensate.hpp"
-#include "opencv2/stitching/matchers.hpp"
-#include "opencv2/stitching/motion_estimators.hpp"
-#include "opencv2/stitching/seam_finders.hpp"
-#include "opencv2/stitching/util.hpp"
-#include "opencv2/stitching/warpers.hpp"
-
-#endif // __OPENCV_STITCHING_HPP__
diff --git a/modules/stitching/src/autocalib.cpp b/modules/stitching/src/autocalib.cpp
index 806fe3d315..b82c286c3d 100644
--- a/modules/stitching/src/autocalib.cpp
+++ b/modules/stitching/src/autocalib.cpp
@@ -39,27 +39,32 @@
 // the use of this software, even if advised of the possibility of such damage.
 //
 //M*/
+
 #include "precomp.hpp"
 
 using namespace std;
 using namespace cv;
 
-namespace
+namespace {
+
+template<typename _Tp> static inline bool
+decomposeCholesky(_Tp* A, size_t astep, int m)
 {
-    template<typename _Tp> static inline bool
-    decomposeCholesky(_Tp* A, size_t astep, int m)
-    {
-        if (!Cholesky(A, astep, m, 0, 0, 0))
-            return false;
-        astep /= sizeof(A[0]);
-        for (int i = 0; i < m; ++i)
-            A[i*astep + i] = (_Tp)(1./A[i*astep + i]);
-        return true;
-    }
+    if (!Cholesky(A, astep, m, 0, 0, 0))
+        return false;
+    astep /= sizeof(A[0]);
+    for (int i = 0; i < m; ++i)
+        A[i*astep + i] = (_Tp)(1./A[i*astep + i]);
+    return true;
+}
+
 } // namespace
 
 
-void cv::focalsFromHomography(const Mat& H, double &f0, double &f1, bool &f0_ok, bool &f1_ok)
+namespace cv {
+namespace detail {
+
+void focalsFromHomography(const Mat& H, double &f0, double &f1, bool &f0_ok, bool &f1_ok)
 {
     CV_Assert(H.type() == CV_64F && H.size() == Size(3, 3));
 
@@ -90,7 +95,7 @@ void cv::focalsFromHomography(const Mat& H, double &f0, double &f1, bool &f0_ok,
 }
 
 
-void cv::estimateFocal(const vector<ImageFeatures> &features, const vector<MatchesInfo> &pairwise_matches,
+void estimateFocal(const vector<ImageFeatures> &features, const vector<MatchesInfo> &pairwise_matches,
                        vector<double> &focals)
 {
     const int num_images = static_cast<int>(features.size());
@@ -131,7 +136,7 @@ void cv::estimateFocal(const vector<ImageFeatures> &features, const vector<Match
 }
 
 
-bool cv::calibrateRotatingCamera(const vector<Mat> &Hs, Mat &K)
+bool calibrateRotatingCamera(const vector<Mat> &Hs, Mat &K)
 {
     int m = static_cast<int>(Hs.size());
     CV_Assert(m >= 1);
@@ -181,3 +186,6 @@ bool cv::calibrateRotatingCamera(const vector<Mat> &Hs, Mat &K)
     K = W.t();
     return true;
 }
+
+} // namespace detail
+} // namespace cv
diff --git a/modules/stitching/src/blenders.cpp b/modules/stitching/src/blenders.cpp
index 9806d9bbf6..0f07e81d39 100644
--- a/modules/stitching/src/blenders.cpp
+++ b/modules/stitching/src/blenders.cpp
@@ -39,14 +39,17 @@
 // the use of this software, even if advised of the possibility of such damage.
 //
 //M*/
+
 #include "precomp.hpp"
 
 using namespace std;
-using namespace cv;
+
+namespace cv {
+namespace detail {
 
 static const float WEIGHT_EPS = 1e-5f;
 
-Ptr<Blender> cv::Blender::createDefault(int type, bool try_gpu)
+Ptr<Blender> Blender::createDefault(int type, bool try_gpu)
 {
     if (type == NO)
         return new Blender();
@@ -59,13 +62,13 @@ Ptr<Blender> cv::Blender::createDefault(int type, bool try_gpu)
 }
 
 
-void cv::Blender::prepare(const vector<Point> &corners, const vector<Size> &sizes)
+void Blender::prepare(const vector<Point> &corners, const vector<Size> &sizes)
 {
     prepare(resultRoi(corners, sizes));
 }
 
 
-void cv::Blender::prepare(Rect dst_roi)
+void Blender::prepare(Rect dst_roi)
 {
     dst_.create(dst_roi.size(), CV_16SC3);
     dst_.setTo(Scalar::all(0));
@@ -75,7 +78,7 @@ void cv::Blender::prepare(Rect dst_roi)
 }
 
 
-void cv::Blender::feed(const Mat &img, const Mat &mask, Point tl)
+void Blender::feed(const Mat &img, const Mat &mask, Point tl)
 {
     CV_Assert(img.type() == CV_16SC3);
     CV_Assert(mask.type() == CV_8U);
@@ -99,7 +102,7 @@ void cv::Blender::feed(const Mat &img, const Mat &mask, Point tl)
 }
 
 
-void cv::Blender::blend(Mat &dst, Mat &dst_mask)
+void Blender::blend(Mat &dst, Mat &dst_mask)
 {
     dst_.setTo(Scalar::all(0), dst_mask_ == 0);
     dst = dst_;
@@ -109,7 +112,7 @@ void cv::Blender::blend(Mat &dst, Mat &dst_mask)
 }
 
 
-void cv::FeatherBlender::prepare(Rect dst_roi)
+void FeatherBlender::prepare(Rect dst_roi)
 {
     Blender::prepare(dst_roi);
     dst_weight_map_.create(dst_roi.size(), CV_32F);
@@ -117,7 +120,7 @@ void cv::FeatherBlender::prepare(Rect dst_roi)
 }
 
 
-void cv::FeatherBlender::feed(const Mat &img, const Mat &mask, Point tl)
+void FeatherBlender::feed(const Mat &img, const Mat &mask, Point tl)
 {
     CV_Assert(img.type() == CV_16SC3);
     CV_Assert(mask.type() == CV_8U);
@@ -144,7 +147,7 @@ void cv::FeatherBlender::feed(const Mat &img, const Mat &mask, Point tl)
 }
 
 
-void cv::FeatherBlender::blend(Mat &dst, Mat &dst_mask)
+void FeatherBlender::blend(Mat &dst, Mat &dst_mask)
 {
     normalizeUsingWeightMap(dst_weight_map_, dst_);
     dst_mask_ = dst_weight_map_ > WEIGHT_EPS;
@@ -152,14 +155,14 @@ void cv::FeatherBlender::blend(Mat &dst, Mat &dst_mask)
 }
 
 
-cv::MultiBandBlender::MultiBandBlender(int try_gpu, int num_bands)
+MultiBandBlender::MultiBandBlender(int try_gpu, int num_bands)
 {
     setNumBands(num_bands);
     can_use_gpu_ = try_gpu && gpu::getCudaEnabledDeviceCount();
 }
 
 
-void cv::MultiBandBlender::prepare(Rect dst_roi)
+void MultiBandBlender::prepare(Rect dst_roi)
 {
     dst_roi_final_ = dst_roi;
 
@@ -192,7 +195,7 @@ void cv::MultiBandBlender::prepare(Rect dst_roi)
 }
 
 
-void cv::MultiBandBlender::feed(const Mat &img, const Mat &mask, Point tl)
+void MultiBandBlender::feed(const Mat &img, const Mat &mask, Point tl)
 {
     CV_Assert(img.type() == CV_16SC3);
     CV_Assert(mask.type() == CV_8U);
@@ -277,7 +280,7 @@ void cv::MultiBandBlender::feed(const Mat &img, const Mat &mask, Point tl)
 }
 
 
-void cv::MultiBandBlender::blend(Mat &dst, Mat &dst_mask)
+void MultiBandBlender::blend(Mat &dst, Mat &dst_mask)
 {
     for (int i = 0; i <= num_bands_; ++i)
         normalizeUsingWeightMap(dst_band_weights_[i], dst_pyr_laplace_[i]);
@@ -298,7 +301,7 @@ void cv::MultiBandBlender::blend(Mat &dst, Mat &dst_mask)
 //////////////////////////////////////////////////////////////////////////////
 // Auxiliary functions
 
-void cv::normalizeUsingWeightMap(const Mat& weight, Mat& src)
+void normalizeUsingWeightMap(const Mat& weight, Mat& src)
 {
     CV_Assert(weight.type() == CV_32F);
     CV_Assert(src.type() == CV_16SC3);
@@ -317,7 +320,7 @@ void cv::normalizeUsingWeightMap(const Mat& weight, Mat& src)
 }
 
 
-void cv::createWeightMap(const Mat &mask, float sharpness, Mat &weight)
+void createWeightMap(const Mat &mask, float sharpness, Mat &weight)
 {
     CV_Assert(mask.type() == CV_8U);
     distanceTransform(mask, weight, CV_DIST_L1, 3);
@@ -325,7 +328,7 @@ void cv::createWeightMap(const Mat &mask, float sharpness, Mat &weight)
 }
 
 
-void cv::createLaplacePyr(const Mat &img, int num_levels, vector<Mat> &pyr)
+void createLaplacePyr(const Mat &img, int num_levels, vector<Mat> &pyr)
 {
     pyr.resize(num_levels + 1);
     pyr[0] = img;
@@ -340,7 +343,7 @@ void cv::createLaplacePyr(const Mat &img, int num_levels, vector<Mat> &pyr)
 }
 
 
-void cv::createLaplacePyrGpu(const Mat &img, int num_levels, vector<Mat> &pyr)
+void createLaplacePyrGpu(const Mat &img, int num_levels, vector<Mat> &pyr)
 {
     pyr.resize(num_levels + 1);
 
@@ -361,7 +364,7 @@ void cv::createLaplacePyrGpu(const Mat &img, int num_levels, vector<Mat> &pyr)
 }
 
 
-void cv::restoreImageFromLaplacePyr(vector<Mat> &pyr)
+void restoreImageFromLaplacePyr(vector<Mat> &pyr)
 {
     if (pyr.size() == 0)
         return;
@@ -372,3 +375,6 @@ void cv::restoreImageFromLaplacePyr(vector<Mat> &pyr)
         add(tmp, pyr[i - 1], pyr[i - 1]);
     }
 }
+
+} // namespace detail
+} // namespace cv
diff --git a/modules/stitching/src/camera.cpp b/modules/stitching/src/camera.cpp
index 18ef6d2349..79c028278b 100644
--- a/modules/stitching/src/camera.cpp
+++ b/modules/stitching/src/camera.cpp
@@ -1,16 +1,63 @@
+/*M///////////////////////////////////////////////////////////////////////////////////////
+//
+//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
+//
+//  By downloading, copying, installing or using the software you agree to this license.
+//  If you do not agree to this license, do not download, install,
+//  copy or use the software.
+//
+//
+//                          License Agreement
+//                For Open Source Computer Vision Library
+//
+// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
+// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
+// Third party copyrights are property of their respective owners.
+//
+// Redistribution and use in source and binary forms, with or without modification,
+// are permitted provided that the following conditions are met:
+//
+//   * Redistribution's of source code must retain the above copyright notice,
+//     this list of conditions and the following disclaimer.
+//
+//   * Redistribution's in binary form must reproduce the above copyright notice,
+//     this list of conditions and the following disclaimer in the documentation
+//     and/or other materials provided with the distribution.
+//
+//   * The name of the copyright holders may not be used to endorse or promote products
+//     derived from this software without specific prior written permission.
+//
+// This software is provided by the copyright holders and contributors "as is" and
+// any express or implied warranties, including, but not limited to, the implied
+// warranties of merchantability and fitness for a particular purpose are disclaimed.
+// In no event shall the Intel Corporation or contributors be liable for any direct,
+// indirect, incidental, special, exemplary, or consequential damages
+// (including, but not limited to, procurement of substitute goods or services;
+// loss of use, data, or profits; or business interruption) however caused
+// and on any theory of liability, whether in contract, strict liability,
+// or tort (including negligence or otherwise) arising in any way out of
+// the use of this software, even if advised of the possibility of such damage.
+//
+//M*/
+
 #include "precomp.hpp"
 
 using namespace std;
-using namespace cv;
 
-cv::CameraParams::CameraParams() : focal(1), R(Mat::eye(3, 3, CV_64F)), t(Mat::zeros(3, 1, CV_64F)) {}
+namespace cv {
+namespace detail {
+
+CameraParams::CameraParams() : focal(1), R(Mat::eye(3, 3, CV_64F)), t(Mat::zeros(3, 1, CV_64F)) {}
 
-cv::CameraParams::CameraParams(const CameraParams &other) { *this = other; }
+CameraParams::CameraParams(const CameraParams &other) { *this = other; }
 
-const cv::CameraParams& CameraParams::operator =(const CameraParams &other)
+const CameraParams& CameraParams::operator =(const CameraParams &other)
 {
     focal = other.focal;
     R = other.R.clone();
     t = other.t.clone();
     return *this;
 }
+
+} // namespace detail
+} // namespace cv
diff --git a/modules/stitching/src/exposure_compensate.cpp b/modules/stitching/src/exposure_compensate.cpp
index 1eefe525bf..f4864af3a3 100644
--- a/modules/stitching/src/exposure_compensate.cpp
+++ b/modules/stitching/src/exposure_compensate.cpp
@@ -39,13 +39,16 @@
 // the use of this software, even if advised of the possibility of such damage.
 //
 //M*/
+
 #include "precomp.hpp"
 
 using namespace std;
-using namespace cv;
 using namespace cv::gpu;
 
-Ptr<ExposureCompensator> cv::ExposureCompensator::createDefault(int type)
+namespace cv {
+namespace detail {
+
+Ptr<ExposureCompensator> ExposureCompensator::createDefault(int type)
 {
     if (type == NO)
         return new NoExposureCompensator();
@@ -58,8 +61,8 @@ Ptr<ExposureCompensator> cv::ExposureCompensator::createDefault(int type)
 }
 
 
-void cv::ExposureCompensator::feed(const vector<Point> &corners, const vector<Mat> &images,
-                                   const vector<Mat> &masks)
+void ExposureCompensator::feed(const vector<Point> &corners, const vector<Mat> &images,
+                               const vector<Mat> &masks)
 {
     vector<pair<Mat,uchar> > level_masks;
     for (size_t i = 0; i < masks.size(); ++i)
@@ -68,8 +71,8 @@ void cv::ExposureCompensator::feed(const vector<Point> &corners, const vector<Ma
 }
 
 
-void cv::GainCompensator::feed(const vector<Point> &corners, const vector<Mat> &images,
-                               const vector<pair<Mat,uchar> > &masks)
+void GainCompensator::feed(const vector<Point> &corners, const vector<Mat> &images,
+                           const vector<pair<Mat,uchar> > &masks)
 {
     LOGLN("Exposure compensation...");
     int64 t = getTickCount();
@@ -143,13 +146,13 @@ void cv::GainCompensator::feed(const vector<Point> &corners, const vector<Mat> &
 }
 
 
-void cv::GainCompensator::apply(int index, Point /*corner*/, Mat &image, const Mat &/*mask*/)
+void GainCompensator::apply(int index, Point /*corner*/, Mat &image, const Mat &/*mask*/)
 {
     image *= gains_(index, 0);
 }
 
 
-vector<double> cv::GainCompensator::gains() const
+vector<double> GainCompensator::gains() const
 {
     vector<double> gains_vec(gains_.rows);
     for (int i = 0; i < gains_.rows; ++i)
@@ -158,7 +161,7 @@ vector<double> cv::GainCompensator::gains() const
 }
 
 
-void cv::BlocksGainCompensator::feed(const vector<Point> &corners, const vector<Mat> &images,
+void BlocksGainCompensator::feed(const vector<Point> &corners, const vector<Mat> &images,
                                      const vector<pair<Mat,uchar> > &masks)
 {
     CV_Assert(corners.size() == images.size() && images.size() == masks.size());
@@ -218,7 +221,7 @@ void cv::BlocksGainCompensator::feed(const vector<Point> &corners, const vector<
 }
 
 
-void cv::BlocksGainCompensator::apply(int index, Point /*corner*/, Mat &image, const Mat &/*mask*/)
+void BlocksGainCompensator::apply(int index, Point /*corner*/, Mat &image, const Mat &/*mask*/)
 {
     CV_Assert(image.type() == CV_8UC3);
 
@@ -240,3 +243,6 @@ void cv::BlocksGainCompensator::apply(int index, Point /*corner*/, Mat &image, c
         }
     }
 }
+
+} // namespace detail
+} // namespace cv
diff --git a/modules/stitching/src/matchers.cpp b/modules/stitching/src/matchers.cpp
index aa430d2acb..af43d18d64 100644
--- a/modules/stitching/src/matchers.cpp
+++ b/modules/stitching/src/matchers.cpp
@@ -39,118 +39,313 @@
 // the use of this software, even if advised of the possibility of such damage.
 //
 //M*/
+
 #include "precomp.hpp"
 
 using namespace std;
 using namespace cv;
+using namespace cv::detail;
 using namespace cv::gpu;
 
-void cv::FeaturesFinder::operator ()(const Mat &image, ImageFeatures &features)
-{ 
-    find(image, features);
-    features.img_size = image.size();
-    //features.img = image.clone();
-}
+namespace {
+
+class CpuSurfFeaturesFinder : public FeaturesFinder
+{
+public:
+    CpuSurfFeaturesFinder(double hess_thresh, int num_octaves, int num_layers,
+                          int num_octaves_descr, int num_layers_descr)
+    {
+        detector_ = new SurfFeatureDetector(hess_thresh, num_octaves, num_layers);
+        extractor_ = new SurfDescriptorExtractor(num_octaves_descr, num_layers_descr);
+    }
+
+protected:
+    void find(const Mat &image, ImageFeatures &features);
+
+private:
+    Ptr<FeatureDetector> detector_;
+    Ptr<DescriptorExtractor> extractor_;
+};
 
 
-namespace
+class GpuSurfFeaturesFinder : public FeaturesFinder
 {
-    class CpuSurfFeaturesFinder : public FeaturesFinder
+public:
+    GpuSurfFeaturesFinder(double hess_thresh, int num_octaves, int num_layers,
+                          int num_octaves_descr, int num_layers_descr)
     {
-    public:
-        CpuSurfFeaturesFinder(double hess_thresh, int num_octaves, int num_layers, 
-                              int num_octaves_descr, int num_layers_descr) 
-        {
-            detector_ = new SurfFeatureDetector(hess_thresh, num_octaves, num_layers);
-            extractor_ = new SurfDescriptorExtractor(num_octaves_descr, num_layers_descr);
-        }
+        surf_.keypointsRatio = 0.1f;
+        surf_.hessianThreshold = hess_thresh;
+        surf_.extended = false;
+        num_octaves_ = num_octaves;
+        num_layers_ = num_layers;
+        num_octaves_descr_ = num_octaves_descr;
+        num_layers_descr_ = num_layers_descr;
+    }
+
+    void releaseMemory();
+
+protected:
+    void find(const Mat &image, ImageFeatures &features);
+
+private:
+    GpuMat image_;
+    GpuMat gray_image_;
+    SURF_GPU surf_;
+    GpuMat keypoints_;
+    GpuMat descriptors_;
+    int num_octaves_, num_layers_;
+    int num_octaves_descr_, num_layers_descr_;
+};
+
+
+void CpuSurfFeaturesFinder::find(const Mat &image, ImageFeatures &features)
+{
+    Mat gray_image;
+    CV_Assert(image.depth() == CV_8U);
+    cvtColor(image, gray_image, CV_BGR2GRAY);
+    detector_->detect(gray_image, features.keypoints);
+    extractor_->compute(gray_image, features.keypoints, features.descriptors);
+}
+
+
+void GpuSurfFeaturesFinder::find(const Mat &image, ImageFeatures &features)
+{
+    CV_Assert(image.depth() == CV_8U);
+
+    ensureSizeIsEnough(image.size(), image.type(), image_);
+    image_.upload(image);
+
+    ensureSizeIsEnough(image.size(), CV_8UC1, gray_image_);
+    cvtColor(image_, gray_image_, CV_BGR2GRAY);
+
+    surf_.nOctaves = num_octaves_;
+    surf_.nOctaveLayers = num_layers_;
+    surf_.upright = false;
+    surf_(gray_image_, GpuMat(), keypoints_);
+
+    surf_.nOctaves = num_octaves_descr_;
+    surf_.nOctaveLayers = num_layers_descr_;
+    surf_.upright = true;
+    surf_(gray_image_, GpuMat(), keypoints_, descriptors_, true);
+    surf_.downloadKeypoints(keypoints_, features.keypoints);
+
+    descriptors_.download(features.descriptors);
+}
+
+void GpuSurfFeaturesFinder::releaseMemory()
+{
+    surf_.releaseMemory();
+    image_.release();
+    gray_image_.release();
+    keypoints_.release();
+    descriptors_.release();
+}
+
 
-    protected:
-        void find(const Mat &image, ImageFeatures &features);
+//////////////////////////////////////////////////////////////////////////////
+
+struct DistIdxPair
+{
+    bool operator<(const DistIdxPair &other) const { return dist < other.dist; }
+    double dist;
+    int idx;
+};
 
-    private:
-        Ptr<FeatureDetector> detector_;
-        Ptr<DescriptorExtractor> extractor_;
-    };
 
+struct MatchPairsBody
+{
+    MatchPairsBody(const MatchPairsBody& other)
+            : matcher(other.matcher), features(other.features),
+              pairwise_matches(other.pairwise_matches), near_pairs(other.near_pairs) {}
+
+    MatchPairsBody(FeaturesMatcher &matcher, const vector<ImageFeatures> &features,
+                   vector<MatchesInfo> &pairwise_matches, vector<pair<int,int> > &near_pairs)
+            : matcher(matcher), features(features),
+              pairwise_matches(pairwise_matches), near_pairs(near_pairs) {}
 
-    class GpuSurfFeaturesFinder : public FeaturesFinder
+    void operator ()(const BlockedRange &r) const
     {
-    public:
-        GpuSurfFeaturesFinder(double hess_thresh, int num_octaves, int num_layers, 
-                              int num_octaves_descr, int num_layers_descr) 
+        const int num_images = static_cast<int>(features.size());
+        for (int i = r.begin(); i < r.end(); ++i)
         {
-            surf_.keypointsRatio = 0.1f;
-            surf_.hessianThreshold = hess_thresh;
-            surf_.extended = false;
-            num_octaves_ = num_octaves;
-            num_layers_ = num_layers;
-            num_octaves_descr_ = num_octaves_descr;
-            num_layers_descr_ = num_layers_descr;
+            int from = near_pairs[i].first;
+            int to = near_pairs[i].second;
+            int pair_idx = from*num_images + to;
+
+            matcher(features[from], features[to], pairwise_matches[pair_idx]);
+            pairwise_matches[pair_idx].src_img_idx = from;
+            pairwise_matches[pair_idx].dst_img_idx = to;
+
+            size_t dual_pair_idx = to*num_images + from;
+
+            pairwise_matches[dual_pair_idx] = pairwise_matches[pair_idx];
+            pairwise_matches[dual_pair_idx].src_img_idx = to;
+            pairwise_matches[dual_pair_idx].dst_img_idx = from;
+
+            if (!pairwise_matches[pair_idx].H.empty())
+                pairwise_matches[dual_pair_idx].H = pairwise_matches[pair_idx].H.inv();
+
+            for (size_t j = 0; j < pairwise_matches[dual_pair_idx].matches.size(); ++j)
+                std::swap(pairwise_matches[dual_pair_idx].matches[j].queryIdx,
+                          pairwise_matches[dual_pair_idx].matches[j].trainIdx);
+            LOG(".");
         }
+    }
+
+    FeaturesMatcher &matcher;
+    const vector<ImageFeatures> &features;
+    vector<MatchesInfo> &pairwise_matches;
+    vector<pair<int,int> > &near_pairs;
 
-        void releaseMemory();
+private:
+    void operator =(const MatchPairsBody&);
+};
 
-    protected:
-        void find(const Mat &image, ImageFeatures &features);
 
-    private:
-        GpuMat image_;
-        GpuMat gray_image_;
-        SURF_GPU surf_;
-        GpuMat keypoints_;
-        GpuMat descriptors_;
-        int num_octaves_, num_layers_;
-        int num_octaves_descr_, num_layers_descr_;
-    };
+//////////////////////////////////////////////////////////////////////////////
+
+typedef set<pair<int,int> > MatchesSet;
+
+// These two classes are aimed to find features matches only, not to
+// estimate homography
+
+class CpuMatcher : public FeaturesMatcher
+{
+public:
+    CpuMatcher(float match_conf) : FeaturesMatcher(true), match_conf_(match_conf) {}
+    void match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo& matches_info);
+
+private:
+    float match_conf_;
+};
 
 
-    void CpuSurfFeaturesFinder::find(const Mat &image, ImageFeatures &features)
+class GpuMatcher : public FeaturesMatcher
+{
+public:
+    GpuMatcher(float match_conf) : match_conf_(match_conf) {}
+    void match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo& matches_info);
+
+    void releaseMemory();
+
+private:
+    float match_conf_;
+    GpuMat descriptors1_, descriptors2_;
+    GpuMat train_idx_, distance_, all_dist_;
+    vector< vector<DMatch> > pair_matches;
+};
+
+
+void CpuMatcher::match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo& matches_info)
+{
+    matches_info.matches.clear();
+    FlannBasedMatcher matcher;
+    vector< vector<DMatch> > pair_matches;
+    MatchesSet matches;
+
+    // Find 1->2 matches
+    matcher.knnMatch(features1.descriptors, features2.descriptors, pair_matches, 2);
+    for (size_t i = 0; i < pair_matches.size(); ++i)
     {
-        Mat gray_image;
-        CV_Assert(image.depth() == CV_8U);
-        cvtColor(image, gray_image, CV_BGR2GRAY);
-        detector_->detect(gray_image, features.keypoints);
-        extractor_->compute(gray_image, features.keypoints, features.descriptors);
+        if (pair_matches[i].size() < 2)
+            continue;
+        const DMatch& m0 = pair_matches[i][0];
+        const DMatch& m1 = pair_matches[i][1];
+        if (m0.distance < (1.f - match_conf_) * m1.distance)
+        {
+            matches_info.matches.push_back(m0);
+            matches.insert(make_pair(m0.queryIdx, m0.trainIdx));
+        }
     }
-  
 
-    void GpuSurfFeaturesFinder::find(const Mat &image, ImageFeatures &features)
+    // Find 2->1 matches
+    pair_matches.clear();
+    matcher.knnMatch(features2.descriptors, features1.descriptors, pair_matches, 2);
+    for (size_t i = 0; i < pair_matches.size(); ++i)
     {
-        CV_Assert(image.depth() == CV_8U);
+        if (pair_matches[i].size() < 2)
+            continue;
+        const DMatch& m0 = pair_matches[i][0];
+        const DMatch& m1 = pair_matches[i][1];
+        if (m0.distance < (1.f - match_conf_) * m1.distance)
+            if (matches.find(make_pair(m0.trainIdx, m0.queryIdx)) == matches.end())
+                matches_info.matches.push_back(DMatch(m0.trainIdx, m0.queryIdx, m0.distance));
+    }
+}
+
 
-        ensureSizeIsEnough(image.size(), image.type(), image_);
-        image_.upload(image);
+void GpuMatcher::match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo& matches_info)
+{
+    matches_info.matches.clear();
 
-        ensureSizeIsEnough(image.size(), CV_8UC1, gray_image_);
-        cvtColor(image_, gray_image_, CV_BGR2GRAY);
+    ensureSizeIsEnough(features1.descriptors.size(), features1.descriptors.type(), descriptors1_);
+    ensureSizeIsEnough(features2.descriptors.size(), features2.descriptors.type(), descriptors2_);
 
-        surf_.nOctaves = num_octaves_;
-        surf_.nOctaveLayers = num_layers_;
-        surf_.upright = false;
-        surf_(gray_image_, GpuMat(), keypoints_);
+    descriptors1_.upload(features1.descriptors);
+    descriptors2_.upload(features2.descriptors);
 
-        surf_.nOctaves = num_octaves_descr_;
-        surf_.nOctaveLayers = num_layers_descr_;
-        surf_.upright = true;
-        surf_(gray_image_, GpuMat(), keypoints_, descriptors_, true);
-        surf_.downloadKeypoints(keypoints_, features.keypoints);
+    BruteForceMatcher_GPU< L2<float> > matcher;
+    MatchesSet matches;
 
-        descriptors_.download(features.descriptors);
+    // Find 1->2 matches
+    pair_matches.clear();
+    matcher.knnMatch(descriptors1_, descriptors2_, train_idx_, distance_, all_dist_, 2);
+    matcher.knnMatchDownload(train_idx_, distance_, pair_matches);
+    for (size_t i = 0; i < pair_matches.size(); ++i)
+    {
+        if (pair_matches[i].size() < 2)
+            continue;
+        const DMatch& m0 = pair_matches[i][0];
+        const DMatch& m1 = pair_matches[i][1];
+        if (m0.distance < (1.f - match_conf_) * m1.distance)
+        {
+            matches_info.matches.push_back(m0);
+            matches.insert(make_pair(m0.queryIdx, m0.trainIdx));
+        }
     }
 
-    void GpuSurfFeaturesFinder::releaseMemory()
+    // Find 2->1 matches
+    pair_matches.clear();
+    matcher.knnMatch(descriptors2_, descriptors1_, train_idx_, distance_, all_dist_, 2);
+    matcher.knnMatchDownload(train_idx_, distance_, pair_matches);
+    for (size_t i = 0; i < pair_matches.size(); ++i)
     {
-        surf_.releaseMemory();
-        image_.release();
-        gray_image_.release();
-        keypoints_.release();
-        descriptors_.release();
+        if (pair_matches[i].size() < 2)
+            continue;
+        const DMatch& m0 = pair_matches[i][0];
+        const DMatch& m1 = pair_matches[i][1];
+        if (m0.distance < (1.f - match_conf_) * m1.distance)
+            if (matches.find(make_pair(m0.trainIdx, m0.queryIdx)) == matches.end())
+                matches_info.matches.push_back(DMatch(m0.trainIdx, m0.queryIdx, m0.distance));
     }
+}
+
+void GpuMatcher::releaseMemory()
+{
+    descriptors1_.release();
+    descriptors2_.release();
+    train_idx_.release();
+    distance_.release();
+    all_dist_.release();
+    vector< vector<DMatch> >().swap(pair_matches);
+}
+
 } // namespace
 
 
-cv::SurfFeaturesFinder::SurfFeaturesFinder(bool try_use_gpu, double hess_thresh, int num_octaves, int num_layers,
+namespace cv {
+namespace detail {
+
+void FeaturesFinder::operator ()(const Mat &image, ImageFeatures &features)
+{ 
+    find(image, features);
+    features.img_size = image.size();
+    //features.img = image.clone();
+}
+
+
+SurfFeaturesFinder::SurfFeaturesFinder(bool try_use_gpu, double hess_thresh, int num_octaves, int num_layers,
                                        int num_octaves_descr, int num_layers_descr)
 {
     if (try_use_gpu && getCudaEnabledDeviceCount() > 0)
@@ -160,12 +355,13 @@ cv::SurfFeaturesFinder::SurfFeaturesFinder(bool try_use_gpu, double hess_thresh,
 }
 
 
-void cv::SurfFeaturesFinder::find(const Mat &image, ImageFeatures &features)
+void SurfFeaturesFinder::find(const Mat &image, ImageFeatures &features)
 {
     (*impl_)(image, features);
 }
 
-void cv::SurfFeaturesFinder::releaseMemory()
+
+void SurfFeaturesFinder::releaseMemory()
 {
     impl_->releaseMemory();
 }
@@ -173,11 +369,11 @@ void cv::SurfFeaturesFinder::releaseMemory()
 
 //////////////////////////////////////////////////////////////////////////////
 
-cv::MatchesInfo::MatchesInfo() : src_img_idx(-1), dst_img_idx(-1), num_inliers(0), confidence(0) {}
+MatchesInfo::MatchesInfo() : src_img_idx(-1), dst_img_idx(-1), num_inliers(0), confidence(0) {}
 
-cv::MatchesInfo::MatchesInfo(const MatchesInfo &other) { *this = other; }
+MatchesInfo::MatchesInfo(const MatchesInfo &other) { *this = other; }
 
-const cv::MatchesInfo& MatchesInfo::operator =(const MatchesInfo &other)
+const MatchesInfo& MatchesInfo::operator =(const MatchesInfo &other)
 {
     src_img_idx = other.src_img_idx;
     dst_img_idx = other.dst_img_idx;
@@ -192,69 +388,7 @@ const cv::MatchesInfo& MatchesInfo::operator =(const MatchesInfo &other)
 
 //////////////////////////////////////////////////////////////////////////////
 
-namespace
-{
-
-    struct DistIdxPair
-    {
-        bool operator<(const DistIdxPair &other) const { return dist < other.dist; }
-        double dist;
-        int idx;
-    };
-
-
-    struct MatchPairsBody
-    {
-        MatchPairsBody(const MatchPairsBody& other)
-                : matcher(other.matcher), features(other.features),
-                  pairwise_matches(other.pairwise_matches), near_pairs(other.near_pairs) {}
-
-        MatchPairsBody(FeaturesMatcher &matcher, const vector<ImageFeatures> &features,
-                       vector<MatchesInfo> &pairwise_matches, vector<pair<int,int> > &near_pairs)
-                : matcher(matcher), features(features),
-                  pairwise_matches(pairwise_matches), near_pairs(near_pairs) {}
-
-        void operator ()(const BlockedRange &r) const
-        {
-            const int num_images = static_cast<int>(features.size());
-            for (int i = r.begin(); i < r.end(); ++i)
-            {
-                int from = near_pairs[i].first;
-                int to = near_pairs[i].second;
-                int pair_idx = from*num_images + to;
-
-                matcher(features[from], features[to], pairwise_matches[pair_idx]);
-                pairwise_matches[pair_idx].src_img_idx = from;
-                pairwise_matches[pair_idx].dst_img_idx = to;
-
-                size_t dual_pair_idx = to*num_images + from;
-
-                pairwise_matches[dual_pair_idx] = pairwise_matches[pair_idx];
-                pairwise_matches[dual_pair_idx].src_img_idx = to;
-                pairwise_matches[dual_pair_idx].dst_img_idx = from;
-
-                if (!pairwise_matches[pair_idx].H.empty())
-                    pairwise_matches[dual_pair_idx].H = pairwise_matches[pair_idx].H.inv();
-
-                for (size_t j = 0; j < pairwise_matches[dual_pair_idx].matches.size(); ++j)
-                    std::swap(pairwise_matches[dual_pair_idx].matches[j].queryIdx,
-                              pairwise_matches[dual_pair_idx].matches[j].trainIdx);
-                LOG(".");
-            }
-        }
-
-        FeaturesMatcher &matcher;
-        const vector<ImageFeatures> &features;
-        vector<MatchesInfo> &pairwise_matches;
-        vector<pair<int,int> > &near_pairs;
-
-    private:
-        void operator =(const MatchPairsBody&);
-    };
-} // namespace
-
-
-void cv::FeaturesMatcher::operator ()(const vector<ImageFeatures> &features, vector<MatchesInfo> &pairwise_matches)
+void FeaturesMatcher::operator ()(const vector<ImageFeatures> &features, vector<MatchesInfo> &pairwise_matches)
 {
     const int num_images = static_cast<int>(features.size());
 
@@ -276,138 +410,7 @@ void cv::FeaturesMatcher::operator ()(const vector<ImageFeatures> &features, vec
 
 //////////////////////////////////////////////////////////////////////////////
 
-namespace 
-{
-    typedef set<pair<int,int> > MatchesSet;
-
-    // These two classes are aimed to find features matches only, not to 
-    // estimate homography
-
-    class CpuMatcher : public FeaturesMatcher
-    {
-    public:
-        CpuMatcher(float match_conf) : FeaturesMatcher(true), match_conf_(match_conf) {}
-        void match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo& matches_info);
-
-    private:
-        float match_conf_;
-    };
-
-
-    class GpuMatcher : public FeaturesMatcher
-    {
-    public:
-        GpuMatcher(float match_conf) : match_conf_(match_conf) {}
-        void match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo& matches_info);
-
-        void releaseMemory();
-
-    private:
-        float match_conf_;
-        GpuMat descriptors1_, descriptors2_;
-        GpuMat train_idx_, distance_, all_dist_;
-        vector< vector<DMatch> > pair_matches;
-    };
-
-
-    void CpuMatcher::match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo& matches_info)
-    {
-        matches_info.matches.clear();
-        FlannBasedMatcher matcher;
-        vector< vector<DMatch> > pair_matches;        
-        MatchesSet matches;
-
-        // Find 1->2 matches
-        matcher.knnMatch(features1.descriptors, features2.descriptors, pair_matches, 2);
-        for (size_t i = 0; i < pair_matches.size(); ++i)
-        {
-            if (pair_matches[i].size() < 2)
-                continue;
-            const DMatch& m0 = pair_matches[i][0];
-            const DMatch& m1 = pair_matches[i][1];
-            if (m0.distance < (1.f - match_conf_) * m1.distance)
-            {
-                matches_info.matches.push_back(m0);
-                matches.insert(make_pair(m0.queryIdx, m0.trainIdx));
-            }
-        }
-
-        // Find 2->1 matches
-        pair_matches.clear();
-        matcher.knnMatch(features2.descriptors, features1.descriptors, pair_matches, 2);
-        for (size_t i = 0; i < pair_matches.size(); ++i)
-        {
-            if (pair_matches[i].size() < 2)
-                continue;
-            const DMatch& m0 = pair_matches[i][0];
-            const DMatch& m1 = pair_matches[i][1];
-            if (m0.distance < (1.f - match_conf_) * m1.distance)
-                if (matches.find(make_pair(m0.trainIdx, m0.queryIdx)) == matches.end())
-                    matches_info.matches.push_back(DMatch(m0.trainIdx, m0.queryIdx, m0.distance));
-        }
-    }
-       
-
-    void GpuMatcher::match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo& matches_info)
-    {
-        matches_info.matches.clear(); 
-
-        ensureSizeIsEnough(features1.descriptors.size(), features1.descriptors.type(), descriptors1_);
-        ensureSizeIsEnough(features2.descriptors.size(), features2.descriptors.type(), descriptors2_);
-
-        descriptors1_.upload(features1.descriptors);
-        descriptors2_.upload(features2.descriptors);
-
-        BruteForceMatcher_GPU< L2<float> > matcher;
-        MatchesSet matches;
-
-        // Find 1->2 matches
-        pair_matches.clear();
-        matcher.knnMatch(descriptors1_, descriptors2_, train_idx_, distance_, all_dist_, 2);
-        matcher.knnMatchDownload(train_idx_, distance_, pair_matches);
-        for (size_t i = 0; i < pair_matches.size(); ++i)
-        {
-            if (pair_matches[i].size() < 2)
-                continue;
-            const DMatch& m0 = pair_matches[i][0];
-            const DMatch& m1 = pair_matches[i][1];
-            if (m0.distance < (1.f - match_conf_) * m1.distance)
-            {
-                matches_info.matches.push_back(m0);
-                matches.insert(make_pair(m0.queryIdx, m0.trainIdx));
-            }
-        }
-
-        // Find 2->1 matches
-        pair_matches.clear();
-        matcher.knnMatch(descriptors2_, descriptors1_, train_idx_, distance_, all_dist_, 2);
-        matcher.knnMatchDownload(train_idx_, distance_, pair_matches);
-        for (size_t i = 0; i < pair_matches.size(); ++i)
-        {
-            if (pair_matches[i].size() < 2)
-                continue;
-            const DMatch& m0 = pair_matches[i][0];
-            const DMatch& m1 = pair_matches[i][1];
-            if (m0.distance < (1.f - match_conf_) * m1.distance)
-                if (matches.find(make_pair(m0.trainIdx, m0.queryIdx)) == matches.end())
-                    matches_info.matches.push_back(DMatch(m0.trainIdx, m0.queryIdx, m0.distance));
-        }
-    }
-
-    void GpuMatcher::releaseMemory()
-    {
-        descriptors1_.release();
-        descriptors2_.release();
-        train_idx_.release();
-        distance_.release();
-        all_dist_.release();
-        vector< vector<DMatch> >().swap(pair_matches);
-    }
-
-} // namespace
-
-
-cv::BestOf2NearestMatcher::BestOf2NearestMatcher(bool try_use_gpu, float match_conf, int num_matches_thresh1, int num_matches_thresh2)
+BestOf2NearestMatcher::BestOf2NearestMatcher(bool try_use_gpu, float match_conf, int num_matches_thresh1, int num_matches_thresh2)
 {
     if (try_use_gpu && getCudaEnabledDeviceCount() > 0)
         impl_ = new GpuMatcher(match_conf);
@@ -420,7 +423,7 @@ cv::BestOf2NearestMatcher::BestOf2NearestMatcher(bool try_use_gpu, float match_c
 }
 
 
-void cv::BestOf2NearestMatcher::match(const ImageFeatures &features1, const ImageFeatures &features2,
+void BestOf2NearestMatcher::match(const ImageFeatures &features1, const ImageFeatures &features2,
                                   MatchesInfo &matches_info)
 {
     (*impl_)(features1, features2, matches_info);
@@ -498,7 +501,10 @@ void cv::BestOf2NearestMatcher::match(const ImageFeatures &features1, const Imag
     matches_info.H = findHomography(src_points, dst_points, CV_RANSAC);
 }
 
-void cv::BestOf2NearestMatcher::releaseMemory()
+void BestOf2NearestMatcher::releaseMemory()
 {
     impl_->releaseMemory();
 }
+
+} // namespace detail
+} // namespace cv
diff --git a/modules/stitching/src/motion_estimators.cpp b/modules/stitching/src/motion_estimators.cpp
index 5b29dd8548..786acd72fa 100644
--- a/modules/stitching/src/motion_estimators.cpp
+++ b/modules/stitching/src/motion_estimators.cpp
@@ -39,56 +39,69 @@
 // the use of this software, even if advised of the possibility of such damage.
 //
 //M*/
+
 #include "precomp.hpp"
 
 using namespace std;
 using namespace cv;
+using namespace cv::detail;
+
+namespace {
 
-namespace
+struct IncDistance
 {
+    IncDistance(vector<int> &dists) : dists(&dists[0]) {}
+    void operator ()(const GraphEdge &edge) { dists[edge.to] = dists[edge.from] + 1; }
+    int* dists;
+};
 
-    struct IncDistance
-    {
-        IncDistance(vector<int> &dists) : dists(&dists[0]) {}
-        void operator ()(const GraphEdge &edge) { dists[edge.to] = dists[edge.from] + 1; }
-        int* dists;
-    };
 
+struct CalcRotation
+{
+    CalcRotation(int num_images, const vector<MatchesInfo> &pairwise_matches, vector<CameraParams> &cameras)
+        : num_images(num_images), pairwise_matches(&pairwise_matches[0]), cameras(&cameras[0]) {}
 
-    struct CalcRotation
+    void operator ()(const GraphEdge &edge)
     {
-        CalcRotation(int num_images, const vector<MatchesInfo> &pairwise_matches, vector<CameraParams> &cameras)
-            : num_images(num_images), pairwise_matches(&pairwise_matches[0]), cameras(&cameras[0]) {}
+        int pair_idx = edge.from * num_images + edge.to;
 
-        void operator ()(const GraphEdge &edge)
-        {
-            int pair_idx = edge.from * num_images + edge.to;
+        double f_from = cameras[edge.from].focal;
+        double f_to = cameras[edge.to].focal;
+
+        Mat K_from = Mat::eye(3, 3, CV_64F);
+        K_from.at<double>(0, 0) = f_from;
+        K_from.at<double>(1, 1) = f_from;
 
-            double f_from = cameras[edge.from].focal;
-            double f_to = cameras[edge.to].focal;
+        Mat K_to = Mat::eye(3, 3, CV_64F);
+        K_to.at<double>(0, 0) = f_to;
+        K_to.at<double>(1, 1) = f_to;
 
-            Mat K_from = Mat::eye(3, 3, CV_64F);
-            K_from.at<double>(0, 0) = f_from;
-            K_from.at<double>(1, 1) = f_from;
+        Mat R = K_from.inv() * pairwise_matches[pair_idx].H.inv() * K_to;
+        cameras[edge.to].R = cameras[edge.from].R * R;
+    }
 
-            Mat K_to = Mat::eye(3, 3, CV_64F);
-            K_to.at<double>(0, 0) = f_to;
-            K_to.at<double>(1, 1) = f_to;
+    int num_images;
+    const MatchesInfo* pairwise_matches;
+    CameraParams* cameras;
+};
 
-            Mat R = K_from.inv() * pairwise_matches[pair_idx].H.inv() * K_to;
-            cameras[edge.to].R = cameras[edge.from].R * R;
-        }
 
-        int num_images;
-        const MatchesInfo* pairwise_matches;
-        CameraParams* cameras;
-    };
+//////////////////////////////////////////////////////////////////////////////
+
+void calcDeriv(const Mat &err1, const Mat &err2, double h, Mat res)
+{
+    for (int i = 0; i < err1.rows; ++i)
+        res.at<double>(i, 0) = (err2.at<double>(i, 0) - err1.at<double>(i, 0)) / h;
+}
 
 } // namespace
 
 
-void cv::HomographyBasedEstimator::estimate(const vector<ImageFeatures> &features, const vector<MatchesInfo> &pairwise_matches,
-                                            vector<CameraParams> &cameras)
+namespace cv {
+namespace detail {
+
+void HomographyBasedEstimator::estimate(const vector<ImageFeatures> &features, const vector<MatchesInfo> &pairwise_matches,
+                                        vector<CameraParams> &cameras)
 {
     LOGLN("Estimating rotations...");
     int64 t = getTickCount();
@@ -135,8 +148,8 @@ void cv::HomographyBasedEstimator::estimate(const vector<ImageFeatures> &feature
 
 //////////////////////////////////////////////////////////////////////////////
 
-void cv::BundleAdjuster::estimate(const vector<ImageFeatures> &features, const vector<MatchesInfo> &pairwise_matches,
-                                  vector<CameraParams> &cameras)
+void BundleAdjuster::estimate(const vector<ImageFeatures> &features, const vector<MatchesInfo> &pairwise_matches,
+                              vector<CameraParams> &cameras)
 {
     if (cost_space_ == NO)
         return;
@@ -250,7 +263,7 @@ void cv::BundleAdjuster::estimate(const vector<ImageFeatures> &features, const v
 }
 
 
-void cv::BundleAdjuster::calcError(Mat &err)
+void BundleAdjuster::calcError(Mat &err)
 {
     err.create(total_num_matches_ * 3, 1, CV_64F);
 
@@ -314,19 +327,7 @@ void cv::BundleAdjuster::calcError(Mat &err)
 }
 
 
-namespace
-{
-
-    void calcDeriv(const Mat &err1, const Mat &err2, double h, Mat res)
-    {
-        for (int i = 0; i < err1.rows; ++i)
-            res.at<double>(i, 0) = (err2.at<double>(i, 0) - err1.at<double>(i, 0)) / h;
-    }
-
-} // namespace
-
-
-void cv::BundleAdjuster::calcJacobian()
+void BundleAdjuster::calcJacobian()
 {
     J_.create(total_num_matches_ * 3, num_images_ * 4, CV_64F);
 
@@ -374,7 +375,7 @@ void cv::BundleAdjuster::calcJacobian()
 //////////////////////////////////////////////////////////////////////////////
 
 // TODO replace SVD with eigen
-void cv::waveCorrect(vector<Mat> &rmats)
+void waveCorrect(vector<Mat> &rmats)
 {
     LOGLN("Wave correcting...");
     int64 t = getTickCount();
@@ -415,7 +416,7 @@ void cv::waveCorrect(vector<Mat> &rmats)
 
 //////////////////////////////////////////////////////////////////////////////
 
-string cv::matchesGraphAsString(vector<string> &pathes, vector<MatchesInfo> &pairwise_matches,
+string matchesGraphAsString(vector<string> &pathes, vector<MatchesInfo> &pairwise_matches,
                                 float conf_threshold)
 {
     stringstream str;
@@ -481,7 +482,7 @@ string cv::matchesGraphAsString(vector<string> &pathes, vector<MatchesInfo> &pai
     return str.str();
 }
 
-vector<int> cv::leaveBiggestComponent(vector<ImageFeatures> &features,  vector<MatchesInfo> &pairwise_matches,
+vector<int> leaveBiggestComponent(vector<ImageFeatures> &features,  vector<MatchesInfo> &pairwise_matches,
                                       float conf_threshold)
 {
     const int num_images = static_cast<int>(features.size());
@@ -539,7 +540,7 @@ vector<int> cv::leaveBiggestComponent(vector<ImageFeatures> &features,  vector<M
 }
 
 
-void cv::findMaxSpanningTree(int num_images, const vector<MatchesInfo> &pairwise_matches,
+void findMaxSpanningTree(int num_images, const vector<MatchesInfo> &pairwise_matches,
                              Graph &span_tree, vector<int> &centers)
 {
     Graph graph(num_images);
@@ -608,3 +609,7 @@ void cv::findMaxSpanningTree(int num_images, const vector<MatchesInfo> &pairwise
             centers.push_back(i);
     CV_Assert(centers.size() > 0 && centers.size() <= 2);
 }
+
+} // namespace detail
+} // namespace cv
+
diff --git a/modules/stitching/src/precomp.hpp b/modules/stitching/src/precomp.hpp
index 895966939d..32f50c7b7c 100644
--- a/modules/stitching/src/precomp.hpp
+++ b/modules/stitching/src/precomp.hpp
@@ -52,15 +52,15 @@
 #include <set>
 #include <functional>
 #include <sstream>
-#include "opencv2/stitching/autocalib.hpp"
-#include "opencv2/stitching/blenders.hpp"
-#include "opencv2/stitching/camera.hpp"
-#include "opencv2/stitching/exposure_compensate.hpp"
-#include "opencv2/stitching/matchers.hpp"
-#include "opencv2/stitching/motion_estimators.hpp"
-#include "opencv2/stitching/seam_finders.hpp"
-#include "opencv2/stitching/util.hpp"
-#include "opencv2/stitching/warpers.hpp"
+#include "opencv2/stitching/detail/autocalib.hpp"
+#include "opencv2/stitching/detail/blenders.hpp"
+#include "opencv2/stitching/detail/camera.hpp"
+#include "opencv2/stitching/detail/exposure_compensate.hpp"
+#include "opencv2/stitching/detail/matchers.hpp"
+#include "opencv2/stitching/detail/motion_estimators.hpp"
+#include "opencv2/stitching/detail/seam_finders.hpp"
+#include "opencv2/stitching/detail/util.hpp"
+#include "opencv2/stitching/detail/warpers.hpp"
 #include "opencv2/core/core.hpp"
 #include "opencv2/core/internal.hpp"
 #include "opencv2/imgproc/imgproc.hpp"
diff --git a/modules/stitching/src/seam_finders.cpp b/modules/stitching/src/seam_finders.cpp
index 68a777c69a..7a964bff35 100644
--- a/modules/stitching/src/seam_finders.cpp
+++ b/modules/stitching/src/seam_finders.cpp
@@ -39,12 +39,15 @@
 // the use of this software, even if advised of the possibility of such damage.
 //
 //M*/
+
 #include "precomp.hpp"
 
 using namespace std;
-using namespace cv;
 
-Ptr<SeamFinder> cv::SeamFinder::createDefault(int type)
+namespace cv {
+namespace detail {
+
+Ptr<SeamFinder> SeamFinder::createDefault(int type)
 {
     if (type == NO)
         return new NoSeamFinder();
@@ -59,7 +62,7 @@ Ptr<SeamFinder> cv::SeamFinder::createDefault(int type)
 }
 
 
-void cv::PairwiseSeamFinder::find(const vector<Mat> &src, const vector<Point> &corners,
+void PairwiseSeamFinder::find(const vector<Mat> &src, const vector<Point> &corners,
                                   vector<Mat> &masks)
 {
     LOGLN("Finding seams...");
@@ -86,7 +89,7 @@ void cv::PairwiseSeamFinder::find(const vector<Mat> &src, const vector<Point> &c
 }
 
 
-void cv::VoronoiSeamFinder::findInPair(size_t first, size_t second, Rect roi)
+void VoronoiSeamFinder::findInPair(size_t first, size_t second, Rect roi)
 {
     const int gap = 10;
     Mat submask1(roi.height + 2 * gap, roi.width + 2 * gap, CV_8U);
@@ -140,7 +143,7 @@ void cv::VoronoiSeamFinder::findInPair(size_t first, size_t second, Rect roi)
 }
 
 
-class cv::GraphCutSeamFinder::Impl : public PairwiseSeamFinder
+class GraphCutSeamFinder::Impl : public PairwiseSeamFinder
 {
 public:
     Impl(int cost_type, float terminal_cost, float bad_region_penalty)
@@ -163,7 +166,7 @@ private:
 };
 
 
-void cv::GraphCutSeamFinder::Impl::find(const vector<Mat> &src, const vector<Point> &corners,
+void GraphCutSeamFinder::Impl::find(const vector<Mat> &src, const vector<Point> &corners,
                                         vector<Mat> &masks)
 {
     // Compute gradients
@@ -194,7 +197,7 @@ void cv::GraphCutSeamFinder::Impl::find(const vector<Mat> &src, const vector<Poi
 }
 
 
-void cv::GraphCutSeamFinder::Impl::setGraphWeightsColor(const Mat &img1, const Mat &img2,
+void GraphCutSeamFinder::Impl::setGraphWeightsColor(const Mat &img1, const Mat &img2,
                                                         const Mat &mask1, const Mat &mask2, GCGraph<float> &graph)
 {
     const Size img_size = img1.size();
@@ -242,7 +245,7 @@ void cv::GraphCutSeamFinder::Impl::setGraphWeightsColor(const Mat &img1, const M
 }
 
 
-void cv::GraphCutSeamFinder::Impl::setGraphWeightsColorGrad(
+void GraphCutSeamFinder::Impl::setGraphWeightsColorGrad(
         const Mat &img1, const Mat &img2, const Mat &dx1, const Mat &dx2, 
         const Mat &dy1, const Mat &dy2, const Mat &mask1, const Mat &mask2, 
         GCGraph<float> &graph)
@@ -296,7 +299,7 @@ void cv::GraphCutSeamFinder::Impl::setGraphWeightsColorGrad(
 }
 
 
-void cv::GraphCutSeamFinder::Impl::findInPair(size_t first, size_t second, Rect roi)
+void GraphCutSeamFinder::Impl::findInPair(size_t first, size_t second, Rect roi)
 {
     Mat img1 = images_[first], img2 = images_[second];
     Mat dx1 = dx_[first], dx2 = dx_[second];
@@ -394,12 +397,16 @@ void cv::GraphCutSeamFinder::Impl::findInPair(size_t first, size_t second, Rect
 }
 
 
-cv::GraphCutSeamFinder::GraphCutSeamFinder(int cost_type, float terminal_cost, float bad_region_penalty)
+GraphCutSeamFinder::GraphCutSeamFinder(int cost_type, float terminal_cost, float bad_region_penalty)
     : impl_(new Impl(cost_type, terminal_cost, bad_region_penalty)) {}
 
 
-void cv::GraphCutSeamFinder::find(const vector<Mat> &src, const vector<Point> &corners,
+void GraphCutSeamFinder::find(const vector<Mat> &src, const vector<Point> &corners,
                               vector<Mat> &masks)
 {
     impl_->find(src, corners, masks);
 }
+
+} // namespace detail
+} // namespace cv
+
diff --git a/modules/stitching/src/util.cpp b/modules/stitching/src/util.cpp
index 7311b0bf10..57a95f352d 100644
--- a/modules/stitching/src/util.cpp
+++ b/modules/stitching/src/util.cpp
@@ -42,9 +42,11 @@
 #include "precomp.hpp"
 
 using namespace std;
-using namespace cv;
 
-void cv::DisjointSets::createOneElemSets(int n)
+namespace cv {
+namespace detail {
+
+void DisjointSets::createOneElemSets(int n)
 {
     rank_.assign(n, 0);
     size.assign(n, 1);
@@ -54,7 +56,7 @@ void cv::DisjointSets::createOneElemSets(int n)
 }
 
 
-int cv::DisjointSets::findSetByElem(int elem)
+int DisjointSets::findSetByElem(int elem)
 {
     int set = elem;
     while (set != parent[set])
@@ -70,7 +72,7 @@ int cv::DisjointSets::findSetByElem(int elem)
 }
 
 
-int cv::DisjointSets::mergeSets(int set1, int set2)
+int DisjointSets::mergeSets(int set1, int set2)
 {
     if (rank_[set1] < rank_[set2]) 
     {
@@ -91,13 +93,13 @@ int cv::DisjointSets::mergeSets(int set1, int set2)
 }
 
 
-void cv::Graph::addEdge(int from, int to, float weight)
+void Graph::addEdge(int from, int to, float weight)
 {
     edges_[from].push_back(GraphEdge(from, to, weight));
 }
 
 
-bool cv::overlapRoi(Point tl1, Point tl2, Size sz1, Size sz2, Rect &roi)
+bool overlapRoi(Point tl1, Point tl2, Size sz1, Size sz2, Rect &roi)
 {
     int x_tl = max(tl1.x, tl2.x);
     int y_tl = max(tl1.y, tl2.y);
@@ -112,7 +114,7 @@ bool cv::overlapRoi(Point tl1, Point tl2, Size sz1, Size sz2, Rect &roi)
 }
 
 
-Rect cv::resultRoi(const vector<Point> &corners, const vector<Mat> &images)
+Rect resultRoi(const vector<Point> &corners, const vector<Mat> &images)
 {
     vector<Size> sizes(images.size());
     for (size_t i = 0; i < images.size(); ++i)
@@ -121,7 +123,7 @@ Rect cv::resultRoi(const vector<Point> &corners, const vector<Mat> &images)
 }
 
 
-Rect cv::resultRoi(const vector<Point> &corners, const vector<Size> &sizes)
+Rect resultRoi(const vector<Point> &corners, const vector<Size> &sizes)
 {
     CV_Assert(sizes.size() == corners.size());
     Point tl(numeric_limits<int>::max(), numeric_limits<int>::max());
@@ -137,7 +139,7 @@ Rect cv::resultRoi(const vector<Point> &corners, const vector<Size> &sizes)
 }
 
 
-Point cv::resultTl(const vector<Point> &corners)
+Point resultTl(const vector<Point> &corners)
 {
     Point tl(numeric_limits<int>::max(), numeric_limits<int>::max());
     for (size_t i = 0; i < corners.size(); ++i)
@@ -149,7 +151,7 @@ Point cv::resultTl(const vector<Point> &corners)
 }
 
 
-void cv::selectRandomSubset(int count, int size, vector<int> &subset)
+void selectRandomSubset(int count, int size, vector<int> &subset)
 {
     subset.clear();
     for (int i = 0; i < size; ++i)
@@ -161,3 +163,6 @@ void cv::selectRandomSubset(int count, int size, vector<int> &subset)
         }
     }
 }
+
+} // namespace detail
+} // namespace cv
diff --git a/modules/stitching/src/warpers.cpp b/modules/stitching/src/warpers.cpp
index 096506cf43..d747093d9a 100644
--- a/modules/stitching/src/warpers.cpp
+++ b/modules/stitching/src/warpers.cpp
@@ -39,12 +39,15 @@
 // the use of this software, even if advised of the possibility of such damage.
 //
 //M*/
+
 #include "precomp.hpp"
 
 using namespace std;
-using namespace cv;
 
-Ptr<Warper> cv::Warper::createByCameraFocal(float focal, int type, bool try_gpu)
+namespace cv {
+namespace detail {
+
+Ptr<Warper> Warper::createByCameraFocal(float focal, int type, bool try_gpu)
 {
     bool can_use_gpu = try_gpu && gpu::getCudaEnabledDeviceCount();
     if (type == PLANE)
@@ -58,7 +61,7 @@ Ptr<Warper> cv::Warper::createByCameraFocal(float focal, int type, bool try_gpu)
 }
 
 
-void cv::ProjectorBase::setTransformation(const Mat &R)
+void ProjectorBase::setTransformation(const Mat &R)
 {
     CV_Assert(R.size() == Size(3, 3));
     CV_Assert(R.type() == CV_32F);
@@ -73,7 +76,7 @@ void cv::ProjectorBase::setTransformation(const Mat &R)
 }
 
 
-void cv::PlaneWarper::detectResultRoi(Point &dst_tl, Point &dst_br)
+void PlaneWarper::detectResultRoi(Point &dst_tl, Point &dst_br)
 {
     float tl_uf = numeric_limits<float>::max();
     float tl_vf = numeric_limits<float>::max();
@@ -105,14 +108,14 @@ void cv::PlaneWarper::detectResultRoi(Point &dst_tl, Point &dst_br)
 }
 
 
-Point cv::PlaneWarperGpu::warp(const Mat &src, float focal, const cv::Mat &R, cv::Mat &dst, int interp_mode, int border_mode)
+Point PlaneWarperGpu::warp(const Mat &src, float focal, const Mat &R, Mat &dst, int interp_mode, int border_mode)
 {
     src_size_ = src.size();
     projector_.size = src.size();
     projector_.focal = focal;
     projector_.setTransformation(R);
 
-    cv::Point dst_tl, dst_br;
+    Point dst_tl, dst_br;
     detectResultRoi(dst_tl, dst_br);
 
     gpu::buildWarpPlaneMaps(src.size(), Rect(dst_tl, Point(dst_br.x+1, dst_br.y+1)),
@@ -131,7 +134,7 @@ Point cv::PlaneWarperGpu::warp(const Mat &src, float focal, const cv::Mat &R, cv
 }
 
 
-void cv::SphericalWarper::detectResultRoi(Point &dst_tl, Point &dst_br)
+void SphericalWarper::detectResultRoi(Point &dst_tl, Point &dst_br)
 {
     detectResultRoiByBorder(dst_tl, dst_br);
 
@@ -175,7 +178,7 @@ void cv::SphericalWarper::detectResultRoi(Point &dst_tl, Point &dst_br)
 }
 
 
-Point cv::SphericalWarperGpu::warp(const Mat &src, float focal, const Mat &R, Mat &dst,
+Point SphericalWarperGpu::warp(const Mat &src, float focal, const Mat &R, Mat &dst,
                                    int interp_mode, int border_mode)
 {
     src_size_ = src.size();
@@ -183,7 +186,7 @@ Point cv::SphericalWarperGpu::warp(const Mat &src, float focal, const Mat &R, Ma
     projector_.focal = focal;
     projector_.setTransformation(R);
 
-    cv::Point dst_tl, dst_br;
+    Point dst_tl, dst_br;
     detectResultRoi(dst_tl, dst_br);
 
     gpu::buildWarpSphericalMaps(src.size(), Rect(dst_tl, Point(dst_br.x+1, dst_br.y+1)),
@@ -202,7 +205,7 @@ Point cv::SphericalWarperGpu::warp(const Mat &src, float focal, const Mat &R, Ma
 }
 
 
-Point cv::CylindricalWarperGpu::warp(const Mat &src, float focal, const Mat &R, Mat &dst,
+Point CylindricalWarperGpu::warp(const Mat &src, float focal, const Mat &R, Mat &dst,
                                      int interp_mode, int border_mode)
 {
     src_size_ = src.size();
@@ -210,7 +213,7 @@ Point cv::CylindricalWarperGpu::warp(const Mat &src, float focal, const Mat &R,
     projector_.focal = focal;
     projector_.setTransformation(R);
 
-    cv::Point dst_tl, dst_br;
+    Point dst_tl, dst_br;
     detectResultRoi(dst_tl, dst_br);
 
     gpu::buildWarpCylindricalMaps(src.size(), Rect(dst_tl, Point(dst_br.x+1, dst_br.y+1)),
@@ -228,3 +231,5 @@ Point cv::CylindricalWarperGpu::warp(const Mat &src, float focal, const Mat &R,
     return dst_tl;
 }
 
+} // namespace detail
+} // namespace cv
diff --git a/samples/cpp/stitching.cpp b/samples/cpp/stitching.cpp
index 4d2215662e..813e7183ff 100644
--- a/samples/cpp/stitching.cpp
+++ b/samples/cpp/stitching.cpp
@@ -49,11 +49,20 @@
 //    Matthew Brown and David G. Lowe. 2007.
 
 #include <fstream>
-#include "opencv2/stitching/stitching.hpp"
 #include "opencv2/highgui/highgui.hpp"
+#include "opencv2/stitching/detail/autocalib.hpp"
+#include "opencv2/stitching/detail/blenders.hpp"
+#include "opencv2/stitching/detail/camera.hpp"
+#include "opencv2/stitching/detail/exposure_compensate.hpp"
+#include "opencv2/stitching/detail/matchers.hpp"
+#include "opencv2/stitching/detail/motion_estimators.hpp"
+#include "opencv2/stitching/detail/seam_finders.hpp"
+#include "opencv2/stitching/detail/util.hpp"
+#include "opencv2/stitching/detail/warpers.hpp"
 
 using namespace std;
 using namespace cv;
+using namespace cv::detail;
 
 void printUsage()
 {