43 #include <visp/vpConfig.h>
44 #include <visp/vpDebug.h>
53 #if defined (VISP_HAVE_OPENCV)
56 #include <visp/vpOpenCVGrabber.h>
57 #include <visp/vpImage.h>
58 #include <visp/vpImageIo.h>
59 #include <visp/vpDisplayOpenCV.h>
60 #include <visp/vpParseArgv.h>
61 #include <visp/vpTime.h>
64 #define GETOPTARGS "dhn:o:D:"
76 void usage(
const char *name,
const char *badparam,
unsigned int &nframes, std::string &opath)
79 Acquire and display images using OpenCV library.\n\
82 %s [-d] [-n] [-o] [-h] \n", name);
87 Turn off the display.\n\
90 Type of device to detect. \n\
91 It can be ANY, MIL, VFW, V4L, V4L2, DC1394, CMU1394, DSHOW. \n\
94 Number of frames to acquire. \n\
97 Filename for image saving. \n\
99 The %%d is for the image numbering.\n\
103 \n", nframes, opath.c_str());
105 fprintf(stderr,
"ERROR: \n" );
106 fprintf(stderr,
"\nBad parameter [%s]\n", badparam);
126 bool getOptions(
int argc,
const char **argv,
bool &display,
127 unsigned int &nframes,
bool &save, std::string &opath,
int &deviceType)
134 case 'd': display =
false;
break;
136 if (strcmp( optarg ,
"ANY") == 0 ) {deviceType = CV_CAP_ANY;}
137 else if ( strcmp( optarg ,
"MIL") == 0) {deviceType = CV_CAP_MIL;}
138 else if ( strcmp( optarg ,
"VFW") == 0) {deviceType = CV_CAP_VFW;}
139 else if ( strcmp( optarg ,
"V4L") == 0) {deviceType = CV_CAP_V4L;}
140 else if ( strcmp( optarg ,
"V4L2") == 0) {deviceType = CV_CAP_V4L2;}
141 else if ( strcmp( optarg ,
"DC1394") == 0) {deviceType = CV_CAP_DC1394;}
142 else if ( strcmp( optarg ,
"CMU1394") == 0) {deviceType = CV_CAP_CMU1394;}
143 else if ( strcmp( optarg ,
"DSHOW") == 0) {deviceType = CV_CAP_DSHOW;}
144 else {std::cout <<
"Unknown type of device" << std::endl;
148 nframes = (
unsigned int)atoi(optarg);
break;
151 opath = optarg;
break;
152 case 'h': usage(argv[0], NULL, nframes, opath);
return false;
break;
155 usage(argv[0], optarg, nframes, opath);
160 if ((c == 1) || (c == -1)) {
162 usage(argv[0], NULL, nframes, opath);
163 std::cerr <<
"ERROR: " << std::endl;
164 std::cerr <<
" Bad argument " << optarg << std::endl << std::endl;
180 main(
int argc,
const char ** argv)
182 bool opt_display =
true;
183 unsigned nframes = 50;
185 int deviceType = CV_CAP_ANY;
199 std::string opath =
"/tmp/I%04d.ppm";
200 # elif defined(WIN32)
201 std::string opath =
"C:/temp/I%04d.ppm";
206 std::string opath =
"/tmp/I%04d.pgm";
207 # elif defined(WIN32)
208 std::string opath =
"C:/temp/I%04d.pgm";
213 if (getOptions(argc, argv, opt_display, nframes, save, opath, deviceType) ==
false) {
230 vpCTRACE <<
"Cannot acquire an image... "
231 <<
"Check if a camera is connected to your computer."
236 std::cout <<
"Image size: width : " << I.
getWidth() <<
" height: "
243 display.
init(I,100,100,
"OpenCV framegrabber");
247 double tbegin=0, tend=0, tloop=0, ttotal=0;
252 for (
unsigned i = 0; i < nframes; i++) {
263 char buf[FILENAME_MAX];
264 sprintf(buf, opath.c_str(), i);
265 std::string filename(buf);
266 std::cout <<
"Write: " << filename << std::endl;
270 tloop = tend - tbegin;
272 std::cout <<
"loop time: " << tloop <<
" ms" << std::endl;
275 std::cout <<
"Mean loop time: " << ttotal / nframes <<
" ms" << std::endl;
276 std::cout <<
"Mean frequency: " << 1000./(ttotal / nframes) <<
" fps" << std::endl;
281 vpCERROR <<
"Failure: exit" << std::endl;
285 #else // defined (VISP_HAVE_OPENCV)
289 vpTRACE(
"OpenCV is not available...") ;
291 #endif // defined (VISP_HAVE_OPENCV)