opencv on mbed

Dependencies:   mbed

Committer:
joeverbout
Date:
Thu Mar 31 21:16:38 2016 +0000
Revision:
0:ea44dc9ed014
OpenCV on mbed attempt

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joeverbout 0:ea44dc9ed014 1 /*M///////////////////////////////////////////////////////////////////////////////////////
joeverbout 0:ea44dc9ed014 2 //
joeverbout 0:ea44dc9ed014 3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
joeverbout 0:ea44dc9ed014 4 //
joeverbout 0:ea44dc9ed014 5 // By downloading, copying, installing or using the software you agree to this license.
joeverbout 0:ea44dc9ed014 6 // If you do not agree to this license, do not download, install,
joeverbout 0:ea44dc9ed014 7 // copy or use the software.
joeverbout 0:ea44dc9ed014 8 //
joeverbout 0:ea44dc9ed014 9 //
joeverbout 0:ea44dc9ed014 10 // License Agreement
joeverbout 0:ea44dc9ed014 11 // For Open Source Computer Vision Library
joeverbout 0:ea44dc9ed014 12 //
joeverbout 0:ea44dc9ed014 13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
joeverbout 0:ea44dc9ed014 14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
joeverbout 0:ea44dc9ed014 15 // Third party copyrights are property of their respective owners.
joeverbout 0:ea44dc9ed014 16 //
joeverbout 0:ea44dc9ed014 17 // Redistribution and use in source and binary forms, with or without modification,
joeverbout 0:ea44dc9ed014 18 // are permitted provided that the following conditions are met:
joeverbout 0:ea44dc9ed014 19 //
joeverbout 0:ea44dc9ed014 20 // * Redistribution's of source code must retain the above copyright notice,
joeverbout 0:ea44dc9ed014 21 // this list of conditions and the following disclaimer.
joeverbout 0:ea44dc9ed014 22 //
joeverbout 0:ea44dc9ed014 23 // * Redistribution's in binary form must reproduce the above copyright notice,
joeverbout 0:ea44dc9ed014 24 // this list of conditions and the following disclaimer in the documentation
joeverbout 0:ea44dc9ed014 25 // and/or other materials provided with the distribution.
joeverbout 0:ea44dc9ed014 26 //
joeverbout 0:ea44dc9ed014 27 // * The name of the copyright holders may not be used to endorse or promote products
joeverbout 0:ea44dc9ed014 28 // derived from this software without specific prior written permission.
joeverbout 0:ea44dc9ed014 29 //
joeverbout 0:ea44dc9ed014 30 // This software is provided by the copyright holders and contributors "as is" and
joeverbout 0:ea44dc9ed014 31 // any express or implied warranties, including, but not limited to, the implied
joeverbout 0:ea44dc9ed014 32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
joeverbout 0:ea44dc9ed014 33 // In no event shall the Intel Corporation or contributors be liable for any direct,
joeverbout 0:ea44dc9ed014 34 // indirect, incidental, special, exemplary, or consequential damages
joeverbout 0:ea44dc9ed014 35 // (including, but not limited to, procurement of substitute goods or services;
joeverbout 0:ea44dc9ed014 36 // loss of use, data, or profits; or business interruption) however caused
joeverbout 0:ea44dc9ed014 37 // and on any theory of liability, whether in contract, strict liability,
joeverbout 0:ea44dc9ed014 38 // or tort (including negligence or otherwise) arising in any way out of
joeverbout 0:ea44dc9ed014 39 // the use of this software, even if advised of the possibility of such damage.
joeverbout 0:ea44dc9ed014 40 //
joeverbout 0:ea44dc9ed014 41 //M*/
joeverbout 0:ea44dc9ed014 42
joeverbout 0:ea44dc9ed014 43 #ifndef __OPENCV_STITCHING_MOTION_ESTIMATORS_HPP__
joeverbout 0:ea44dc9ed014 44 #define __OPENCV_STITCHING_MOTION_ESTIMATORS_HPP__
joeverbout 0:ea44dc9ed014 45
joeverbout 0:ea44dc9ed014 46 #include "opencv2/core.hpp"
joeverbout 0:ea44dc9ed014 47 #include "matchers.hpp"
joeverbout 0:ea44dc9ed014 48 #include "util.hpp"
joeverbout 0:ea44dc9ed014 49 #include "camera.hpp"
joeverbout 0:ea44dc9ed014 50
joeverbout 0:ea44dc9ed014 51 namespace cv {
joeverbout 0:ea44dc9ed014 52 namespace detail {
joeverbout 0:ea44dc9ed014 53
joeverbout 0:ea44dc9ed014 54 //! @addtogroup stitching_rotation
joeverbout 0:ea44dc9ed014 55 //! @{
joeverbout 0:ea44dc9ed014 56
joeverbout 0:ea44dc9ed014 57 /** @brief Rotation estimator base class.
joeverbout 0:ea44dc9ed014 58
joeverbout 0:ea44dc9ed014 59 It takes features of all images, pairwise matches between all images and estimates rotations of all
joeverbout 0:ea44dc9ed014 60 cameras.
joeverbout 0:ea44dc9ed014 61
joeverbout 0:ea44dc9ed014 62 @note The coordinate system origin is implementation-dependent, but you can always normalize the
joeverbout 0:ea44dc9ed014 63 rotations in respect to the first camera, for instance. :
joeverbout 0:ea44dc9ed014 64 */
joeverbout 0:ea44dc9ed014 65 class CV_EXPORTS Estimator
joeverbout 0:ea44dc9ed014 66 {
joeverbout 0:ea44dc9ed014 67 public:
joeverbout 0:ea44dc9ed014 68 virtual ~Estimator() {}
joeverbout 0:ea44dc9ed014 69
joeverbout 0:ea44dc9ed014 70 /** @brief Estimates camera parameters.
joeverbout 0:ea44dc9ed014 71
joeverbout 0:ea44dc9ed014 72 @param features Features of images
joeverbout 0:ea44dc9ed014 73 @param pairwise_matches Pairwise matches of images
joeverbout 0:ea44dc9ed014 74 @param cameras Estimated camera parameters
joeverbout 0:ea44dc9ed014 75 @return True in case of success, false otherwise
joeverbout 0:ea44dc9ed014 76 */
joeverbout 0:ea44dc9ed014 77 bool operator ()(const std::vector<ImageFeatures> &features,
joeverbout 0:ea44dc9ed014 78 const std::vector<MatchesInfo> &pairwise_matches,
joeverbout 0:ea44dc9ed014 79 std::vector<CameraParams> &cameras)
joeverbout 0:ea44dc9ed014 80 { return estimate(features, pairwise_matches, cameras); }
joeverbout 0:ea44dc9ed014 81
joeverbout 0:ea44dc9ed014 82 protected:
joeverbout 0:ea44dc9ed014 83 /** @brief This method must implement camera parameters estimation logic in order to make the wrapper
joeverbout 0:ea44dc9ed014 84 detail::Estimator::operator()_ work.
joeverbout 0:ea44dc9ed014 85
joeverbout 0:ea44dc9ed014 86 @param features Features of images
joeverbout 0:ea44dc9ed014 87 @param pairwise_matches Pairwise matches of images
joeverbout 0:ea44dc9ed014 88 @param cameras Estimated camera parameters
joeverbout 0:ea44dc9ed014 89 @return True in case of success, false otherwise
joeverbout 0:ea44dc9ed014 90 */
joeverbout 0:ea44dc9ed014 91 virtual bool estimate(const std::vector<ImageFeatures> &features,
joeverbout 0:ea44dc9ed014 92 const std::vector<MatchesInfo> &pairwise_matches,
joeverbout 0:ea44dc9ed014 93 std::vector<CameraParams> &cameras) = 0;
joeverbout 0:ea44dc9ed014 94 };
joeverbout 0:ea44dc9ed014 95
joeverbout 0:ea44dc9ed014 96 /** @brief Homography based rotation estimator.
joeverbout 0:ea44dc9ed014 97 */
joeverbout 0:ea44dc9ed014 98 class CV_EXPORTS HomographyBasedEstimator : public Estimator
joeverbout 0:ea44dc9ed014 99 {
joeverbout 0:ea44dc9ed014 100 public:
joeverbout 0:ea44dc9ed014 101 HomographyBasedEstimator(bool is_focals_estimated = false)
joeverbout 0:ea44dc9ed014 102 : is_focals_estimated_(is_focals_estimated) {}
joeverbout 0:ea44dc9ed014 103
joeverbout 0:ea44dc9ed014 104 private:
joeverbout 0:ea44dc9ed014 105 virtual bool estimate(const std::vector<ImageFeatures> &features,
joeverbout 0:ea44dc9ed014 106 const std::vector<MatchesInfo> &pairwise_matches,
joeverbout 0:ea44dc9ed014 107 std::vector<CameraParams> &cameras);
joeverbout 0:ea44dc9ed014 108
joeverbout 0:ea44dc9ed014 109 bool is_focals_estimated_;
joeverbout 0:ea44dc9ed014 110 };
joeverbout 0:ea44dc9ed014 111
joeverbout 0:ea44dc9ed014 112 /** @brief Base class for all camera parameters refinement methods.
joeverbout 0:ea44dc9ed014 113 */
joeverbout 0:ea44dc9ed014 114 class CV_EXPORTS BundleAdjusterBase : public Estimator
joeverbout 0:ea44dc9ed014 115 {
joeverbout 0:ea44dc9ed014 116 public:
joeverbout 0:ea44dc9ed014 117 const Mat refinementMask() const { return refinement_mask_.clone(); }
joeverbout 0:ea44dc9ed014 118 void setRefinementMask(const Mat &mask)
joeverbout 0:ea44dc9ed014 119 {
joeverbout 0:ea44dc9ed014 120 CV_Assert(mask.type() == CV_8U && mask.size() == Size(3, 3));
joeverbout 0:ea44dc9ed014 121 refinement_mask_ = mask.clone();
joeverbout 0:ea44dc9ed014 122 }
joeverbout 0:ea44dc9ed014 123
joeverbout 0:ea44dc9ed014 124 double confThresh() const { return conf_thresh_; }
joeverbout 0:ea44dc9ed014 125 void setConfThresh(double conf_thresh) { conf_thresh_ = conf_thresh; }
joeverbout 0:ea44dc9ed014 126
joeverbout 0:ea44dc9ed014 127 TermCriteria termCriteria() { return term_criteria_; }
joeverbout 0:ea44dc9ed014 128 void setTermCriteria(const TermCriteria& term_criteria) { term_criteria_ = term_criteria; }
joeverbout 0:ea44dc9ed014 129
joeverbout 0:ea44dc9ed014 130 protected:
joeverbout 0:ea44dc9ed014 131 /** @brief Construct a bundle adjuster base instance.
joeverbout 0:ea44dc9ed014 132
joeverbout 0:ea44dc9ed014 133 @param num_params_per_cam Number of parameters per camera
joeverbout 0:ea44dc9ed014 134 @param num_errs_per_measurement Number of error terms (components) per match
joeverbout 0:ea44dc9ed014 135 */
joeverbout 0:ea44dc9ed014 136 BundleAdjusterBase(int num_params_per_cam, int num_errs_per_measurement)
joeverbout 0:ea44dc9ed014 137 : num_params_per_cam_(num_params_per_cam),
joeverbout 0:ea44dc9ed014 138 num_errs_per_measurement_(num_errs_per_measurement)
joeverbout 0:ea44dc9ed014 139 {
joeverbout 0:ea44dc9ed014 140 setRefinementMask(Mat::ones(3, 3, CV_8U));
joeverbout 0:ea44dc9ed014 141 setConfThresh(1.);
joeverbout 0:ea44dc9ed014 142 setTermCriteria(TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 1000, DBL_EPSILON));
joeverbout 0:ea44dc9ed014 143 }
joeverbout 0:ea44dc9ed014 144
joeverbout 0:ea44dc9ed014 145 // Runs bundle adjustment
joeverbout 0:ea44dc9ed014 146 virtual bool estimate(const std::vector<ImageFeatures> &features,
joeverbout 0:ea44dc9ed014 147 const std::vector<MatchesInfo> &pairwise_matches,
joeverbout 0:ea44dc9ed014 148 std::vector<CameraParams> &cameras);
joeverbout 0:ea44dc9ed014 149
joeverbout 0:ea44dc9ed014 150 /** @brief Sets initial camera parameter to refine.
joeverbout 0:ea44dc9ed014 151
joeverbout 0:ea44dc9ed014 152 @param cameras Camera parameters
joeverbout 0:ea44dc9ed014 153 */
joeverbout 0:ea44dc9ed014 154 virtual void setUpInitialCameraParams(const std::vector<CameraParams> &cameras) = 0;
joeverbout 0:ea44dc9ed014 155 /** @brief Gets the refined camera parameters.
joeverbout 0:ea44dc9ed014 156
joeverbout 0:ea44dc9ed014 157 @param cameras Refined camera parameters
joeverbout 0:ea44dc9ed014 158 */
joeverbout 0:ea44dc9ed014 159 virtual void obtainRefinedCameraParams(std::vector<CameraParams> &cameras) const = 0;
joeverbout 0:ea44dc9ed014 160 /** @brief Calculates error vector.
joeverbout 0:ea44dc9ed014 161
joeverbout 0:ea44dc9ed014 162 @param err Error column-vector of length total_num_matches \* num_errs_per_measurement
joeverbout 0:ea44dc9ed014 163 */
joeverbout 0:ea44dc9ed014 164 virtual void calcError(Mat &err) = 0;
joeverbout 0:ea44dc9ed014 165 /** @brief Calculates the cost function jacobian.
joeverbout 0:ea44dc9ed014 166
joeverbout 0:ea44dc9ed014 167 @param jac Jacobian matrix of dimensions
joeverbout 0:ea44dc9ed014 168 (total_num_matches \* num_errs_per_measurement) x (num_images \* num_params_per_cam)
joeverbout 0:ea44dc9ed014 169 */
joeverbout 0:ea44dc9ed014 170 virtual void calcJacobian(Mat &jac) = 0;
joeverbout 0:ea44dc9ed014 171
joeverbout 0:ea44dc9ed014 172 // 3x3 8U mask, where 0 means don't refine respective parameter, != 0 means refine
joeverbout 0:ea44dc9ed014 173 Mat refinement_mask_;
joeverbout 0:ea44dc9ed014 174
joeverbout 0:ea44dc9ed014 175 int num_images_;
joeverbout 0:ea44dc9ed014 176 int total_num_matches_;
joeverbout 0:ea44dc9ed014 177
joeverbout 0:ea44dc9ed014 178 int num_params_per_cam_;
joeverbout 0:ea44dc9ed014 179 int num_errs_per_measurement_;
joeverbout 0:ea44dc9ed014 180
joeverbout 0:ea44dc9ed014 181 const ImageFeatures *features_;
joeverbout 0:ea44dc9ed014 182 const MatchesInfo *pairwise_matches_;
joeverbout 0:ea44dc9ed014 183
joeverbout 0:ea44dc9ed014 184 // Threshold to filter out poorly matched image pairs
joeverbout 0:ea44dc9ed014 185 double conf_thresh_;
joeverbout 0:ea44dc9ed014 186
joeverbout 0:ea44dc9ed014 187 //Levenberg–Marquardt algorithm termination criteria
joeverbout 0:ea44dc9ed014 188 TermCriteria term_criteria_;
joeverbout 0:ea44dc9ed014 189
joeverbout 0:ea44dc9ed014 190 // Camera parameters matrix (CV_64F)
joeverbout 0:ea44dc9ed014 191 Mat cam_params_;
joeverbout 0:ea44dc9ed014 192
joeverbout 0:ea44dc9ed014 193 // Connected images pairs
joeverbout 0:ea44dc9ed014 194 std::vector<std::pair<int,int> > edges_;
joeverbout 0:ea44dc9ed014 195 };
joeverbout 0:ea44dc9ed014 196
joeverbout 0:ea44dc9ed014 197
joeverbout 0:ea44dc9ed014 198 /** @brief Implementation of the camera parameters refinement algorithm which minimizes sum of the reprojection
joeverbout 0:ea44dc9ed014 199 error squares
joeverbout 0:ea44dc9ed014 200
joeverbout 0:ea44dc9ed014 201 It can estimate focal length, aspect ratio, principal point.
joeverbout 0:ea44dc9ed014 202 You can affect only on them via the refinement mask.
joeverbout 0:ea44dc9ed014 203 */
joeverbout 0:ea44dc9ed014 204 class CV_EXPORTS BundleAdjusterReproj : public BundleAdjusterBase
joeverbout 0:ea44dc9ed014 205 {
joeverbout 0:ea44dc9ed014 206 public:
joeverbout 0:ea44dc9ed014 207 BundleAdjusterReproj() : BundleAdjusterBase(7, 2) {}
joeverbout 0:ea44dc9ed014 208
joeverbout 0:ea44dc9ed014 209 private:
joeverbout 0:ea44dc9ed014 210 void setUpInitialCameraParams(const std::vector<CameraParams> &cameras);
joeverbout 0:ea44dc9ed014 211 void obtainRefinedCameraParams(std::vector<CameraParams> &cameras) const;
joeverbout 0:ea44dc9ed014 212 void calcError(Mat &err);
joeverbout 0:ea44dc9ed014 213 void calcJacobian(Mat &jac);
joeverbout 0:ea44dc9ed014 214
joeverbout 0:ea44dc9ed014 215 Mat err1_, err2_;
joeverbout 0:ea44dc9ed014 216 };
joeverbout 0:ea44dc9ed014 217
joeverbout 0:ea44dc9ed014 218
joeverbout 0:ea44dc9ed014 219 /** @brief Implementation of the camera parameters refinement algorithm which minimizes sum of the distances
joeverbout 0:ea44dc9ed014 220 between the rays passing through the camera center and a feature. :
joeverbout 0:ea44dc9ed014 221
joeverbout 0:ea44dc9ed014 222 It can estimate focal length. It ignores the refinement mask for now.
joeverbout 0:ea44dc9ed014 223 */
joeverbout 0:ea44dc9ed014 224 class CV_EXPORTS BundleAdjusterRay : public BundleAdjusterBase
joeverbout 0:ea44dc9ed014 225 {
joeverbout 0:ea44dc9ed014 226 public:
joeverbout 0:ea44dc9ed014 227 BundleAdjusterRay() : BundleAdjusterBase(4, 3) {}
joeverbout 0:ea44dc9ed014 228
joeverbout 0:ea44dc9ed014 229 private:
joeverbout 0:ea44dc9ed014 230 void setUpInitialCameraParams(const std::vector<CameraParams> &cameras);
joeverbout 0:ea44dc9ed014 231 void obtainRefinedCameraParams(std::vector<CameraParams> &cameras) const;
joeverbout 0:ea44dc9ed014 232 void calcError(Mat &err);
joeverbout 0:ea44dc9ed014 233 void calcJacobian(Mat &jac);
joeverbout 0:ea44dc9ed014 234
joeverbout 0:ea44dc9ed014 235 Mat err1_, err2_;
joeverbout 0:ea44dc9ed014 236 };
joeverbout 0:ea44dc9ed014 237
joeverbout 0:ea44dc9ed014 238
joeverbout 0:ea44dc9ed014 239 enum WaveCorrectKind
joeverbout 0:ea44dc9ed014 240 {
joeverbout 0:ea44dc9ed014 241 WAVE_CORRECT_HORIZ,
joeverbout 0:ea44dc9ed014 242 WAVE_CORRECT_VERT
joeverbout 0:ea44dc9ed014 243 };
joeverbout 0:ea44dc9ed014 244
joeverbout 0:ea44dc9ed014 245 /** @brief Tries to make panorama more horizontal (or vertical).
joeverbout 0:ea44dc9ed014 246
joeverbout 0:ea44dc9ed014 247 @param rmats Camera rotation matrices.
joeverbout 0:ea44dc9ed014 248 @param kind Correction kind, see detail::WaveCorrectKind.
joeverbout 0:ea44dc9ed014 249 */
joeverbout 0:ea44dc9ed014 250 void CV_EXPORTS waveCorrect(std::vector<Mat> &rmats, WaveCorrectKind kind);
joeverbout 0:ea44dc9ed014 251
joeverbout 0:ea44dc9ed014 252
joeverbout 0:ea44dc9ed014 253 //////////////////////////////////////////////////////////////////////////////
joeverbout 0:ea44dc9ed014 254 // Auxiliary functions
joeverbout 0:ea44dc9ed014 255
joeverbout 0:ea44dc9ed014 256 // Returns matches graph representation in DOT language
joeverbout 0:ea44dc9ed014 257 String CV_EXPORTS matchesGraphAsString(std::vector<String> &pathes, std::vector<MatchesInfo> &pairwise_matches,
joeverbout 0:ea44dc9ed014 258 float conf_threshold);
joeverbout 0:ea44dc9ed014 259
joeverbout 0:ea44dc9ed014 260 std::vector<int> CV_EXPORTS leaveBiggestComponent(
joeverbout 0:ea44dc9ed014 261 std::vector<ImageFeatures> &features,
joeverbout 0:ea44dc9ed014 262 std::vector<MatchesInfo> &pairwise_matches,
joeverbout 0:ea44dc9ed014 263 float conf_threshold);
joeverbout 0:ea44dc9ed014 264
joeverbout 0:ea44dc9ed014 265 void CV_EXPORTS findMaxSpanningTree(
joeverbout 0:ea44dc9ed014 266 int num_images, const std::vector<MatchesInfo> &pairwise_matches,
joeverbout 0:ea44dc9ed014 267 Graph &span_tree, std::vector<int> &centers);
joeverbout 0:ea44dc9ed014 268
joeverbout 0:ea44dc9ed014 269 //! @} stitching_rotation
joeverbout 0:ea44dc9ed014 270
joeverbout 0:ea44dc9ed014 271 } // namespace detail
joeverbout 0:ea44dc9ed014 272 } // namespace cv
joeverbout 0:ea44dc9ed014 273
joeverbout 0:ea44dc9ed014 274 #endif // __OPENCV_STITCHING_MOTION_ESTIMATORS_HPP__
joeverbout 0:ea44dc9ed014 275