Skip to content

Commit

Permalink
Merge pull request #185 from zivid/add-normals-to-pcl-samples
Browse files Browse the repository at this point in the history
Add normals to pcl samples
  • Loading branch information
SatjaSivcev authored Nov 5, 2021
2 parents d092e7b + e3f452d commit 223e70c
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 126 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ There are two main categories of samples: **Camera** and **Applications**. The s
- **Visualization**
- [**CaptureFromFileCameraVis3D**][CaptureFromFileCameraVis3D-url] - Capture point clouds, with color, from the virtual Zivid camera, and visualize it. Currently supported by Zivid One.
- [**CaptureVis3D**][CaptureVis3D-url] - Capture point clouds, with color, from the Zivid camera, and visualize it.
- [**CaptureWritePCLVis3D**][CaptureWritePCLVis3D-url] - Capture point clouds, with color, from the Zivid camera, save it to PCD file format, and visualize it.
- [**CaptureWritePCLVis3D**][CaptureWritePCLVis3D-url] - Capture point clouds, with color and with/without normals, from the Zivid camera, convert it to
PCL format, save it to PCD file, and visualize it.
- **Dependencies:**
- [Point Cloud Library][point-cloud-library-url] version 1.2 or newer
- [**CaptureHDRVisNormals**][CaptureHDRVisNormals-url] - Capture Zivid point clouds, with color and normals, and visualize it in 3D and as a normal map.
- **Dependencies:**
- [Point Cloud Library][point-cloud-library-url] version 1.2 or newer
- [**ReadPCLVis3D**][ReadPCLVis3D-url] - Read point cloud from PCL file and visualize it.
- [**ReadPCLVis3D**][ReadPCLVis3D-url] - Read point cloud, with color and with/without normals from PCD file and visualize it.
- **Dependencies:**
- [Point Cloud Library][point-cloud-library-url] version 1.2 or newer
- **FileFormats**
Expand Down
37 changes: 22 additions & 15 deletions source/Applications/Advanced/MaskPointCloud/MaskPointCloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,28 @@ The ZDF file for this sample can be found under the main instructions for Zivid

#include <opencv2/opencv.hpp>

#include <pcl/visualization/cloud_viewer.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>

#include <iostream>
#include <thread>

namespace
{
void visualizePointCloud(const pcl::PointCloud<pcl::PointXYZRGB> &pointCloud)
void visualizePointCloud(const boost::shared_ptr<pcl::PointCloud<pcl::PointXYZRGB>> &pointCloud)
{
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloudPTR(new pcl::PointCloud<pcl::PointXYZRGB>);
*cloudPTR = pointCloud;
std::cout << "Running PCL visualizer. Block until window closes" << std::endl;
pcl::visualization::CloudViewer viewer("Simple Cloud Viewer");
viewer.showCloud(cloudPTR);
auto viewer = boost::make_shared<pcl::visualization::PCLVisualizer>("Viewer");

viewer->addPointCloud<pcl::PointXYZRGB>(pointCloud);

viewer->setCameraPosition(0, 0, 100, 0, -1, 0);

std::cout << "Press r to centre and zoom the viewer so that the entire cloud is visible" << std::endl;
std::cout << "Press q to exit the viewer application" << std::endl;
while(!viewer.wasStopped())
while(!viewer->wasStopped())
{
viewer->spinOnce(100);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}

Expand Down Expand Up @@ -82,7 +87,8 @@ namespace
return zColorMap;
}

pcl::PointCloud<pcl::PointXYZRGB> maskPointCloud(const Zivid::PointCloud &pointCloud, const cv::Mat &mask)
boost::shared_ptr<pcl::PointCloud<pcl::PointXYZRGB>> maskPointCloud(const Zivid::PointCloud &pointCloud,
const cv::Mat &mask)
{
const auto data = pointCloud.copyPointsXYZColorsRGBA();
const int height = data.height();
Expand Down Expand Up @@ -119,7 +125,7 @@ namespace
}
}

return maskedPointCloud;
return boost::make_shared<pcl::PointCloud<pcl::PointXYZRGB>>(maskedPointCloud);
}

void visualizeDepthMap(const pcl::PointCloud<pcl::PointXYZRGB> &pointCloud)
Expand All @@ -132,7 +138,7 @@ namespace
cv::waitKey(0);
}

