Visual Servoing Platform  version 3.6.1 under development (2024-05-18)
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 void usage(const char *name, const char *badparam, unsigned &nframes, std::string &opath);
61 bool getOptions(int argc, const char **argv, bool &display, unsigned int &nframes, bool &save, std::string &opath);
62 
73 void usage(const char *name, const char *badparam, unsigned &nframes, std::string &opath)
74 {
75  fprintf(stdout, "\n\
76 Acquire images using CMU 1394 Digital Camera SDK (available under Windows only) and display\n\
77 it using GDI or OpenCV if GDI is not available.\n\
78 \n\
79 SYNOPSIS\n\
80  %s [-d] [-n] [-o] [-h] \n",
81  name);
82 
83  fprintf(stdout, "\n\
84 OPTIONS: Default\n\
85  -d \n\
86  Turn off the display.\n\
87 \n\
88  -n [%%u] %u\n\
89  Number of frames to acquire. \n\
90 \n\
91  -o [%%s] \n\
92  Filename for image saving. \n\
93  Example: -o %s\n\
94  The %%d is for the image numbering.\n\
95 \n\
96  -h \n\
97  Print the help.\n\
98 \n",
99 nframes, opath.c_str());
100  if (badparam) {
101  fprintf(stderr, "ERROR: \n");
102  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
103  }
104 }
121 bool getOptions(int argc, const char **argv, bool &display, unsigned int &nframes, bool &save, std::string &opath)
122 {
123  const char *optarg_;
124  int c;
125  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
126 
127  switch (c) {
128  case 'd':
129  display = false;
130  break;
131  case 'n':
132  nframes = (unsigned int)atoi(optarg_);
133  break;
134  case 'o':
135  save = true;
136  opath = optarg_;
137  break;
138  case 'h':
139  usage(argv[0], nullptr, nframes, opath);
140  return false;
141  break;
142 
143  default:
144  usage(argv[0], optarg_, nframes, opath);
145  return false;
146  break;
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)
168 int 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(HAVE_OPENCV_HIGHGUI)
216 #elif defined(VISP_HAVE_GDI)
218 #endif
219  if (opt_display) {
220  display.init(I, 100, 100, "Current image");
221  }
222 #endif
223 
224  try {
225  double tbegin = 0, ttotal = 0;
226 
227  ttotal = 0;
228  tbegin = vpTime::measureTimeMs();
229  // Loop for image acquisition and display
230  for (unsigned i = 0; i < nframes; i++) {
231  // Acquires an RGBa image
232  g.acquire(I);
233 
234 #if (defined(VISP_HAVE_GDI) || defined(HAVE_OPENCV_HIGHGUI))
235  if (opt_display) {
236  // Displays the grabbed rgba image
238  vpDisplay::flush(I);
239  if (vpDisplay::getClick(I, false)) // A click to exit
240  break;
241  }
242 #endif
243 
244  if (save) {
245  char buf[FILENAME_MAX];
246  snprintf(buf, FILENAME_MAX, opath.c_str(), i);
247  std::string filename(buf);
248  std::cout << "Write: " << filename << std::endl;
249  vpImageIo::write(I, filename);
250  }
251  double tend = vpTime::measureTimeMs();
252  double tloop = tend - tbegin;
253  tbegin = tend;
254  std::cout << "loop time: " << tloop << " ms" << std::endl;
255  ttotal += tloop;
256  }
257  std::cout << "Mean loop time: " << ttotal / nframes << " ms" << std::endl;
258  std::cout << "Mean frequency: " << 1000. / (ttotal / nframes) << " fps" << std::endl;
259  return EXIT_SUCCESS;
260  }
261  catch (const vpException &e) {
262  std::cout << "Catch an exception: " << e << std::endl;
263  return EXIT_FAILURE;
264  }
265 }
266 #else
267 int main()
268 {
269  std::cout << "This example requires CMU 1394 Digital Camera SDK. " << std::endl;
270  std::cout << "Tip if you are on a windows-like system:" << std::endl;
271  std::cout << "- Install CMU 1394 SDK, configure again ViSP using cmake and build again this example" << std::endl;
272  return EXIT_SUCCESS;
273 }
274 #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:128
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:59
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:287
unsigned int getWidth() const
Definition: vpImage.h:245
unsigned int getHeight() const
Definition: vpImage.h:184
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
void display(vpImage< unsigned char > &I, const std::string &title)
Display a gray-scale image.
VISP_EXPORT double measureTimeMs()