2 #include <opencv2/imgproc.hpp> 3 #include <opencv2/rgbd/kinfu.hpp> 24 std::vector<Mat>
channels(points.channels());
32 color.convertTo(color, CV_8UC1, 255 / (max - min), -255 * min / (max - min));
34 applyColorMap(color, color, COLORMAP_JET);
53 gluPerspective(65, 1.3, 0.01
f, 10.0
f);
57 gluLookAt(0, 0, 0, 0, 0, 1, 0, -1, 0);
70 for (
int i = 0;
i < points.rows;
i++)
73 float x = points.at<
float>(
i, 0);
74 float y = points.at<
float>(
i, 1);
75 float z = points.at<
float>(
i, 2);
78 float nx = normals.at<
float>(
i, 0);
79 float ny = normals.at<
float>(
i, 1);
80 float nz = normals.at<
float>(
i, 2);
83 uchar
r = color.at<uchar>(
i, 0);
84 uchar
g = color.at<uchar>(
i, 1);
85 uchar
b = color.at<uchar>(
i, 2);
104 const size_t buffer_size = 50;
105 char fname[buffer_size];
107 struct tm *
now = localtime(&t);
108 strftime(fname, buffer_size,
"%m%d%y %H%M%S.ply", now);
109 std::cout <<
"exporting to" << fname << std::endl;
116 std::ofstream
out(fname);
118 out <<
"format binary_little_endian 1.0\n";
119 out <<
"comment pointcloud saved from Realsense Viewer\n";
120 out <<
"element vertex " << points.rows <<
"\n";
121 out <<
"property float" <<
sizeof(float) * 8 <<
" x\n";
122 out <<
"property float" <<
sizeof(float) * 8 <<
" y\n";
123 out <<
"property float" <<
sizeof(float) * 8 <<
" z\n";
125 out <<
"property float" <<
sizeof(float) * 8 <<
" nx\n";
126 out <<
"property float" <<
sizeof(float) * 8 <<
" ny\n";
127 out <<
"property float" <<
sizeof(float) * 8 <<
" nz\n";
129 out <<
"property uchar red\n";
130 out <<
"property uchar green\n";
131 out <<
"property uchar blue\n";
132 out <<
"end_header\n";
136 for (
int i = 0;
i < points.rows;
i++)
139 out.write(reinterpret_cast<const char*>(&(points.at<
float>(
i, 0))),
sizeof(
float));
140 out.write(reinterpret_cast<const char*>(&(points.at<
float>(
i, 1))),
sizeof(
float));
141 out.write(reinterpret_cast<const char*>(&(points.at<
float>(
i, 2))),
sizeof(
float));
144 out.write(reinterpret_cast<const char*>(&(normals.at<
float>(
i, 0))),
sizeof(
float));
145 out.write(reinterpret_cast<const char*>(&(normals.at<
float>(
i, 1))),
sizeof(
float));
146 out.write(reinterpret_cast<const char*>(&(normals.at<
float>(
i, 2))),
sizeof(
float));
149 out.write(reinterpret_cast<const char*>(&(color.at<uchar>(
i, 0))),
sizeof(
uint8_t));
150 out.write(reinterpret_cast<const char*>(&(color.at<uchar>(
i, 1))),
sizeof(
uint8_t));
151 out.write(reinterpret_cast<const char*>(&(color.at<uchar>(
i, 2))),
sizeof(
uint8_t));
162 std::lock_guard<std::mutex>
lock(_mtx);
167 std::lock_guard<std::mutex>
lock(_mtx);
180 int main(
int argc,
char **argv)
184 Ptr<Params>
params = Params::defaultParams();
206 depth_scale = dpt.get_depth_scale();
219 d = decimation.process(
d);
220 auto w =
d.get_width();
221 auto h =
d.get_height();
227 params->frameSize =
size;
234 kf = KinFu::create(params);
236 bool after_reset =
false;
240 window app(1280, 720,
"RealSense KinectFusion Example");
244 std::atomic_bool stopped(
false);
247 std::thread calc_cloud_thread([&]() {
257 d = decimation.process(
d);
258 d = spatial.process(
d);
259 d = temporal.process(
d);
262 uint16_t* p_depth_frame =
reinterpret_cast<uint16_t*
>(
const_cast<void*
>(
d.get_data()));
263 #pragma omp parallel for schedule(dynamic) //Using OpenMP to try to parallelise the loop 264 for (
int y = 0;
y <
h;
y++)
266 auto depth_pixel_index =
y *
w;
267 for (
int x = 0;
x <
w;
x++, ++depth_pixel_index)
270 if (p_depth_frame[depth_pixel_index] > clipping_dist)
272 p_depth_frame[depth_pixel_index] = 0;
281 Mat
f(h,
w, CV_16UC1, (
void*)
d.get_data());
282 UMat frame(h,
w, CV_16UC1);
287 if (!kf->update(frame))
303 kf->getCloud(points, normals);
306 if (!points.empty() && !normals.empty())
309 points.copyTo(_points);
311 normals.copyTo(_normals);
314 points_queue.
push(_points);
315 normals_queue.
push(_normals);
320 catch (
const std::exception&
e)
334 if (!points.empty() && !normals.empty())
338 calc_cloud_thread.join();
static const textual_icon lock
frameset wait_for_frames(unsigned int timeout_ms=RS2_DEFAULT_TIMEOUT) const
GLboolean GLboolean GLboolean b
void colorize_pointcloud(const Mat points, Mat &color)
void export_to_ply(Mat points, Mat normals)
std::vector< uint32_t > split(const std::string &s, char delim)
GLdouble GLdouble GLdouble w
void draw_kinfu_pointcloud(glfw_state &app_state, Mat points, Mat normals)
GLfloat GLfloat GLfloat GLfloat h
int main(int argc, char **argv)
depth_frame get_depth_frame() const
#define GL_COLOR_MATERIAL
int try_get_next_item(Mat &item)
GLenum const GLfloat * params
void enable_stream(rs2_stream stream_type, int stream_index, int width, int height, rs2_format format=RS2_FORMAT_ANY, int framerate=0)
#define GL_DEPTH_BUFFER_BIT
const GLuint GLenum const void * binary
typename::boost::move_detail::remove_reference< T >::type && move(T &&t) BOOST_NOEXCEPT
std::array< float, 3 > color
#define GL_ALL_ATTRIB_BITS
void register_glfw_callbacks(window &app, glfw_state &app_state)
GLdouble GLdouble GLint GLint const GLdouble * points