libvisiontransfer  6.4.0
opencv_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 // 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/imagepair.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 SceneScan 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 SceneScan
45  ImageTransfer imageTransfer(devices[0]);
46 
47  // Receive one image
48  std::cout << "Receiving image..." << std::endl;
49  ImagePair imagePair;
50  while(!imageTransfer.receiveImagePair(imagePair)) {
51  }
52 
53  // Convert to OpenCV images
54  cv::Mat image0, image1;
55  imagePair.toOpenCVImage(0, image0);
56  imagePair.toOpenCVImage(1, image1);
57  cv::imwrite("image0.png", image0);
58  cv::imwrite("image1.png", image1);
59  std::cout << "Written image0.png and image1.png with OpenCV" << std::endl;
60 
61  } catch(const std::exception& ex) {
62  std::cerr << "Exception occurred: " << ex.what() << std::endl;
63  }
64 
65  return 0;
66 }
DeviceList discoverDevices()
Discovers new devices and returns the list of all devices that have been found.
void toOpenCVImage(int imageNumber, cv::Mat &dest, bool convertRgbToBgr=true)
Converts one image of the pair to an OpenCV image.
Class for synchronous transfer of image pairs.
Definition: imagetransfer.h:36
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