-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdenseFlow_gpu.cpp
More file actions
133 lines (115 loc) · 4.06 KB
/
denseFlow_gpu.cpp
File metadata and controls
133 lines (115 loc) · 4.06 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
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
#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/gpu/gpu.hpp"
#include <stdio.h>
#include <iostream>
using namespace cv;
using namespace cv::gpu;
static void convertFlowToImage(const Mat &flow_x, const Mat &flow_y, Mat &img_x, Mat &img_y,
double lowerBound, double higherBound) {
#define CAST(v, L, H) ((v) > (H) ? 255 : (v) < (L) ? 0 : cvRound(255*((v) - (L))/((H)-(L))))
for (int i = 0; i < flow_x.rows; ++i) {
for (int j = 0; j < flow_y.cols; ++j) {
float x = flow_x.at<float>(i,j);
float y = flow_y.at<float>(i,j);
img_x.at<uchar>(i,j) = CAST(x, lowerBound, higherBound);
img_y.at<uchar>(i,j) = CAST(y, lowerBound, higherBound);
}
}
#undef CAST
}
static void drawOptFlowMap(const Mat& flow, Mat& cflowmap, int step,double, const Scalar& color){
for(int y = 0; y < cflowmap.rows; y += step)
for(int x = 0; x < cflowmap.cols; 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);
}
}
int main(int argc, char** argv){
// IO operation
const char* keys =
{
"{ f | frameDir | imgs | directory path of frames }"
"{ n | frameNum | 100 | count of frames }"
"{ x | xFlowFile | flow_x | filename of flow x component }"
"{ y | yFlowFile | flow_y | filename of flow x component }"
"{ b | bound | 15 | specify the maximum of optical flow}"
"{ t | type | 0 | specify the optical flow algorithm }"
"{ d | device_id | 0 | set gpu id}"
"{ s | step | 1 | specify the step for frame sampling}"
};
CommandLineParser cmd(argc, argv, keys);
string frameDir = cmd.get<string>("frameDir");
int total = cmd.get<int>("frameNum");
string xFlowFile = cmd.get<string>("xFlowFile");
string yFlowFile = cmd.get<string>("yFlowFile");
int bound = cmd.get<int>("bound");
int type = cmd.get<int>("type");
int device_id = cmd.get<int>("device_id");
int step = cmd.get<int>("step");
int frame_num = 0;
Mat image, prev_image, prev_grey, grey, frame, flow_x, flow_y;
GpuMat frame_0, frame_1, flow_u, flow_v;
setDevice(device_id);
FarnebackOpticalFlow alg_farn;
OpticalFlowDual_TVL1_GPU alg_tvl1;
BroxOpticalFlow alg_brox(0.197f, 50.0f, 0.8f, 10, 77, 10);
while(frame_num < total) {
char tmp[20];
sprintf(tmp,"img_%05d.jpg",int(frame_num));
frame = imread(frameDir + tmp, CV_LOAD_IMAGE_GRAYSCALE);
if(!frame.data) // Check for invalid input
{
std::cout << "File Number Error:" << frameDir + tmp << std::endl ;
return -1;
}
if(frame_num == 0) {
grey.create(frame.size(), CV_8UC1);
prev_grey.create(frame.size(), CV_8UC1);
frame.copyTo(prev_grey);
frame_num += step;
continue;
}
frame.copyTo(grey);
// Mat prev_grey_, grey_;
// resize(prev_grey, prev_grey_, Size(453, 342));
// resize(grey, grey_, Size(453, 342));
frame_0.upload(prev_grey);
frame_1.upload(grey);
// GPU optical flow
switch(type){
case 0:
alg_farn(frame_0,frame_1,flow_u,flow_v);
break;
case 1:
alg_tvl1(frame_0,frame_1,flow_u,flow_v);
break;
case 2:
GpuMat d_frame0f, d_frame1f;
frame_0.convertTo(d_frame0f, CV_32F, 1.0 / 255.0);
frame_1.convertTo(d_frame1f, CV_32F, 1.0 / 255.0);
alg_brox(d_frame0f, d_frame1f, flow_u,flow_v);
break;
}
flow_u.download(flow_x);
flow_v.download(flow_y);
// Output optical flow
Mat imgX(flow_x.size(),CV_8UC1);
Mat imgY(flow_y.size(),CV_8UC1);
convertFlowToImage(flow_x,flow_y, imgX, imgY, -bound, bound);
sprintf(tmp,"_%05d.jpg",int(frame_num));
// Mat imgX_, imgY_, image_;
// resize(imgX,imgX_,cv::Size(340,256));
// resize(imgY,imgY_,cv::Size(340,256));
// resize(image,image_,cv::Size(340,256));
imwrite(xFlowFile + tmp,imgX);
imwrite(yFlowFile + tmp,imgY);
std::swap(prev_grey, grey);
frame_num = frame_num + step;
}
return 0;
}