libvisiontransfer  10.6.0
reconstruct3d_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 #include <visiontransfer/deviceenumeration.h>
16 #include <visiontransfer/asynctransfer.h>
17 #include <visiontransfer/imageset.h>
18 #include <visiontransfer/reconstruct3d.h>
19 #include <iostream>
20 #include <exception>
21 #include <stdio.h>
22 
23 #ifdef _MSC_VER
24 // Visual studio does not come with snprintf
25 #define snprintf _snprintf_s
26 #endif
27 
28 using namespace visiontransfer;
29 
30 int main() {
31  try {
32  // Search for Nerian stereo devices
33  DeviceEnumeration deviceEnum;
34  DeviceEnumeration::DeviceList devices =
35  deviceEnum.discoverDevices();
36  if(devices.size() == 0) {
37  std::cout << "No devices discovered!" << std::endl;
38  return -1;
39  }
40 
41  // Print devices
42  std::cout << "Discovered devices:" << std::endl;
43  for(unsigned int i = 0; i< devices.size(); i++) {
44  std::cout << devices[i].toString() << std::endl;
45  }
46  std::cout << std::endl;
47 
48  // Create an image transfer object that receives data from
49  // the first detected device
50  AsyncTransfer asyncTransfer(devices[0]);
51 
52  // Receive 100 images
53  for(int imgNum=0; imgNum<100; imgNum++) {
54  std::cout << "Receiving image set " << imgNum << std::endl;
55 
56  // Receive image
57  ImageSet imageSet;
58  while(!asyncTransfer.collectReceivedImageSet(imageSet,
59  0.1 /*timeout*/)) {
60  // Keep on trying until reception is successful
61  }
62 
63  // Project to 3D pointcloud
64  Reconstruct3D recon3d;
65  float* pointcloud = recon3d.createPointMap(imageSet, 0 /*minimum disparity*/);
66 
67  // ... insert additional code to process pointcloud ...
68 
69  // Write PLY file
70  char fileName[100];
71  snprintf(fileName, sizeof(fileName), "pointcloud%03d.ply", imgNum);
72  recon3d.writePlyFile(fileName, imageSet,
73  2.0, /* max distance in meters*/
74  true /* binary format*/);
75  }
76  } catch(const std::exception& ex) {
77  std::cerr << "Exception occurred: " << ex.what() << std::endl;
78  }
79 
80  return 0;
81 }
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::Reconstruct3D::writePlyFile
void writePlyFile(const char *file, const ImageSet &imageSet, double maxZ=(std::numeric_limits< double >::max)(), bool binary=false, ColorSource colSource=COLOR_AUTO, unsigned short maxDisparity=0xFFF)
Projects the given disparity map to 3D points and exports the result to a PLY file.
Definition: reconstruct3d.cpp:113
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::AsyncTransfer
Class for asynchronous transfer of image sets.
Definition: asynctransfer.h:45
visiontransfer::Reconstruct3D::createPointMap
float * createPointMap(const ImageSet &imageSet, unsigned short minDisparity=0, unsigned short maxDisparity=0xFFF)
Reconstructs the 3D location of each pixel in the given disparity map.
Definition: reconstruct3d.cpp:91
Allied Vision