-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstabilizer.cpp
114 lines (92 loc) · 3.8 KB
/
stabilizer.cpp
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
#include "stabilizer.hpp"
VideoStabilizer::VideoStabilizer(const VideoStabilizerParams& params)
: m_l1Smoother(params.lag, params.smoother_memory, params.lambda)
{
m_params = params;
}
cv::Mat VideoStabilizer::processFrame(const cv::Mat& inputFrame)
{
// 1) Increment our frame index
++m_frameIndex;
// 2) Store the incoming frame for delayed output
m_frameBuffer.push_back(inputFrame.clone());
// 3) Measure transform from the previous frame to current
SimilarityTransform currentMeas;
bool success = aligner.AlignNextFrame(inputFrame, currentMeas, m_params.aligner);
#if 0
if (!success) {
std::cout << "Alignment failed for frame " << m_frameIndex << std::endl;
}
#endif
// 4) Update the smoother, which returns the earliest measurement that is
// now fully “smoothed” after seeing lag_ future measurements.
bool reset = !success;
SimilarityTransform earliestSmoothed;
if (m_params.enable_smoother) {
m_l1Smoother.update(currentMeas, earliestSmoothed);
}
// If alignment fails, we reset accum (like original code).
if (reset) {
m_accum = SimilarityTransform(); // identity
}
// 5) Store the new measurement (for future finalization)
m_measurementBuffer.push_back(currentMeas);
// 6) Check if we have finalized an old measurement
// i.e. the smoother has done “lag” updates since that old measurement
bool hasFinalized = (m_measurementBuffer.size() > (size_t)m_params.lag);
cv::Mat outputFrame; // empty unless we finalize
if (hasFinalized)
{
// 6a) Pop the earliest measurement from the queue
SimilarityTransform earliestMeas = m_measurementBuffer.front();
m_measurementBuffer.pop_front();
SimilarityTransform jitter;
if (m_params.enable_smoother) {
jitter = earliestMeas.compose( earliestSmoothed.inverse() );
} else {
jitter = earliestMeas; // <-- (A) purely raw measurement
}
// 6c) Compose into newAccum
SimilarityTransform newAccum = m_accum.compose(jitter);
// 6d) Check displacement for partial or full reset
double displacement = newAccum.maxCornerDisplacement(
inputFrame.cols, inputFrame.rows);
double decay = 1.0;
if (displacement > m_params.max_disp) {
decay = m_params.max_decay;
} else if (displacement > m_params.min_disp) {
double f = (displacement - m_params.min_disp) / (m_params.max_disp - m_params.min_disp);
f = std::max(0.0, std::min(1.0, f));
decay = m_params.min_decay * (1.0 - f) + m_params.max_decay * f;
} else {
decay = m_params.min_decay;
}
newAccum.TX *= decay;
newAccum.TY *= decay;
newAccum.A *= decay;
newAccum.B *= decay;
m_accum = newAccum;
// 6e) Pop the corresponding earliest frame
if (!m_frameBuffer.empty())
{
cv::Mat frameToStabilize = m_frameBuffer.front();
m_frameBuffer.pop_front();
// 6f) Warp it by newAccum.inverse()
SimilarityTransform correction = newAccum.inverse();
cv::Mat stabilized = warpBySimilarityTransform(
frameToStabilize, correction);
// 7) Optional crop
if (m_params.crop_pixels > 0) {
cv::Rect roi(
m_params.crop_pixels, m_params.crop_pixels,
stabilized.cols - 2*m_params.crop_pixels,
stabilized.rows - 2*m_params.crop_pixels
);
stabilized = stabilized(roi);
}
outputFrame = stabilized;
}
}
// If nothing was finalized, outputFrame is empty
return outputFrame;
}