To extend this pipeline to create a point cloud that contains only the 3D points that are in the mask you need a RGB-D device. In our case we will consider a Realsense camera. The pipeline becomes:
configure the realsense grabber to acquire color and depth aligned images
create the point cloud object. Note here the Z_min and Z_max variables, which are used further in the code to remove 3D points that are too close or too far from the camera
using the mask and the depth image update the point cloud using vpImageConvert::depthToPointCloud(). The corresponding point cloud is available in pointcloud variable.
$ cd $VISP_WS/visp-build/tutorial/segmentation/color
$ ./tutorial-hsv-segmentation-pcl --hsv-thresholds calib/hsv-thresholds.yml
In the next section we show how to display the point cloud.
Point could viewer
In tutorial-hsv-segmentation-pcl-viewer.cpp you will find an extension of the previous code with the introduction of vpDisplayPCL class that allows to visualize the point cloud in 3D with optionally additional texture information taken from the color image.
To add the point cloud viewer feature:
Fisrt you need to include vpDisplayPCL header
#include <visp3/gui/vpDisplayPCL.h>
Then, we declare two pcl objects for textured and untextured point clouds respectively
Since the point cloud viewer will run in a separate thread, to avoid concurrent access to pointcloud or pointcloud_color objects, we introduce a mutex, declare the PCL viewer object and launch the viewer thread, either for textured or untextured point clouds
At this point, you should see something similar to the following video, where the top left image corresponds to the live stream provided by a Realsense D435 camera, the top right image to the HSV yellow color segmentation, and the bottom right image to the PCL point cloud viewer used to visualize the textured point cloud corresponding to the yellow pixels in real time.
Known issue
Segfault in PCL viewer
There is a known issue related to the call to PCLVisualizer::spinOnce() function that is used in vpDisplayPCL class. This issue is reported in https://github.com/PointCloudLibrary/pcl/issues/5237 and occurs with PCL 1.12.1 and VTK 9.1 on Ubuntu 22.04.
The workaround consists in installing the lastest PCL release.
First uninstall libpcl-dev package
$ sudo apt remove libpcl-dev
Then download and install latest PCL release from source. In our case we installed PCL 1.14.0 on Ubuntu 22.04 with:
$ mkdir -p $VISP_WS/3rdparty/pcl
$ cd $VISP_WS/3rdparty/pcl
$ git clone --branch pcl-1.14.0 https://github.com/PointCloudLibrary/pcl pcl-1.14.0
$ mkdir pcl-1.14.0/build
$ cd pcl-1.14.0/build
$ make -j$(nproc)
$ sudo make install
Finally, create a fresh ViSP configuration and build.
Warning
It could be nice to create a backup of the visp-build folder before removing it.
$ cd $VISP_WS
$ rm -rf visp-build
$ mkdir visp-build && cd visp-build
$ cmake ../visp
$ make -j$(nproc)