1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
8 //
9 //
10 // License Agreement
11 // For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
22 //
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
26 //
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42
43 #ifndef __OPENCV_VIDEOSTAB_MOTION_STABILIZING_HPP__
44 #define __OPENCV_VIDEOSTAB_MOTION_STABILIZING_HPP__
45
46 #include <vector>
47 #include <utility>
48 #include "opencv2/core.hpp"
49 #include "opencv2/videostab/global_motion.hpp"
50
51 namespace cv
52 {
53 namespace videostab
54 {
55
56 //! @addtogroup videostab_motion
57 //! @{
58
59 class CV_EXPORTS IMotionStabilizer
60 {
61 public:
~IMotionStabilizer()62 virtual ~IMotionStabilizer() {}
63
64 //! assumes that [0, size-1) is in or equals to [range.first, range.second)
65 virtual void stabilize(
66 int size, const std::vector<Mat> &motions, std::pair<int,int> range,
67 Mat *stabilizationMotions) = 0;
68 };
69
70 class CV_EXPORTS MotionStabilizationPipeline : public IMotionStabilizer
71 {
72 public:
pushBack(Ptr<IMotionStabilizer> stabilizer)73 void pushBack(Ptr<IMotionStabilizer> stabilizer) { stabilizers_.push_back(stabilizer); }
empty() const74 bool empty() const { return stabilizers_.empty(); }
75
76 virtual void stabilize(
77 int size, const std::vector<Mat> &motions, std::pair<int,int> range,
78 Mat *stabilizationMotions);
79
80 private:
81 std::vector<Ptr<IMotionStabilizer> > stabilizers_;
82 };
83
84 class CV_EXPORTS MotionFilterBase : public IMotionStabilizer
85 {
86 public:
~MotionFilterBase()87 virtual ~MotionFilterBase() {}
88
89 virtual Mat stabilize(
90 int idx, const std::vector<Mat> &motions, std::pair<int,int> range) = 0;
91
92 virtual void stabilize(
93 int size, const std::vector<Mat> &motions, std::pair<int,int> range,
94 Mat *stabilizationMotions);
95 };
96
97 class CV_EXPORTS GaussianMotionFilter : public MotionFilterBase
98 {
99 public:
100 GaussianMotionFilter(int radius = 15, float stdev = -1.f);
101
102 void setParams(int radius, float stdev = -1.f);
radius() const103 int radius() const { return radius_; }
stdev() const104 float stdev() const { return stdev_; }
105
106 virtual Mat stabilize(
107 int idx, const std::vector<Mat> &motions, std::pair<int,int> range);
108
109 private:
110 int radius_;
111 float stdev_;
112 std::vector<float> weight_;
113 };
114
GaussianMotionFilter(int _radius,float _stdev)115 inline GaussianMotionFilter::GaussianMotionFilter(int _radius, float _stdev) { setParams(_radius, _stdev); }
116
117 class CV_EXPORTS LpMotionStabilizer : public IMotionStabilizer
118 {
119 public:
120 LpMotionStabilizer(MotionModel model = MM_SIMILARITY);
121
setMotionModel(MotionModel val)122 void setMotionModel(MotionModel val) { model_ = val; }
motionModel() const123 MotionModel motionModel() const { return model_; }
124
setFrameSize(Size val)125 void setFrameSize(Size val) { frameSize_ = val; }
frameSize() const126 Size frameSize() const { return frameSize_; }
127
setTrimRatio(float val)128 void setTrimRatio(float val) { trimRatio_ = val; }
trimRatio() const129 float trimRatio() const { return trimRatio_; }
130
setWeight1(float val)131 void setWeight1(float val) { w1_ = val; }
weight1() const132 float weight1() const { return w1_; }
133
setWeight2(float val)134 void setWeight2(float val) { w2_ = val; }
weight2() const135 float weight2() const { return w2_; }
136
setWeight3(float val)137 void setWeight3(float val) { w3_ = val; }
weight3() const138 float weight3() const { return w3_; }
139
setWeight4(float val)140 void setWeight4(float val) { w4_ = val; }
weight4() const141 float weight4() const { return w4_; }
142
143 virtual void stabilize(
144 int size, const std::vector<Mat> &motions, std::pair<int,int> range,
145 Mat *stabilizationMotions);
146
147 private:
148 MotionModel model_;
149 Size frameSize_;
150 float trimRatio_;
151 float w1_, w2_, w3_, w4_;
152
153 std::vector<double> obj_, collb_, colub_;
154 std::vector<int> rows_, cols_;
155 std::vector<double> elems_, rowlb_, rowub_;
156
set(int row,int col,double coef)157 void set(int row, int col, double coef)
158 {
159 rows_.push_back(row);
160 cols_.push_back(col);
161 elems_.push_back(coef);
162 }
163 };
164
165 CV_EXPORTS Mat ensureInclusionConstraint(const Mat &M, Size size, float trimRatio);
166
167 CV_EXPORTS float estimateOptimalTrimRatio(const Mat &M, Size size);
168
169 //! @}
170
171 } // namespace videostab
172 } // namespace
173
174 #endif
175