Added local outlier rejector. Added rigid motion estimator. Refactored videostab module.
This commit is contained in:
@@ -48,8 +48,10 @@
|
||||
#include <fstream>
|
||||
#include "opencv2/core/core.hpp"
|
||||
#include "opencv2/features2d/features2d.hpp"
|
||||
#include "opencv2/videostab/optical_flow.hpp"
|
||||
#include "opencv2/opencv_modules.hpp"
|
||||
#include "opencv2/videostab/optical_flow.hpp"
|
||||
#include "opencv2/videostab/motion_core.hpp"
|
||||
#include "opencv2/videostab/outlier_rejection.hpp"
|
||||
|
||||
#if HAVE_OPENCV_GPU
|
||||
#include "opencv2/gpu/gpu.hpp"
|
||||
@@ -60,44 +62,9 @@ namespace cv
|
||||
namespace videostab
|
||||
{
|
||||
|
||||
enum MotionModel
|
||||
{
|
||||
MM_TRANSLATION = 0,
|
||||
MM_TRANSLATION_AND_SCALE = 1,
|
||||
MM_SIMILARITY = 2,
|
||||
MM_AFFINE = 3,
|
||||
MM_HOMOGRAPHY = 4,
|
||||
MM_UNKNOWN = 5
|
||||
};
|
||||
|
||||
CV_EXPORTS Mat estimateGlobalMotionLeastSquares(
|
||||
int npoints, Point2f *points0, Point2f *points1, int model = MM_AFFINE, float *rmse = 0);
|
||||
|
||||
struct CV_EXPORTS RansacParams
|
||||
{
|
||||
int size; // subset size
|
||||
float thresh; // max error to classify as inlier
|
||||
float eps; // max outliers ratio
|
||||
float prob; // probability of success
|
||||
|
||||
RansacParams() : size(0), thresh(0), eps(0), prob(0) {}
|
||||
RansacParams(int size, float thresh, float eps, float prob)
|
||||
: size(size), thresh(thresh), eps(eps), prob(prob) {}
|
||||
|
||||
static RansacParams default2dMotion(MotionModel model)
|
||||
{
|
||||
CV_Assert(model < MM_UNKNOWN);
|
||||
if (model == MM_TRANSLATION)
|
||||
return RansacParams(1, 0.5f, 0.5f, 0.99f);
|
||||
if (model == MM_TRANSLATION_AND_SCALE)
|
||||
return RansacParams(2, 0.5f, 0.5f, 0.99f);
|
||||
if (model == MM_SIMILARITY)
|
||||
return RansacParams(2, 0.5f, 0.5f, 0.99f);
|
||||
if (model == MM_AFFINE)
|
||||
return RansacParams(3, 0.5f, 0.5f, 0.99f);
|
||||
return RansacParams(4, 0.5f, 0.5f, 0.99f);
|
||||
}
|
||||
};
|
||||
|
||||
CV_EXPORTS Mat estimateGlobalMotionRobust(
|
||||
const std::vector<Point2f> &points0, const std::vector<Point2f> &points1,
|
||||
@@ -106,8 +73,7 @@ CV_EXPORTS Mat estimateGlobalMotionRobust(
|
||||
|
||||
class CV_EXPORTS GlobalMotionEstimatorBase
|
||||
{
|
||||
public:
|
||||
GlobalMotionEstimatorBase() : motionModel_(MM_UNKNOWN) {}
|
||||
public:
|
||||
virtual ~GlobalMotionEstimatorBase() {}
|
||||
|
||||
virtual void setMotionModel(MotionModel val) { motionModel_ = val; }
|
||||
@@ -116,6 +82,8 @@ public:
|
||||
virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0) = 0;
|
||||
|
||||
protected:
|
||||
GlobalMotionEstimatorBase(MotionModel model) { setMotionModel(model); }
|
||||
|
||||
MotionModel motionModel_;
|
||||
};
|
||||
|
||||
@@ -140,7 +108,27 @@ private:
|
||||
Ptr<GlobalMotionEstimatorBase> estimator_;
|
||||
};
|
||||
|
||||
class CV_EXPORTS PyrLkRobustMotionEstimator : public GlobalMotionEstimatorBase
|
||||
class CV_EXPORTS PyrLkRobustMotionEstimatorBase : public GlobalMotionEstimatorBase
|
||||
{
|
||||
public:
|
||||
virtual void setRansacParams(const RansacParams &val) { ransacParams_ = val; }
|
||||
virtual RansacParams ransacParams() const { return ransacParams_; }
|
||||
|
||||
virtual void setOutlierRejector(Ptr<IOutlierRejector> val) { outlierRejector_ = val; }
|
||||
virtual Ptr<IOutlierRejector> outlierRejector() const { return outlierRejector_; }
|
||||
|
||||
virtual void setMinInlierRatio(float val) { minInlierRatio_ = val; }
|
||||
virtual float minInlierRatio() const { return minInlierRatio_; }
|
||||
|
||||
protected:
|
||||
PyrLkRobustMotionEstimatorBase(MotionModel model);
|
||||
|
||||
RansacParams ransacParams_;
|
||||
Ptr<IOutlierRejector> outlierRejector_;
|
||||
float minInlierRatio_;
|
||||
};
|
||||
|
||||
class CV_EXPORTS PyrLkRobustMotionEstimator : public PyrLkRobustMotionEstimatorBase
|
||||
{
|
||||
public:
|
||||
PyrLkRobustMotionEstimator(MotionModel model = MM_AFFINE);
|
||||
@@ -151,12 +139,6 @@ public:
|
||||
void setOptFlowEstimator(Ptr<ISparseOptFlowEstimator> val) { optFlowEstimator_ = val; }
|
||||
Ptr<ISparseOptFlowEstimator> optFlowEstimator() const { return optFlowEstimator_; }
|
||||
|
||||
void setRansacParams(const RansacParams &val) { ransacParams_ = val; }
|
||||
RansacParams ransacParams() const { return ransacParams_; }
|
||||
|
||||
void setMinInlierRatio(float val) { minInlierRatio_ = val; }
|
||||
float minInlierRatio() const { return minInlierRatio_; }
|
||||
|
||||
void setGridSize(Size val) { gridSize_ = val; }
|
||||
Size gridSize() const { return gridSize_; }
|
||||
|
||||
@@ -165,8 +147,6 @@ public:
|
||||
private:
|
||||
Ptr<FeatureDetector> detector_;
|
||||
Ptr<ISparseOptFlowEstimator> optFlowEstimator_;
|
||||
RansacParams ransacParams_;
|
||||
float minInlierRatio_;
|
||||
Size gridSize_;
|
||||
|
||||
std::vector<uchar> status_;
|
||||
@@ -176,30 +156,25 @@ private:
|
||||
};
|
||||
|
||||
#if HAVE_OPENCV_GPU
|
||||
class CV_EXPORTS PyrLkRobustMotionEstimatorGpu : public GlobalMotionEstimatorBase
|
||||
class CV_EXPORTS PyrLkRobustMotionEstimatorGpu : public PyrLkRobustMotionEstimatorBase
|
||||
{
|
||||
public:
|
||||
PyrLkRobustMotionEstimatorGpu(MotionModel model = MM_AFFINE);
|
||||
|
||||
void setRansacParams(const RansacParams &val) { ransacParams_ = val; }
|
||||
RansacParams ransacParams() const { return ransacParams_; }
|
||||
|
||||
void setMinInlierRatio(float val) { minInlierRatio_ = val; }
|
||||
float minInlierRatio() const { return minInlierRatio_; }
|
||||
|
||||
virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0);
|
||||
Mat estimate(const gpu::GpuMat &frame0, const gpu::GpuMat &frame1, bool *ok = 0);
|
||||
|
||||
private:
|
||||
gpu::GoodFeaturesToTrackDetector_GPU detector_;
|
||||
SparsePyrLkOptFlowEstimatorGpu optFlowEstimator_;
|
||||
RansacParams ransacParams_;
|
||||
float minInlierRatio_;
|
||||
|
||||
gpu::GpuMat frame0_, grayFrame0_, frame1_;
|
||||
gpu::GpuMat pointsPrev_, points_;
|
||||
Mat hostPointsPrev_, hostPoints_;
|
||||
gpu::GpuMat status_;
|
||||
|
||||
Mat hostPointsPrev_, hostPoints_;
|
||||
std::vector<Point2f> hostPointsPrevGood_, hostPointsGood_;
|
||||
std::vector<uchar> rejectionStatus_;
|
||||
};
|
||||
#endif
|
||||
|
||||
|
103
modules/videostab/include/opencv2/videostab/motion_core.hpp
Normal file
103
modules/videostab/include/opencv2/videostab/motion_core.hpp
Normal file
@@ -0,0 +1,103 @@
|
||||
/*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-2011, 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_VIDEOSTAB_MOTION_CORE_HPP__
|
||||
#define __OPENCV_VIDEOSTAB_MOTION_CORE_HPP__
|
||||
|
||||
#include <cmath>
|
||||
#include "opencv2/core/core.hpp"
|
||||
|
||||
namespace cv
|
||||
{
|
||||
namespace videostab
|
||||
{
|
||||
|
||||
enum MotionModel
|
||||
{
|
||||
MM_TRANSLATION = 0,
|
||||
MM_TRANSLATION_AND_SCALE = 1,
|
||||
MM_RIGID = 2,
|
||||
MM_SIMILARITY = 3,
|
||||
MM_AFFINE = 4,
|
||||
MM_HOMOGRAPHY = 5,
|
||||
MM_UNKNOWN = 6
|
||||
};
|
||||
|
||||
struct CV_EXPORTS RansacParams
|
||||
{
|
||||
int size; // subset size
|
||||
float thresh; // max error to classify as inlier
|
||||
float eps; // max outliers ratio
|
||||
float prob; // probability of success
|
||||
|
||||
RansacParams() : size(0), thresh(0), eps(0), prob(0) {}
|
||||
RansacParams(int size, float thresh, float eps, float prob)
|
||||
: size(size), thresh(thresh), eps(eps), prob(prob) {}
|
||||
|
||||
int niters() const
|
||||
{
|
||||
return static_cast<int>(
|
||||
std::ceil(std::log(1 - prob) / std::log(1 - std::pow(1 - eps, size))));
|
||||
}
|
||||
|
||||
static RansacParams default2dMotion(MotionModel model)
|
||||
{
|
||||
CV_Assert(model < MM_UNKNOWN);
|
||||
if (model == MM_TRANSLATION)
|
||||
return RansacParams(1, 0.5f, 0.5f, 0.99f);
|
||||
if (model == MM_TRANSLATION_AND_SCALE)
|
||||
return RansacParams(2, 0.5f, 0.5f, 0.99f);
|
||||
if (model == MM_RIGID)
|
||||
return RansacParams(2, 0.5f, 0.5f, 0.99f);
|
||||
if (model == MM_SIMILARITY)
|
||||
return RansacParams(2, 0.5f, 0.5f, 0.99f);
|
||||
if (model == MM_AFFINE)
|
||||
return RansacParams(3, 0.5f, 0.5f, 0.99f);
|
||||
return RansacParams(4, 0.5f, 0.5f, 0.99f);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
} // namespace videostab
|
||||
} // namespace cv
|
||||
|
||||
#endif
|
@@ -78,11 +78,11 @@ class CV_EXPORTS PyrLkOptFlowEstimatorBase
|
||||
public:
|
||||
PyrLkOptFlowEstimatorBase() { setWinSize(Size(21, 21)); setMaxLevel(3); }
|
||||
|
||||
void setWinSize(Size val) { winSize_ = val; }
|
||||
Size winSize() const { return winSize_; }
|
||||
virtual void setWinSize(Size val) { winSize_ = val; }
|
||||
virtual Size winSize() const { return winSize_; }
|
||||
|
||||
void setMaxLevel(int val) { maxLevel_ = val; }
|
||||
int maxLevel() const { return maxLevel_; }
|
||||
virtual void setMaxLevel(int val) { maxLevel_ = val; }
|
||||
virtual int maxLevel() const { return maxLevel_; }
|
||||
|
||||
protected:
|
||||
Size winSize_;
|
||||
|
@@ -0,0 +1,96 @@
|
||||
/*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-2011, 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_VIDEOSTAB_OUTLIER_REJECTION_HPP__
|
||||
#define __OPENCV_VIDEOSTAB_OUTLIER_REJECTION_HPP__
|
||||
|
||||
#include <vector>
|
||||
#include "opencv2/core/core.hpp"
|
||||
#include "opencv2/videostab/motion_core.hpp"
|
||||
|
||||
namespace cv
|
||||
{
|
||||
namespace videostab
|
||||
{
|
||||
|
||||
class CV_EXPORTS IOutlierRejector
|
||||
{
|
||||
public:
|
||||
virtual ~IOutlierRejector() {}
|
||||
|
||||
virtual void process(
|
||||
Size frameSize, InputArray points0, InputArray points1, OutputArray mask) = 0;
|
||||
};
|
||||
|
||||
class CV_EXPORTS NullOutlierRejector : public IOutlierRejector
|
||||
{
|
||||
public:
|
||||
virtual void process(
|
||||
Size frameSize, InputArray points0, InputArray points1, OutputArray mask);
|
||||
};
|
||||
|
||||
class CV_EXPORTS TranslationBasedLocalOutlierRejector : public IOutlierRejector
|
||||
{
|
||||
public:
|
||||
TranslationBasedLocalOutlierRejector();
|
||||
|
||||
void setCellSize(Size val) { cellSize_ = val; }
|
||||
Size cellSize() const { return cellSize_; }
|
||||
|
||||
void setRansacParams(RansacParams val) { ransacParams_ = val; }
|
||||
RansacParams ransacParams() const { return ransacParams_; }
|
||||
|
||||
virtual void process(
|
||||
Size frameSize, InputArray points0, InputArray points1, OutputArray mask);
|
||||
|
||||
private:
|
||||
Size cellSize_;
|
||||
RansacParams ransacParams_;
|
||||
|
||||
typedef std::vector<int> Cell;
|
||||
std::vector<Cell> grid_;
|
||||
};
|
||||
|
||||
} // namespace videostab
|
||||
} // namespace cv
|
||||
|
||||
#endif
|
@@ -40,7 +40,7 @@
|
||||
//
|
||||
//M*/
|
||||
|
||||
// References:
|
||||
// REFERENCES
|
||||
// 1. "Full-Frame Video Stabilization with Motion Inpainting"
|
||||
// Yasuyuki Matsushita, Eyal Ofek, Weina Ge, Xiaoou Tang, Senior Member, and Heung-Yeung Shum
|
||||
// 2. "Auto-Directed Video Stabilization with Robust L1 Optimal Camera Paths"
|
||||
|
@@ -101,12 +101,12 @@ public:
|
||||
class CV_EXPORTS MoreAccurateMotionWobbleSuppressorBase : public WobbleSuppressorBase
|
||||
{
|
||||
public:
|
||||
MoreAccurateMotionWobbleSuppressorBase() { setPeriod(30); }
|
||||
|
||||
void setPeriod(int val) { period_ = val; }
|
||||
int period() const { return period_; }
|
||||
virtual void setPeriod(int val) { period_ = val; }
|
||||
virtual int period() const { return period_; }
|
||||
|
||||
protected:
|
||||
MoreAccurateMotionWobbleSuppressorBase() { setPeriod(30); }
|
||||
|
||||
int period_;
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user