libvisiontransfer  10.0.0
imagetransfer_example.cpp
1 /*******************************************************************************
2  * Copyright (c) 2022 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/imagetransfer.h>
17 #include <visiontransfer/imageset.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  // Search for Nerian stereo devices
31  DeviceEnumeration deviceEnum;
32  DeviceEnumeration::DeviceList devices =
33  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 device
48  ImageTransfer imageTransfer(devices[0]);
49 
50  // Receive 100 images
51  for(int imgNum=0; imgNum<100; imgNum++) {
52  std::cout << "Receiving image set " << imgNum << std::endl;
53 
54  // Receive image
55  ImageSet imageSet;
56  while(!imageTransfer.receiveImageSet(imageSet)) {
57  // Keep on trying until reception is successful
58  }
59 
60  // Write all included images one after another
61  for(int i = 0; i < imageSet.getNumberOfImages(); i++) {
62  // Create PGM file
63  char fileName[100];
64  snprintf(fileName, sizeof(fileName), "image%03d_%d.pgm", i,
65  imgNum);
66 
67  imageSet.writePgmFile(i, fileName);
68  }
69  }
70 
71  return 0;
72 }
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 set to a PGM or PPM file.
Definition: imageset.cpp:106
int getNumberOfImages() const
Returns the number of images in this set.
Definition: imageset.h:407
Class for synchronous transfer of image sets.
Definition: imagetransfer.h:36
A set of one to three images, but usually two (the left camera image and the disparity map)...
Definition: imageset.h:38
Allows for the discovery of devices in the network.
Nerian Vision Technologies