-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopticalFlow.cpp
434 lines (372 loc) · 16 KB
/
opticalFlow.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
* opticalFlow.cpp
*
* Created on: Mar 16, 2016
*/
/*************************************** Includes ***************************************/
// General
#include <iostream>
#include <pthread.h>
#include <time.h>
// OpenCV
#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/videoio/videoio.hpp"
#include "opencv2/highgui/highgui.hpp"
// Our files
#include "perspective.cpp"
#include "globals.h"
#include "mainwindow.h"
/*************************************** Namespaces ***************************************/
using namespace cv;
using namespace std;
/*************************************** Global Variables *********************************/
/* import global variables */
locationStruct currLocation;
locationStruct lastFlowStep;
locationStruct lastFlowStepSections[4];
locationStruct gpsLocation;
string currentTime;
int end_run;
typedef struct{
UMat frameSection;
UMat prevFrameSection;
int32_t index;
}sectionInfo;
/*************************************** Auxiliary functions ******************************/
/* this function finds the right location according yaw angle*/
locationStruct calculateNewLocationByYaw(locationStruct lastFlowStep){
float yYaw = eulerFromSensors.yaw - M_PI/2;
locationStruct outputLocation;
outputLocation.x = lastFlowStep.x * cos(eulerFromSensors.yaw) + lastFlowStep.y * cos(yYaw);
outputLocation.y = -(lastFlowStep.x * sin(eulerFromSensors.yaw) + lastFlowStep.y * sin(yYaw));
return outputLocation;
}
/* this function draws the flow on the screen and accumulates the distance the UAV traveled */
#ifdef VIDEO_ACTIVE
void drawOptFlowMap(const Mat& flow, UMat& cflowmap, int step,
double, const Scalar& color)
{
// the distance each pixel traveled per two frames
double distPixelx = 0, distPixely = 0;
// count the number of pixels
int counter = 0;
// draw the optical flow and sum the distance that all the pixels have traveled
for(int y = cflowmap.rows*0.25; y < cflowmap.rows*0.75; y += step)
for(int x = cflowmap.cols*0.25; x < cflowmap.cols*0.75; x += step)
{
const Point2f& fxy = flow.at<Point2f>(y, x);
line(cflowmap, Point(x,y), Point(cvRound(x+fxy.x), cvRound(y+fxy.y)), color);
circle(cflowmap, Point(x,y), 2, color, -1);
distPixelx += fxy.x;
distPixely += fxy.y;
counter++;
}
// update the global variable - location of the UAV
//#ifdef YAW_ACTIVE
// locationStruct notUpdatedFlowStep;
// notUpdatedFlowStep.x = distPixelx/counter;
// notUpdatedFlowStep.y = distPixely/counter;
// lastFlowStep = calculateNewLocationByYaw(notUpdatedFlowStep);
//#else
lastFlowStep.x = distPixelx/counter;
lastFlowStep.y = distPixely/counter;
//#endif
}
#else
void calcAvgOpticalFlow(const Mat& flow, int step)
{
// the distance each pixel traveled per two frames
double distPixelx = 0, distPixely = 0;
// count the number of pixels
int counter = 0;
// draw the optical flow and sum the distance that all the pixels have traveled
for(int y = flow.rows*0.25; y < flow.rows*0.75; y += step)
for(int x = flow.cols*0.25; x < flow.cols*0.75; x += step)
{
const Point2f& fxy = flow.at<Point2f>(y, x);
distPixelx += fxy.x;
distPixely += fxy.y;
counter++;
}
// update the global variable - location of the UAV
//#ifdef YAW_ACTIVE
// locationStruct notUpdatedFlowStep;
// notUpdatedFlowStep.x = distPixelx/counter;
// notUpdatedFlowStep.y = distPixely/counter;
// lastFlowStep = calculateNewLocationByYaw(notUpdatedFlowStep);
//#else
lastFlowStep.x = distPixelx/counter;
lastFlowStep.y = distPixely/counter;
//#endif
}
#endif
void calcAvgOpticalFlowPerSection(const Mat& flow, int step, int index, float corners[4])
{
// the distance each pixel traveled per two frames
double distPixelx = 0, distPixely = 0;
// count the number of pixels
int counter = 0;
// draw the optical flow and sum the distance that all the pixels have traveled
for(int y = flow.rows*corners[2]; y < flow.rows*corners[3]; y += step)
for(int x = flow.cols*corners[0]; x < flow.cols*corners[1]; x += step)
{
const Point2f& fxy = flow.at<Point2f>(y, x);
distPixelx += fxy.x;
distPixely += fxy.y;
counter++;
}
lastFlowStepSections[index].x = distPixelx/counter;
lastFlowStepSections[index].y = distPixely/counter;
}
void rotateImage(const Mat &input, UMat &output, double roll, double pitch, double yaw,
double dx, double dy, double dz, double f, double cx, double cy)
{
// Camera Calibration Intrinsics Matrix
Mat A2 = (Mat_<double>(3,4) <<
f, 0, cx, 0,
0, f, cy, 0,
0, 0, 1, 0);
// Inverted Camera Calibration Intrinsics Matrix
Mat A1 = (Mat_<double>(4,3) <<
1/f, 0, -cx/f,
0, 1/f, -cy/f,
0, 0, 0,
0, 0, 1);
// Rotation matrices around the X, Y, and Z axis
Mat RX = (Mat_<double>(4, 4) <<
1, 0, 0, 0,
0, cos(roll), -sin(roll), 0,
0, sin(roll), cos(roll), 0,
0, 0, 0, 1);
Mat RY = (Mat_<double>(4, 4) <<
cos(pitch), 0, sin(pitch), 0,
0, 1, 0, 0,
-sin(pitch), 0, cos(pitch), 0,
0, 0, 0, 1);
Mat RZ = (Mat_<double>(4, 4) <<
cos(yaw), -sin(yaw), 0, 0,
sin(yaw), cos(yaw), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
// Translation matrix
Mat T = (Mat_<double>(4, 4) <<
1, 0, 0, dx,
0, 1, 0, dy,
0, 0, 1, dz,
0, 0, 0, 1);
// Compose rotation matrix with (RX, RY, RZ)
Mat R = RZ * RY * RX;
// Final transformation matrix
Mat H = A2 * (T * (R * A1));
// Apply matrix transformation
warpPerspective(input, output, H, input.size(), INTER_LANCZOS4);
}
void rotateFrame(const Mat &input, UMat &output, Mat &A , double roll, double pitch, double yaw){
Mat Rx = (Mat_<double>(3, 3) <<
1, 0, 0,
0, cos(roll), -sin(roll),
0, sin(roll), cos(roll));
Mat Ry = (Mat_<double>(3, 3) <<
cos(pitch), 0, sin(pitch),
0, 1, 0,
-sin(pitch), 0, cos(pitch));
Mat Rz = (Mat_<double>(3, 3) <<
cos(yaw), -sin(yaw), 0,
sin(yaw), cos(yaw), 0,
0, 0, 1);
Mat R = Rz*Ry*Rx;
Mat H = A*R*A.inv();
Mat T = (Mat_<double>(1,3) << WIDTH_RES/2, HEIGHT_RES/2, 1);
Mat W = H*T.t();
double bound_h = HEIGHT_RES*abs(cos(roll));
double bound_w = WIDTH_RES*abs(cos(pitch));
double dx = bound_w/2 - W.at<double>(0,0);
double dy = bound_h/2 - W.at<double>(0,1);
Mat temp = (Mat_<double>(3,3) << 1, 0, dx,
0, 1, dy,
0, 0, 1);
Mat trans = temp*H;
warpPerspective(input, output, H, input.size());
}
/* Calculate optical flow per section */
void *OpticalFlowPerSection(void *currSectionInfo)
{
Mat flow;
UMat uflow;
// 1. cast the input pointer to the desired format
sectionInfo *currSection = (sectionInfo *)currSectionInfo;
// 2. send to optical flow algorithm
calcOpticalFlowFarneback(currSection->prevFrameSection, currSection->frameSection, uflow, 0.5, 3/*def 3 */, 10/* def 15*/, 3, 3, 1.2 /* def 1.2*/, 0);
uflow.copyTo(flow);
float corners[4] = {0.09,0.91,0.09,0.91};
calcAvgOpticalFlowPerSection(flow, 16, currSection->index, corners);
return NULL;
}
/* calculate the location of the UAV according the input frame from the camera and the angles of the body*/
int opticalFlow(int source, MainWindow &w){
int tempX = 0;
int tempY = 0;
end_run = 0;
cout << "Capture from: " << endl << source << endl;
// capture from camera
VideoCapture cap(0);
if( !cap.isOpened() )
return -1;
//get fps of video
// double fps = cap.get(CV_CAP_PROP_FPS);
// Set Resolution - The Default Resolution Is 640 x 480
cap.set(CV_CAP_PROP_FRAME_WIDTH,WIDTH_RES);
cap.set(CV_CAP_PROP_FRAME_HEIGHT,HEIGHT_RES);
// cap.set(CV_CAP_PROP_CONVERT_RGB, 0);
/* set calibration parameters and variables for storing the current location */
/******************************************************************************/
Mat flow, cflow, undistortFrame, processedFrame, origFrame, croppedFrame;
Mat cameraMatrix = (Mat_<double>(3,3) <<
4.0902279881001898e+02, 0, 1.5975000000000000e+02,
0, 4.0902279881001898e+02, 1.1975000000000000e+02,
0, 0, 1
);
Mat distCoeffs = (Mat_<double>(5,1) <<
-3.4107530298867622e-02,
3.8417307420301627e-01,
0,
0,
-1.0237742683067783e+00
);
UMat gray, prevgray, uflow;
#ifdef VIDEO_ACTIVE
namedWindow("flow", WINDOW_NORMAL);
#endif
currLocation.x = 0;
currLocation.y = 0;
int i;
locationStruct predLocation;
double rovX, rovY; // range of view in both axis
/* open files for output data ***/
/******************************************************************************/
cout << "The local date and time is: " << currentTime << endl;
string locationFileName = "./outputs/" + currentTime + "location.txt";
string anglesFileName = "./outputs/" + currentTime + "angles.txt";
FILE * pLocationFile = fopen (locationFileName.c_str(), "w");
FILE * pAnglesFile = fopen (anglesFileName.c_str(), "w");
/* compare every two frames ***/
/******************************************************************************/
// the distorted function can't open the first frame- so ignore it
for(i = 0; i < 2; i++);
// for each frame calculate optical flow
for(;;)
{
// take out frame- still distorted
cap >> origFrame;
cvtColor(origFrame, processedFrame, COLOR_BGR2GRAY);
rotateImage(processedFrame, gray, eulerFromSensors.roll, eulerFromSensors.pitch, 0, 0, 0, 1, cameraMatrix.at<double>(0,0),
cameraMatrix.at<double>(0,2),cameraMatrix.at<double>(1,2));
if( !prevgray.empty() )
{
#ifdef VIDEO_ACTIVE
// show camera view
imshow("flow", prevgray);
#endif
// calculate flow per section
sectionInfo topLeft, topRight;
sectionInfo bottomLeft, bottomRight;
topLeft.frameSection = UMat(gray, Range(0.2*HEIGHT_RES,HEIGHT_RES*0.55), Range(0.2*WIDTH_RES,WIDTH_RES*0.55));
topLeft.prevFrameSection = UMat(prevgray, Range(0.2*HEIGHT_RES,HEIGHT_RES*0.55), Range(0.2*WIDTH_RES,WIDTH_RES*0.55));
topLeft.index = 0;
topRight.frameSection = UMat(gray, Range(0.2*HEIGHT_RES,HEIGHT_RES*0.55), Range(WIDTH_RES*0.45, WIDTH_RES*0.8));
topRight.prevFrameSection = UMat(prevgray, Range(0.2*HEIGHT_RES,HEIGHT_RES*0.55), Range(WIDTH_RES*0.45, WIDTH_RES*0.8));
topRight.index = 1;
bottomLeft.frameSection = UMat(gray, Range(0.45*HEIGHT_RES,HEIGHT_RES*0.8), Range(0.2*WIDTH_RES,WIDTH_RES*0.55));
bottomLeft.prevFrameSection = UMat(prevgray, Range(0.45*HEIGHT_RES,HEIGHT_RES*0.8), Range(0.2*WIDTH_RES,WIDTH_RES*0.55));
bottomLeft.index = 2;
bottomRight.frameSection = UMat(gray, Range(0.45*HEIGHT_RES,HEIGHT_RES*0.8), Range(WIDTH_RES*0.45, WIDTH_RES*0.8));
bottomRight.prevFrameSection = UMat(prevgray, Range(0.45*HEIGHT_RES,HEIGHT_RES*0.8), Range(WIDTH_RES*0.45, WIDTH_RES*0.8));
bottomRight.index = 3;
pthread_t topLeft_thread, topRight_thread;
pthread_t bottomLeft_thread, bottomRight_thread;
pthread_create(&topLeft_thread, NULL, OpticalFlowPerSection, &topLeft);
pthread_create(&topRight_thread, NULL, OpticalFlowPerSection, &topRight);
pthread_create(&bottomLeft_thread, NULL, OpticalFlowPerSection, &bottomLeft);
pthread_create(&bottomRight_thread, NULL, OpticalFlowPerSection, &bottomRight);
pthread_join(topLeft_thread, NULL);
pthread_join(topRight_thread, NULL);
pthread_join(bottomLeft_thread, NULL);
pthread_join(bottomRight_thread, NULL);
// merge the outputs
lastFlowStep.x = (lastFlowStepSections[0].x + lastFlowStepSections[1].x + lastFlowStepSections[2].x + lastFlowStepSections[3].x)/4;
lastFlowStep.y = (lastFlowStepSections[0].y + lastFlowStepSections[1].y + lastFlowStepSections[2].y + lastFlowStepSections[3].y)/4;
// get average
//#ifdef VIDEO_ACTIVE
// drawOptFlowMap(flow, prevgray, 16, 1.5, Scalar(0, 255, 0));
// imshow("flow", prevgray);
//#else
// calcAvgOpticalFlow(flow, 16);
//#endif
// calculate range of view - 2*tan(fov/2)*distance
#ifdef SONAR_ACTIVE
// currently dont take the median, take the last sample
rovX = 2*0.44523*100*distanceSonar*cos(eulerFromSensors.roll)*cos(eulerFromSensors.pitch);//*height.median; // 2 * tan(48/2) * dist(cm)
rovY = 2*0.32492*100*distanceSonar*cos(eulerFromSensors.roll)*cos(eulerFromSensors.pitch);//*height.median; // 2 * tan(36/2) * dist(cm)
#else
double dist=87/(cos(eulerFromSensors.roll)*cos(eulerFromSensors.pitch)); // distance from surface in cm
rovX = 2*0.44523*dist; // 2 * tan(48/2) * dist
rovY = 2*0.32492*dist; // 2 * tan(36/2) * dist
#endif
if(eulerSpeedChanged.load())
{
eulerSpeedChanged.store(false);
predLocation.x = ((eulerFromSensors.pitch-prevEulerFromSensors.pitch)*(180/PI)*WIDTH_RES) / 48;
predLocation.y = ((eulerFromSensors.roll-prevEulerFromSensors.roll)*(180/PI)*HEIGHT_RES) / 36;
cout << "Sonar with factor: " << distanceSonar << endl;
cout << "Yaw: " << eulerFromSensors.yaw << endl;
// calculate final x, y location
locationStruct locationCorrectionAfterYaw;
locationCorrectionAfterYaw.x = ((lastFlowStep.x + predLocation.x)/WIDTH_RES)*rovX;
locationCorrectionAfterYaw.y = ((lastFlowStep.y - predLocation.y)/HEIGHT_RES)*rovY;
#ifdef YAW_ACTIVE
locationCorrectionAfterYaw = calculateNewLocationByYaw(locationCorrectionAfterYaw);
#endif
currLocation.x -= locationCorrectionAfterYaw.x;
currLocation.y += locationCorrectionAfterYaw.y;
tempX += lastFlowStep.x;
tempY += lastFlowStep.y;
w.AngleCorrectionUpdate(tempX, tempY, -predLocation.x, predLocation.y);
tempX = 0;
tempY = 0;
}
else{
// calculate final x, y location
locationStruct locationCorrectionAfterYaw;
locationCorrectionAfterYaw.x = (lastFlowStep.x/WIDTH_RES)*rovX;
locationCorrectionAfterYaw.y = (lastFlowStep.y/HEIGHT_RES)*rovY;
#ifdef YAW_ACTIVE
locationCorrectionAfterYaw = calculateNewLocationByYaw(locationCorrectionAfterYaw);
#endif
// calculate final x, y location
currLocation.x -= locationCorrectionAfterYaw.x;
currLocation.y += locationCorrectionAfterYaw.y;
tempX += lastFlowStep.x;
tempY += lastFlowStep.y;
}
// output to files
fprintf(pLocationFile,"%f %f\n", currLocation.x, currLocation.y);
fprintf(pAnglesFile,"%f %f %f\n", eulerFromSensors.pitch*(180/PI), eulerFromSensors.roll*(180/PI), eulerFromSensors.yaw*(180/PI));
// Update Plots
w.UpdatePlot(currLocation.x,currLocation.y);
}
//break conditions
if(waitKey(1)>=0)
break;
if(end_run)
break;
std::swap(prevgray, gray);
}
#ifdef VIDEO_ACTIVE
destroyWindow("flow");
#endif
// close the files
fclose(pLocationFile);
fclose(pAnglesFile);
return 0;
}