pcl::PointCloud<pcl::PointXYZRGB> convertToPCLPointCloud(const Zivid::PointCloud &pointCloud)
boost::shared_ptr<pcl::PointCloud<pcl::PointXYZRGB>> convertToPCLPointCloud(const Zivid::PointCloud &pointCloud)
{
const auto data = pointCloud.copyData<Zivid::PointXYZColorRGBA>();

Expand All @@ -153,7 +159,7 @@ namespace
pointCloudPCL.points[i].g = data(i).color.g; // NOLINT(cppcoreguidelines-pro-type-union-access)
pointCloudPCL.points[i].b = data(i).color.b; // NOLINT(cppcoreguidelines-pro-type-union-access)
}
return pointCloudPCL;
return boost::make_shared<pcl::PointCloud<pcl::PointXYZRGB>>(pointCloudPCL);
}

} // namespace
Expand Down Expand Up @@ -194,16 +200,17 @@ int main()
visualizePointCloud(pointCloudPCL);

std::cout << "Displaying depth map before masking" << std::endl;
visualizeDepthMap(pointCloudPCL);
visualizeDepthMap(*pointCloudPCL);

std::cout << "Masking point cloud" << std::endl;
const pcl::PointCloud<pcl::PointXYZRGB> maskedPointCloudPCL = maskPointCloud(pointCloud, mask);
const boost::shared_ptr<pcl::PointCloud<pcl::PointXYZRGB>> maskedPointCloudPCL =
maskPointCloud(pointCloud, mask);

std::cout << "Displaying point cloud after masking" << std::endl;
visualizePointCloud(maskedPointCloudPCL);

std::cout << "Displaying depth map after masking" << std::endl;
visualizeDepthMap(maskedPointCloudPCL);
visualizeDepthMap(*maskedPointCloudPCL);
}

