-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathONI_capture.cpp
192 lines (154 loc) · 5.33 KB
/
ONI_capture.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
// ONI_capture.cpp : Defines the entry point for the application.
//
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <string>
#include <direct.h>
#include <ctime>
#include <Windows.h>
#include "OpenNI.h"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/calib3d/calib3d.hpp"
using namespace std;
#define SAMPLE_READ_WAIT_TIMEOUT 5000 //2000ms
int main(int argc, char** argv)
{
bool saveFrame = true;
std::string outDir = "..\\CapturedSequence\\";
std::time_t timestamp = std::time(0);
std::stringstream ss;
ss << timestamp;
std::string ts = ss.str();
std::string outputFolder = std::string(outDir + ts);
std::string depthFolder = std::string(outputFolder + "\\depth\\");
std::string colorFolder = std::string(outputFolder + "\\color\\");
if (saveFrame) {
if ((CreateDirectory(outputFolder.c_str(), NULL)) &&
(CreateDirectory(depthFolder.c_str(), NULL) || ERROR_ALREADY_EXISTS == GetLastError()) &&
(CreateDirectory(colorFolder.c_str(), NULL) || ERROR_ALREADY_EXISTS == GetLastError()))
{
cout << "Saving frame to: " << outputFolder << "\n";
}
else {
cout << "Fail to create output folder: " << outputFolder << "\n";
}
}
openni::Status result = openni::STATUS_OK;
//OpenNI2 Frame
openni::VideoFrameRef oniDepthImg;
cv::Mat mImageDepth_colormap, mImageDepth_scale, mImageColor, mImageOverlay;
char key = 0;
// Set up openni
result = openni::OpenNI::initialize();
if (result != openni::STATUS_OK)
{
printf("Initialize failed\n%s\n", openni::OpenNI::getExtendedError());
return 1;
}
// Open camera device
openni::Array<openni::DeviceInfo> deviceList;
openni::OpenNI::enumerateDevices(&deviceList);
int cc = deviceList.getSize();
if (cc <= 0)
{
cout << "No device found\n";
return -1;
}
const char* deviceUri;
deviceUri = deviceList[0].getUri();
cout << "Device URI: " << deviceUri << "\n";
openni::Device device;
result = device.open(deviceUri);
// set depth video mode
openni::VideoMode modeDepth;
modeDepth.setResolution(640, 480);
modeDepth.setFps(30);
modeDepth.setPixelFormat(openni::PIXEL_FORMAT_DEPTH_1_MM);
// create depth stream
openni::VideoStream oniDepthStream;
oniDepthStream.setVideoMode(modeDepth);
oniDepthStream.create(device, openni::SENSOR_DEPTH);
int changedStreamDummy;
openni::VideoStream* pStream = &oniDepthStream;
if (device.isImageRegistrationModeSupported(openni::IMAGE_REGISTRATION_DEPTH_TO_COLOR))
{
device.setImageRegistrationMode(openni::IMAGE_REGISTRATION_DEPTH_TO_COLOR);
}
device.setDepthColorSyncEnabled(true);
float vfov = oniDepthStream.getVerticalFieldOfView();
float hfov = oniDepthStream.getHorizontalFieldOfView();
cout << "FOV: " << vfov << " : " << hfov << "\n";
// start depth stream
result = oniDepthStream.start();
if (result != openni::STATUS_OK) {
cout << "Fail to open depth stream\n";
}
// start color stream
cv::VideoCapture colorStream(0, cv::CAP_DSHOW);
if (!colorStream.isOpened()) {
cout << "Fail to open color stream\n";
}
colorStream.set(cv::CAP_PROP_BUFFERSIZE, 3);
cout << "FPS: " << colorStream.get(cv::CAP_PROP_FPS) << "\n";
cout << "Buffer Size: " << colorStream.get(cv::CAP_PROP_BUFFERSIZE) << "\n";
cout << "SAR: " << colorStream.get(cv::CAP_PROP_SAR_NUM) << "\n";
string depthWinName = "Depth";
string colorWinName = "Color";
string overlayWinName = "Overlay";
cv::namedWindow(depthWinName, 1);
cv::namedWindow(colorWinName, 1);
cv::namedWindow(overlayWinName, 1);
int frameCount = 0;
while ((key != 27)) // ESC
{
openni::OpenNI::waitForAnyStream(&pStream, 1, &changedStreamDummy, SAMPLE_READ_WAIT_TIMEOUT);
oniDepthStream.readFrame(&oniDepthImg);
colorStream >> mImageColor;
// Parser depth
const cv::Mat mImageDepth(oniDepthImg.getHeight(), oniDepthImg.getWidth(), CV_16UC1, (void*)oniDepthImg.getData());
cv::flip(mImageDepth, mImageDepth, 1);
mImageDepth.convertTo(mImageDepth_scale, CV_8U, 255.0 / 8000.0);
cv::threshold(mImageDepth_scale, mImageDepth_scale, 8000.0, 8000.0, cv::THRESH_TOZERO_INV);
cv::applyColorMap(mImageDepth_scale, mImageDepth_colormap, cv::COLORMAP_JET);
for (int v = 0; v < mImageDepth_colormap.rows; v++){
uchar* p_colormap = mImageDepth_colormap.ptr<uchar>(v);
uchar* p_scale= mImageDepth_scale.ptr<uchar>(v);
for (int u = 0; u < mImageDepth_colormap.cols; u++) {
if (p_scale[u] == 0) {
p_colormap[u * 3 + 0] = 0;
p_colormap[u * 3 + 1] = 0;
p_colormap[u * 3 + 2] = 0;
}
}
}
// Show img
cv::imshow(depthWinName, mImageDepth_colormap);
cv::imshow(colorWinName, mImageColor);
cv::addWeighted(mImageColor, 0.7, mImageDepth_colormap, 0.5, 0, mImageOverlay);
cv::imshow(overlayWinName, mImageOverlay);
// Save frame
if (saveFrame) {
std::stringstream fs;
fs << std::setfill('0') << std::setw(6) << frameCount;
std::string countStr = fs.str();
std::string depthFileName = std::string(depthFolder + countStr + ".depth.png");
std::string colorFileName = std::string(colorFolder + countStr + ".color.jpg");
cv::imwrite(depthFileName, mImageDepth);
cv::imwrite(colorFileName, mImageColor);
}
key = cv::waitKey(1);
frameCount++;
}
//destroy
oniDepthStream.destroy();
device.close();
openni::OpenNI::shutdown();
colorStream.release();
return 0;
}