Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
grab1394Two.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 * Firewire cameras video capture.
33 *
34*****************************************************************************/
35
49#include <iostream>
50#include <list>
51#include <sstream>
52#include <stdio.h>
53#include <stdlib.h>
54#include <visp3/core/vpConfig.h>
55#include <visp3/core/vpDebug.h>
56#if defined(VISP_HAVE_DC1394)
57
58#include <visp3/core/vpDisplay.h>
59#include <visp3/core/vpImage.h>
60#include <visp3/core/vpRGBa.h>
61#include <visp3/core/vpTime.h>
62#include <visp3/gui/vpDisplayX.h>
63#include <visp3/io/vpImageIo.h>
64#include <visp3/io/vpParseArgv.h>
65#include <visp3/sensor/vp1394TwoGrabber.h>
66
67#define GRAB_CxOLOR
68
69// List of allowed command line options
70#define GETOPTARGS "b:c:df:g:hH:L:mn:io:p:rsT:v:W:"
71#define DUAL_ACQ
72
88void usage(const char *name, const char *badparam, unsigned int camera, const unsigned int &nframes,
89 const std::string &opath, const unsigned int &roi_left, const unsigned int &roi_top,
90 const unsigned int &roi_width, const unsigned int &roi_height, const unsigned int &ringbuffersize,
91 const unsigned int &panControl)
92{
93 if (badparam)
94 fprintf(stderr, "\nERROR: Bad parameter [%s]\n", badparam);
95
96 fprintf(stderr, "\n\
97SYNOPSIS\n\
98 %s [-v <video mode>] [-f <framerate>] \n\
99 [-g <color coding>] [-c <camera id>] [-m] [-n <frames>] \n\
100 [-i] [-s] [-d] [-o <filename>] [-L <format 7 roi left position>] \n\
101 [-T <format 7 roi top position>] [-W <format 7 roi width>] \n\
102 [-H <format 7 roi height>] [-b <ring buffer size>] \n\
103 [-p <pan control value>] [-R] [-h]\n\
104 \n\
105DESCRIPTION\n\
106 Test for firewire camera image acquisition.\n\
107 \n\
108EXAMPLES\n\
109 \n\
110 %s -s\n\
111 Indicates the current settings for the first camera found on the bus.\n\n\
112 %s -i\n\
113 Gives information on the first camera found on the bus.\n\n\
114 %s -s -m\n\
115 Indicates the current settings for all the cameras found on the bus.\n\n\
116 %s -i -m\n\
117 Gives information on all the cameras found on the bus.\n\
118 %s -c 1\n\
119 Grab images from camera 1.\n\n\
120 %s -m\n\
121 Grab images from all the cameras.\n\n\
122 \n\
123 If a stereo camera is connected to the bus like the PointGrey Bumblebee,\n\
124 you may set the pan control to select the camera view:\n\
125 %s -p 0\n\
126 Transmit right imge.\n\
127 %s -p 1\n\
128 Transmit left imge.\n\
129 \n\
130OPTIONS Default\n\
131 -v [%%u] : Video mode to set for the active camera.\n\
132 Use -s option so see which are the supported \n\
133 video modes. You can select the active \n\
134 camera using -c option.\n\
135 \n\
136 -f [%%u] : Framerate to set for the active camera.\n\
137 Use -s option so see which are the supported \n\
138 framerates. You can select the active \n\
139 camera using -c option.\n\
140 \n\
141 -g [%%u] : Color coding to set for the active camera\n\
142 in format 7 video mode. Use -s option so see if \n\
143 format 7 is supported by the camera and if so, \n\
144 which are the supported color codings. You can \n\
145 select the active camera using -c option.\n\
146 See -t <top>, -l <left>, -w <width>, \n\
147 -h <height> option to set format 7 roi.\n\
148 \n\
149 -L [%%u] : Format 7 region of interest (roi) left %u\n\
150 position. This option is only used if video\n\
151 mode is format 7.\n\
152 \n\
153 -T [%%u] : Format 7 region of interest (roi) top %u\n\
154 position. This option is only used if video\n\
155 mode is format 7.\n\
156 \n\
157 -W [%%u] : Format 7 region of interest (roi) width. %u\n\
158 Is set to zero, use the maximum width. This\n\
159 option is only used if video mode is format 7.\n\
160 \n\
161 -H [%%u] : Format 7 region of interest (roi) height. %u\n\
162 Is set to zero, use the maximum height. This\n\
163 option is only used if video mode is format 7.\n\
164 \n\
165 -c [%%u] : Active camera identifier. %u\n\
166 Zero is for the first camera found on the bus.\n\
167 \n\
168 -m : Flag to active multi camera acquisition. \n\
169 You need at least two cameras connected on \n\
170 the bus.\n\
171 \n\
172 -n [%%u] : Number of frames to acquire. %u\n\
173 \n\
174 -i : Flag to print camera information.\n\
175 \n\
176 -s : Print camera settings capabilities such as \n\
177 video mode and framerates available and exit.\n\
178 \n\
179 -d : Flag to turn off image display.\n\
180 \n\
181 -b [%%u] : Ring buffer size used during capture %u\n\
182 \n\
183 -p [%%u] : Pan control value used to control single or %u\n\
184 multiple image transmission from stereo vision \n\
185 cameras by setting the PAN register 0x884.\n\
186 \n\
187 -o [%%s] : Filename for image saving. \n\
188 Example: -o %s\n\
189 The first %%d is for the camera id. The second\n\
190 %%04d is for the image numbering. The format is set \n\
191 by the extension of the file (ex .png, .pgm, ...) \n\
192 \n\
193 -r : Reset the bus attached to the first camera found.\n\
194 Bus reset may help to make firewire working if the\n\
195 program was not properly stopped by a CTRL-C.\n\
196 \n\
197 -h : Print this help.\n\
198 \n",
199 name, name, name, name, name, name, name, name, name, roi_left, roi_top, roi_width, roi_height, camera,
200 nframes, ringbuffersize, panControl, opath.c_str());
201}
202
245bool read_options(int argc, const char **argv, bool &multi, unsigned int &camera, unsigned int &nframes,
246 bool &verbose_info, bool &verbose_settings, bool &videomode_is_set,
247 vp1394TwoGrabber::vp1394TwoVideoModeType &videomode, bool &framerate_is_set,
248 vp1394TwoGrabber::vp1394TwoFramerateType &framerate, bool &colorcoding_is_set,
249 vp1394TwoGrabber::vp1394TwoColorCodingType &colorcoding, bool &ringbuffersize_is_set,
250 unsigned int &ringbuffersize, bool &display, bool &save, std::string &opath, unsigned int &roi_left,
251 unsigned int &roi_top, unsigned int &roi_width, unsigned int &roi_height, bool &reset,
252 unsigned int &panControl, bool &panControl_is_set)
253{
254 /*
255 * Lecture des options.
256 */
257 const char *optarg_;
258 int c;
259
260 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
261 switch (c) {
262 case 'c':
263 camera = (unsigned int)atoi(optarg_);
264 break;
265 case 'd':
266 display = false;
267 break;
268 case 'f':
269 framerate_is_set = true;
270 framerate = (vp1394TwoGrabber::vp1394TwoFramerateType)atoi(optarg_);
271 break;
272 case 'g':
273 colorcoding_is_set = true;
274 colorcoding = (vp1394TwoGrabber::vp1394TwoColorCodingType)atoi(optarg_);
275 break;
276 case 'H':
277 roi_height = (unsigned int)atoi(optarg_);
278 break;
279 case 'i':
280 verbose_info = true;
281 break;
282 case 'L':
283 roi_left = (unsigned int)atoi(optarg_);
284 break;
285 case 'm':
286 multi = true;
287 break;
288 case 'n':
289 nframes = (unsigned int)atoi(optarg_);
290 break;
291 case 'o':
292 save = true;
293 opath = optarg_;
294 break;
295 case 'b':
296 ringbuffersize_is_set = true;
297 ringbuffersize = (unsigned int)atoi(optarg_);
298 break;
299 case 'p':
300 panControl = (unsigned int)atoi(optarg_);
301 panControl_is_set = true;
302 break;
303 case 'r':
304 reset = true;
305 break;
306 case 's':
307 verbose_settings = true;
308 break;
309 case 'T':
310 roi_top = (unsigned int)atoi(optarg_);
311 break;
312 case 'v':
313 videomode_is_set = true;
314 videomode = (vp1394TwoGrabber::vp1394TwoVideoModeType)atoi(optarg_);
315 break;
316 case 'W':
317 roi_width = (unsigned int)atoi(optarg_);
318 break;
319 case 'h':
320 case '?':
321 usage(argv[0], NULL, camera, nframes, opath, roi_left, roi_top, roi_width, roi_height, ringbuffersize,
322 panControl);
323 exit(0);
324 break;
325 }
326 }
327
328 if ((c == 1) || (c == -1)) {
329 // standalone param or error
330 usage(argv[0], NULL, camera, nframes, opath, roi_left, roi_top, roi_width, roi_height, ringbuffersize, panControl);
331 std::cerr << "ERROR: " << std::endl;
332 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
333 return false;
334 }
335 return true;
336}
337
347int main(int argc, const char **argv)
348{
349 try {
350 unsigned int camera = 0;
351 bool multi = false;
352 bool verbose_info = false;
353 bool verbose_settings = false;
354 bool display = true;
355 unsigned int nframes = 50;
356 unsigned int offset;
357 bool videomode_is_set = false;
359 bool framerate_is_set = false;
361 bool colorcoding_is_set = false;
363 bool ringbuffersize_is_set = false;
364 unsigned int ringbuffersize = 4;
365 bool save = false;
366 bool reset = false;
367 unsigned int panControl = 0;
368 bool panControl_is_set = false;
369
370 // Format 7 roi
371 unsigned int roi_left = 0, roi_top = 0, roi_width = 0, roi_height = 0;
372
373 // Default output path for image saving
374 std::string opath = "/tmp/I%d-%04d.ppm";
375
376 if (read_options(argc, argv, multi, camera, nframes, verbose_info, verbose_settings, videomode_is_set, videomode,
377 framerate_is_set, framerate, colorcoding_is_set, colorcoding, ringbuffersize_is_set,
378 ringbuffersize, display, save, opath, roi_left, roi_top, roi_width, roi_height, reset, panControl,
379 panControl_is_set)) {
380 return EXIT_FAILURE;
381 }
382
383 // Create a grabber
384 vp1394TwoGrabber g(reset);
385
386 if (reset) {
387 // The experience shows that for some Marlin cameras (F131B and
388 // F033C) a tempo of 1s is requested after a bus reset.
389 vpTime::wait(1000); // Wait 1000 ms
390 }
391
392 // Number of cameras connected on the bus
393 unsigned int ncameras = 0;
394 g.getNumCameras(ncameras);
395
396 std::cout << "Number of cameras on the bus: " << ncameras << std::endl;
397
398 // Check the consistancy of the options
399 if (multi) {
400 // ckeck if two cameras are connected
401 if (ncameras < 2) {
402 std::cout << "You have only " << ncameras << " camera connected on the bus." << std::endl;
403 std::cout << "It is not possible to active multi-camera acquisition." << std::endl;
404 std::cout << "Disable -m command line option, or connect an other " << std::endl;
405 std::cout << "cameras on the bus." << std::endl;
406 g.close();
407 return EXIT_FAILURE;
408 }
409 }
410 if (camera >= ncameras) {
411 std::cout << "You have only " << ncameras;
412 std::cout << " camera connected on the bus." << std::endl;
413 std::cout << "It is not possible to select camera " << camera << std::endl;
414 std::cout << "Check your -c <camera> command line option." << std::endl;
415 g.close();
416 return EXIT_FAILURE;
417 }
418
419 if (multi) {
420 camera = 0; // to over write a bad option usage
421 } else {
422 ncameras = 1; // acquisition from only one camera
423 }
424 // Offset is used to set the correspondancy between and image and the
425 // camera. For example, images comming from camera (i+offset) are
426 // available in I[i]
427 offset = camera;
428
429 // Display information for each camera
430 if (verbose_info || verbose_settings) {
431 for (unsigned int i = 0; i < ncameras; i++) {
432
433 g.setCamera(i + offset);
434
435 if (verbose_info)
436 g.printCameraInfo();
437
438 if (verbose_settings) {
442 std::list<vp1394TwoGrabber::vp1394TwoVideoModeType> lmode;
443 std::list<vp1394TwoGrabber::vp1394TwoFramerateType> lfps;
444 std::list<vp1394TwoGrabber::vp1394TwoColorCodingType> lcoding;
445 std::list<vp1394TwoGrabber::vp1394TwoVideoModeType>::const_iterator it_lmode;
446 std::list<vp1394TwoGrabber::vp1394TwoFramerateType>::const_iterator it_lfps;
447 std::list<vp1394TwoGrabber::vp1394TwoColorCodingType>::const_iterator it_lcoding;
448 uint64_t guid;
449
450 g.getVideoMode(curmode);
451 g.getFramerate(curfps);
452 g.getColorCoding(curcoding);
453 g.getVideoModeSupported(lmode);
454 g.getGuid(guid);
455
456 std::cout << "----------------------------------------------------------" << std::endl
457 << "---- Video modes and framerates supported by camera " << i + offset << " ----" << std::endl
458 << "---- with guid 0x" << std::hex << guid << " ----" << std::endl
459 << "---- * is for the current settings ----" << std::endl
460 << "---- between ( ) you have the corresponding option ----" << std::endl
461 << "---- to use. ----" << std::endl
462 << "----------------------------------------------------------" << std::endl;
463
464 for (it_lmode = lmode.begin(); it_lmode != lmode.end(); ++it_lmode) {
465 // Parse the list of supported modes
467 if (curmode == supmode)
468 std::cout << " * " << vp1394TwoGrabber::videoMode2string(supmode) << " (-v " << (int)supmode << ")"
469 << std::endl;
470 else
471 std::cout << " " << vp1394TwoGrabber::videoMode2string(supmode) << " (-v " << (int)supmode << ")"
472 << std::endl;
473
474 if (g.isVideoModeFormat7(supmode)) {
475 // Format 7 video mode; no framerate setting, but color
476 // coding setting
477 g.getColorCodingSupported(supmode, lcoding);
478 for (it_lcoding = lcoding.begin(); it_lcoding != lcoding.end(); ++it_lcoding) {
480 supcoding = *it_lcoding;
481 if ((curmode == supmode) && (supcoding == curcoding))
482 std::cout << " * " << vp1394TwoGrabber::colorCoding2string(supcoding) << " (-g " << (int)supcoding
483 << ")" << std::endl;
484 else
485 std::cout << " " << vp1394TwoGrabber::colorCoding2string(supcoding) << " (-g " << (int)supcoding
486 << ")" << std::endl;
487 }
488 } else {
489
490 // Parse the list of supported framerates for a supported mode
491 g.getFramerateSupported(supmode, lfps);
492 for (it_lfps = lfps.begin(); it_lfps != lfps.end(); ++it_lfps) {
494 if ((curmode == supmode) && (supfps == curfps))
495 std::cout << " * " << vp1394TwoGrabber::framerate2string(supfps) << " (-f " << (int)supfps << ")"
496 << std::endl;
497 else
498 std::cout << " " << vp1394TwoGrabber::framerate2string(supfps) << " (-f " << (int)supfps << ")"
499 << std::endl;
500 }
501 }
502 }
503 std::cout << "----------------------------------------------------------" << std::endl;
504 }
505 }
506 return EXIT_SUCCESS;
507 }
508
509 // If requested set the PAN register 0x884 to control single or
510 // multiple image transmission from stereo vision cameras.
511 if (panControl_is_set) {
512 g.setPanControl(panControl);
513 }
514
515 // If required modify camera settings
516 if (videomode_is_set) {
517 g.setCamera(camera);
518 g.setVideoMode(videomode);
519 } else {
520 // get The actual video mode
521 g.setCamera(camera);
522 g.getVideoMode(videomode);
523 }
524 if (framerate_is_set) {
525 g.setCamera(camera);
526 g.setFramerate(framerate);
527 }
528 if (colorcoding_is_set) {
529 g.setCamera(camera);
530 g.setColorCoding(colorcoding);
531 }
532 if (ringbuffersize_is_set) {
533 g.setRingBufferSize(ringbuffersize);
534 }
535
536 // In format 7 set roi to the hole image
537 if (g.isVideoModeFormat7(videomode))
538 g.setFormat7ROI(roi_left, roi_top, roi_width, roi_height);
539
540 // Array to know if color images or grey level images are acquired
541 bool *grab_color = new bool[ncameras];
542
543#ifdef VISP_HAVE_X11
544 // allocate a display for each camera to consider
545 vpDisplayX *d = NULL;
546 if (display)
547 d = new vpDisplayX[ncameras];
548#endif
549
550 // allocate an Grey and color image for each camera to consider
551 vpImage<vpRGBa> *Ic = new vpImage<vpRGBa>[ncameras];
553
554 // Do a first acquisition to initialise the display
555 for (unsigned int i = 0; i < ncameras; i++) {
556 // Set the active camera on the bus
557 g.setCamera(i + offset);
558 // Ask each camera to know if color images or grey level images are
559 // acquired
560 grab_color[i] = g.isColor();
561 // Acquire the first image
562 if (grab_color[i]) {
563 g.acquire(Ic[i]);
564 std::cout << "Image size for camera " << i + offset << " : width: " << Ic[i].getWidth()
565 << " height: " << Ic[i].getHeight() << std::endl;
566
567#ifdef VISP_HAVE_X11
568 if (display) {
569 // Initialise the display
570 std::stringstream title;
571 title << "Images captured by camera ";
572 title << i + offset;
573 d[i].init(Ic[i], (int)(100 + i * 50), (int)(100 + i * 50), title.str());
574 vpDisplay::display(Ic[i]);
575 vpDisplay::flush(Ic[i]);
576 }
577#endif
578 } else {
579 g.acquire(Ig[i]);
580 std::cout << "Image size for camera " << i + offset << " : width: " << Ig[i].getWidth()
581 << " height: " << Ig[i].getHeight() << std::endl;
582
583#ifdef VISP_HAVE_X11
584 if (display) {
585 // Initialise the display
586 std::stringstream title;
587 title << "Images captured by camera ";
588 title << i + offset;
589 d[i].init(Ig[i], (int)(100 + i * 50), (int)(100 + i * 50), title.str());
590 vpDisplay::display(Ig[i]);
591 vpDisplay::flush(Ig[i]);
592 }
593#endif
594 }
595 }
596
597 // Main loop for single or multi-camera acquisition and display
598 std::cout << "Capture in process..." << std::endl;
599
600 double tbegin = 0, ttotal = 0;
601
602 ttotal = 0;
603 tbegin = vpTime::measureTimeMs();
604 for (unsigned int i = 0; i < nframes; i++) {
605 for (unsigned int c = 0; c < ncameras; c++) {
606 // Set the active camera on the bus
607 g.setCamera(c + offset);
608 // Acquire an image
609 if (grab_color[c]) {
610 g.acquire(Ic[c]);
611#ifdef VISP_HAVE_X11
612 if (display) {
613 // Display the last image acquired
614 vpDisplay::display(Ic[c]);
615 vpDisplay::flush(Ic[c]);
616 }
617#endif
618 } else {
619 g.acquire(Ig[c]);
620#ifdef VISP_HAVE_X11
621 if (display) {
622 // Display the last image acquired
623 vpDisplay::display(Ig[c]);
624 vpDisplay::flush(Ig[c]);
625 }
626#endif
627 }
628 if (save) {
629 char buf[FILENAME_MAX];
630 snprintf(buf, FILENAME_MAX, opath.c_str(), c + offset, i);
631 std::string filename(buf);
632 std::cout << "Write: " << filename << std::endl;
633 if (grab_color[c]) {
634 vpImageIo::write(Ic[c], filename);
635 } else {
636 vpImageIo::write(Ig[c], filename);
637 }
638 }
639 }
640 double tend = vpTime::measureTimeMs();
641 double tloop = tend - tbegin;
642 tbegin = tend;
643 std::cout << "loop time: " << tloop << " ms" << std::endl;
644 ttotal += tloop;
645 }
646
647 std::cout << "Mean loop time: " << ttotal / nframes << " ms" << std::endl;
648 std::cout << "Mean frequency: " << 1000. / (ttotal / nframes) << " fps" << std::endl;
649
650 // Release the framegrabber
651 g.close();
652
653 // Free memory
654
655 delete[] Ic;
656 delete[] Ig;
657 delete[] grab_color;
658
659#ifdef VISP_HAVE_X11
660 if (display)
661 delete[] d;
662#endif
663 return EXIT_SUCCESS;
664 } catch (const vpException &e) {
665 std::cout << "Catch an exception: " << e << std::endl;
666 return EXIT_FAILURE;
667 }
668}
669#else
670int main()
671{
672 std::cout << "This example requires dc1394 SDK. " << std::endl;
673 std::cout << "Tip if you are on a unix-like system:" << std::endl;
674 std::cout << "- Install libdc1394-2, configure again ViSP using cmake and build again this example" << std::endl;
675 return EXIT_SUCCESS;
676}
677
678#endif
Class for firewire ieee1394 video devices using libdc1394-2.x api.
static std::string colorCoding2string(vp1394TwoColorCodingType colorcoding)
static std::string framerate2string(vp1394TwoFramerateType fps)
static std::string videoMode2string(vp1394TwoVideoModeType videomode)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition vpDisplayX.h:132
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="")
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 of the vpImage class member functions.
Definition vpImage.h:135
unsigned int getWidth() const
Definition vpImage.h:242
unsigned int getHeight() const
Definition vpImage.h:184
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
VISP_EXPORT double measureTimeMs()
VISP_EXPORT int wait(double t0, double t)