53 #include <visp/vpConfig.h>
54 #include <visp/vpDebug.h>
56 #if ((defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI)) && (VISP_HAVE_OPENCV_VERSION >= 0x020000))
60 #include <visp/vpFernClassifier.h>
61 #include <visp/vpParseArgv.h>
62 #include <visp/vpConfig.h>
63 #include <visp/vpOpenCVGrabber.h>
64 #include <visp/vpImage.h>
65 #include <visp/vpDisplayX.h>
66 #include <visp/vpDisplayGTK.h>
67 #include <visp/vpDisplayGDI.h>
68 #include <visp/vpHomography.h>
69 #include <visp/vpImageIo.h>
70 #include <visp/vpIoTools.h>
71 #include <visp/vpTime.h>
74 #define GETOPTARGS "hlcdb:i:sp"
84 void usage(
const char *name,
const char *badparam)
87 Detection of points of interests and matching using the Ferns classifier. The \
88 object needs first to be learned (-l option). This learning process will create\
89 a file used to detect the object.\n\
92 %s [-l] [-h] [-b] [-c] [-d] [-p] [-i] [-s]\n", name);
99 -i <input image path> \n\
100 Set image input path.\n\
101 From this path read \"ViSP-images/line/image.%%04d.pgm\"\n\
103 Setting the VISP_INPUT_IMAGE_PATH environment\n\
104 variable produces the same behaviour than using\n\
108 database filename to use (default is ./dataFern).\n\
111 Disable the mouse click. Useful to automaze the \n\
112 execution of this program without humain intervention.\n\
115 Turn off the display.\n\
118 Turn off the use of the sequence and use a webcam.\n\
121 display points of interest.\n\
124 Print this help.\n");
127 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
148 bool getOptions(
int argc,
const char **argv,
149 bool &isLearning, std::string& dataFile,
bool& click_allowed,
bool& display,
bool& displayPoints,
bool& useSequence, std::string& ipath)
156 case 'c': click_allowed =
false;
break;
157 case 'd': display =
false;
break;
158 case 'l': isLearning =
true;
break;
159 case 'h': usage(argv[0], NULL);
return false;
break;
160 case 'b': dataFile = optarg;
break;
161 case 'p': displayPoints =
true;
break;
162 case 's': useSequence =
false;
break;
163 case 'i': ipath = optarg;
break;
165 usage(argv[0], optarg);
170 if ((c == 1) || (c == -1)) {
172 usage(argv[0], NULL);
173 std::cerr <<
"ERROR: " << std::endl;
174 std::cerr <<
" Bad argument " << optarg << std::endl << std::endl;
184 main(
int argc,
const char** argv)
186 bool isLearning =
false;
187 std::string dataFile(
"./dataFern");
188 bool opt_click_allowed =
true;
189 bool opt_display =
true;
190 std::string objectName(
"object");
191 bool displayPoints =
false;
192 bool useSequence =
true;
193 std::string opt_ipath;
195 std::string env_ipath;
197 std::string filename;
201 char *ptenv = getenv(
"VISP_INPUT_IMAGE_PATH");
207 if (! env_ipath.empty()){
212 if (getOptions(argc, argv,
213 isLearning, dataFile, opt_click_allowed, opt_display, displayPoints, useSequence, opt_ipath) ==
false) {
218 if (useSequence && !opt_ipath.empty()){
224 if (useSequence && !opt_ipath.empty() && !env_ipath.empty()) {
225 if (ipath != env_ipath) {
226 std::cout << std::endl
227 <<
"WARNING: " << std::endl;
228 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
229 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
230 <<
" we skip the environment variable." << std::endl;
235 if (useSequence && opt_ipath.empty() && env_ipath.empty()){
236 usage(argv[0], NULL);
237 std::cerr << std::endl
238 <<
"ERROR:" << std::endl;
239 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
241 <<
" environment variable to specify the location of the " << std::endl
242 <<
" image path where test images are located." << std::endl << std::endl;
257 std::ostringstream s;
258 s.setf(std::ios::right, std::ios::adjustfield);
259 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
".pgm";
260 filename = dirname + s.str();
270 vpCTRACE <<
"Load: " << filename << std::endl;
281 std::cerr << std::endl
282 <<
"ERROR:" << std::endl;
283 std::cerr <<
" Cannot read " << filename << std::endl;
284 std::cerr <<
" Check your -i " << ipath <<
" option " << std::endl
285 <<
" or VISP_INPUT_IMAGE_PATH environment variable."
298 std::cout <<
"problem to initialise the framegrabber" << std::endl;
306 #if defined VISP_HAVE_X11
308 #elif defined VISP_HAVE_GTK
310 #elif defined VISP_HAVE_GDI
314 #if defined VISP_HAVE_X11
316 #elif defined VISP_HAVE_GTK
318 #elif defined VISP_HAVE_GDI
327 displayRef.
init(Iref, 100, 100,
"Reference image") ;
332 if (opt_display && opt_click_allowed){
333 std::cout <<
"Click on the top left and the bottom right corners to define the reference plane" << std::endl;
334 for (
int i=0 ; i < 2 ; i++){
336 std::cout << corners[i] << std::endl;
350 if (opt_click_allowed){
351 std::cout <<
"Click on the image to continue" << std::endl;
355 vpRect roi(corners[0], corners[1]);
357 std::cout <<
"> train the classifier on the selected plane. (may take up to several minutes)." << std::endl;
370 std::cout <<
"unknown error, line " << __LINE__ << std::endl;
373 fern.
record(objectName, dataFile);
379 std::cout <<
"unknown error, line " << __LINE__ << std::endl;
381 std::cout << __LINE__ << std::endl;
385 vpERROR_TRACE(
"cannot load the database with the specified name. Has the object been learned with the -l option? ");
390 fern.
load(dataFile, objectName);
393 vpERROR_TRACE(
"cannot load the database with the specified name. Has the object been learned with the -l option? ");
400 display.
init(I, 110 + (
int)Iref.
getWidth(), 100,
"Current image") ;
405 if (opt_display && opt_click_allowed){
406 std::cout <<
"Click on the current image to continue" << std::endl;
408 (
char*)
"Click on the current image to continue",
vpColor::red);
421 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
".pgm";
422 filename = dirname + s.str();
447 std::cout <<
"unknown error line " << __LINE__ << std::endl;
450 std::cout <<
"matching " << nbpts <<
" points : " <<
vpTime::measureTimeMs () - t0 <<
" ms" << std::endl;
470 #if ( ! (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI)) )
471 vpERROR_TRACE(
"You do not have X11, GTK or GDI display functionalities...");
473 vpERROR_TRACE(
"You do not have OpenCV-2.0.0 or a more recent release...");