libvisiontransfer  6.4.0
pcl_example.cpp
1 /*******************************************************************************
2  * Copyright (c) 2019 Nerian Vision 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/imagepair.h>
24 #include <visiontransfer/reconstruct3d.h>
25 #include <iostream>
26 #include <exception>
27 #include <stdio.h>
28 
29 #ifdef _MSC_VER
30 // Visual studio does not come with snprintf
31 #define snprintf _snprintf_s
32 #endif
33 
34 using namespace visiontransfer;
35 
36 int main() {
37  try {
38  // Search for SceneScan devices
39  DeviceEnumeration deviceEnum;
40  DeviceEnumeration::DeviceList devices = deviceEnum.discoverDevices();
41  if(devices.size() == 0) {
42  std::cout << "No devices discovered!" << std::endl;
43  return -1;
44  }
45 
46  // Create an image transfer object that receives data from
47  // the first detected SceneScan
48  ImageTransfer imageTransfer(devices[0]);
49 
50  // Receive one image
51  std::cout << "Receiving image..." << std::endl;
52  ImagePair imagePair;
53  while(!imageTransfer.receiveImagePair(imagePair)) {
54  }
55 
56  // Project to PCL point cloud
57  Reconstruct3D recon3d;
58  pcl::PointCloud<pcl::PointXYZI>::Ptr cloud = recon3d.createXYZICloud(imagePair, "world");
59 
60  // Extract all points up to 5 meters
61  pcl::PointIndices::Ptr inliers(new pcl::PointIndices());
62  for (unsigned int i = 0; i < cloud->size(); i++) {
63  if(cloud->points[i].z < 5) {
64  inliers->indices.push_back(i);
65  }
66  }
67  pcl::ExtractIndices<pcl::PointXYZI> extract;
68  extract.setInputCloud(cloud);
69  extract.setIndices(inliers);
70  extract.filterDirectly(cloud);
71 
72  // Write point cloud
73  pcl::io::savePCDFile("cloud.pcd", *cloud, false);
74  std::cout << "Written point cloud to cloud.pcd" << std::endl;
75 
76  } catch(const std::exception& ex) {
77  std::cerr << "Exception occurred: " << ex.what() << std::endl;
78  }
79 
80  return 0;
81 }
Transforms a disparity map into a set of 3D points.
Definition: reconstruct3d.h:31
DeviceList discoverDevices()
Discovers new devices and returns the list of all devices that have been found.
Class for synchronous transfer of image pairs.
Definition: imagetransfer.h:36
pcl::PointCloud< pcl::PointXYZI >::Ptr createXYZICloud(const ImagePair &imagePair, const char *frameId, unsigned short minDisparity=0)
Projects the given disparity map to a PCL point cloud, including pixel intensities.
Allows for the discovery of devices in the network.
A set of two images, which are usually the left camera image and the disparity map.
Definition: imagepair.h:33
Nerian Vision Technologies