Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
testPylonGrabber.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Basler cameras video capture using Pylon SDK.
32  */
33 
40 #include <visp3/core/vpConfig.h>
41 #include <visp3/core/vpDebug.h>
42 
43 #include <iostream>
44 #include <string>
45 
46 #if defined(VISP_HAVE_PYLON)
47 
48 #include <visp3/core/vpIoTools.h>
49 #include <visp3/io/vpImageIo.h>
50 #include <visp3/sensor/vpPylonFactory.h>
51 
52 int main()
53 {
54 #ifdef ENABLE_VISP_NAMESPACE
55  using namespace VISP_NAMESPACE_NAME;
56 #endif
57  try {
58  std::cout << "Basler camera test with Pylon in progress..." << std::endl;
59 
60  // Get the user name
61  std::string username;
62  vpIoTools::getUserName(username);
63  std::string outputpath = "/tmp/" + username;
64  vpIoTools::makeDirectory(outputpath);
65 
66  // Creation of an empty image container
68 
70  // Creation of a framegrabber
72  std::string guid;
73 
74  // Get the number of cameras connected on the bus
75  unsigned int ncameras; // Number of cameras on the bus
76  ncameras = g->getNumCameras();
77  for (unsigned int i = 0; i < ncameras; i++) {
78  g->setCameraIndex(i);
79  guid = g->getCameraSerial(i);
80  std::cout << "Detected camera with serial: " << guid << std::endl;
81  }
82  // If more than one camera connected, use the first one
83  if (ncameras > 1) {
84  g->setCameraIndex(0);
85  guid = g->getCameraSerial(0);
86  std::cout << "Use camera with serial: " << guid << std::endl;
87  // to be sure that the setCamera() in the next line with
88  // guid as parameter works
89  g->setCameraIndex(0);
90  g->setCameraSerial(guid);
91  }
92  g->getCameraInfo(std::cout);
93 
94  std::cout << "Frame rate: " << g->getFrameRate() << std::endl;
95  std::cout << "Gain: " << g->getGain() << std::endl;
96  std::cout << "Gamma: " << g->getGamma() << std::endl;
97  std::cout << "Exposure time (ms): " << g->getExposure() << std::endl;
98  float blackLevel = g->getBlackLevel();
99  std::cout << "Black level: " << blackLevel << std::endl;
100 
101  for (int i = 0; i < 10; i++)
102  g->acquire(I);
103  g->close();
104  std::cout << "Current image size: " << g->getWidth() << "x" << g->getHeight() << std::endl;
105 
106  std::string filename = outputpath + "/imagetest1.pgm";
107  std::cout << "Write image: " << filename << std::endl;
108  vpImageIo::write(I, filename);
109 
110  std::cout << "New connection..." << std::endl;
111  g->open(I);
112  g->close();
113 
114  std::cout << "New connection..." << std::endl;
115  g->open(I);
116  g->close();
117  filename = outputpath + "/imagetest2.pgm";
118  std::cout << "Write image: " << filename << std::endl;
119  vpImageIo::write(I, filename);
120  }
121  catch (const vpException &e) {
122  vpCERROR << e.what() << std::endl;
123  }
124  catch (const std::exception &e) {
125  vpCERROR << e.what() << std::endl;
126  }
127  catch (...) {
128  vpCERROR << "Failure: exit" << std::endl;
129  }
130 }
131 #else
132 int main()
133 {
134 #ifdef ENABLE_VISP_NAMESPACE
135  using namespace VISP_NAMESPACE_NAME;
136 #endif
137  vpTRACE("Basler Pylon grabber capabilities are not available...\n"
138  "You should install pylon SDK to use this binary.");
139 }
140 
141 #endif
error that can be emitted by ViSP classes.
Definition: vpException.h:60
const char * what() const
Definition: vpException.cpp:71
unsigned int getWidth() const
Return the number of columns in the image.
unsigned int getHeight() const
Return the number of rows in the image.
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:291
static std::string getUserName()
Definition: vpIoTools.cpp:285
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:550
Factory singleton class to create vpPylonGrabber subclass instances.
static vpPylonFactory & instance()
Get the vpPylonFactory singleton.
@ BASLER_GIGE
Basler GigE camera.
vpPylonGrabber * createPylonGrabber(DeviceClass dev_class)
Create an object of vpPylonGrabber.
virtual void close()=0
Stop active camera capturing images and disconnect the active camera.
virtual float getFrameRate()=0
virtual unsigned int getNumCameras()=0
Get the number of cameras of specific subclasses. GigE, USB, etc.
virtual void setCameraSerial(const std::string &serial)=0
virtual float getBlackLevel()=0
virtual float getGain()=0
virtual void acquire(vpImage< unsigned char > &I)=0
virtual float getExposure()=0
virtual void open(vpImage< unsigned char > &I)=0
virtual std::string getCameraSerial(unsigned int index)=0
virtual std::ostream & getCameraInfo(std::ostream &os)=0
virtual void setCameraIndex(unsigned int index)=0
virtual float getGamma()=0