include/opencv2/stitching/detail/motion_estimators.hpp
Go to the documentation of this file.
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_MOTION_ESTIMATORS_HPP__
00044 #define __OPENCV_STITCHING_MOTION_ESTIMATORS_HPP__
00045 
00046 #include "opencv2/core/core.hpp"
00047 #include "matchers.hpp"
00048 #include "util.hpp"
00049 #include "camera.hpp"
00050 
00051 namespace cv {
00052 namespace detail {
00053 
00054 class CV_EXPORTS Estimator
00055 {
00056 public:
00057     virtual ~Estimator() {}
00058 
00059     void operator ()(const std::vector<ImageFeatures> &features, const std::vector<MatchesInfo> &pairwise_matches,
00060                      std::vector<CameraParams> &cameras)
00061         { estimate(features, pairwise_matches, cameras); }
00062 
00063 protected:
00064     virtual void estimate(const std::vector<ImageFeatures> &features, const std::vector<MatchesInfo> &pairwise_matches,
00065                           std::vector<CameraParams> &cameras) = 0;
00066 };
00067 
00068 
00069 class CV_EXPORTS HomographyBasedEstimator : public Estimator
00070 {
00071 public:
00072     HomographyBasedEstimator(bool is_focals_estimated = false)
00073         : is_focals_estimated_(is_focals_estimated) {}
00074 
00075 private:
00076     void estimate(const std::vector<ImageFeatures> &features, const std::vector<MatchesInfo> &pairwise_matches,
00077                   std::vector<CameraParams> &cameras);
00078 
00079     bool is_focals_estimated_;
00080 };
00081 
00082 
00083 class CV_EXPORTS BundleAdjusterBase : public Estimator
00084 {
00085 public:
00086     const Mat refinementMask() const { return refinement_mask_.clone(); }
00087     void setRefinementMask(const Mat &mask)
00088     {
00089         CV_Assert(mask.type() == CV_8U && mask.size() == Size(3, 3));
00090         refinement_mask_ = mask.clone();
00091     }
00092 
00093     double confThresh() const { return conf_thresh_; }
00094     void setConfThresh(double conf_thresh) { conf_thresh_ = conf_thresh; }
00095 
00096     CvTermCriteria termCriteria() { return term_criteria_; }
00097     void setTermCriteria(const CvTermCriteria& term_criteria) { term_criteria_ = term_criteria; }
00098 
00099 protected:
00100     BundleAdjusterBase(int num_params_per_cam, int num_errs_per_measurement)
00101         : num_params_per_cam_(num_params_per_cam),
00102           num_errs_per_measurement_(num_errs_per_measurement)
00103     {
00104         setRefinementMask(Mat::ones(3, 3, CV_8U));
00105         setConfThresh(1.);
00106         setTermCriteria(cvTermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 1000, DBL_EPSILON));
00107     }
00108 
00109     // Runs bundle adjustment
00110     virtual void estimate(const std::vector<ImageFeatures> &features,
00111                           const std::vector<MatchesInfo> &pairwise_matches,
00112                           std::vector<CameraParams> &cameras);
00113 
00114     virtual void setUpInitialCameraParams(const std::vector<CameraParams> &cameras) = 0;
00115     virtual void obtainRefinedCameraParams(std::vector<CameraParams> &cameras) const = 0;
00116     virtual void calcError(Mat &err) = 0;
00117     virtual void calcJacobian(Mat &jac) = 0;
00118 
00119     // 3x3 8U mask, where 0 means don't refine respective parameter, != 0 means refine
00120     Mat refinement_mask_;
00121 
00122     int num_images_;
00123     int total_num_matches_;
00124 
00125     int num_params_per_cam_;
00126     int num_errs_per_measurement_;
00127 
00128     const ImageFeatures *features_;
00129     const MatchesInfo *pairwise_matches_;
00130 
00131     // Threshold to filter out poorly matched image pairs
00132     double conf_thresh_;
00133 
00134     //Levenberg–Marquardt algorithm termination criteria
00135     CvTermCriteria term_criteria_;
00136 
00137     // Camera parameters matrix (CV_64F)
00138     Mat cam_params_;
00139 
00140     // Connected images pairs
00141     std::vector<std::pair<int,int> > edges_;
00142 };
00143 
00144 
00145 // Minimizes reprojection error.
00146 // It can estimate focal length, aspect ratio, principal point.
00147 // You can affect only on them via the refinement mask.
00148 class CV_EXPORTS BundleAdjusterReproj : public BundleAdjusterBase
00149 {
00150 public:
00151     BundleAdjusterReproj() : BundleAdjusterBase(7, 2) {}
00152 
00153 private:
00154     void setUpInitialCameraParams(const std::vector<CameraParams> &cameras);
00155     void obtainRefinedCameraParams(std::vector<CameraParams> &cameras) const;
00156     void calcError(Mat &err);
00157     void calcJacobian(Mat &jac);
00158 
00159     Mat err1_, err2_;
00160 };
00161 
00162 
00163 // Minimizes sun of ray-to-ray distances.
00164 // It can estimate focal length. It ignores the refinement mask for now.
00165 class CV_EXPORTS BundleAdjusterRay : public BundleAdjusterBase
00166 {
00167 public:
00168     BundleAdjusterRay() : BundleAdjusterBase(4, 3) {}
00169 
00170 private:
00171     void setUpInitialCameraParams(const std::vector<CameraParams> &cameras);
00172     void obtainRefinedCameraParams(std::vector<CameraParams> &cameras) const;
00173     void calcError(Mat &err);
00174     void calcJacobian(Mat &jac);
00175 
00176     Mat err1_, err2_;
00177 };
00178 
00179 
00180 enum WaveCorrectKind
00181 {
00182     WAVE_CORRECT_HORIZ,
00183     WAVE_CORRECT_VERT
00184 };
00185 
00186 void CV_EXPORTS waveCorrect(std::vector<Mat> &rmats, WaveCorrectKind kind);
00187 
00188 
00190 // Auxiliary functions
00191 
00192 // Returns matches graph representation in DOT language
00193 std::string CV_EXPORTS matchesGraphAsString(std::vector<std::string> &pathes, std::vector<MatchesInfo> &pairwise_matches,
00194                                             float conf_threshold);
00195 
00196 std::vector<int> CV_EXPORTS leaveBiggestComponent(std::vector<ImageFeatures> &features, std::vector<MatchesInfo> &pairwise_matches,
00197                                                   float conf_threshold);
00198 
00199 void CV_EXPORTS findMaxSpanningTree(int num_images, const std::vector<MatchesInfo> &pairwise_matches,
00200                                     Graph &span_tree, std::vector<int> &centers);
00201 
00202 } // namespace detail
00203 } // namespace cv
00204 
00205 #endif // __OPENCV_STITCHING_MOTION_ESTIMATORS_HPP__