-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcamFusion_Student.cpp
367 lines (297 loc) · 13.6 KB
/
camFusion_Student.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
#include <iostream>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "camFusion.hpp"
#include "dataStructures.h"
using namespace std;
// Create groups of Lidar points whose projection into the camera falls into the same bounding box
void clusterLidarWithROI(std::vector<BoundingBox> & boundingBoxes,
std::vector<LidarPoint> & lidarPoints,
float shrinkFactor,
cv::Mat & P_rect_xx,
cv::Mat & R_rect_xx,
cv::Mat & RT)
{
// loop over all Lidar points and associate them to a 2D bounding box
cv::Mat X(4, 1, cv::DataType<double>::type);
cv::Mat Y(3, 1, cv::DataType<double>::type);
for(auto it1 = lidarPoints.begin(); it1 != lidarPoints.end(); ++it1)
{
// assemble vector for matrix-vector-multiplication
X.at<double>(0, 0) = it1->x;
X.at<double>(1, 0) = it1->y;
X.at<double>(2, 0) = it1->z;
X.at<double>(3, 0) = 1;
// project Lidar point into camera
Y = P_rect_xx * R_rect_xx * RT * X;
cv::Point pt;
// pixel coordinates
pt.x = Y.at<double>(0, 0) / Y.at<double>(2, 0);
pt.y = Y.at<double>(1, 0) / Y.at<double>(2, 0);
vector<vector<BoundingBox>::iterator> enclosingBoxes; // pointers to all bounding boxes which enclose the current Lidar point
for(vector<BoundingBox>::iterator it2 = boundingBoxes.begin(); it2 != boundingBoxes.end(); ++it2)
{
// shrink current bounding box slightly to avoid having too many outlier points around the edges
cv::Rect smallerBox;
smallerBox.x = (*it2).roi.x + shrinkFactor * (*it2).roi.width / 2.0;
smallerBox.y = (*it2).roi.y + shrinkFactor * (*it2).roi.height / 2.0;
smallerBox.width = (*it2).roi.width * (1 - shrinkFactor);
smallerBox.height = (*it2).roi.height * (1 - shrinkFactor);
// check wether point is within current bounding box
if(smallerBox.contains(pt))
{
enclosingBoxes.push_back(it2);
}
} // eof loop over all bounding boxes
// check wether point has been enclosed by one or by multiple boxes
if(enclosingBoxes.size() == 1)
{
// add Lidar point to bounding box
enclosingBoxes[0]->lidarPoints.push_back(*it1);
}
} // eof loop over all Lidar points
}
/*
* The show3DObjects() function below can handle different output image sizes, but the text output has been manually tuned to fit the 2000x2000 size.
* However, you can make this function work for other sizes too.
* For instance, to use a 1000x1000 size, adjusting the text positions by dividing them by 2.
*/
void show3DObjects(vector<BoundingBox> & boundingBoxes,
cv::Size worldSize,
cv::Size imageSize,
bool bWait,
ResultLineItem & result,
const string & detector,
const string & descriptor)
{
// create topview image
cv::Mat topviewImg(imageSize, CV_8UC3, cv::Scalar(255, 255, 255));
for(auto it1 = boundingBoxes.begin(); it1 != boundingBoxes.end(); ++it1)
{
// create randomized color for current 3D object
cv::RNG rng(it1->boxID);
cv::Scalar currColor = cv::Scalar(rng.uniform(0, 150), rng.uniform(0, 150), rng.uniform(0, 150));
// plot Lidar points into top view image
int top = 1e8, left = 1e8, bottom = 0.0, right = 0.0;
float xwmin = 1e8, ywmin = 1e8, ywmax = -1e8;
for(auto it2 = it1->lidarPoints.begin(); it2 != it1->lidarPoints.end(); ++it2)
{
// world coordinates
float xw = (*it2).x; // world position in m with x facing forward from sensor
float yw = (*it2).y; // world position in m with y facing left from sensor
xwmin = xwmin < xw ? xwmin : xw;
ywmin = ywmin < yw ? ywmin : yw;
ywmax = ywmax > yw ? ywmax : yw;
// top-view coordinates
int y = (-xw * imageSize.height / worldSize.height) + imageSize.height;
int x = (-yw * imageSize.width / worldSize.width) + imageSize.width / 2;
// find enclosing rectangle
top = top < y ? top : y;
left = left < x ? left : x;
bottom = bottom > y ? bottom : y;
right = right > x ? right : x;
// draw individual point
cv::circle(topviewImg, cv::Point(x, y), 4, currColor, -1);
}
// draw enclosing rectangle
cv::rectangle(topviewImg, cv::Point(left, top), cv::Point(right, bottom), cv::Scalar(0, 0, 0), 2);
// augment object with some key data
char str1[200], str2[200];
sprintf(str1, "id=%d, #pts=%d", it1->boxID, (int) it1->lidarPoints.size());
putText(topviewImg, str1, cv::Point2f(left - 250, bottom + 50), cv::FONT_ITALIC, 2, currColor);
sprintf(str2, "xmin=%2.2f m, yw=%2.2f m", xwmin, ywmax - ywmin);
putText(topviewImg, str2, cv::Point2f(left - 250, bottom + 125), cv::FONT_ITALIC, 2, currColor);
}
// plot distance markers
float lineSpacing = 2.0; // gap between distance markers
int nMarkers = floor(worldSize.height / lineSpacing);
for(size_t i = 0; i < nMarkers; ++i)
{
int y = (-(i * lineSpacing) * imageSize.height / worldSize.height) + imageSize.height;
cv::line(topviewImg, cv::Point(0, y), cv::Point(imageSize.width, y), cv::Scalar(255, 0, 0));
}
// display image
string windowName = "3D Objects";
if(false)
{
cv::namedWindow(windowName, 1);
cv::imshow(windowName, topviewImg);
}
if(bWait)
{
cv::waitKey(0); // wait for key to be pressed
}
string outputFilename = "../" + GetLidarFilename(detector, descriptor, result.frame);
cv::imwrite(outputFilename, topviewImg);
}
// associate a given bounding box with the keypoints it contains
void clusterKptMatchesWithROI(BoundingBox & boundingBox,
std::vector<cv::KeyPoint> & kptsPrev,
std::vector<cv::KeyPoint> & kptsCurr,
std::vector<cv::DMatch> & kptMatches)
{
std::vector<double> euclideanDistance;
for(auto it = kptMatches.begin(); it != kptMatches.end(); it++)
{
int currKptIndex = (*it).trainIdx;
const auto & currKeyPoint = kptsCurr[currKptIndex];
if(boundingBox.roi.contains(currKeyPoint.pt))
{
int prevKptIndex = (*it).queryIdx;
const auto & prevKeyPoint = kptsPrev[prevKptIndex];
euclideanDistance.push_back(cv::norm(currKeyPoint.pt - prevKeyPoint.pt));
}
}
int pair_num = euclideanDistance.size();
double euclideanDistanceMean = std::accumulate(euclideanDistance.begin(), euclideanDistance.end(), 0.0) / pair_num;
for(auto it = kptMatches.begin(); it != kptMatches.end(); it++)
{
int currKptIndex = (*it).trainIdx;
const auto & currKeyPoint = kptsCurr[currKptIndex];
if(boundingBox.roi.contains(currKeyPoint.pt))
{
int prevKptIndex = (*it).queryIdx;
const auto & prevKeyPoint = kptsPrev[prevKptIndex];
double temp = cv::norm(currKeyPoint.pt - prevKeyPoint.pt);
double euclideanDistanceMean_Augment = euclideanDistanceMean * 1.3;
if(temp < euclideanDistanceMean_Augment)
{
boundingBox.keypoints.push_back(currKeyPoint);
boundingBox.kptMatches.push_back(*it);
}
}
}
}
// Compute time-to-collision (TTC) based on keypoint correspondences in successive images
void computeTTCCamera(std::vector<cv::KeyPoint> & kptsPrev,
std::vector<cv::KeyPoint> & kptsCurr,
std::vector<cv::DMatch> kptMatches,
double frameRate,
double & TTC,
cv::Mat *visImg)
{
// compute distance ratios between all matched keypoints
vector<double> distRatios; // stores the distance ratios for all keypoints between curr. and prev. frame
for(auto it1 = kptMatches.begin(); it1 != kptMatches.end() - 1; ++it1)
{ // outer keypoint loop
// get current keypoint and its matched partner in the prev. frame
cv::KeyPoint kpOuterCurr = kptsCurr.at(it1->trainIdx);
cv::KeyPoint kpOuterPrev = kptsPrev.at(it1->queryIdx);
for(auto it2 = kptMatches.begin() + 1; it2 != kptMatches.end(); ++it2)
{ // inner keypoint loop
double minDist = 100.0; // min. required distance
// get next keypoint and its matched partner in the prev. frame
cv::KeyPoint kpInnerCurr = kptsCurr.at(it2->trainIdx);
cv::KeyPoint kpInnerPrev = kptsPrev.at(it2->queryIdx);
// compute distances and distance ratios
double distCurr = cv::norm(kpOuterCurr.pt - kpInnerCurr.pt);
double distPrev = cv::norm(kpOuterPrev.pt - kpInnerPrev.pt);
if(distPrev > std::numeric_limits<double>::epsilon() && distCurr >= minDist)
{ // avoid division by zero
double distRatio = distCurr / distPrev;
distRatios.push_back(distRatio);
}
} // eof inner loop over all matched kpts
} // eof outer loop over all matched kpts
// only continue if list of distance ratios is not empty
if(distRatios.size() == 0)
{
TTC = NAN;
return;
}
// compute camera-based TTC from distance ratios
//double meanDistRatio = std::accumulate(distRatios.begin(), distRatios.end(), 0.0) / distRatios.size();
std::sort(distRatios.begin(), distRatios.end());
long medIndex = floor(distRatios.size() / 2.0);
double medDistRatio = distRatios.size() % 2 == 0 ? (distRatios[medIndex - 1] + distRatios[medIndex]) / 2.0
: distRatios[medIndex]; // compute median dist. ratio to remove outlier influence
double dT = 1 / frameRate;
TTC = -dT / (1 - medDistRatio);
}
void computeTTCLidar(std::vector<LidarPoint> & lidarPointsPreviousFrame,
std::vector<LidarPoint> & lidarPointsCurrentFrame,
double frameRate,
double & TTC)
{
// Sort ascending on the x coordinate only
std::sort(lidarPointsPreviousFrame.begin(), lidarPointsPreviousFrame.end(), [](LidarPoint a, LidarPoint b) {
return a.x < b.x;
}
);
std::sort(lidarPointsCurrentFrame.begin(), lidarPointsCurrentFrame.end(), [](LidarPoint a, LidarPoint b) {
return a.x < b.x;
}
);
/**
Using the constant-velocity model (as opposed to a constant-acceleration model)
TTC = d1 * dt / (d0 - d1);
where:
d0: the previous frame's closing distance (front-to-rear bumper)
d1: the current frame's closing distance (front-to-rear bumper)
dt: (delta t) the time elapsed between images (1 / frameRate)
Note: this function does not take into account the distance from the lidar origin to the front bumper of our vehicle.
It also does not account for the curvature or protrusions from the rear bumper of the preceding vehicle.
*/
double d0 = lidarPointsPreviousFrame[lidarPointsPreviousFrame.size() / 2].x;
double d1 = lidarPointsCurrentFrame[lidarPointsCurrentFrame.size() / 2].x;
double dt = 1.0 / frameRate;
TTC = d1 * dt / (d0 - d1);
}
void matchBoundingBoxes(std::vector<cv::DMatch> & matches,
std::map<int, int> & boundingBoxBestMatches,
DataFrame & previousFrame,
DataFrame & currentFrame)
{
std::multimap<int, int> boundingBoxMatches{}; // A pair of IDs to track bounding boxes
for(const auto & match : matches)
{
cv::KeyPoint keypointsPreviousFrame = previousFrame.keypoints[match.queryIdx];
cv::KeyPoint keypointsCurrentFrame = currentFrame.keypoints[match.trainIdx];
unsigned int boxIdPreviousFrame;
unsigned int boxIdCurrentFrame;
for(const auto & boundingBox : previousFrame.boundingBoxes)
{
if(boundingBox.roi.contains(keypointsPreviousFrame.pt))
{
boxIdPreviousFrame = boundingBox.boxID;
}
}
for(const auto & boundingBox : currentFrame.boundingBoxes)
{
if(boundingBox.roi.contains(keypointsCurrentFrame.pt))
{
boxIdCurrentFrame = boundingBox.boxID;
}
}
boundingBoxMatches.insert({boxIdCurrentFrame, boxIdPreviousFrame});
}
vector<int> boundingBoxIdsCurrentFrame{};
for(const auto & boundingBox : currentFrame.boundingBoxes)
{
boundingBoxIdsCurrentFrame.push_back(boundingBox.boxID);
}
for(const int boxIdCurrentFrame : boundingBoxIdsCurrentFrame)
{
auto it = boundingBoxMatches.equal_range(boxIdCurrentFrame);
unordered_map<int, int> boundingBoxIdMatches;
for(auto itr = it.first; itr != it.second; ++itr)
{
boundingBoxIdMatches[itr->second]++;
}
// find the max frequency
unsigned int maxBoxID = 0;
int matchingBoxID = -1;
for(const auto & boxIdMatch : boundingBoxIdMatches)
{
if(maxBoxID < boxIdMatch.second)
{
matchingBoxID = boxIdMatch.first;
maxBoxID = boxIdMatch.second;
}
}
boundingBoxBestMatches.insert({matchingBoxID, boxIdCurrentFrame});
}
}