-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval_jitter.cpp
More file actions
75 lines (63 loc) · 2.03 KB
/
Copy patheval_jitter.cpp
File metadata and controls
75 lines (63 loc) · 2.03 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
#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
// Compute the 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;
}
int main(int argc, char** argv)
{
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " video1 [video2 ...]\n";
return 1;
}
for (int idx = 1; idx < argc; ++idx) {
std::string path = argv[idx];
cv::VideoCapture cap(path);
if (!cap.isOpened()) {
std::cerr << "Cannot open video: " << path << "\n";
continue;
}
cv::Mat prev, prevGray;
if (!cap.read(prev)) {
std::cerr << "Empty video: " << path << "\n";
continue;
}
cv::cvtColor(prev, prevGray, cv::COLOR_BGR2GRAY);
std::vector<double> frameMeds;
cv::Mat frame, gray;
while (cap.read(frame)) {
cv::cvtColor(frame, 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> flows(2);
cv::split(flow, flows);
cv::Mat mag;
cv::magnitude(flows[0], flows[1], mag);
// flatten to 1‑D vector<float>
mag = mag.reshape(1,1);
std::vector<float> mf; mag.copyTo(mf);
if (!mf.empty()) {
size_t n = mf.size()/2;
std::nth_element(mf.begin(), mf.begin()+n, mf.end());
frameMeds.push_back(mf[n]);
}
prevGray = gray;
}
double medJitter = median(frameMeds);
std::cout << path << "\tmedian_jitter_px=" << medJitter << "\n";
}
return 0;
}