forked from ivipsourcecode/DS-SLAM
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSegment.cc
135 lines (117 loc) · 4.29 KB
/
Segment.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
/*
*--------------------------------------------------------------------------------------------------
* DS-SLAM: A Semantic Visual SLAM towards Dynamic Environments
* Author(s):
* Chao Yu, Zuxin Liu, Xinjun Liu, Fugui Xie, Yi Yang, Qi Wei, Fei Qiao [email protected]
* Created by Yu [email protected]
* --------------------------------------------------------------------------------------------------
* DS-SLAM is a optimized SLAM system based on the famous ORB-SLAM2. If you haven't learn ORB_SLAM2 code,
* you'd better to be familiar with ORB_SLAM2 project first. Compared to ORB_SLAM2,
* we add anther two threads including semantic segmentation thread and densemap creation thread.
* You should pay attention to Frame.cc, ORBmatcher.cc, Pointcloudmapping.cc and Segment.cc.
*
* @article{murORB2,
* title={{ORB-SLAM2}: an Open-Source {SLAM} System for Monocular, Stereo and {RGB-D} Cameras},
* author={Mur-Artal, Ra\'ul and Tard\'os, Juan D.},
* journal={IEEE Transactions on Robotics},
* volume={33},
* number={5},
* pages={1255--1262},
* doi = {10.1109/TRO.2017.2705103},
* year={2017}
* }
* --------------------------------------------------------------------------------------------------
* Copyright (C) 2018, iVip Lab @ EE, THU (https://ivip-tsinghua.github.io/iViP-Homepage/) and
* Advanced Mechanism and Roboticized Equipment Lab. All rights reserved.
*
* Licensed under the GPLv3 License;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://github.com/ivipsourcecode/DS-SLAM/blob/master/LICENSE
*--------------------------------------------------------------------------------------------------
*/
#include "Segment.h"
#include "Tracking.h"
#include "Camera.h"
#include <fstream>
#define SKIP_NUMBER 1
using namespace std;
namespace ORB_SLAM2
{
Segment::Segment(const string &pascal_prototxt, const string &pascal_caffemodel, const string &pascal_png):mbFinishRequested(false),mSkipIndex(SKIP_NUMBER),mSegmentTime(0),imgIndex(0)
{
model_file = pascal_prototxt;
trained_file = pascal_caffemodel;
LUT_file = pascal_png;
label_colours = cv::imread(LUT_file,1);
cv::cvtColor(label_colours, label_colours, CV_RGB2BGR);
mImgSegmentLatest=cv::Mat(Camera::height,Camera::width,CV_8UC1);
mbNewImgFlag=false;
}
void Segment::SetTracker(Tracking *pTracker)
{
mpTracker=pTracker;
}
bool Segment::isNewImgArrived()
{
unique_lock<mutex> lock(mMutexGetNewImg);
if(mbNewImgFlag)
{
mbNewImgFlag=false;
return true;
}
else
return false;
}
void Segment::Run()
{
classifier=new Classifier(model_file, trained_file);
cout << "Load model ..."<<endl;
while(1)
{
usleep(1);
if(!isNewImgArrived())
continue;
cout << "Wait for new RGB img time =" << endl;
if(mSkipIndex==SKIP_NUMBER)
{
std::chrono::steady_clock::time_point t3 = std::chrono::steady_clock::now();
// Recognise by Semantin segmentation
mImgSegment=classifier->Predict(mImg, label_colours);
mImgSegment_color = mImgSegment.clone();
cv::cvtColor(mImgSegment,mImgSegment_color, CV_GRAY2BGR);
LUT(mImgSegment_color, label_colours, mImgSegment_color_final);
cv::resize(mImgSegment, mImgSegment, cv::Size(Camera::width,Camera::height) );
cv::resize(mImgSegment_color_final, mImgSegment_color_final, cv::Size(Camera::width,Camera::height) );
std::chrono::steady_clock::time_point t4 = std::chrono::steady_clock::now();
mSegmentTime+=std::chrono::duration_cast<std::chrono::duration<double> >(t4 - t3).count();
mSkipIndex=0;
imgIndex++;
}
mSkipIndex++;
ProduceImgSegment();
if(CheckFinish())
{
break;
}
}
}
bool Segment::CheckFinish()
{
unique_lock<mutex> lock(mMutexFinish);
return mbFinishRequested;
}
void Segment::RequestFinish()
{
unique_lock<mutex> lock(mMutexFinish);
mbFinishRequested=true;
}
void Segment::ProduceImgSegment()
{
std::unique_lock <std::mutex> lock(mMutexNewImgSegment);
mImgTemp=mImgSegmentLatest;
mImgSegmentLatest=mImgSegment;
mImgSegment=mImgTemp;
mpTracker->mbNewSegImgFlag=true;
}
}