Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
grabFlyCapture.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 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 * Acquire images using FlyCapture SDK.
32 */
33
40
41#include <iostream>
42
43#include <visp3/core/vpConfig.h>
44
45#if defined(VISP_HAVE_FLYCAPTURE)
46
47#include <visp3/core/vpImage.h>
48#include <visp3/core/vpImageConvert.h>
49#include <visp3/gui/vpDisplayFactory.h>
50#include <visp3/io/vpImageIo.h>
51#include <visp3/io/vpParseArgv.h>
52#include <visp3/sensor/vpFlyCaptureGrabber.h>
53
54#define GETOPTARGS "cdhi:n:o:"
55
56#ifdef ENABLE_VISP_NAMESPACE
57using namespace VISP_NAMESPACE_NAME;
58#endif
59
70void usage(const char *name, const char *badparam, unsigned int icamera, std::string &opath)
71{
72 fprintf(stdout, "\n\
73Acquire and display images using PointGrey FlyCapture SDK.\n\
74\n\
75SYNOPSIS\n\
76 %s [-c] [-d] [-i <camera index>] [-o <output image filename>] [-h] \n",
77 name);
78
79 fprintf(stdout, "\n\
80OPTIONS: Default\n\
81 -c \n\
82 Disable mouse click and acquire only 10 images.\n\
83\n\
84 -d \n\
85 Turn off the display.\n\
86\n\
87 -i [%%d] %u\n\
88 Camera index to connect (0 for the first one). \n\
89\n\
90 -o [%%s]\n\
91 Filename for image saving. \n\
92 Example: -o %s\n\
93 The %%d is for the image numbering.\n\
94\n\
95 -h \n\
96 Print the help.\n\
97\n",
98icamera, opath.c_str());
99
100 if (badparam) {
101 fprintf(stderr, "ERROR: \n");
102 fprintf(stderr, "\nBad parameter [%s]\n", badparam);
103 }
104}
105
122bool getOptions(int argc, const char **argv, bool &display, bool &click, bool &save, std::string &opath,
123 unsigned int &icamera)
124{
125 const char *optarg_;
126 int c;
127 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
128
129 switch (c) {
130 case 'c':
131 click = false;
132 break;
133 case 'd':
134 display = false;
135 break;
136 case 'i':
137 icamera = static_cast<unsigned int>(atoi(optarg_));
138 break;
139 case 'o':
140 save = true;
141 opath = optarg_;
142 break;
143 case 'h':
144 usage(argv[0], nullptr, icamera, opath);
145 return false;
146
147 default:
148 usage(argv[0], optarg_, icamera, opath);
149 return false;
150 }
151 }
152
153 if ((c == 1) || (c == -1)) {
154 // standalone param or error
155 usage(argv[0], nullptr, icamera, opath);
156 std::cerr << "ERROR: " << std::endl;
157 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
158 return false;
159 }
160
161 return true;
162}
163
164// usage: binary <device name>
165// device name: 0 is the default to dial with the first camera,
166// 1 to dial with a second camera attached to the computer
167int main(int argc, const char **argv)
168{
169#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
170 std::shared_ptr<vpDisplay> display;
171#else
172 vpDisplay *display = nullptr;
173#endif
174 try {
175 bool opt_display = true;
176 bool opt_click = true;
177 bool opt_save = false;
178 unsigned int opt_icamera = 0;
179 std::string opt_opath = "I%04d.pgm";
180 // vpImage<vpRGBa> I; // for color images
181 vpImage<unsigned char> I; // for gray images
182
183 // Read the command line options
184 if (getOptions(argc, argv, opt_display, opt_click, opt_save, opt_opath, opt_icamera) == false) {
185 return EXIT_SUCCESS;
186 }
187
188 std::cout << "Use device : " << opt_icamera << std::endl;
190 g.setCameraIndex(opt_icamera); // open the default camera
191 g.open(I);
192 std::cout << "Camera serial: " << g.getCameraSerial(g.getCameraIndex()) << std::endl;
193 std::cout << "Image size : " << I.getWidth() << " " << I.getHeight() << std::endl;
194
195 if (opt_display) {
196#if defined(VISP_HAVE_DISPLAY)
197#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
199#else
201#endif
202#else
203 std::cout << "No image viewer is available..." << std::endl;
204#endif
205 }
206
207 for (;;) {
208 g.acquire(I); // get a new frame from camera
209
210 if (opt_save) {
211 static unsigned int frame = 0;
212 char buf[FILENAME_MAX];
213 snprintf(buf, FILENAME_MAX, opt_opath.c_str(), frame++);
214 std::string filename(buf);
215 std::cout << "Write: " << filename << std::endl;
216 vpImageIo::write(I, filename);
217 }
218
220 vpDisplay::displayText(I, 10, 10, "A click to quit...", vpColor::red);
222 if (opt_click && opt_display) {
223 if (vpDisplay::getClick(I, false) == true)
224 break;
225 }
226 else {
227 static unsigned int cpt = 0;
228 if (cpt++ == 10)
229 break;
230 }
231 }
232#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
233 if (display != nullptr) {
234 delete display;
235 }
236#endif
237
238 // The camera connection will be closed automatically in vpFlyCapture
239 // destructor
240 return EXIT_SUCCESS;
241 }
242 catch (const vpException &e) {
243#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
244 if (display != nullptr) {
245 delete display;
246 }
247#endif
248 std::cout << "Catch an exception: " << e.getStringMessage() << std::endl;
249 return EXIT_FAILURE;
250 }
251}
252
253#else
254int main()
255{
256 std::cout << "You do not have PointGrey FlyCapture SDK enabled..." << std::endl;
257 std::cout << "Tip:" << std::endl;
258 std::cout << "- Install FlyCapture SDK, configure again ViSP using cmake and build again this example" << std::endl;
259 return EXIT_SUCCESS;
260}
261#endif
static const vpColor red
Definition vpColor.h:198
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Definition vpException.h:60
void setCameraIndex(unsigned int index)
static unsigned int getCameraSerial(unsigned int index)
void open(vpImage< unsigned char > &I)
void acquire(vpImage< unsigned char > &I)
unsigned int getCameraIndex() const
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition of the vpImage class member functions.
Definition vpImage.h:131
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.