Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
grab1394CMU.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Video capture example based on CMU 1394 Digital Camera SDK.
33  *
34 *****************************************************************************/
35 
42 #include <iostream>
43 #include <stdio.h>
44 #include <stdlib.h>
45 
46 #include <visp3/core/vpConfig.h>
47 #include <visp3/core/vpImage.h>
48 #include <visp3/core/vpTime.h>
49 #include <visp3/gui/vpDisplayGDI.h>
50 #include <visp3/gui/vpDisplayOpenCV.h>
51 #include <visp3/io/vpImageIo.h>
52 #include <visp3/io/vpParseArgv.h>
53 #include <visp3/sensor/vp1394CMUGrabber.h>
54 
55 #define GRAB_COLOR
56 
57 // List of allowed command line options
58 #define GETOPTARGS "dhn:o:"
59 
60 #ifdef ENABLE_VISP_NAMESPACE
61 using namespace VISP_NAMESPACE_NAME;
62 #endif
63 
64 
65 void usage(const char *name, const char *badparam, unsigned &nframes, std::string &opath);
66 bool getOptions(int argc, const char **argv, bool &display, unsigned int &nframes, bool &save, std::string &opath);
67 
78 void usage(const char *name, const char *badparam, unsigned &nframes, std::string &opath)
79 {
80  fprintf(stdout, "\n\
81 Acquire images using CMU 1394 Digital Camera SDK (available under Windows only) and display\n\
82 it using GDI or OpenCV if GDI is not available.\n\
83 \n\
84 SYNOPSIS\n\
85  %s [-d] [-n] [-o] [-h] \n",
86  name);
87 
88  fprintf(stdout, "\n\
89 OPTIONS: Default\n\
90  -d \n\
91  Turn off the display.\n\
92 \n\
93  -n [%%u] %u\n\
94  Number of frames to acquire. \n\
95 \n\
96  -o [%%s] \n\
97  Filename for image saving. \n\
98  Example: -o %s\n\
99  The %%d is for the image numbering.\n\
100 \n\
101  -h \n\
102  Print the help.\n\
103 \n",
104 nframes, opath.c_str());
105  if (badparam) {
106  fprintf(stderr, "ERROR: \n");
107  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
108  }
109 }
126 bool getOptions(int argc, const char **argv, bool &display, unsigned int &nframes, bool &save, std::string &opath)
127 {
128  const char *optarg_;
129  int c;
130  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
131 
132  switch (c) {
133  case 'd':
134  display = false;
135  break;
136  case 'n':
137  nframes = (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, nframes, opath);
145  return false;
146  break;
147 
148  default:
149  usage(argv[0], optarg_, nframes, opath);
150  return false;
151  break;
152  }
153  }
154 
155  if ((c == 1) || (c == -1)) {
156  // standalone param or error
157  usage(argv[0], nullptr, nframes, opath);
158  std::cerr << "ERROR: " << std::endl;
159  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
160  return false;
161  }
162 
163  return true;
164 }
165 
172 #if defined(VISP_HAVE_CMU1394)
173 int main(int argc, const char **argv)
174 {
175  bool opt_display = true;
176  unsigned nframes = 50;
177  bool save = false;
178 
179 // Declare an image. It size is not defined yet. It will be defined when the
180 // image will acquired the first time.
181 #ifdef GRAB_COLOR
182  vpImage<vpRGBa> I; // This is a color image (in RGBa format)
183 #else
184  vpImage<unsigned char> I; // This is a B&W image
185 #endif
186 
187 // Set default output image name for saving
188 #ifdef GRAB_COLOR
189  // Color images will be saved in PGM P6 format
190  std::string opath = "C:/temp/I%04d.ppm";
191 #else
192  // B&W images will be saved in PGM P5 format
193  std::string opath = "C:/temp/I%04d.pgm";
194 #endif
195 
196  // Read the command line options
197  if (getOptions(argc, argv, opt_display, nframes, save, opath) == false) {
198  return EXIT_FAILURE;
199  }
200 
201  // Create the grabber
203  unsigned short gain_min, gain_max;
204  g.getGainMinMax(gain_min, gain_max);
205  std::cout << "Gain range [" << gain_min << ", " << gain_max << "]" << std::endl;
206  unsigned short shutter_min, shutter_max;
207  g.getShutterMinMax(shutter_min, shutter_max);
208  std::cout << "Shutter range [" << shutter_min << ", " << shutter_max << "]" << std::endl;
209  g.setFramerate(4); // 30 fps
210  std::cout << "Actual framerate: " << g.getFramerate() << std::endl;
211  g.setVideoMode(0, 0);
212  g.acquire(I);
213 
214  std::cout << "Image size: width : " << I.getWidth() << " height: " << I.getHeight() << std::endl;
215 
216 #if (defined(VISP_HAVE_GDI) || defined(HAVE_OPENCV_HIGHGUI))
217 
218 // Creates a display
219 #if defined(HAVE_OPENCV_HIGHGUI)
220  vpDisplayOpenCV display;
221 #elif defined(VISP_HAVE_GDI)
222  vpDisplayGDI display;
223 #endif
224  if (opt_display) {
225  display.init(I, 100, 100, "Current image");
226  }
227 #endif
228 
229  try {
230  double tbegin = 0, ttotal = 0;
231 
232  ttotal = 0;
233  tbegin = vpTime::measureTimeMs();
234  // Loop for image acquisition and display
235  for (unsigned i = 0; i < nframes; i++) {
236  // Acquires an RGBa image
237  g.acquire(I);
238 
239 #if (defined(VISP_HAVE_GDI) || defined(HAVE_OPENCV_HIGHGUI))
240  if (opt_display) {
241  // Displays the grabbed rgba image
243  vpDisplay::flush(I);
244  if (vpDisplay::getClick(I, false)) // A click to exit
245  break;
246  }
247 #endif
248 
249  if (save) {
250  char buf[FILENAME_MAX];
251  snprintf(buf, FILENAME_MAX, opath.c_str(), i);
252  std::string filename(buf);
253  std::cout << "Write: " << filename << std::endl;
254  vpImageIo::write(I, filename);
255  }
256  double tend = vpTime::measureTimeMs();
257  double tloop = tend - tbegin;
258  tbegin = tend;
259  std::cout << "loop time: " << tloop << " ms" << std::endl;
260  ttotal += tloop;
261  }
262  std::cout << "Mean loop time: " << ttotal / nframes << " ms" << std::endl;
263  std::cout << "Mean frequency: " << 1000. / (ttotal / nframes) << " fps" << std::endl;
264  return EXIT_SUCCESS;
265  }
266  catch (const vpException &e) {
267  std::cout << "Catch an exception: " << e << std::endl;
268  return EXIT_FAILURE;
269  }
270 }
271 #else
272 int main()
273 {
274  std::cout << "This example requires CMU 1394 Digital Camera SDK. " << std::endl;
275  std::cout << "Tip if you are on a windows-like system:" << std::endl;
276  std::cout << "- Install CMU 1394 SDK, configure again ViSP using cmake and build again this example" << std::endl;
277  return EXIT_SUCCESS;
278 }
279 #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)
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
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: vpImageIo.cpp:291
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
VISP_EXPORT double measureTimeMs()