-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid_search_smoother.cpp
More file actions
287 lines (232 loc) · 8.91 KB
/
grid_search_smoother.cpp
File metadata and controls
287 lines (232 loc) · 8.91 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "stabilizer.hpp"
#include <opencv2/opencv.hpp>
#include <HalideRuntime.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <thread>
#include <mutex>
#include <atomic>
// ----------------------------------------------------------------------------------
// Utility helpers copied from grid_search_align.cpp
// ----------------------------------------------------------------------------------
// Compute median of a vector<double>
static double median(std::vector<double>& v)
{
if (v.empty()) return 0.0;
size_t n = v.size() / 2;
std::nth_element(v.begin(), v.begin() + n, v.end());
double med = v[n];
if (v.size() % 2 == 0)
{
std::nth_element(v.begin(), v.begin() + n - 1, v.end());
med = 0.5 * (med + v[n - 1]);
}
return med;
}
// Measure jitter (median optical-flow magnitude) for a sequence of frames
static double measure_jitter(const std::vector<cv::Mat>& frames)
{
if (frames.size() < 2) return 0.0;
std::vector<double> meds;
cv::Mat prevGray;
cv::cvtColor(frames[0], prevGray, cv::COLOR_BGR2GRAY);
for (size_t i = 1; i < frames.size(); ++i)
{
cv::Mat gray;
cv::cvtColor(frames[i], gray, cv::COLOR_BGR2GRAY);
cv::Mat flow;
cv::calcOpticalFlowFarneback(prevGray, gray, flow,
0.5, 3, 15, 3, 5, 1.2, 0);
std::vector<cv::Mat> comps(2);
cv::split(flow, comps);
cv::Mat mag;
cv::magnitude(comps[0], comps[1], mag);
mag = mag.reshape(1, 1); // flatten to single row
std::vector<float> magVec;
mag.copyTo(magVec);
if (!magVec.empty())
{
size_t n = magVec.size() / 2;
std::nth_element(magVec.begin(), magVec.begin() + n, magVec.end());
meds.push_back(magVec[n]);
}
prevGray = gray;
}
return median(meds);
}
// ----------------------------------------------------------------------------------
struct Combo
{
// Smoother parameters
int lag;
int mem;
double lambda;
// Reset/decay parameters
double min_disp;
double max_disp;
double min_decay;
double max_decay;
};
// ----------------------------------------------------------------------------------
int main(int argc, char** argv)
{
if (argc < 2)
{
std::cerr << "Usage: " << argv[0] << " video.mp4 [-j N]" << std::endl;
return 1;
}
// ------------------------------------------------------------------
// Parse CLI arguments
// ------------------------------------------------------------------
std::string videoPath;
int jobCount = std::thread::hardware_concurrency();
for (int i = 1; i < argc; ++i)
{
std::string arg(argv[i]);
if (arg == "-j" || arg == "--jobs")
{
if (i + 1 >= argc)
{
std::cerr << "Missing value after " << arg << std::endl;
return 1;
}
jobCount = std::max(1, std::atoi(argv[++i]));
}
else
{
videoPath = arg;
}
}
if (videoPath.empty())
{
std::cerr << "Video path not provided" << std::endl;
return 1;
}
// ------------------------------------------------------------------
// Configure threading for Halide + OpenCV to avoid nested-parallelism
// ------------------------------------------------------------------
halide_set_num_threads(1);
cv::setNumThreads(1);
// ------------------------------------------------------------------
// Load entire video into memory
// ------------------------------------------------------------------
cv::VideoCapture cap(videoPath);
if (!cap.isOpened())
{
std::cerr << "Cannot open " << videoPath << std::endl;
return 1;
}
std::vector<cv::Mat> frames;
cv::Mat frm;
while (cap.read(frm))
{
frames.push_back(frm.clone());
}
if (frames.size() < 2)
{
std::cerr << "Video too short" << std::endl;
return 1;
}
double inputJitter = measure_jitter(frames);
std::cout << "Input median jitter: " << inputJitter << " px" << std::endl;
// ------------------------------------------------------------------
// Parameter grid
// ------------------------------------------------------------------
// Smoother parameters to consider (same as old synthetic search)
const int lagVals[] = {3, 5, 8, 10};
const int memVals[] = {5, 8, 10};
const double lambdaVals[] = {4.0, 6.0, 8.0, 10.0};
// Displacement / decay parameters
const double minDispVals[] = {16.0, 32.0, 48.0};
const double maxDispVals[] = {64.0, 96.0, 128.0};
const double minDecayVals[] = {0.99, 0.95, 0.9};
const double maxDecayVals[] = {0.7, 0.5, 0.3};
std::vector<Combo> combos;
for (int lag : lagVals)
for (int mem : memVals)
for (double lam : lambdaVals)
for (double mind : minDispVals)
for (double maxd : maxDispVals)
if (mind < maxd)
for (double mindec : minDecayVals)
for (double maxdec : maxDecayVals)
if (mindec > maxdec)
combos.push_back({lag, mem, lam, mind, maxd, mindec, maxdec});
const size_t total = combos.size();
std::cout << "Evaluating " << total << " parameter combinations using "
<< jobCount << " threads" << std::endl;
// ------------------------------------------------------------------
// Multithreaded grid search
// ------------------------------------------------------------------
std::atomic<size_t> nextIdx{0};
std::atomic<size_t> doneCnt{0};
double bestRatio = 1e9;
Combo bestCombo{0,0,0,0,0,0,0};
std::mutex bestMutex;
auto tStart = std::chrono::steady_clock::now();
auto worker = [&]() {
while (true)
{
size_t idx = nextIdx.fetch_add(1);
if (idx >= total) break;
const Combo& c = combos[idx];
VideoStabilizerParams params;
// Smoother configuration
params.lag = c.lag;
params.smoother_memory = c.mem;
params.lambda = c.lambda;
// Displacement / decay configuration
params.min_disp = c.min_disp;
params.max_disp = c.max_disp;
params.min_decay = c.min_decay;
params.max_decay = c.max_decay;
VideoStabilizer stab(params);
std::vector<cv::Mat> outs;
for (const auto& f : frames)
{
cv::Mat o = stab.processFrame(f);
if (!o.empty()) outs.push_back(o.clone());
}
if (outs.size() < 2) continue; // insufficient output frames
double outJitter = measure_jitter(outs);
double ratio = outJitter / inputJitter;
size_t finished = ++doneCnt;
{
std::lock_guard<std::mutex> lk(bestMutex);
auto now = std::chrono::steady_clock::now();
double sec = std::chrono::duration<double>(now - tStart).count();
std::cout << "[" << finished << "/" << total << "] "
<< "lag=" << c.lag << " mem=" << c.mem << " lambda=" << c.lambda
<< " minDisp=" << c.min_disp << " maxDisp=" << c.max_disp
<< " minDecay=" << c.min_decay << " maxDecay=" << c.max_decay
<< " outJit=" << outJitter << " ratio=" << ratio
<< " elapsed=" << sec << "s";
if (ratio < bestRatio)
{
bestRatio = ratio;
bestCombo = c;
std::cout << " ** new best **";
}
std::cout << std::endl;
}
}
};
std::vector<std::thread> threads;
for (int i = 0; i < jobCount; ++i) threads.emplace_back(worker);
for (auto& t : threads) t.join();
// ------------------------------------------------------------------
// Report best result
// ------------------------------------------------------------------
std::cout << "\nBest parameters:" << std::endl;
std::cout << " lag = " << bestCombo.lag << std::endl;
std::cout << " smoother_memory = " << bestCombo.mem << std::endl;
std::cout << " lambda = " << bestCombo.lambda << std::endl;
std::cout << " min_disp = " << bestCombo.min_disp << std::endl;
std::cout << " max_disp = " << bestCombo.max_disp << std::endl;
std::cout << " min_decay = " << bestCombo.min_decay << std::endl;
std::cout << " max_decay = " << bestCombo.max_decay << std::endl;
std::cout << " jitter ratio = " << bestRatio << std::endl;
return 0;
}