libvisiontransfer  10.6.0
pcl_example.cpp
1 /*******************************************************************************
2  * Copyright (c) 2023 Allied Vision Technologies GmbH
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *******************************************************************************/
14 
15 // PCL headers must be included first!
16 #include <pcl/pcl_base.h>
17 #include <pcl/point_types.h>
18 #include <pcl/io/pcd_io.h>
19 #include <pcl/filters/extract_indices.h>
20 
21 #include <visiontransfer/deviceenumeration.h>
22 #include <visiontransfer/imagetransfer.h>
23 #include <visiontransfer/imageset.h>
24 #include <visiontransfer/reconstruct3d.h>
25 #include <iostream>
26 #include <exception>
27 #include <stdio.h>
28 
29 using namespace visiontransfer;
30 
31 #define RGB_CLOUD // Undefine for monochrome cameras
32 
33 #ifdef RGB_CLOUD
34  typedef pcl::PointXYZRGB PclPointType;
35 #else
36  typedef pcl::PointXYZI PclCloudType;
37 #endif
38 
39 int main() {
40  try {
41  // Search for Nerian stereo devices
42  DeviceEnumeration deviceEnum;
43  DeviceEnumeration::DeviceList devices = deviceEnum.discoverDevices();
44  if(devices.size() == 0) {
45  std::cout << "No devices discovered!" << std::endl;
46  return -1;
47  }
48 
49  // Create an image transfer object that receives data from
50  // the first detected device
51  ImageTransfer imageTransfer(devices[0]);
52 
53  // Receive one image
54  std::cout << "Receiving image set ..." << std::endl;
55  ImageSet imageSet;
56  while(!imageTransfer.receiveImageSet(imageSet)) {
57  }
58 
59  // Project to PCL point cloud
60  Reconstruct3D recon3d;
61  pcl::PointCloud<PclPointType>::Ptr cloud;
62 #ifdef RGB_CLOUD
63  cloud = recon3d.createXYZRGBCloud(imageSet, "world");
64 #else
65  cloud = recon3d.createXYZICloud(imageSet, "world");
66 #endif
67 
68  // Extract all points up to 5 meters
69  pcl::PointIndices::Ptr inliers(new pcl::PointIndices());
70  for (unsigned int i = 0; i < cloud->size(); i++) {
71  if(cloud->points[i].z < 5) {
72  inliers->indices.push_back(i);
73  }
74  }
75  pcl::ExtractIndices<PclPointType> extract;
76  extract.setInputCloud(cloud);
77  extract.setIndices(inliers);
78  extract.filterDirectly(cloud);
79 
80  // Write point cloud
81  pcl::io::savePCDFile("cloud.pcd", *cloud, false);
82  std::cout << "Written point cloud to cloud.pcd" << std::endl;
83 
84  } catch(const std::exception& ex) {
85  std::cerr << "Exception occurred: " << ex.what() << std::endl;
86  }
87 
88  return 0;
89 }
visiontransfer::DeviceEnumeration::discoverDevices
DeviceList discoverDevices()
Discovers new devices and returns the list of all devices that have been found.
Definition: deviceenumeration.h:66
visiontransfer::DeviceEnumeration
Allows for the discovery of devices in the network.
Definition: deviceenumeration.h:42
visiontransfer::ImageSet
A set of one to three images, but usually two (the left camera image and the disparity map)....
Definition: imageset.h:50
visiontransfer::Reconstruct3D
Transforms a disparity map into a set of 3D points.
Definition: reconstruct3d.h:47
visiontransfer::Reconstruct3D::createXYZICloud
pcl::PointCloud< pcl::PointXYZI >::Ptr createXYZICloud(const ImageSet &imageSet, const char *frameId, unsigned short minDisparity=0)
Projects the given disparity map to a PCL point cloud, including pixel intensities.
Definition: reconstruct3d-pcl.h:64
visiontransfer::Reconstruct3D::createXYZRGBCloud
pcl::PointCloud< pcl::PointXYZRGB >::Ptr createXYZRGBCloud(const ImageSet &imageSet, const char *frameId, unsigned short minDisparity=0)
Projects the given disparity map to a PCL point cloud, including pixel RGB data.
Definition: reconstruct3d-pcl.h:107
visiontransfer::ImageTransfer
Class for synchronous transfer of image sets.
Definition: imagetransfer.h:52
Allied Vision