catch(const std::exception &e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,14 @@ stitchedPointCloud.points.resize(k);

```cpp
//Simple Cloud Visualization
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloudPTR(new pcl::PointCloud<pcl::PointXYZRGB>);
boost::shared_ptr<pcl::PointCloud<pcl::PointXYZRGB>> cloudPTR(new pcl::PointCloud<pcl::PointXYZRGB>);
*cloudPTR = stitchedPointCloud;

std::cout << "Run the PCL visualizer. Block until window closes" << std::endl;
pcl::visualization::CloudViewer viewer("Simple Cloud Viewer");
viewer.showCloud(cloudPTR);
std::cout << "Press r to centre and zoom the viewer so that the entire cloud is visible" << std::endl;
std::cout << "Press q to me exit the viewer application" << std::endl;
std::cout << "Press q to exit the viewer application" << std::endl;
while(!viewer.wasStopped())
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,14 @@ int main(int argc, char **argv)
std::cout << "Got " << validPoints << " out of " << maxNumberOfPoints << " points" << std::endl;

// Simple Cloud Visualization
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloudPTR(new pcl::PointCloud<pcl::PointXYZRGB>);
boost::shared_ptr<pcl::PointCloud<pcl::PointXYZRGB>> cloudPTR(new pcl::PointCloud<pcl::PointXYZRGB>);
*cloudPTR = stitchedPointCloud;

std::cout << "Run the PCL visualizer. Block until window closes" << std::endl;
pcl::visualization::CloudViewer viewer("Simple Cloud Viewer");
viewer.showCloud(cloudPTR);
std::cout << "Press r to centre and zoom the viewer so that the entire cloud is visible" << std::endl;
std::cout << "Press q to me exit the viewer application" << std::endl;
std::cout << "Press q to exit the viewer application" << std::endl;
while(!viewer.wasStopped())
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,14 @@ int main(int argc, char **argv)
std::cout << "Got " << validPoints << " out of " << maxNumberOfPoints << " points" << std::endl;

// Simple Cloud Visualization
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloudPTR(new pcl::PointCloud<pcl::PointXYZRGB>);
boost::shared_ptr<pcl::PointCloud<pcl::PointXYZRGB>> cloudPTR(new pcl::PointCloud<pcl::PointXYZRGB>);
*cloudPTR = stitchedPointCloud;

std::cout << "Run the PCL visualizer. Block until window closes" << std::endl;
pcl::visualization::CloudViewer viewer("Simple Cloud Viewer");
viewer.showCloud(cloudPTR);
std::cout << "Press r to centre and zoom the viewer so that the entire cloud is visible" << std::endl;
std::cout << "Press q to me exit the viewer application" << std::endl;
std::cout << "Press q to exit the viewer application" << std::endl;
while(!viewer.wasStopped())
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This sample depends on ArUco libraries in OpenCV with extra modules (https://git

#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/visualization/pcl_visualizer.h>

#include <opencv2/aruco.hpp>

Expand Down Expand Up @@ -237,13 +237,13 @@ namespace
return gray;
}

pcl::PointCloud<pcl::PointXYZRGB>::Ptr roiBoxPointCloud(const Zivid::PointCloud &pointCloud,
const float roiBoxBottomLeftCornerX,
const float roiBoxBottomLeftCornerY,
const float roiBoxBottomLeftCornerZ,
const float roiBoxLength,
const float roiBoxWidth,
const float roiBoxHeight)
boost::shared_ptr<pcl::PointCloud<pcl::PointXYZRGB>> roiBoxPointCloud(const Zivid::PointCloud &pointCloud,
const float roiBoxBottomLeftCornerX,
const float roiBoxBottomLeftCornerY,
const float roiBoxBottomLeftCornerZ,
const float roiBoxLength,
const float roiBoxWidth,
const float roiBoxHeight)
{
const auto data = pointCloud.copyPointsXYZColorsRGBA();
const int height = data.height();
Expand Down Expand Up @@ -341,10 +341,10 @@ namespace
return zColorMap;
}

void visualizeDepthMap(const boost::shared_ptr<pcl::PointCloud<pcl::PointXYZRGB>> &pointCloud)
void visualizeDepthMap(const pcl::PointCloud<pcl::PointXYZRGB> &pointCloud)
{
// Converting to Depth map in OpenCV format
cv::Mat zColorMap = pointCloudToCvZ(*pointCloud);
cv::Mat zColorMap = pointCloudToCvZ(pointCloud);
// Visualizing Depth map
cv::namedWindow("Depth map", cv::WINDOW_AUTOSIZE);
cv::imshow("Depth map", zColorMap);
Expand All @@ -361,7 +361,7 @@ namespace
viewer->setCameraPosition(0, 0, -100, -1, 0, 0);

std::cout << "Press r to centre and zoom the viewer so that the entire cloud is visible" << std::endl;
std::cout << "Press q to me exit the viewer application" << std::endl;
std::cout << "Press q to exit the viewer application" << std::endl;
while(!viewer->wasStopped())
{
viewer->spinOnce(100);
Expand Down Expand Up @@ -442,19 +442,20 @@ int main()
<< "Height: " << roiBoxHeight << std::endl;

std::cout << "Filtering the point cloud beased on ROI Box" << std::endl;
const pcl::PointCloud<pcl::PointXYZRGB>::Ptr roiPointCloudPCL = roiBoxPointCloud(pointCloud,
roiBoxBottomLeftCornerX,
roiBoxBottomLeftCornerY,
roiBoxBottomLeftCornerZ,
roiBoxLength,
roiBoxWidth,
roiBoxHeight);
const boost::shared_ptr<pcl::PointCloud<pcl::PointXYZRGB>> roiPointCloudPCL =
roiBoxPointCloud(pointCloud,
roiBoxBottomLeftCornerX,
roiBoxBottomLeftCornerY,
roiBoxBottomLeftCornerZ,
roiBoxLength,
roiBoxWidth,
roiBoxHeight);

std::cout << "Displaying transformed point cloud after ROI Box filtering" << std::endl;
visualizePointCloud(roiPointCloudPCL);

std::cout << "Displaying depth map of the transformed point cloud after ROI Box filtering" << std::endl;
visualizeDepthMap(roiPointCloudPCL);
visualizeDepthMap(*roiPointCloudPCL);
}
catch(const std::exception &e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ map. For scenes with high dynamic range we combine multiple acquisitions to get
namespace
{
void visualizePointCloudAndNormalsPCL(
const boost::shared_ptr<pcl::PointCloud<pcl::PointXYZRGB>> &pointCloudPCL,
const boost::shared_ptr<pcl::PointCloud<pcl::PointXYZRGBNormal>> &pointCloudWithNormalsPCL)
const boost::shared_ptr<pcl::PointCloud<pcl::PointXYZRGB>> &pointCloud,
const boost::shared_ptr<pcl::PointCloud<pcl::PointXYZRGBNormal>> &pointCloudWithNormals)
{
auto viewer = boost::make_shared<pcl::visualization::PCLVisualizer>("Viewer");

int viewRgb(0);
viewer->createViewPort(0.0, 0.0, 0.5, 1.0, viewRgb);
viewer->addText("Cloud RGB", 0, 0, "RGBText", viewRgb);
viewer->addPointCloud<pcl::PointXYZRGB>(pointCloudPCL, "cloud", viewRgb);
viewer->addPointCloud<pcl::PointXYZRGB>(pointCloud, "cloud", viewRgb);

const int normalsSkipped = 10;
std::cout << "Note! 1 out of " << normalsSkipped << " normals are visualized" << std::endl;

int viewNormals(0);
viewer->createViewPort(0.5, 0.0, 1.0, 1.0, viewNormals);
viewer->addText("Cloud Normals", 0, 0, "NormalsText", viewNormals);
viewer->addPointCloud<pcl::PointXYZRGBNormal>(pointCloudWithNormalsPCL, "cloudNormals", viewNormals);
viewer->addPointCloudNormals<pcl::PointXYZRGBNormal, pcl::PointXYZRGBNormal>(pointCloudWithNormalsPCL,
pointCloudWithNormalsPCL,
viewer->addPointCloud<pcl::PointXYZRGBNormal>(pointCloudWithNormals, "cloudNormals", viewNormals);
viewer->addPointCloudNormals<pcl::PointXYZRGBNormal, pcl::PointXYZRGBNormal>(pointCloudWithNormals,
pointCloudWithNormals,
normalsSkipped,
1,
"normals",
Expand All @@ -42,34 +42,35 @@ namespace
viewer->setCameraPosition(0, 0, -100, 0, -1, 0);

std::cout << "Press r to centre and zoom the viewer so that the entire cloud is visible" << std::endl;
std::cout << "Press q to me exit the viewer application" << std::endl;
std::cout << "Press q to exit the viewer application" << std::endl;
while(!viewer->wasStopped())
{
viewer->spinOnce(100);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}

pcl::PointCloud<pcl::PointXYZRGB>::Ptr zividToPclPoints(const Zivid::Array2D<Zivid::PointXYZColorRGBA> &data)
boost::shared_ptr<pcl::PointCloud<pcl::PointXYZRGB>> convertToPCLPointCloud(
const Zivid::Array2D<Zivid::PointXYZColorRGBA> &data)
{
auto pointCloudPCL = boost::make_shared<pcl::PointCloud<pcl::PointXYZRGB>>();
pointCloudPCL->width = data.width();
pointCloudPCL->height = data.height();
pointCloudPCL->is_dense = false;
pointCloudPCL->points.resize(data.size());
auto pointCloud = boost::make_shared<pcl::PointCloud<pcl::PointXYZRGB>>();
pointCloud->width = data.width();
pointCloud->height = data.height();
pointCloud->is_dense = false;
pointCloud->points.resize(data.size());
for(size_t i = 0; i < data.size(); ++i)
{
pointCloudPCL->points[i].x = data(i).point.x; // NOLINT(cppcoreguidelines-pro-type-union-access)
pointCloudPCL->points[i].y = data(i).point.y; // NOLINT(cppcoreguidelines-pro-type-union-access)
pointCloudPCL->points[i].z = data(i).point.z; // NOLINT(cppcoreguidelines-pro-type-union-access)
pointCloudPCL->points[i].r = data(i).color.r; // NOLINT(cppcoreguidelines-pro-type-union-access)
pointCloudPCL->points[i].g = data(i).color.g; // NOLINT(cppcoreguidelines-pro-type-union-access)
pointCloudPCL->points[i].b = data(i).color.b; // NOLINT(cppcoreguidelines-pro-type-union-access)
pointCloud->points[i].x = data(i).point.x; // NOLINT(cppcoreguidelines-pro-type-union-access)
pointCloud->points[i].y = data(i).point.y; // NOLINT(cppcoreguidelines-pro-type-union-access)
pointCloud->points[i].z = data(i).point.z; // NOLINT(cppcoreguidelines-pro-type-union-access)
pointCloud->points[i].r = data(i).color.r; // NOLINT(cppcoreguidelines-pro-type-union-access)
pointCloud->points[i].g = data(i).color.g; // NOLINT(cppcoreguidelines-pro-type-union-access)
pointCloud->points[i].b = data(i).color.b; // NOLINT(cppcoreguidelines-pro-type-union-access)
}
return pointCloudPCL;
return pointCloud;
}

pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr zividToPclVisualizationNormals(
boost::shared_ptr<pcl::PointCloud<pcl::PointXYZRGBNormal>> convertToPCLVisualizationNormals(
const Zivid::Array2D<Zivid::PointXYZColorRGBA> &data,
const Zivid::Array2D<Zivid::NormalXYZ> &normals)
{
Expand Down Expand Up @@ -124,13 +125,13 @@ int main()

std::cout << "Creating PCL point cloud structure" << std::endl;
const auto data = pointCloud.copyData<Zivid::PointXYZColorRGBA>();
const auto pointCloudPCL = zividToPclPoints(data);
const auto pointCloudPCL = convertToPCLPointCloud(data);

std::cout << "Computing point cloud normals" << std::endl;
const auto normals = pointCloud.copyData<Zivid::NormalXYZ>();

std::cout << "Creating PCL normals structure suited for visualization" << std::endl;
const auto pointCloudWithNormalsPCL = zividToPclVisualizationNormals(data, normals);
const auto pointCloudWithNormalsPCL = convertToPCLVisualizationNormals(data, normals);

std::cout << "Visualizing normals" << std::endl;
visualizePointCloudAndNormalsPCL(pointCloudPCL, pointCloudWithNormalsPCL);
Expand Down
Loading

0 comments on commit 223e70c

Please sign in to comment.