43 #include <visp/vpConfig.h>
44 #include <visp/vpDebug.h>
53 #if defined (VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION < 0x020408)
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:"
66 void usage(
const char *name,
const char *badparam,
unsigned int &nframes, std::string &opath);
67 bool getOptions(
int argc,
const char **argv,
bool &display,
68 unsigned int &nframes,
bool &save, std::string &opath,
int &deviceType);
80 void usage(
const char *name,
const char *badparam,
unsigned int &nframes, std::string &opath)
83 Acquire and display images using OpenCV library.\n\
86 %s [-d] [-n] [-o] [-h] \n", name);
91 Turn off the display.\n\
94 Type of device to detect. \n\
95 It can be ANY, MIL, VFW, V4L, V4L2, DC1394, CMU1394, DSHOW. \n\
98 Number of frames to acquire. \n\
101 Filename for image saving. \n\
103 The %%d is for the image numbering.\n\
107 \n", nframes, opath.c_str());
109 fprintf(stderr,
"ERROR: \n" );
110 fprintf(stderr,
"\nBad parameter [%s]\n", badparam);
130 bool getOptions(
int argc,
const char **argv,
bool &display,
131 unsigned int &nframes,
bool &save, std::string &opath,
int &deviceType)
138 case 'd': display =
false;
break;
140 if (strcmp( optarg_ ,
"ANY") == 0 ) {deviceType = CV_CAP_ANY;}
141 else if ( strcmp( optarg_ ,
"MIL") == 0) {deviceType = CV_CAP_MIL;}
142 else if ( strcmp( optarg_ ,
"VFW") == 0) {deviceType = CV_CAP_VFW;}
143 else if ( strcmp( optarg_ ,
"V4L") == 0) {deviceType = CV_CAP_V4L;}
144 else if ( strcmp( optarg_ ,
"V4L2") == 0) {deviceType = CV_CAP_V4L2;}
145 else if ( strcmp( optarg_ ,
"DC1394") == 0) {deviceType = CV_CAP_DC1394;}
146 else if ( strcmp( optarg_ ,
"CMU1394") == 0) {deviceType = CV_CAP_CMU1394;}
147 else if ( strcmp( optarg_ ,
"DSHOW") == 0) {deviceType = CV_CAP_DSHOW;}
148 else {std::cout <<
"Unknown type of device" << std::endl;
152 nframes = (
unsigned int)atoi(optarg_);
break;
155 opath = optarg_;
break;
156 case 'h': usage(argv[0], NULL, nframes, opath);
return false;
break;
159 usage(argv[0], optarg_, nframes, opath);
164 if ((c == 1) || (c == -1)) {
166 usage(argv[0], NULL, nframes, opath);
167 std::cerr <<
"ERROR: " << std::endl;
168 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
184 main(
int argc,
const char ** argv)
187 bool opt_display =
true;
188 unsigned nframes = 50;
190 int deviceType = CV_CAP_ANY;
203 # if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
204 std::string opath =
"/tmp/I%04d.ppm";
205 # elif defined(_WIN32)
206 std::string opath =
"C:/temp/I%04d.ppm";
210 # if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
211 std::string opath =
"/tmp/I%04d.pgm";
212 # elif defined(_WIN32)
213 std::string opath =
"C:/temp/I%04d.pgm";
218 if (getOptions(argc, argv, opt_display, nframes, save, opath, deviceType) ==
false) {
222 vpOpenCVGrabber grabber ;
225 grabber.setDeviceType(deviceType);
235 vpCTRACE <<
"Cannot acquire an image... "
236 <<
"Check if a camera is connected to your computer."
241 std::cout <<
"Image size: width : " << I.
getWidth() <<
" height: "
248 display.
init(I,100,100,
"OpenCV framegrabber");
251 double tbegin=0, tend=0, tloop=0, ttotal=0;
256 for (
unsigned i = 0; i < nframes; i++) {
267 char buf[FILENAME_MAX];
268 sprintf(buf, opath.c_str(), i);
269 std::string filename(buf);
270 std::cout <<
"Write: " << filename << std::endl;
274 tloop = tend - tbegin;
276 std::cout <<
"loop time: " << tloop <<
" ms" << std::endl;
279 std::cout <<
"Mean loop time: " << ttotal / nframes <<
" ms" << std::endl;
280 std::cout <<
"Mean frequency: " << 1000./(ttotal / nframes) <<
" fps" << std::endl;
285 std::cout <<
"Catch an exception: " << e << std::endl;
289 #else // defined (VISP_HAVE_OPENCV)
293 vpTRACE(
"OpenCV is not available...") ;
295 #endif // defined (VISP_HAVE_OPENCV)
static void write(const vpImage< unsigned char > &I, const char *filename)
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
unsigned int getWidth() const
error that can be emited by ViSP classes.
static double measureTimeMs()
static void flush(const vpImage< unsigned char > &I)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the opencv library.
unsigned int getHeight() const