-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptical_flow.cpp
More file actions
64 lines (46 loc) · 1.3 KB
/
Copy pathoptical_flow.cpp
File metadata and controls
64 lines (46 loc) · 1.3 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
#include <iostream>
#include <string>
#include <sstream>
#include <opencv2/features2d.hpp>
#include "opencv2/highgui.hpp"
#include <opencv2/imgcodecs.hpp>
#include "opencv2/imgproc.hpp"
#include <opencv2/video.hpp>
#include <opencv2/videoio.hpp>
using namespace std;
using namespace cv;
string WIN_SRC = "Camera";
/* NOTES
Dense vs Sparse optical flow
dense (farnerback's algorithm)
- requires the use of every pixel in every computation
- slow but accurate
sparse (lucas-kanade algorithm)
- only requires a certain subset of pixels to perform a computation
- fast but potentially inaccurate
*/
int main() {
VideoCapture cap(1);
namedWindow(WIN_SRC, WINDOW_AUTOSIZE);
moveWindow(WIN_SRC, 20, 20);
Mat old_frame, old_gray;
std::vector<Point> old_corners;
cap >> old_frame;
cvtColor(old_frame, old_gray, COLOR_BGR2GRAY);
goodFeaturesToTrack(old_gray, old_corners, 100, 0.3, 7);
Mat frame, gray;
std::vector<Point> corners;
while (true) {
cap >> frame;
if (frame.empty()) break;
cvtColor(frame, gray, COLOR_BGR2GRAY);
Mat status, err;
calcOpticalFlowPyrLK(old_gray, gray, old_corners, corners, status, err);
gray.copyTo(old_gray);
old_corners = corners;
imshow(WIN_SRC, frame);
if (char key = waitKey(30) == 27) break;
}
destroyAllWindows();
return 0;
}