39 #include <visp3/core/vpConfig.h> 40 #include <visp3/core/vpDebug.h> 49 #if defined (VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION < 0x020408) 52 #include <visp3/sensor/vpOpenCVGrabber.h> 53 #include <visp3/core/vpImage.h> 54 #include <visp3/io/vpImageIo.h> 55 #include <visp3/gui/vpDisplayOpenCV.h> 56 #include <visp3/io/vpParseArgv.h> 57 #include <visp3/core/vpTime.h> 60 #define GETOPTARGS "dhn:o:D:" 62 void usage(
const char *name,
const char *badparam,
unsigned int &nframes, std::string &opath);
63 bool getOptions(
int argc,
const char **argv,
bool &display,
64 unsigned int &nframes,
bool &save, std::string &opath,
int &deviceType);
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)
183 bool opt_display =
true;
184 unsigned nframes = 50;
186 int deviceType = CV_CAP_ANY;
199 # if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 200 std::string opath =
"/tmp/I%04d.ppm";
201 # elif defined(_WIN32) 202 std::string opath =
"C:/temp/I%04d.ppm";
206 # if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 207 std::string opath =
"/tmp/I%04d.pgm";
208 # elif defined(_WIN32) 209 std::string opath =
"C:/temp/I%04d.pgm";
214 if (getOptions(argc, argv, opt_display, nframes, save, opath, deviceType) ==
false) {
218 vpOpenCVGrabber grabber ;
221 grabber.setDeviceType(deviceType);
231 vpCTRACE <<
"Cannot acquire an image... " 232 <<
"Check if a camera is connected to your computer." 237 std::cout <<
"Image size: width : " << I.
getWidth() <<
" height: " 244 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 std::cout <<
"Catch an exception: " << e << std::endl;
285 #else // defined (VISP_HAVE_OPENCV) 289 vpTRACE(
"OpenCV is not available...") ;
291 #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 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.
VISP_EXPORT double measureTimeMs()
unsigned int getHeight() const