mirror of
https://github.com/opencv/opencv.git
synced 2024-12-11 14:39:11 +08:00
c17afe0fab
[GSOC] New camera model for stitching pipeline * implement estimateAffine2D estimates affine transformation using robust RANSAC method. * uses RANSAC framework in calib3d * includes accuracy test * uses SVD decomposition for solving 3 point equation * implement estimateAffinePartial2D estimates limited affine transformation * includes accuracy test * stitching: add affine matcher initial version of matcher that estimates affine transformation * stitching: added affine transform estimator initial version of estimator that simply chain transformations in homogeneous coordinates * calib3d: rename estimateAffine3D test test Calib3d_EstimateAffineTransform rename to Calib3d_EstimateAffine3D. This is more descriptive and prevents confusion with estimateAffine2D tests. * added perf test for estimateAffine functions tests both estimateAffine2D and estimateAffinePartial2D * calib3d: compare error in square in estimateAffine2D * incorporates fix from #6768 * rerun affine estimation on inliers * stitching: new API for parallel feature finding due to ABI breakage new functionality is added to `FeaturesFinder2`, `SurfFeaturesFinder2` and `OrbFeaturesFinder2` * stitching: add tests for parallel feature find API * perf test (about linear speed up) * accuracy test compares results with serial version * stitching: use dynamic_cast to overcome ABI issues adding parallel API to FeaturesFinder breaks ABI. This commit uses dynamic_cast and hardcodes thread-safe finders to avoid breaking ABI. This should be replaced by proper method similar to FeaturesMatcher on next ABI break. * use estimateAffinePartial2D in AffineBestOf2NearestMatcher * add constructor to AffineBestOf2NearestMatcher * allows to choose between full affine transform and partial affine transform. Other params are the as for BestOf2NearestMatcher * added protected field * samples: stitching_detailed support affine estimator and matcher * added new flags to choose matcher and estimator * stitching: rework affine matcher represent transformation in homogeneous coordinates affine matcher: remove duplicite code rework flow to get rid of duplicite code affine matcher: do not center points to (0, 0) it is not needed for affine model. it should not affect estimation in any way. affine matcher: remove unneeded cv namespacing * stitching: add stub bundle adjuster * adds stub bundle adjuster that does nothing * can be used in place of standard bundle adjusters to omit bundle adjusting step * samples: stitching detailed, support no budle adjust * uses new NoBundleAdjuster * added affine warper * uses R to get whole affine transformation and propagates rotation and translation to plane warper * add affine warper factory class * affine warper: compensate transformation * samples: stitching_detailed add support for affine warper * add Stitcher::create method this method follows similar constructor methods and returns smart pointer. This allows constructing Stitcher according to OpenCV guidelines. * supports multiple stitcher configurations (PANORAMA and SCANS) for convenient setup * returns cv::Ptr * stitcher: dynamicaly determine correct estimator we need to use affine estimator for affine matcher * preserves ABI (but add hints for ABI 4) * uses dynamic_cast hack to inject correct estimator * sample stitching: add support for multiple modes shows how to use different configurations of stitcher easily (panorama stitching and scans affine model) * stitcher: find features in parallel use new FeatureFinder API to find features in parallel. Parallelized using TBB. * stitching: disable parallel feature finding for OCL it does not bring much speedup to run features finder in parallel when OpenCL is enabled, because finder needs to wait for OCL device. Also, currently ORB is not thread-safe when OCL is enabled. * stitching: move matcher tests move matchers tests perf_stich.cpp -> perf_matchers.cpp * stitching: add affine stiching integration test test basic affine stitching (SCANS mode of stitcher) with images that have only translation between them * enable surf for stitching tests stitching.b12 test was failing with surf investigated the issue, surf is producing good result. Transformation is only slightly different from ORB, so that resulting pano does not exactly match ORB's result. That caused sanity check to fail. * added size checks similar to other tests * sanity check will be applied only for ORB * stitching: fix wrong estimator choice if case was exactly wrong, estimators were chosen wrong added logging for estimated transformation * enable surf for matchers stitching tests * enable SURF * rework sanity checking. Check estimated transform instead of matches. Est. transform should be more stable and comparable between SURF and ORB. * remove regression checking for VectorFeatures tests. It has a lot if data andtest is the same as previous except it test different vector size for performance, so sanity checking does not add any value here. Added basic sanity asserts instead. * stitching tests: allow relative error for transform * allows .01 relative error for estimated homography sanity check in stitching matchers tests * fix VS warning stitching tests: increase relative error increase relative error to make it pass on all platforms (results are still good). stitching test: allow bigger relative error transformation can differ in small values (with small absolute difference, but large relative difference). transformation output still looks usable for all platforms. This difference affects only mac and windows, linux passes fine with small difference. * stitching: add tests for affine matcher uses s1, s2 images. added also new sanity data. * stitching tests: use different data for matchers tests this data should yeild more stable transformation (it has much more matches, especially for surf). Sanity data regenerated. * stitching test: rework tests for matchers * separated rotation and translations as they are different by scale. * use appropriate absolute error for them separately. (relative error does not work for values near zero.) * stitching: fix affine warper compensation calculation of rotation and translation extracted for plane warper was wrong * stitching test: enable surf for opencl integration tests * enable SURF with correct guard (HAVE_OPENCV_XFEATURES2D) * add OPENCL guard and correct namespace as usual for opencl tests * stitching: add ocl accuracy test for affine warper test consistent results with ocl on and off * stitching: add affine warper ocl perf test add affine warper to existing warper perf tests. Added new sanity data. * stitching: do not overwrite inliers in affine matcher * estimation is run second time on inliers only, inliers produces in second run will not be therefore correct for all matches * calib3d: add Levenberg–Marquardt refining to estimateAffine2D* functions this adds affine Levenberg–Marquardt refining to estimateAffine2D functions similar to what is done in findHomography. implements Levenberg–Marquardt refinig for both full affine and partial affine transformations. * stitching: remove reestimation step in affine matcher reestimation step is not needed. estimateAffine2D* functions are running their own reestimation on inliers using the Levenberg-Marquardt algorithm, which is better than simply rerunning RANSAC on inliers. * implement partial affine bundle adjuster bundle adjuster that expect affine transform with 4DOF. Refines parameters for all cameras together. stitching: fix bug in BundleAdjusterAffinePartial * use the invers properly * use static buffer for invers to speed it up * samples: add affine bundle adjuster option to stitching_detailed * add support for using affine bundle adjuster with 4DOF * improve logging of initial intristics * sttiching: add affine bundle adjuster test * fix build warnings * stitching: increase limit on sanity check prevents spurious test failures on mac. values are still pretty fine. * stitching: set affine bundle adjuster for SCANS mode * fix bug with AffineBestOf2NearestMatcher (we want to select affine partial mode) * select right bundle adjuster * stitching: increase error bound for matcher tests * this prevents failure on mac. tranformation is still ok. * stitching: implement affine bundle adjuster * implements affine bundle adjuster that is using full affine transform * existing test case modified to test both affinePartial an full affine bundle adjuster * add stitching tutorial * show basic usage of stitching api (Stitcher class) * stitching: add more integration test for affine stitching * added new datasets to existing testcase * removed unused include * calib3d: move `haveCollinearPoints` to common header * added comment to make that this also checks too close points * calib3d: redone checkSubset for estimateAffine* callback * use common function to check collinearity * this also ensures that point will not be too close to each other * calib3d: change estimateAffine* functions API * more similar to `findHomography`, `findFundamentalMat`, `findEssentialMat` and similar * follows standard recommended semantic INPUTS, OUTPUTS, FLAGS * allows to disable refining * supported LMEDS robust method (tests yet to come) along with RANSAC * extended docs with some tips * calib3d: rewrite estimateAffine2D test * rewrite in googletest style * parametrize to test both robust methods (RANSAC and LMEDS) * get rid of boilerplate * calib3d: rework estimateAffinePartial2D test * rework in googletest style * add testing for LMEDS * calib3d: rework estimateAffine*2D perf test * test for LMEDS speed * test with/without Levenberg-Marquart * remove sanity checking (this is covered by accuracy tests) * calib3d: improve estimateAffine*2D tests * test transformations in loop * improves test by testing more potential transformations * calib3d: rewrite kernels for estimateAffine*2D functions * use analytical solution instead of SVD * this version is faster especially for smaller amount of points * calib3d: tune up perf of estimateAffine*2D functions * avoid copying inliers * avoid converting input points if not necessary * check only `from` point for collinearity, as `to` does not affect stability of transform * tutorials: add commands examples to stitching tutorials * add some examples how to run stitcher sample code * mention stitching_detailed.cpp * calib3d: change computeError for estimateAffine*2D * do error computing in floats instead of doubles this have required precision + we were storing the result in float anyway. This make code faster and allows auto-vectorization by smart compilers. * documentation: mention estimateAffine*2D function * refer to new functions on appropriate places * prefer estimateAffine*2D over estimateRigidTransform * stitching: add camera models documentations * mention camera models in module documentation to give user a better overview and reduce confusion
321 lines
13 KiB
C++
321 lines
13 KiB
C++
/*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*/
|
|
|
|
#ifndef OPENCV_STITCHING_STITCHER_HPP
|
|
#define OPENCV_STITCHING_STITCHER_HPP
|
|
|
|
#include "opencv2/core.hpp"
|
|
#include "opencv2/features2d.hpp"
|
|
#include "opencv2/stitching/warpers.hpp"
|
|
#include "opencv2/stitching/detail/matchers.hpp"
|
|
#include "opencv2/stitching/detail/motion_estimators.hpp"
|
|
#include "opencv2/stitching/detail/exposure_compensate.hpp"
|
|
#include "opencv2/stitching/detail/seam_finders.hpp"
|
|
#include "opencv2/stitching/detail/blenders.hpp"
|
|
#include "opencv2/stitching/detail/camera.hpp"
|
|
|
|
|
|
#if defined(Status)
|
|
# warning Detected X11 'Status' macro definition, it can cause build conflicts. Please, include this header before any X11 headers.
|
|
#endif
|
|
|
|
|
|
/**
|
|
@defgroup stitching Images stitching
|
|
|
|
This figure illustrates the stitching module pipeline implemented in the Stitcher class. Using that
|
|
class it's possible to configure/remove some steps, i.e. adjust the stitching pipeline according to
|
|
the particular needs. All building blocks from the pipeline are available in the detail namespace,
|
|
one can combine and use them separately.
|
|
|
|
The implemented stitching pipeline is very similar to the one proposed in @cite BL07 .
|
|
|
|
![stitching pipeline](StitchingPipeline.jpg)
|
|
|
|
Camera models
|
|
-------------
|
|
|
|
There are currently 2 camera models implemented in stitching pipeline.
|
|
|
|
- _Homography model_ expecting perspective transformations between images
|
|
implemented in @ref cv::detail::BestOf2NearestMatcher cv::detail::HomographyBasedEstimator
|
|
cv::detail::BundleAdjusterReproj cv::detail::BundleAdjusterRay
|
|
- _Affine model_ expecting affine transformation with 6 DOF or 4 DOF implemented in
|
|
@ref cv::detail::AffineBestOf2NearestMatcher cv::detail::AffineBasedEstimator
|
|
cv::detail::BundleAdjusterAffine cv::detail::BundleAdjusterAffinePartial cv::AffineWarper
|
|
|
|
Homography model is useful for creating photo panoramas captured by camera,
|
|
while affine-based model can be used to stitch scans and object captured by
|
|
specialized devices. Use @ref cv::Stitcher::create to get preconfigured pipeline for one
|
|
of those models.
|
|
|
|
@note
|
|
Certain detailed settings of @ref cv::Stitcher might not make sense. Especially
|
|
you should not mix classes implementing affine model and classes implementing
|
|
Homography model, as they work with different transformations.
|
|
|
|
@{
|
|
@defgroup stitching_match Features Finding and Images Matching
|
|
@defgroup stitching_rotation Rotation Estimation
|
|
@defgroup stitching_autocalib Autocalibration
|
|
@defgroup stitching_warp Images Warping
|
|
@defgroup stitching_seam Seam Estimation
|
|
@defgroup stitching_exposure Exposure Compensation
|
|
@defgroup stitching_blend Image Blenders
|
|
@}
|
|
*/
|
|
|
|
namespace cv {
|
|
|
|
//! @addtogroup stitching
|
|
//! @{
|
|
|
|
/** @brief High level image stitcher.
|
|
|
|
It's possible to use this class without being aware of the entire stitching pipeline. However, to
|
|
be able to achieve higher stitching stability and quality of the final images at least being
|
|
familiar with the theory is recommended.
|
|
|
|
@note
|
|
- A basic example on image stitching can be found at
|
|
opencv_source_code/samples/cpp/stitching.cpp
|
|
- A detailed example on image stitching can be found at
|
|
opencv_source_code/samples/cpp/stitching_detailed.cpp
|
|
*/
|
|
class CV_EXPORTS_W Stitcher
|
|
{
|
|
public:
|
|
enum { ORIG_RESOL = -1 };
|
|
enum Status
|
|
{
|
|
OK = 0,
|
|
ERR_NEED_MORE_IMGS = 1,
|
|
ERR_HOMOGRAPHY_EST_FAIL = 2,
|
|
ERR_CAMERA_PARAMS_ADJUST_FAIL = 3
|
|
};
|
|
enum Mode
|
|
{
|
|
/** Mode for creating photo panoramas. Expects images under perspective
|
|
transformation and projects resulting pano to sphere.
|
|
|
|
@sa detail::BestOf2NearestMatcher SphericalWarper
|
|
*/
|
|
PANORAMA = 0,
|
|
/** Mode for composing scans. Expects images under affine transformation does
|
|
not compensate exposure by default.
|
|
|
|
@sa detail::AffineBestOf2NearestMatcher AffineWarper
|
|
*/
|
|
SCANS = 1,
|
|
|
|
};
|
|
|
|
// Stitcher() {}
|
|
/** @brief Creates a stitcher with the default parameters.
|
|
|
|
@param try_use_gpu Flag indicating whether GPU should be used whenever it's possible.
|
|
@return Stitcher class instance.
|
|
*/
|
|
static Stitcher createDefault(bool try_use_gpu = false);
|
|
/** @brief Creates a Stitcher configured in one of the stitching modes.
|
|
|
|
@param mode Scenario for stitcher operation. This is usually determined by source of images
|
|
to stitch and their transformation. Default parameters will be chosen for operation in given
|
|
scenario.
|
|
@param try_use_gpu Flag indicating whether GPU should be used whenever it's possible.
|
|
@return Stitcher class instance.
|
|
*/
|
|
static Ptr<Stitcher> create(Mode mode = PANORAMA, bool try_use_gpu = false);
|
|
|
|
CV_WRAP double registrationResol() const { return registr_resol_; }
|
|
CV_WRAP void setRegistrationResol(double resol_mpx) { registr_resol_ = resol_mpx; }
|
|
|
|
CV_WRAP double seamEstimationResol() const { return seam_est_resol_; }
|
|
CV_WRAP void setSeamEstimationResol(double resol_mpx) { seam_est_resol_ = resol_mpx; }
|
|
|
|
CV_WRAP double compositingResol() const { return compose_resol_; }
|
|
CV_WRAP void setCompositingResol(double resol_mpx) { compose_resol_ = resol_mpx; }
|
|
|
|
CV_WRAP double panoConfidenceThresh() const { return conf_thresh_; }
|
|
CV_WRAP void setPanoConfidenceThresh(double conf_thresh) { conf_thresh_ = conf_thresh; }
|
|
|
|
CV_WRAP bool waveCorrection() const { return do_wave_correct_; }
|
|
CV_WRAP void setWaveCorrection(bool flag) { do_wave_correct_ = flag; }
|
|
|
|
detail::WaveCorrectKind waveCorrectKind() const { return wave_correct_kind_; }
|
|
void setWaveCorrectKind(detail::WaveCorrectKind kind) { wave_correct_kind_ = kind; }
|
|
|
|
Ptr<detail::FeaturesFinder> featuresFinder() { return features_finder_; }
|
|
const Ptr<detail::FeaturesFinder> featuresFinder() const { return features_finder_; }
|
|
void setFeaturesFinder(Ptr<detail::FeaturesFinder> features_finder)
|
|
{ features_finder_ = features_finder; }
|
|
|
|
Ptr<detail::FeaturesMatcher> featuresMatcher() { return features_matcher_; }
|
|
const Ptr<detail::FeaturesMatcher> featuresMatcher() const { return features_matcher_; }
|
|
void setFeaturesMatcher(Ptr<detail::FeaturesMatcher> features_matcher)
|
|
{ features_matcher_ = features_matcher; }
|
|
|
|
const cv::UMat& matchingMask() const { return matching_mask_; }
|
|
void setMatchingMask(const cv::UMat &mask)
|
|
{
|
|
CV_Assert(mask.type() == CV_8U && mask.cols == mask.rows);
|
|
matching_mask_ = mask.clone();
|
|
}
|
|
|
|
Ptr<detail::BundleAdjusterBase> bundleAdjuster() { return bundle_adjuster_; }
|
|
const Ptr<detail::BundleAdjusterBase> bundleAdjuster() const { return bundle_adjuster_; }
|
|
void setBundleAdjuster(Ptr<detail::BundleAdjusterBase> bundle_adjuster)
|
|
{ bundle_adjuster_ = bundle_adjuster; }
|
|
|
|
/* TODO OpenCV ABI 4.x
|
|
Ptr<detail::Estimator> estimator() { return estimator_; }
|
|
const Ptr<detail::Estimator> estimator() const { return estimator_; }
|
|
void setEstimator(Ptr<detail::Estimator> estimator)
|
|
{ estimator_ = estimator; }
|
|
*/
|
|
|
|
Ptr<WarperCreator> warper() { return warper_; }
|
|
const Ptr<WarperCreator> warper() const { return warper_; }
|
|
void setWarper(Ptr<WarperCreator> creator) { warper_ = creator; }
|
|
|
|
Ptr<detail::ExposureCompensator> exposureCompensator() { return exposure_comp_; }
|
|
const Ptr<detail::ExposureCompensator> exposureCompensator() const { return exposure_comp_; }
|
|
void setExposureCompensator(Ptr<detail::ExposureCompensator> exposure_comp)
|
|
{ exposure_comp_ = exposure_comp; }
|
|
|
|
Ptr<detail::SeamFinder> seamFinder() { return seam_finder_; }
|
|
const Ptr<detail::SeamFinder> seamFinder() const { return seam_finder_; }
|
|
void setSeamFinder(Ptr<detail::SeamFinder> seam_finder) { seam_finder_ = seam_finder; }
|
|
|
|
Ptr<detail::Blender> blender() { return blender_; }
|
|
const Ptr<detail::Blender> blender() const { return blender_; }
|
|
void setBlender(Ptr<detail::Blender> b) { blender_ = b; }
|
|
|
|
/** @overload */
|
|
CV_WRAP Status estimateTransform(InputArrayOfArrays images);
|
|
/** @brief These functions try to match the given images and to estimate rotations of each camera.
|
|
|
|
@note Use the functions only if you're aware of the stitching pipeline, otherwise use
|
|
Stitcher::stitch.
|
|
|
|
@param images Input images.
|
|
@param rois Region of interest rectangles.
|
|
@return Status code.
|
|
*/
|
|
Status estimateTransform(InputArrayOfArrays images, const std::vector<std::vector<Rect> > &rois);
|
|
|
|
/** @overload */
|
|
CV_WRAP Status composePanorama(OutputArray pano);
|
|
/** @brief These functions try to compose the given images (or images stored internally from the other function
|
|
calls) into the final pano under the assumption that the image transformations were estimated
|
|
before.
|
|
|
|
@note Use the functions only if you're aware of the stitching pipeline, otherwise use
|
|
Stitcher::stitch.
|
|
|
|
@param images Input images.
|
|
@param pano Final pano.
|
|
@return Status code.
|
|
*/
|
|
Status composePanorama(InputArrayOfArrays images, OutputArray pano);
|
|
|
|
/** @overload */
|
|
CV_WRAP Status stitch(InputArrayOfArrays images, OutputArray pano);
|
|
/** @brief These functions try to stitch the given images.
|
|
|
|
@param images Input images.
|
|
@param rois Region of interest rectangles.
|
|
@param pano Final pano.
|
|
@return Status code.
|
|
*/
|
|
Status stitch(InputArrayOfArrays images, const std::vector<std::vector<Rect> > &rois, OutputArray pano);
|
|
|
|
std::vector<int> component() const { return indices_; }
|
|
std::vector<detail::CameraParams> cameras() const { return cameras_; }
|
|
CV_WRAP double workScale() const { return work_scale_; }
|
|
|
|
private:
|
|
//Stitcher() {}
|
|
|
|
Status matchImages();
|
|
Status estimateCameraParams();
|
|
|
|
double registr_resol_;
|
|
double seam_est_resol_;
|
|
double compose_resol_;
|
|
double conf_thresh_;
|
|
Ptr<detail::FeaturesFinder> features_finder_;
|
|
Ptr<detail::FeaturesMatcher> features_matcher_;
|
|
cv::UMat matching_mask_;
|
|
Ptr<detail::BundleAdjusterBase> bundle_adjuster_;
|
|
/* TODO OpenCV ABI 4.x
|
|
Ptr<detail::Estimator> estimator_;
|
|
*/
|
|
bool do_wave_correct_;
|
|
detail::WaveCorrectKind wave_correct_kind_;
|
|
Ptr<WarperCreator> warper_;
|
|
Ptr<detail::ExposureCompensator> exposure_comp_;
|
|
Ptr<detail::SeamFinder> seam_finder_;
|
|
Ptr<detail::Blender> blender_;
|
|
|
|
std::vector<cv::UMat> imgs_;
|
|
std::vector<std::vector<cv::Rect> > rois_;
|
|
std::vector<cv::Size> full_img_sizes_;
|
|
std::vector<detail::ImageFeatures> features_;
|
|
std::vector<detail::MatchesInfo> pairwise_matches_;
|
|
std::vector<cv::UMat> seam_est_imgs_;
|
|
std::vector<int> indices_;
|
|
std::vector<detail::CameraParams> cameras_;
|
|
double work_scale_;
|
|
double seam_scale_;
|
|
double seam_work_aspect_;
|
|
double warped_image_scale_;
|
|
};
|
|
|
|
CV_EXPORTS_W Ptr<Stitcher> createStitcher(bool try_use_gpu = false);
|
|
|
|
//! @} stitching
|
|
|
|
} // namespace cv
|
|
|
|
#endif // OPENCV_STITCHING_STITCHER_HPP
|