42#include <visp3/core/vpConfig.h>
44#if defined(VISP_HAVE_REALSENSE2) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) && \
45 (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)) && defined(VISP_HAVE_PCL)
50#include <pcl/visualization/cloud_viewer.h>
51#include <pcl/visualization/pcl_visualizer.h>
53#include <visp3/core/vpImage.h>
54#include <visp3/core/vpImageConvert.h>
55#include <visp3/gui/vpDisplayGDI.h>
56#include <visp3/gui/vpDisplayX.h>
57#include <visp3/sensor/vpRealSense2.h>
62pcl::PointCloud<pcl::PointXYZ>::Ptr pointcloud(
new pcl::PointCloud<pcl::PointXYZ>());
63pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointcloud_color(
new pcl::PointCloud<pcl::PointXYZRGB>());
64bool cancelled =
false, update_pointcloud =
false;
69 explicit ViewerWorker(
bool color_mode, std::mutex &mutex) : m_colorMode(color_mode), m_mutex(mutex) {}
74 pcl::visualization::PCLVisualizer::Ptr viewer(
new pcl::visualization::PCLVisualizer(
"3D Viewer " + date));
75 pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(pointcloud_color);
76 pcl::PointCloud<pcl::PointXYZ>::Ptr local_pointcloud(
new pcl::PointCloud<pcl::PointXYZ>());
77 pcl::PointCloud<pcl::PointXYZRGB>::Ptr local_pointcloud_color(
new pcl::PointCloud<pcl::PointXYZRGB>());
79 viewer->setBackgroundColor(0, 0, 0);
80 viewer->initCameraParameters();
81 viewer->setPosition(640 + 80, 480 + 80);
82 viewer->setCameraPosition(0, 0, -0.25, 0, -1, 0);
83 viewer->setSize(640, 480);
86 bool local_update =
false, local_cancelled =
false;
87 while (!local_cancelled) {
89 std::unique_lock<std::mutex> lock(m_mutex, std::try_to_lock);
91 if (lock.owns_lock()) {
92 local_update = update_pointcloud;
93 update_pointcloud =
false;
94 local_cancelled = cancelled;
98 local_pointcloud_color = pointcloud_color->makeShared();
100 local_pointcloud = pointcloud->makeShared();
106 if (local_update && !local_cancelled) {
107 local_update =
false;
111 viewer->addPointCloud<pcl::PointXYZRGB>(local_pointcloud_color, rgb,
"RGB sample cloud");
112 viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1,
115 viewer->addPointCloud<pcl::PointXYZ>(local_pointcloud,
"sample cloud");
116 viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1,
"sample cloud");
121 viewer->updatePointCloud<pcl::PointXYZRGB>(local_pointcloud_color, rgb,
"RGB sample cloud");
123 viewer->updatePointCloud<pcl::PointXYZ>(local_pointcloud,
"sample cloud");
131 std::cout <<
"End of point cloud display thread" << std::endl;
140int main(
int argc,
char *argv[])
142 bool pcl_color =
false;
143 bool show_infrared2 =
false;
144 for (
int i = 1; i < argc; i++) {
145 if (std::string(argv[i]) ==
"--pcl_color") {
147 }
else if (std::string(argv[i]) ==
"--show_infrared2") {
148 show_infrared2 =
true;
152 const int width = 640, height = 480, fps = 30;
155 config.enable_stream(RS2_STREAM_COLOR, width, height, RS2_FORMAT_RGBA8, fps);
156 config.enable_stream(RS2_STREAM_DEPTH, width, height, RS2_FORMAT_Z16, fps);
157 config.enable_stream(RS2_STREAM_INFRARED, 1, width, height, RS2_FORMAT_Y8, fps);
158 config.enable_stream(RS2_STREAM_INFRARED, 2, width, height, RS2_FORMAT_Y8, fps);
161 vpImage<vpRGBa> color(
static_cast<unsigned int>(height),
static_cast<unsigned int>(width));
162 vpImage<vpRGBa> depth_color(
static_cast<unsigned int>(height),
static_cast<unsigned int>(width));
163 vpImage<uint16_t> depth_raw(
static_cast<unsigned int>(height),
static_cast<unsigned int>(width));
172 d1.
init(color, 0, 0,
"Color");
173 d2.
init(depth_color, color.getWidth(), 0,
"Depth");
174 d3.
init(infrared1, 0, color.getHeight() + 100,
"Infrared left");
175 if (show_infrared2) {
176 d4.
init(infrared2, color.getWidth(), color.getHeight() + 100,
"Infrared right");
180 ViewerWorker viewer_pointcloud(pcl_color, mutex);
181 std::thread viewer_thread(&ViewerWorker::run, &viewer_pointcloud);
183 std::vector<double> time_vector;
188 std::lock_guard<std::mutex> lock(mutex);
191 rs.
acquire(
reinterpret_cast<unsigned char *
>(color.bitmap),
reinterpret_cast<unsigned char *
>(depth_raw.bitmap),
192 NULL, pointcloud_color,
reinterpret_cast<unsigned char *
>(infrared1.bitmap),
193 show_infrared2 ?
reinterpret_cast<unsigned char *
>(infrared2.bitmap) : NULL, NULL);
195 rs.
acquire(
reinterpret_cast<unsigned char *
>(color.bitmap),
reinterpret_cast<unsigned char *
>(depth_raw.bitmap),
196 NULL, pointcloud,
reinterpret_cast<unsigned char *
>(infrared1.bitmap),
197 show_infrared2 ?
reinterpret_cast<unsigned char *
>(infrared2.bitmap) : NULL, NULL);
200 update_pointcloud =
true;
229 std::lock_guard<std::mutex> lock(mutex);
232 viewer_thread.join();
234 std::cout <<
"Acquisition - Mean time: " <<
vpMath::getMean(time_vector)
235 <<
" ms ; Median time: " <<
vpMath::getMedian(time_vector) <<
" ms" << std::endl;
243#if !defined(VISP_HAVE_REALSENSE2)
244 std::cout <<
"Install librealsense2 to make this test work." << std::endl;
246#if !(VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
247 std::cout <<
"Build ViSP with c++11 or higher compiler flag (cmake -DUSE_CXX_STANDARD=11) "
248 "to make this test work"
251#if !defined(VISP_HAVE_X11) && !defined(VISP_HAVE_GDI)
252 std::cout <<
"X11 or GDI are needed." << std::endl;
254#if !defined(VISP_HAVE_PCL)
255 std::cout <<
"Install PCL to make this test work." << std::endl;
void start(bool reset=true)
Display for windows using GDI (available on any windows 32 platform).
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="")
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
static void createDepthHistogram(const vpImage< uint16_t > &src_depth, vpImage< vpRGBa > &dest_rgba)
Definition of the vpImage class member functions.
static double getMedian(const std::vector< double > &v)
static double getMean(const std::vector< double > &v)
void acquire(vpImage< unsigned char > &grey, double *ts=NULL)
bool open(const rs2::config &cfg=rs2::config())
VISP_EXPORT std::string getDateTime(const std::string &format="%Y/%m/%d %H:%M:%S")