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.
You can either run the point cloud viewer in a separate thread or in a the main thread (for instance for MacOS users, see vpDisplayPCL documentation). When running 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. When running in the main thread, we have to add the point clouds to the list of known 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.
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)
Known issue on MacOS
On MacOS, you can face the following error:
tutorial-display-pcl *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSWindow drag regions should only be invalidated on the Main Thread!'
libc++abi: terminating due to uncaught exception of type NSException
This problem seems to be due to VTK library that does not like to be run in a non-main thread on MacOS. You can use the vpDisplayPCL class in monothread mode using the method vpDisplayPCL::display. See the PCL issue for more details.
Known issue when using VTK and the server X backends
When the PCL library relies on the VTK library and X11 library as backends for the viewer, you may face the following error if you create either two viewers in threaded mode or one viewer in threaded mode and another one in the main thread:
X Error of failed request: BadAccess (attempt to access private resource denied)
Major opcode of failed request: 152 (GLX)
Minor opcode of failed request: 5 (X_GLXMakeCurrent)