-
Notifications
You must be signed in to change notification settings - Fork 0
/
videostab.h
225 lines (172 loc) · 6.59 KB
/
videostab.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#ifndef VIDEOSTAB_H
#define VIDEOSTAB_H
#include "opencv2/opencv.hpp"
#include <cmath>
// This video stablisation smooths the global trajectory using a sliding average window
// 1. Get previous to current frame transformation (dx, dy, da) for all frames
// 2. Accumulate the transformations to get the image trajectory
// 3. Smooth out the trajectory using an averaging window
// 4. Generate new set of previous to current transform, such that the trajectory ends up being the same as the smoothed trajectory
// 5. Apply the new transformation to the video
//uncomment here to line 148 if want to use CvPlot for plotting camera trajectory
//using namespace std;
//namespace CvPlot
//{
// // A curve.
// class Series
// {
// public:
// // number of points
// unsigned int count;
// float *data;
// // name of the curve
// string label;
// // allow automatic curve color
// bool auto_color;
// CvScalar color;
// Series(void);
// Series(const Series& s);
// ~Series(void);
// // release memory
// void Clear();
// void SetData(int n, float *p);
// void SetColor(CvScalar color, bool auto_color = true);
// void SetColor(int R, int G, int B, bool auto_color = true);
// };
// // a figure comprises of several curves
// class Figure
// {
// private:
// // window name
// string figure_name;
// CvSize figure_size;
// // margin size
// int border_size;
// CvScalar backgroud_color;
// CvScalar axis_color;
// CvScalar text_color;
// // several curves
// vector<Series> plots;
// // manual or automatic range
// bool custom_range_y;
// float y_max;
// float y_min;
// float y_scale;
// bool custom_range_x;
// float x_max;
// float x_min;
// float x_scale;
// // automatically change color for each curve
// int color_index;
// public:
// Figure(const string name);
// ~Figure();
// string GetFigureName();
// Series* Add(const Series &s);
// void Clear();
// void DrawLabels(IplImage *output, int posx, int posy);
// // show plot window
// void Show();
// private:
// Figure();
// void DrawAxis(IplImage *output);
// void DrawPlots(IplImage *output);
// // call before plot
// void Initialize();
// CvScalar GetAutoColor();
// };
// // manage plot windows
// class PlotManager
// {
// private:
// vector<Figure> figure_list;
// Series *active_series;
// Figure *active_figure;
// public:
// // now useless
// bool HasFigure(string wnd);
// Figure* FindFigure(string wnd);
// void Plot(const string figure_name, const float* p, int count, int step,
// int R, int G, int B);
// void Label(string lbl);
// };
// // handle different data types; static methods;
// /**
// * @brief Matlab style plot functions for OpenCV by Changbo (zoccob@gmail). plot and label.
// *
// * @param figure_name required. multiple calls of this function with same figure_name plots multiple curves on a single graph.
// * @param p required. pointer to data.
// * @param count required. number of data.
// * @param step optional. step between data of two points, default 1.
// * @param R optional. assign a color to the curve. if not assigned, the curve will be assigned a unique color automatically.
// * @param G
// * @param B
// */
// template<typename T>
// void plot(const string figure_name, const T* p, int count, int step = 1,
// int R = -1, int G = -1, int B = -1);
// void clear(const string figure_name);
// void label(string lbl);
//}
struct AffineTransformParam
{
AffineTransformParam() {}
AffineTransformParam( double x, double y,
double a, double b,
double c, double d )
: _x( x ) , _y( y )
, _a( a ) , _b( b )
, _c( c ) , _d( d )
{ } // affine
AffineTransformParam( double x, double y,
double a, double b )
: _x( x ) , _y( y )
, _a( a ) , _b( b )
, _c( -b ) , _d( a )
{ } // rigid
//"*": apply transform this and then c2
AffineTransformParam applyTransform( AffineTransformParam & c2 )
{
AffineTransformParam param( this->_x * c2._a + this->_y * c2._b + c2._x,
this->_x * c2._c + this->_y * c2._d + c2._y,
this->_a * c2._a + this->_c * c2._b,
this->_b * c2._a + this->_d * c2._b,
this->_a * c2._c + this->_c * c2._d,
this->_b * c2._c + this->_d * c2._d );
return param;
}
double getRotation()
{
return atan2(this->_b,this->_a);
}
//"="
AffineTransformParam operator =( const AffineTransformParam & rx )
{
return AffineTransformParam( rx._x, rx._y, rx._a, rx._b, rx._c, rx._d );
}
double _x = 0.0f;
double _y = 0.0f;
double _a = 1.0f;
double _b = 0.0f;
double _c = 0.0f;
double _d = 0.1f;
};
class VideoStablizer
{
public:
VideoStablizer( std::string filepath );
bool run( std::string output_path );
void calcSmoothRigidTransform( const std::vector<AffineTransformParam> & transforms,
std::vector<cv::Mat> & optimal_transform,
int height, int width, float crop_ratio);
void plotTrajectory(std::vector<AffineTransformParam> oldT, std::vector<AffineTransformParam> newT);
private:
std::vector<AffineTransformParam> estimateTransform();
std::string _path;
int _num_frames;
const float w1 = 10.0f;
const float w2 = 1.0f;
const float w3 = 100.0f;
const float w_affine = 100.0f;
};
#endif // VIDEOSTAB_H