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) 47 #include <visp3/core/vpImage.h> 48 #include <visp3/core/vpImageConvert.h> 49 #include <visp3/gui/vpDisplayGDI.h> 50 #include <visp3/gui/vpDisplayX.h> 51 #include <visp3/sensor/vpRealSense2.h> 53 #include <pcl/visualization/cloud_viewer.h> 54 #include <pcl/visualization/pcl_visualizer.h> 61 pcl::PointCloud<pcl::PointXYZ>::Ptr pointcloud(
new pcl::PointCloud<pcl::PointXYZ>());
62 pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointcloud_color(
new pcl::PointCloud<pcl::PointXYZRGB>());
63 bool cancelled =
false, update_pointcloud =
false;
68 explicit ViewerWorker(
bool color_mode, std::mutex &mutex) : m_colorMode(color_mode), m_mutex(mutex) {}
73 pcl::visualization::PCLVisualizer::Ptr viewer(
new pcl::visualization::PCLVisualizer(
"3D Viewer " + date));
74 pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(pointcloud_color);
75 pcl::PointCloud<pcl::PointXYZ>::Ptr local_pointcloud(
new pcl::PointCloud<pcl::PointXYZ>());
76 pcl::PointCloud<pcl::PointXYZRGB>::Ptr local_pointcloud_color(
new pcl::PointCloud<pcl::PointXYZRGB>());
78 viewer->setBackgroundColor(0, 0, 0);
79 viewer->initCameraParameters();
80 viewer->setPosition(640 + 80, 480 + 80);
81 viewer->setCameraPosition(0, 0, -0.25, 0, -1, 0);
82 viewer->setSize(640, 480);
85 bool local_update =
false, local_cancelled =
false;
86 while (!local_cancelled) {
88 std::unique_lock<std::mutex> lock(m_mutex, std::try_to_lock);
90 if (lock.owns_lock()) {
91 local_update = update_pointcloud;
92 update_pointcloud =
false;
93 local_cancelled = cancelled;
97 local_pointcloud_color = pointcloud_color->makeShared();
99 local_pointcloud = pointcloud->makeShared();
105 if (local_update && !local_cancelled) {
106 local_update =
false;
110 viewer->addPointCloud<pcl::PointXYZRGB>(local_pointcloud_color, rgb,
"RGB sample cloud");
111 viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1,
114 viewer->addPointCloud<pcl::PointXYZ>(local_pointcloud,
"sample cloud");
115 viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1,
"sample cloud");
120 viewer->updatePointCloud<pcl::PointXYZRGB>(local_pointcloud_color, rgb,
"RGB sample cloud");
122 viewer->updatePointCloud<pcl::PointXYZ>(local_pointcloud,
"sample cloud");
130 std::cout <<
"End of point cloud display thread" << std::endl;
139 int main(
int argc,
char *argv[])
141 bool pcl_color =
false;
142 bool show_infrared2 =
false;
143 for (
int i = 1; i < argc; i++) {
144 if (std::string(argv[i]) ==
"--pcl_color") {
146 }
else if (std::string(argv[i]) ==
"--show_infrared2") {
147 show_infrared2 =
true;
151 const int width = 640, height = 480, fps = 30;
154 config.enable_stream(RS2_STREAM_COLOR, width, height, RS2_FORMAT_RGBA8, fps);
155 config.enable_stream(RS2_STREAM_DEPTH, width, height, RS2_FORMAT_Z16, fps);
156 config.enable_stream(RS2_STREAM_INFRARED, 1, width, height, RS2_FORMAT_Y8, fps);
157 config.enable_stream(RS2_STREAM_INFRARED, 2, width, height, RS2_FORMAT_Y8, fps);
160 vpImage<vpRGBa> color(static_cast<unsigned int>(height), static_cast<unsigned int>(width));
161 vpImage<vpRGBa> depth_color(static_cast<unsigned int>(height), static_cast<unsigned int>(width));
162 vpImage<uint16_t> depth_raw(static_cast<unsigned int>(height), static_cast<unsigned int>(width));
171 d1.
init(color, 0, 0,
"Color");
172 d2.
init(depth_color, color.getWidth(), 0,
"Depth");
173 d3.
init(infrared1, 0, color.getHeight() + 100,
"Infrared left");
174 if (show_infrared2) {
175 d4.
init(infrared2, color.getWidth(), color.getHeight() + 100,
"Infrared right");
179 ViewerWorker viewer_pointcloud(pcl_color, mutex);
180 std::thread viewer_thread(&ViewerWorker::run, &viewer_pointcloud);
182 std::vector<double> time_vector;
187 std::lock_guard<std::mutex> lock(mutex);
190 rs.
acquire(reinterpret_cast<unsigned char *>(color.bitmap),
191 reinterpret_cast<unsigned char *>(depth_raw.bitmap),
192 NULL, pointcloud_color,
193 reinterpret_cast<unsigned char *>(infrared1.bitmap),
194 show_infrared2 ? reinterpret_cast<unsigned char *>(infrared2.bitmap) : NULL,
197 rs.
acquire(reinterpret_cast<unsigned char *>(color.bitmap),
198 reinterpret_cast<unsigned char *>(depth_raw.bitmap),
200 reinterpret_cast<unsigned char *>(infrared1.bitmap),
201 show_infrared2 ? reinterpret_cast<unsigned char *>(infrared2.bitmap) : NULL,
205 update_pointcloud =
true;
236 std::lock_guard<std::mutex> lock(mutex);
239 viewer_thread.join();
241 std::cout <<
"Acquisition - Mean time: " <<
vpMath::getMean(time_vector)
242 <<
" ms ; Median time: " <<
vpMath::getMedian(time_vector) <<
" ms" << std::endl;
250 #if !defined(VISP_HAVE_REALSENSE2) 251 std::cout <<
"Install librealsense2 to make this test work." << std::endl;
253 #if !(VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) 254 std::cout <<
"Build ViSP with c++11 or higher compiler flag (cmake -DUSE_CXX_STANDARD=11) " 255 "to make this test work" 258 #if !defined(VISP_HAVE_X11) && !defined(VISP_HAVE_GDI) 259 std::cout <<
"X11 or GDI are needed." << std::endl;
261 #if !defined(VISP_HAVE_PCL) 262 std::cout <<
"Install PCL to make this test work." << std::endl;
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static double getMedian(const std::vector< double > &v)
Display for windows using GDI (available on any windows 32 platform).
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
static void flush(const vpImage< unsigned char > &I)
bool open(const rs2::config &cfg=rs2::config())
VISP_EXPORT std::string getDateTime(const std::string &format="%Y/%m/%d %H:%M:%S")
void start(bool reset=true)
static double getMean(const std::vector< double > &v)
static void display(const vpImage< unsigned char > &I)
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
void acquire(vpImage< unsigned char > &grey, double *ts=NULL)
static void createDepthHistogram(const vpImage< uint16_t > &src_depth, vpImage< vpRGBa > &dest_rgba)