libvisiontransfer  10.6.0
imagetransfer_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/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 /*
30  * NOTE: The use of ImageTransfer is only advised when operation
31  * without background threads is strictly required. For typical
32  * application use, we recommend to use AsyncTransfer instead
33  * (see asynctransfer_example.cpp).
34  *
35  * When using ImageTransfer directly, make sure to regularly poll
36  * using receiveImageSet(); i.e. limit the frame rate through
37  * the device settings, and not by reducing call frequency.
38  */
39 
40 int main() {
41  // Search for Nerian stereo devices
42  DeviceEnumeration deviceEnum;
43  DeviceEnumeration::DeviceList devices =
44  deviceEnum.discoverDevices();
45  if(devices.size() == 0) {
46  std::cout << "No devices discovered!" << std::endl;
47  return -1;
48  }
49 
50  // Print devices
51  std::cout << "Discovered devices:" << std::endl;
52  for(unsigned int i = 0; i< devices.size(); i++) {
53  std::cout << devices[i].toString() << std::endl;
54  }
55  std::cout << std::endl;
56 
57  // Create an image transfer object that receives data from
58  // the first detected device
59  ImageTransfer imageTransfer(devices[0]);
60 
61  // Receive 100 images
62  for(int imgNum=0; imgNum<100; imgNum++) {
63  std::cout << "Receiving image set " << imgNum << std::endl;
64 
65  // Receive image
66  ImageSet imageSet;
67  while(!imageTransfer.receiveImageSet(imageSet)) {
68  // Keep on trying until reception is successful
69  }
70 
71  // Write all included images one after another
72  for(int i = 0; i < imageSet.getNumberOfImages(); i++) {
73  // Create PGM file
74  char fileName[100];
75  snprintf(fileName, sizeof(fileName), "image%03d_%d.pgm", imgNum, i);
76  imageSet.writePgmFile(i, fileName);
77  }
78  }
79 
80  return 0;
81 }
visiontransfer::ImageSet::getNumberOfImages
int getNumberOfImages() const
Returns the number of images in this set.
Definition: imageset.h:431
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::ImageSet::writePgmFile
void writePgmFile(int imageNumber, const char *fileName) const
Writes one image of the set to a PGM or PPM file.
Definition: imageset.cpp:106
visiontransfer::ImageTransfer
Class for synchronous transfer of image sets.
Definition: imagetransfer.h:52
Allied Vision