libvisiontransfer  6.4.0
asynctransfer_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 #include <visiontransfer/deviceenumeration.h>
16 #include <visiontransfer/asynctransfer.h>
17 #include <visiontransfer/imagepair.h>
18 #include <iostream>
19 #include <exception>
20 #include <stdio.h>
21 
22 #ifdef _MSC_VER
23 // Visual studio does not come with snprintf
24 #define snprintf _snprintf_s
25 #endif
26 
27 using namespace visiontransfer;
28 
29 int main() {
30  try {
31  // Search for SceneScan devices
32  DeviceEnumeration deviceEnum;
33  DeviceEnumeration::DeviceList devices = deviceEnum.discoverDevices();
34  if(devices.size() == 0) {
35  std::cout << "No devices discovered!" << std::endl;
36  return -1;
37  }
38 
39  // Print devices
40  std::cout << "Discovered devices:" << std::endl;
41  for(unsigned int i = 0; i< devices.size(); i++) {
42  std::cout << devices[i].toString() << std::endl;
43  }
44  std::cout << std::endl;
45 
46  // Create an image transfer object that receives data from
47  // the first detected SceneScan
48  AsyncTransfer asyncTransfer(devices[0]);
49 
50  // Receive 100 images
51  for(int i=0; i<100; i++) {
52  std::cout << "Receiving image " << i << std::endl;
53 
54  // Receive image
55  ImagePair imagePair;
56  while(!asyncTransfer.collectReceivedImagePair(imagePair,
57  0.1 /*timeout*/)) {
58  // Keep on trying until reception is successful
59  }
60 
61  // Write both images one after another
62  for(int imageNumber = 0; imageNumber <=1; imageNumber++) {
63  // Create PGM file
64  char fileName[100];
65  snprintf(fileName, sizeof(fileName), "image%03d_%d.pgm", i,
66  imageNumber);
67 
68  imagePair.writePgmFile(imageNumber, fileName);
69  }
70  }
71  } catch(const std::exception& ex) {
72  std::cerr << "Exception occurred: " << ex.what() << std::endl;
73  }
74 
75  return 0;
76 }
DeviceList discoverDevices()
Discovers new devices and returns the list of all devices that have been found.
void writePgmFile(int imageNumber, const char *fileName) const
Writes one image of the pair to a PGM or PPM file.
Definition: imagepair.cpp:95
Class for asynchronous transfer of image pairs.
Definition: asynctransfer.h:33
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