forked from bijustin/YOLO-DynaSLAM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptical_flow_test.cpp
161 lines (151 loc) · 5.05 KB
/
optical_flow_test.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
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/video.hpp>
#include <opencv2/imgcodecs.hpp>
#include <fstream>
#include <chrono>
#include <thread>
#include <unistd.h>
using namespace cv;
using namespace std;
void LoadImages(const string &strFile, vector<string> &vstrImageFilenames,
vector<double> &vTimestamps);
int main(int argc, char **argv)
{
/*
const string about =
"This sample demonstrates Lucas-Kanade Optical Flow calculation.\n"
"The example file can be downloaded from:\n"
" https://www.bogotobogo.com/python/OpenCV_Python/images/mean_shift_tracking/slow_traffic_small.mp4";
const string keys =
"{ h help | | print this help message }"
"{ @image1 | | image1 for compare }"
"{ @image2 | | image2 for compare }";
CommandLineParser parser(argc, argv, keys);
parser.about(about);
if (parser.has("help"))
{
parser.printMessage();
return 0;
}
string filename1 = samples::findFile(parser.get<string>("@image1"));
Mat im1 = imread(filename1);
string filename2 = samples::findFile(parser.get<string>("@image2"));
Mat im2 = imread(filename2);
*/
// Retrieve paths to images
vector<string> vstrImageFilenames;
vector<double> vTimestamps;
string strFile = string(argv[1])+"/rgb.txt";
LoadImages(strFile, vstrImageFilenames, vTimestamps);
int nImages = vstrImageFilenames.size();
// Create some random colors
vector<Scalar> colors;
RNG rng;
for(int i = 0; i < 100; i++)
{
int r = rng.uniform(0, 256);
int g = rng.uniform(0, 256);
int b = rng.uniform(0, 256);
colors.push_back(Scalar(r,g,b));
}
cv::Mat im1, im2;
for (int ni = 0; ni < nImages-1; ni++) {
// Read image from file
im1 = cv::imread(string(argv[1])+"/"+vstrImageFilenames[ni],CV_LOAD_IMAGE_UNCHANGED);
double tframe = vTimestamps[ni];
im2 = cv::imread(string(argv[1])+"/"+vstrImageFilenames[ni+1],CV_LOAD_IMAGE_UNCHANGED);
if(im1.empty())
{
cerr << endl << "Failed to load image at: "
<< string(argv[1]) << "/" << vstrImageFilenames[ni] << endl;
return 1;
}
if(im2.empty())
{
cerr << endl << "Failed to load image at: "
<< string(argv[1]) << "/" << vstrImageFilenames[ni+1] << endl;
return 1;
}
/*
#ifdef COMPILEDWITHC11
std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now();
#else
std::chrono::monotonic_clock::time_point t1 = std::chrono::monotonic_clock::now();
#endif
*/
Mat im1_gray;
vector<Point2f> p0, p1;
// Take first frame and find corners in it
cvtColor(im1, im1_gray, COLOR_BGR2GRAY);
goodFeaturesToTrack(im1_gray, p0, 100, 0.3, 7, Mat(), 7, false, 0.04);
// Create a mask image for drawing purposes
Mat mask = Mat::zeros(im1.size(), im1.type());
//while(true){
Mat im2_gray;
if (im2.empty())
break;
cvtColor(im2, im2_gray, COLOR_BGR2GRAY);
// calculate optical flow
vector<uchar> status;
vector<float> err;
TermCriteria criteria = TermCriteria((TermCriteria::COUNT) + (TermCriteria::EPS), 10, 0.03);
calcOpticalFlowPyrLK(im1_gray, im2_gray, p0, p1, status, err, Size(15,15), 2, criteria);
vector<Point2f> good_new;
for(uint i = 0; i < p0.size(); i++)
{
// Select good points
if(status[i] == 1) {
good_new.push_back(p1[i]);
// draw the tracks
line(mask,p1[i], p0[i], colors[i], 2);
circle(im2, p1[i], 5, colors[i], -1);
}
}
Mat img;
add(im2, mask, img);
imshow("Frame", img);
//imwrite("/result/"+to_string(ni)+".png", img);
cout << ni << ", " << img.size() << endl;
//int keyboard = waitKey(30);
//if (keyboard == 'q' || keyboard == 27)
// break;
// Now update the previous frame and previous points
im1_gray = im2_gray.clone();
p0 = good_new;
//}
//sleep(0.1);
waitKey(100);
}
int keyboard = waitKey(30);
return 0;
}
void LoadImages(const string &strFile, vector<string> &vstrImageFilenames, vector<double> &vTimestamps)
{
ifstream f;
f.open(strFile.c_str());
// skip first three lines
string s0;
getline(f,s0);
getline(f,s0);
getline(f,s0);
while(!f.eof())
{
string s;
getline(f,s);
if(!s.empty())
{
stringstream ss;
ss << s;
double t;
string sRGB;
ss >> t;
vTimestamps.push_back(t);
ss >> sRGB;
vstrImageFilenames.push_back(sRGB);
}
}
}