-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvideo_stab.cpp
102 lines (82 loc) · 2.62 KB
/
video_stab.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
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/video/video.hpp>
#include <iostream>
#define CAM 0
using namespace cv;
using namespace std;
class Tracker {
vector<Point2f> trackedFeatures;
Mat prevGray;
public:
bool freshStart;
Mat_<float> rigidTransform;
Tracker():freshStart(true)
{
rigidTransform = Mat::eye(3,3,CV_32FC1); //affine 2x3 in a 3x3 matrix
}
void processImage(Mat& img)
{
Mat gray; cvtColor(img,gray,CV_BGR2GRAY);
vector<Point2f> corners;
if(trackedFeatures.size() < 200)
{
goodFeaturesToTrack(gray,corners,300,0.01,1);
cout << "found " << corners.size() << " features\n";
for (unsigned int i = 0; i < corners.size(); ++i)
{
trackedFeatures.push_back(corners[i]);
}
}
if(!prevGray.empty()) {
vector<uchar> status; vector<float> errors;
calcOpticalFlowPyrLK(prevGray,gray,trackedFeatures,corners,status,errors,Size(10,10));
if(countNonZero(status) < status.size() * 0.8)
{
cout << "cataclysmic error \n";
rigidTransform = Mat::eye(3,3,CV_32FC1);
trackedFeatures.clear();
prevGray.release();
freshStart = true;
return;
}
else
freshStart = false;
Mat_<float> newRigidTransform = estimateRigidTransform(trackedFeatures,corners,false);
Mat_<float> nrt33 = Mat_<float>::eye(3,3);
newRigidTransform.copyTo(nrt33.rowRange(0,2));
rigidTransform *= nrt33;
trackedFeatures.clear();
for (unsigned int i = 0; i < status.size(); ++i)
{
if(status[i]) {
trackedFeatures.push_back(corners[i]);
}
}
}
for (unsigned int i = 0; i < trackedFeatures.size(); ++i)
{
circle(img,trackedFeatures[i],3,Scalar(0,0,255),CV_FILLED);
}
gray.copyTo(prevGray);
}
};
int main()
{
VideoCapture vc(CAM);
Mat frame,orig,orig_warped,tmp;
Tracker tracker;
cout << "in main" << endl;
while(1)
{
vc >> frame;
if(frame.empty()) break;
frame.copyTo(orig);
tracker.processImage(orig);
Mat invTrans = tracker.rigidTransform.inv(DECOMP_SVD);
warpAffine(orig,orig_warped,invTrans.rowRange(0,2),Size());
imshow("orig",orig_warped);
waitKey(1);
}
vc.release();
}