-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsource.cpp
209 lines (176 loc) · 5.22 KB
/
source.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
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
/*
Thanks Nghia Ho and Chen jia for their excellent work on video-stabilization
I modified the code to use GPU processing via opencl
As a result it can more smoothly process live video streaming
modified by Rishabh Agrawal.
email:rg1995007@gmail.com
visit http://nghiaho.com/?p=2093 for implementation details
*/
#include <opencv2/opencv.hpp>
#include <opencv2/ocl/ocl.hpp>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
using namespace cv;
using namespace cv::ocl;
const int HORIZONTAL_BORDER_CROP = 20;
struct TransformParam
{
TransformParam() {}
TransformParam(double _dx, double _dy, double _da) {
dx = _dx;
dy = _dy;
da = _da;
}
double dx;
double dy;
double da; // angle
};
struct Trajectory
{
Trajectory() {}
Trajectory(double _x, double _y, double _a) {
x = _x;
y = _y;
a = _a;
}
// "+"
friend Trajectory operator+(const Trajectory &c1,const Trajectory &c2){
return Trajectory(c1.x+c2.x,c1.y+c2.y,c1.a+c2.a);
}
//"-"
friend Trajectory operator-(const Trajectory &c1,const Trajectory &c2){
return Trajectory(c1.x-c2.x,c1.y-c2.y,c1.a-c2.a);
}
//"*"
friend Trajectory operator*(const Trajectory &c1,const Trajectory &c2){
return Trajectory(c1.x*c2.x,c1.y*c2.y,c1.a*c2.a);
}
//"/"
friend Trajectory operator/(const Trajectory &c1,const Trajectory &c2){
return Trajectory(c1.x/c2.x,c1.y/c2.y,c1.a/c2.a);
}
//"="
Trajectory operator =(const Trajectory &rx){
x = rx.x;
y = rx.y;
a = rx.a;
return Trajectory(x,y,a);
}
double x;
double y;
double a; // angle
};
int main()
{
VideoCapture cap(0);
Mat image,prev_image;
Mat frame_curr,frame_prev;
oclMat dimage,dprev_image;
oclMat dframe_curr,dframe_prev;
cap>>prev_image;
cvtColor(prev_image,frame_prev,CV_BGR2GRAY);
dprev_image.upload(prev_image);
dframe_prev.upload(frame_prev);
namedWindow("video",CV_WINDOW_AUTOSIZE);
namedWindow("stabilized video",CV_WINDOW_AUTOSIZE);
double a = 0;
double x = 0;
double y = 0;
Trajectory X;//posteriori state estimate
Trajectory X_;//priori estimate
Trajectory P;// posteriori estimate error covariance
Trajectory P_;// priori estimate error covariance
Trajectory K;//gain
Trajectory z;//actual measurement
double pstd = 4e-3;//can be changed
double cstd = 0.25;//can be changed
Trajectory Q(pstd,pstd,pstd);// process noise covariance
Trajectory R(cstd,cstd,cstd);// measurement noise covariance
int k=1;
int vert_border = HORIZONTAL_BORDER_CROP * prev_image.rows / prev_image.cols;
Mat rigidtransform,last_rigidtransform;
while(true)
{
cap>>image;
if(image.empty())
break;
cvtColor(image,frame_curr,CV_BGR2GRAY);
dimage.upload(image);
dframe_curr.upload(frame_curr);
vector<Point2f> prevpts,currpts;
vector<Point2f> prev_corner, cur_corner;
Mat status,err,corners;
oclMat prev_corners,curr_corners;
ocl::GoodFeaturesToTrackDetector_OCL(300)(dframe_prev,prev_corners);
prev_corners.download(corners);
corners.row(0).copyTo(prevpts);
calcOpticalFlowPyrLK(frame_prev,frame_curr,prevpts,currpts,status,err);
// weed out bad matches
for(int i=0; i < status.rows; i++) {
if((int)status.at<uchar>(i,0)==1) {
prev_corner.push_back(prevpts[i]);
cur_corner.push_back(currpts[i]);
}
}
Mat rigidtrans=estimateRigidTransform(prev_corner,cur_corner,false);
if(rigidtrans.empty())
{
rigidtrans=last_rigidtransform.clone();
}
last_rigidtransform=rigidtrans.clone();
double dx = rigidtrans.at<double>(0,2);
double dy = rigidtrans.at<double>(1,2);
double da = atan2(rigidtrans.at<double>(1,0), rigidtrans.at<double>(0,0));
x += dx;
y += dy;
a += da;
z = Trajectory(x,y,a);
if(k==1)
{
// intial guesses
X = Trajectory(0,0,0); //Initial estimate, set 0
P =Trajectory(1,1,1); //set error variance,set 1
}
else
{
//time update prediction
X_ = X; //X_(k) = X(k-1);
P_ = P+Q; //P_(k) = P(k-1)+Q;
// measurement update correction
K = P_/( P_+R ); //gain;K(k) = P_(k)/( P_(k)+R );
X = X_+K*(z-X_); //z-X_ is residual,X(k) = X_(k)+K(k)*(z(k)-X_(k));
P = (Trajectory(1,1,1)-K)*P_; //P(k) = (1-K(k))*P_(k);
}
double diff_x = X.x - x;
double diff_y = X.y - y;
double diff_a = X.a - a;
dx = dx + diff_x;
dy = dy + diff_y;
da = da + diff_a;
rigidtrans.at<double>(0,0) = cos(da);
rigidtrans.at<double>(0,1) = -sin(da);
rigidtrans.at<double>(1,0) = sin(da);
rigidtrans.at<double>(1,1) = cos(da);
rigidtrans.at<double>(0,2) = dx;
rigidtrans.at<double>(1,2) = dy;
oclMat dfinal_frame;
Mat final_frame;
ocl::warpAffine(dprev_image,dfinal_frame,rigidtrans,Size(640,480));
dfinal_frame.download(final_frame);
final_frame = final_frame(Range(vert_border, final_frame.rows-vert_border), Range(HORIZONTAL_BORDER_CROP, final_frame.cols-HORIZONTAL_BORDER_CROP));
// Resize cur2 back to cur size, for better side by side comparison
resize(final_frame, final_frame, image.size());
//Mat roi=final_frame(Range(50,430),Range(50,590));
imshow("video",image);
imshow("stabilized video",final_frame);
waitKey(1);
prev_image=image.clone();
frame_curr.copyTo(frame_prev);
dprev_image.upload(prev_image);
dframe_prev.upload(frame_prev);
k++;
}
return 0;
}