libvisiontransfer  10.6.0
opencv_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 // OpenCV headers must be included first!
16 #include <opencv2/opencv.hpp>
17 
18 #include <visiontransfer/deviceenumeration.h>
19 #include <visiontransfer/imagetransfer.h>
20 #include <visiontransfer/imageset.h>
21 #include <visiontransfer/reconstruct3d.h>
22 #include <iostream>
23 #include <exception>
24 #include <stdio.h>
25 
26 #ifdef _MSC_VER
27 // Visual studio does not come with snprintf
28 #define snprintf _snprintf_s
29 #endif
30 
31 using namespace visiontransfer;
32 
33 int main() {
34  try {
35  // Search for Nerian stereo devices
36  DeviceEnumeration deviceEnum;
37  DeviceEnumeration::DeviceList devices = deviceEnum.discoverDevices();
38  if(devices.size() == 0) {
39  std::cout << "No devices discovered!" << std::endl;
40  return -1;
41  }
42 
43  // Create an image transfer object that receives data from
44  // the first detected device
45  ImageTransfer imageTransfer(devices[0]);
46 
47  // Receive one image
48  std::cout << "Receiving image set..." << std::endl;
49  ImageSet imageSet;
50  while(!imageTransfer.receiveImageSet(imageSet)) {
51  }
52 
53  // Convert to OpenCV images
54  for (int imageNumber=0; imageNumber < imageSet.getNumberOfImages(); imageNumber++) {
55  char fileName[100];
56  cv::Mat convertedImage;
57  snprintf(fileName, sizeof(fileName), "image%d.pgm", imageNumber);
58  imageSet.toOpenCVImage(imageNumber, convertedImage);
59  cv::imwrite(fileName, convertedImage);
60  std::cout << "Written " << fileName << " with OpenCV" << std::endl;
61  }
62 
63  } catch(const std::exception& ex) {
64  std::cerr << "Exception occurred: " << ex.what() << std::endl;
65  }
66 
67  return 0;
68 }
visiontransfer::ImageSet::toOpenCVImage
void toOpenCVImage(int imageNumber, cv::Mat &dest, bool convertRgbToBgr=true)
Converts one image of the set to an OpenCV image.
Definition: imageset-opencv.h:39
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::ImageTransfer
Class for synchronous transfer of image sets.
Definition: imagetransfer.h:52
Allied Vision