Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
grab1394CMU.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 * Video capture example based on CMU 1394 Digital Camera SDK.
32 */
33
40#include <iostream>
41#include <stdio.h>
42#include <stdlib.h>
43
44#include <visp3/core/vpConfig.h>
45#include <visp3/core/vpImage.h>
46#include <visp3/core/vpTime.h>
47#include <visp3/gui/vpDisplayFactory.h>
48#include <visp3/io/vpImageIo.h>
49#include <visp3/io/vpParseArgv.h>
50#include <visp3/sensor/vp1394CMUGrabber.h>
51
52#define GRAB_COLOR
53
54// List of allowed command line options
55#define GETOPTARGS "dhn:o:"
56
57#ifdef ENABLE_VISP_NAMESPACE
58using namespace VISP_NAMESPACE_NAME;
59#endif
60
61
62void usage(const char *name, const char *badparam, unsigned &nframes, std::string &opath);
63bool getOptions(int argc, const char **argv, bool &display, unsigned int &nframes, bool &save, std::string &opath);
64
75void usage(const char *name, const char *badparam, unsigned &nframes, std::string &opath)
76{
77 fprintf(stdout, "\n\
78Acquire images using CMU 1394 Digital Camera SDK (available under Windows only) and display\n\
79it using GDI or OpenCV if GDI is not available.\n\
80\n\
81SYNOPSIS\n\
82 %s [-d] [-n] [-o] [-h] \n",
83 name);
84
85 fprintf(stdout, "\n\
86OPTIONS: Default\n\
87 -d \n\
88 Turn off the display.\n\
89\n\
90 -n [%%u] %u\n\
91 Number of frames to acquire. \n\
92\n\
93 -o [%%s] \n\
94 Filename for image saving. \n\
95 Example: -o %s\n\
96 The %%d is for the image numbering.\n\
97\n\
98 -h \n\
99 Print the help.\n\
100\n",
101nframes, opath.c_str());
102 if (badparam) {
103 fprintf(stderr, "ERROR: \n");
104 fprintf(stderr, "\nBad parameter [%s]\n", badparam);
105 }
106}
123bool getOptions(int argc, const char **argv, bool &display, unsigned int &nframes, bool &save, std::string &opath)
124{
125 const char *optarg_;
126 int c;
127 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
128
129 switch (c) {
130 case 'd':
131 display = false;
132 break;
133 case 'n':
134 nframes = static_cast<unsigned int>(atoi(optarg_));
135 break;
136 case 'o':
137 save = true;
138 opath = optarg_;
139 break;
140 case 'h':
141 usage(argv[0], nullptr, nframes, opath);
142 return false;
143
144 default:
145 usage(argv[0], optarg_, nframes, opath);
146 return false;
147 }
148 }
149
150 if ((c == 1) || (c == -1)) {
151 // standalone param or error
152 usage(argv[0], nullptr, nframes, opath);
153 std::cerr << "ERROR: " << std::endl;
154 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
155 return false;
156 }
157
158 return true;
159}
160
167#if defined(VISP_HAVE_CMU1394)
168int main(int argc, const char **argv)
169{
170 bool opt_display = true;
171 unsigned nframes = 50;
172 bool save = false;
173
174// Declare an image. It size is not defined yet. It will be defined when the
175// image will acquired the first time.
176#ifdef GRAB_COLOR
177 vpImage<vpRGBa> I; // This is a color image (in RGBa format)
178#else
179 vpImage<unsigned char> I; // This is a B&W image
180#endif
181
182// Set default output image name for saving
183#ifdef GRAB_COLOR
184 // Color images will be saved in PGM P6 format
185 std::string opath = "C:/temp/I%04d.ppm";
186#else
187 // B&W images will be saved in PGM P5 format
188 std::string opath = "C:/temp/I%04d.pgm";
189#endif
190
191 // Read the command line options
192 if (getOptions(argc, argv, opt_display, nframes, save, opath) == false) {
193 return EXIT_FAILURE;
194 }
195
196 // Create the grabber
198 unsigned short gain_min, gain_max;
199 g.getGainMinMax(gain_min, gain_max);
200 std::cout << "Gain range [" << gain_min << ", " << gain_max << "]" << std::endl;
201 unsigned short shutter_min, shutter_max;
202 g.getShutterMinMax(shutter_min, shutter_max);
203 std::cout << "Shutter range [" << shutter_min << ", " << shutter_max << "]" << std::endl;
204 g.setFramerate(4); // 30 fps
205 std::cout << "Actual framerate: " << g.getFramerate() << std::endl;
206 g.setVideoMode(0, 0);
207 g.acquire(I);
208
209 std::cout << "Image size: width : " << I.getWidth() << " height: " << I.getHeight() << std::endl;
210
211#if (defined(VISP_HAVE_GDI) || defined(HAVE_OPENCV_HIGHGUI))
212
213// Creates a display
214#if defined(VISP_HAVE_DISPLAY)
215#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
216 std::shared_ptr<vpDisplay> display = vpDisplayFactory::createDisplay();
217#else
219#endif
220 if (opt_display) {
221 display->init(I, 100, 100, "Current image");
222 }
223#endif
224#endif
225
226 try {
227 double tbegin = 0, ttotal = 0;
228
229 ttotal = 0;
230 tbegin = vpTime::measureTimeMs();
231 // Loop for image acquisition and display
232 for (unsigned i = 0; i < nframes; i++) {
233 // Acquires an RGBa image
234 g.acquire(I);
235
236#if defined(VISP_HAVE_DISPLAY)
237 if (opt_display) {
238 // Displays the grabbed rgba image
241 if (vpDisplay::getClick(I, false)) // A click to exit
242 break;
243 }
244#endif
245
246 if (save) {
247 char buf[FILENAME_MAX];
248 snprintf(buf, FILENAME_MAX, opath.c_str(), i);
249 std::string filename(buf);
250 std::cout << "Write: " << filename << std::endl;
251 vpImageIo::write(I, filename);
252 }
253 double tend = vpTime::measureTimeMs();
254 double tloop = tend - tbegin;
255 tbegin = tend;
256 std::cout << "loop time: " << tloop << " ms" << std::endl;
257 ttotal += tloop;
258 }
259 std::cout << "Mean loop time: " << ttotal / nframes << " ms" << std::endl;
260 std::cout << "Mean frequency: " << 1000. / (ttotal / nframes) << " fps" << std::endl;
261
262#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11) && defined(VISP_HAVE_DISPLAY)
263 if (display != nullptr) {
264 delete display;
265 }
266#endif
267 return EXIT_SUCCESS;
268 }
269 catch (const vpException &e) {
270 std::cout << "Catch an exception: " << e << std::endl;
271#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11) && defined(VISP_HAVE_DISPLAY)
272 if (display != nullptr) {
273 delete display;
274 }
275#endif
276 return EXIT_FAILURE;
277 }
278}
279#else
280int main()
281{
282 std::cout << "This example requires CMU 1394 Digital Camera SDK. " << std::endl;
283 std::cout << "Tip if you are on a windows-like system:" << std::endl;
284 std::cout << "- Install CMU 1394 SDK, configure again ViSP using cmake and build again this example" << std::endl;
285 return EXIT_SUCCESS;
286}
287#endif
Firewire cameras video capture based on CMU 1394 Digital Camera SDK.
void getGainMinMax(unsigned short &min, unsigned short &max)
void setVideoMode(unsigned long format, unsigned long mode)
void acquire(vpImage< unsigned char > &I)
void setFramerate(unsigned long fps)
void getShutterMinMax(unsigned short &min, unsigned short &max)
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)
error that can be emitted by ViSP classes.
Definition vpException.h:60
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.
VISP_EXPORT double measureTimeMs()