-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathevaluate.cc
363 lines (309 loc) · 9.18 KB
/
evaluate.cc
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
#include<chrono>
#include<mutex>
#include<ros/ros.h>
#include<rosbag/bag.h>
#include<rosbag/chunked_file.h>
#include<rosbag/view.h>
#include<rosbag/query.h>
#include <stdio.h>
#include "eigen3/Eigen/Dense"
#include "opencv2/opencv.hpp"
#include <boost/filesystem.hpp>
#include "sys/types.h"
#include "sys/sysinfo.h"
#include "sensor_msgs/Imu.h"
#include "sensor_msgs/Image.h"
#include "src/tools/transport_util.h"
#include "geometry_msgs/PoseStamped.h"
#include "motion_stereo/stereo_mapper.h"
#include "src/tools/tic_toc.h"
const int downSampleTimes = 0 ;
using namespace std;
const int aggNum = 3 ;
const int interval = 6 ;
const int candidateNum = aggNum*interval + 10 ;
float maxDepth = 20.0 ;
float minDepth = 0.2 ;
//TUM
//float fx = 525.0 ;
//float fy = 525.0 ;
//float cx = 319.5 ;
//float cy = 239.5 ;
//ICL-NUIM
float fx = 481.2017 ;
float fy = -480.0002 ;
float cx = 319.5 ;
float cy = 239.5 ;
ros::Publisher pub_depth ;
class ImageMeasurement
{
public:
ros::Time t;
cv::Mat image;
ImageMeasurement(const ros::Time& _t, const cv::Mat& _image)
{
t = _t;
image = _image.clone();
}
ImageMeasurement(const ImageMeasurement& i)
{
t = i.t;
image = i.image.clone();
}
~ImageMeasurement() { ;}
};
struct PoseElement
{
ros::Time t;
double tx,ty,tz;
double qx,qy,qz,qw;
};
std::list<ImageMeasurement> imageBuf;
std::mutex mMutexImg;
std::list<PoseElement> poseBuf;
std::mutex mMutexPose;
StereoMapper motion_stereo_mapper;
void GrabImage(const sensor_msgs::ImageConstPtr& msg)
{
cv::Mat img ;
img = matFromImage(*msg);
//puts("recieved img") ;
unique_lock<mutex> lock(mMutexImg) ;
ros::Time tImage = msg->header.stamp;
imageBuf.push_front(ImageMeasurement(tImage, img));
}
void GrabPose(const geometry_msgs::PoseStamped& pose)
{
PoseElement tt ;
tt.qw = pose.pose.orientation.w ;
tt.qx = pose.pose.orientation.x ;
tt.qy = pose.pose.orientation.y ;
tt.qz = pose.pose.orientation.z ;
tt.tx = pose.pose.position.x;
tt.ty = pose.pose.position.y;
tt.tz = pose.pose.position.z;
tt.t = pose.header.stamp;
unique_lock<mutex> lock(mMutexPose) ;
poseBuf.push_front(tt);
}
int lastSize;
struct meshingFrame
{
cv::Mat img ;
Eigen::Matrix3d R;
Eigen::Vector3d t;
};
static std::vector<meshingFrame> toAggNonKFs ;
void depthUp(cv::Mat& in, cv::Mat& out)
{
cv::Mat tmp = cv::Mat(in.rows*2, in.cols*2, in.type() ) ;
for( int i = 0 ; i < in.rows; i++ )
{
for( int j = 0; j < in.cols; j++ )
{
tmp.at<float>((i<<1), (j<<1)) = in.at<float>(i, j) ;
tmp.at<float>((i<<1)+1, (j<<1)) = in.at<float>(i, j) ;
tmp.at<float>((i<<1), (j<<1)+1) = in.at<float>(i, j) ;
tmp.at<float>((i<<1)+1, (j<<1)+1) = in.at<float>(i, j) ;
}
}
out = tmp.clone() ;
}
void processImg()
{
std::list<ImageMeasurement>::iterator iterImg, iterImg_pre ;
std::list<PoseElement>::iterator iterPose, iterPose_pre ;
cv::Mat curImg, img ;
Eigen::Quaterniond q_R ;
Eigen::Matrix3d cur_R ;
Eigen::Vector3d cur_t ;
cv::Mat cv_R_l, cv_T_l;
Eigen::Matrix3d R ;
Eigen::Vector3d t ;
cv::Mat cv_R_r, cv_T_r;
bool findPose ;
bool flag = false ;
std_msgs::Header head ;
head.frame_id = "world" ;
{
unique_lock<mutex> lock_0(mMutexImg) ;
unique_lock<mutex> lock_1(mMutexPose) ;
if ( imageBuf.size() <= lastSize )
{
return ;
}
if ( flag == false ){
iterImg = imageBuf.begin() ;
iterPose = poseBuf.begin() ;
}
else{
iterImg = imageBuf.begin() ;
iterPose = poseBuf.begin() ;
// iterImg = iterImg_pre ;
// iterPose = iterPose_pre ;
}
if ( iterPose->t < iterImg->t ){
return ;
}
curImg = iterImg->image.clone() ;
head.stamp = iterImg->t ;
for( int i = 0 ; i < downSampleTimes; i++ ){
cv::pyrDown(curImg, curImg, cv::Size(curImg.cols/2, curImg.rows/2) ) ;
}
findPose = false ;
while ( iterPose != poseBuf.end() )
{
if ( iterPose->t != iterImg->t ){
iterPose++ ;
}
else
{
q_R.x() = iterPose->qx ;
q_R.y() = iterPose->qy ;
q_R.z() = iterPose->qz ;
q_R.w() = iterPose->qw ;
cur_R = q_R.toRotationMatrix();
cur_t << iterPose->tx, iterPose->ty, iterPose->tz ;
findPose = true ;
break ;
}
}
if ( findPose == false )
{
ROS_WARN("can not find pose for the current Img") ;
return ;
}
iterImg_pre = iterImg ;
iterPose_pre = iterPose ;
flag = true ;
for( int i = 0 ; i < aggNum; i++ )
{
iterImg = std::next(iterImg, interval ) ;
iterPose = std::next(iterPose, interval ) ;
q_R.x() = iterPose->qx ;
q_R.y() = iterPose->qy ;
q_R.z() = iterPose->qz ;
q_R.w() = iterPose->qw ;
R = q_R.toRotationMatrix();
t << iterPose->tx, iterPose->ty, iterPose->tz ;
toAggNonKFs[i].img = iterImg->image.clone() ;
for( int j = 0 ; j < downSampleTimes; j++ )
{
cv::pyrDown(toAggNonKFs[i].img, toAggNonKFs[i].img,
cv::Size(toAggNonKFs[i].img.cols/2, toAggNonKFs[i].img.rows/2) ) ;
}
//cv::imshow(std::to_string(i), toAggNonKFs[i].img ) ;
toAggNonKFs[i].R = R ;
toAggNonKFs[i].t = t ;
}
lastSize++ ;
imageBuf.pop_back();
poseBuf.pop_back();
}
cv::imshow("curImg", curImg ) ;
// cv::waitKey(0) ;
TicToc tc_sgm ;
cv::eigen2cv(cur_R, cv_R_l);
cv::eigen2cv(cur_t, cv_T_l);
motion_stereo_mapper.initReference(curImg);
for( int i = 0 ; i < aggNum; i++ )
{
img = toAggNonKFs[i].img.clone() ;
R = toAggNonKFs[i].R ;
t = toAggNonKFs[i].t ;
cv::eigen2cv(R, cv_R_r);
cv::eigen2cv(t, cv_T_r);
motion_stereo_mapper.update(img, cv_R_l, cv_T_l, cv_R_r, cv_T_r);
}
cv::Mat curDepth ;
motion_stereo_mapper.outputFusionCPU(curDepth, cur_R, cur_t, fx, fy, cx, cy) ;
for( int j = 0 ; j < downSampleTimes; j++ )
{
depthUp(curDepth, curDepth) ;
//cv::pyrUp(curDepth, curDepth, cv::Size(curDepth.cols*2, curDepth.rows*2) ) ;
}
static double sum_time = 0 ;
static int sum_cnt = 0 ;
sum_time += tc_sgm.toc() ;
sum_cnt++ ;
ROS_WARN("AVERAE CAL TIME %lf", sum_time/sum_cnt );
sensor_msgs::Image msg_img ;
toImageMsg(msg_img, curDepth, head, "32FC1") ;
pub_depth.publish(msg_img) ;
for( int i = 0 ; i < curDepth.rows; i++ )
{
for( int j=0; j < curDepth.cols; j++ )
{
if ( curDepth.at<float>(i, j) < minDepth ) {
curDepth.at<float>(i, j) = 0 ;
}
if ( curDepth.at<float>(i, j) > maxDepth ){
curDepth.at<float>(i, j) = 0 ;
}
}
}
static cv::Mat color_disp, disp_depth;
//disp_depth = curDepth/maxDepth*255;
//disp_depth.convertTo(disp_depth, CV_8U);
cv::normalize(curDepth, disp_depth, 0, 255, CV_MINMAX, CV_8U);
cv::applyColorMap(disp_depth, color_disp, cv::COLORMAP_RAINBOW);
for( int i = 0 ; i < curDepth.rows; i++ )
{
for( int j=0; j < curDepth.cols; j++ )
{
if ( curDepth.at<float>(i, j) < 0.001 ) {
color_disp.at<cv::Vec3b>(i, j)[0] = 0 ;
color_disp.at<cv::Vec3b>(i, j)[1] = 0 ;
color_disp.at<cv::Vec3b>(i, j)[2] = 0 ;
}
}
}
cv::imshow("Current depth", color_disp ) ;
cv::waitKey(1) ;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "mapping");
if ( argc < 2 ){
CASE = 0 ;
}
else {
sscanf(argv[1], "%d", &CASE ) ;
}
ros::start();
ros::NodeHandle nh("~") ;
ros::console::set_logger_level(ROSCONSOLE_DEFAULT_NAME, ros::console::levels::Warn);
string packagePath = ros::package::getPath("mapping");
ros::Subscriber sub_img = nh.subscribe("/image", 100, &GrabImage ) ;
ros::Subscriber sub_pose = nh.subscribe("/cur_pose", 100, &GrabPose ) ;
pub_depth = nh.advertise<sensor_msgs::Image>("/depth", 1000 );
ROS_WARN("CASE=%d", CASE ) ;
for( int i = 0 ; i < downSampleTimes; i++ ){
fx /= 2 ;
fy /= 2 ;
cx = (cx+0.5)/2.0 - 0.5;
cy = (cy+0.5)/2.0 - 0.5;
}
cv::Mat K(3, 3, CV_64F) ;
K.setTo(0.0) ;
K.at<double>(0, 0) = fx ;
K.at<double>(1, 1) = fy ;
K.at<double>(0, 2) = cx ;
K.at<double>(1, 2) = cy ;
K.at<double>(2, 2) = 1.0 ;
float bf = 0.02*fx ;
float dep_sample = 1.0f / (0.15 * 160.0);
motion_stereo_mapper.initIntrinsic( K, bf, dep_sample );
lastSize = aggNum*interval ;
toAggNonKFs.clear();
toAggNonKFs.resize(aggNum+5);
ros::Rate r(1000) ;
while( ros::ok() )
{
processImg();
ros::spinOnce();
r.sleep();
}
ros::shutdown();
return 0;
}