-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.cpp
229 lines (198 loc) · 5.73 KB
/
Logger.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
#include "Logger.h"
Logger::Logger()
: kinect(0),
flir(0),
lastDepthWritten(-1),
writeDepthThread(0),
lastThermalWritten(-1),
writeThermalThread(0),
singleWrite(0)
{
writing.assignValue(false);
tfolderName = "thermal";
dfolderName = "depth";
ifolderName = "infrared";
}
Logger::~Logger()
{
if(writing.getValue())
{
assert(writeDepthThread && writeThermalThread);
StopWriting();
}
if(flir)
{
delete flir;
flir = 0;
}
if(kinect)
{
delete kinect;
kinect = 0;
}
}
bool Logger::ConnectCamera()
{
flir = new EbusFlirInterface();
if(!flir->IsOK())
{
delete flir;
flir = 0;
return false;
}
return true;
}
bool Logger::ConnectKinect()
{
kinect = new OpenNI2Interface();
if(!kinect->ok())
{
delete kinect;
kinect = 0;
return false;
}
return true;
}
void Logger::StartWriting()
{
if(!(flir->IsOK() && flir->isAcquisition.getValue()) || !kinect->ok())
return;
if(writeDepthThread || writing.getValue() || writeThermalThread)
return;
if(!MkDir(tfolderName) || !MkDir(dfolderName)){
std::cout << "make folder failed! Fuck!\n";
return;
}
writing.assignValue(true);
writeDepthThread = new boost::thread(boost::bind(&Logger::DepthWritingThread,
this));
writeThermalThread = new boost::thread(boost::bind(&Logger::ThermalWritingThread,
this));
}
void Logger::StopWriting()
{
if(!writing.getValue())
return;
writing.assignValue(false);
writeDepthThread->join();
writeThermalThread->join();
delete writeDepthThread;
writeDepthThread = 0;
delete writeThermalThread;
writeThermalThread = 0;
}
void Logger::SingleWriting()
{
if(!kinect->ok() || !flir->IsOK())
return;
int latestDepth = kinect->latestDepthIndex.getValue();
if(latestDepth == -1)
{
return;
}
int depthBufferIndex = latestDepth % OpenNI2Interface::numBuffers;
int latestInfrared = kinect->latestInfraredIndex.getValue();
if(latestInfrared == -1)
{
return;
}
int infraredBufferIndex = latestInfrared % OpenNI2Interface::numBuffers;
int latestThermal = flir->latestThermalIndex.getValue();
if(latestThermal == -1)
{
return;
}
int thermalBufferIndex = latestThermal % OpenNI2Interface::numBuffers;
cv::Mat depth(kinect->height, kinect->width, CV_16UC1);
memcpy(depth.data, kinect->frameBuffers[depthBufferIndex].first, kinect->height * kinect->width * 2);
cv::Mat infrared(kinect->height, kinect->width, CV_16UC1);
memcpy(infrared.data, kinect->infraredFrameBuffers[infraredBufferIndex].first, kinect->height * kinect->width * 2);
cv::Mat thermal(flir->height, flir->width, CV_8UC1);
memcpy(thermal.data, flir->frameBuffers[thermalBufferIndex].first, flir->height * flir->width);
cv::Mat fthermal;
cv::flip(thermal, fthermal, 0);
std::string num;
num = boost::lexical_cast<std::string>(singleWrite);
std::string depthfile = dfolderName + "/depth" + num + ".png";
std::string infraredfile = ifolderName + "/infrared" + num + ".png";
std::string thermalfile = tfolderName + "/thermal" + num + ".png";
cv::imwrite(depthfile, depth);
cv::imwrite(infraredfile, infrared);
cv::imwrite(thermalfile, fthermal);
singleWrite++;
}
void Logger::ShowGenWindow( PvGenBrowserWnd *aWnd, PvGenParameterArray *aArray, const QString &aTitle, QWidget * gui )
{
if ( aWnd->GetQWidget()->isVisible() )
{
aWnd->SetGenParameterArray( NULL );
aWnd->Close();
return;
}
aWnd->SetTitle( aTitle.toAscii().data() );
aWnd->ShowModeless(gui);
aWnd->SetGenParameterArray( aArray );
}
OpenNI2Interface * Logger::getKinect()
{
return kinect;
}
EbusFlirInterface * Logger::getFlir()
{
return flir;
}
void Logger::ThermalWritingThread()
{
while(writing.getValue())
{
int lastThermal = flir->latestThermalIndex.getValue();
if(lastThermal == -1)
{
continue;
}
int bufferIndex = lastThermal % EbusFlirInterface::numBuffers;
if(lastThermalWritten == bufferIndex)
{
continue;
}
cv::Mat1b thermal(flir->height, flir->width, flir->frameBuffers[bufferIndex].first);
cv::Mat fthermal;
cv::flip(thermal, fthermal, 0);
std::string imagename = "";
imagename = boost::lexical_cast<std::string>(flir->frameBuffers[bufferIndex].second);
imagename = tfolderName + "/" + imagename + ".png";
cv::imwrite(imagename, fthermal);
lastThermalWritten = bufferIndex;
}
}
void Logger::DepthWritingThread()
{
while(writing.getValue())
{
int lastDepth = kinect->latestDepthIndex.getValue();
if(lastDepth == -1)
{
continue;
}
int bufferIndex = lastDepth % OpenNI2Interface::numBuffers;
if(lastDepthWritten == bufferIndex)
{
continue;
}
cv::Mat1w depth(kinect->height, kinect->width, (unsigned short *)kinect->frameBuffers[bufferIndex].first);
std::string imagename = "";
imagename = boost::lexical_cast<std::string>(kinect->frameBuffers[bufferIndex].second);
imagename = dfolderName + "/" + imagename + ".png";
cv::imwrite(imagename, depth);
lastDepthWritten = bufferIndex;
}
}
bool Logger::MkDir(std::string dirFolder)
{
if(access(dirFolder.c_str(), 0) == 0)
return true;
else{
int flag = mkdir(dirFolder.c_str(), S_IRWXU);
return flag == 0;
}
}