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) && (VISP_HAVE_OPENCV_VERSION < 0x030000))
60 #include <visp/vpFernClassifier.h>
61 #include <visp/vpParseArgv.h>
62 #include <visp/vpConfig.h>
63 #include <visp/vpImage.h>
64 #include <visp/vpDisplayX.h>
65 #include <visp/vpDisplayGTK.h>
66 #include <visp/vpDisplayGDI.h>
67 #include <visp/vpHomography.h>
68 #include <visp/vpImageIo.h>
69 #include <visp/vpIoTools.h>
70 #include <visp/vpTime.h>
73 #define GETOPTARGS "hlcdb:i:p"
75 void usage(
const char *name,
const char *badparam);
76 bool getOptions(
int argc,
const char **argv,
bool &isLearning, std::string& dataFile,
bool& click_allowed,
77 bool& display,
bool& displayPoints, std::string& ipath);
87 void usage(
const char *name,
const char *badparam)
90 Detection of points of interests and matching using the Ferns classifier. The \
91 object needs first to be learned (-l option). This learning process will create\
92 a file used to detect the object.\n\
95 %s [-l] [-h] [-b] [-c] [-d] [-p] [-i]\n", name);
102 -i <input image path> \n\
103 Set image input path.\n\
104 From this path read \"ViSP-images/line/image.%%04d.pgm\"\n\
106 Setting the VISP_INPUT_IMAGE_PATH environment\n\
107 variable produces the same behaviour than using\n\
111 database filename to use (default is ./dataFern).\n\
114 Disable the mouse click. Useful to automaze the \n\
115 execution of this program without humain intervention.\n\
118 Turn off the display.\n\
121 display points of interest.\n\
124 Print this help.\n");
127 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
147 bool getOptions(
int argc,
const char **argv,
bool &isLearning, std::string& dataFile,
bool& click_allowed,
148 bool& display,
bool& displayPoints, std::string& ipath)
155 case 'c': click_allowed =
false;
break;
156 case 'd': display =
false;
break;
157 case 'l': isLearning =
true;
break;
158 case 'h': usage(argv[0], NULL);
return false;
break;
159 case 'b': dataFile = optarg_;
break;
160 case 'p': displayPoints =
true;
break;
161 case 'i': ipath = optarg_;
break;
163 usage(argv[0], optarg_);
168 if ((c == 1) || (c == -1)) {
170 usage(argv[0], NULL);
171 std::cerr <<
"ERROR: " << std::endl;
172 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
182 main(
int argc,
const char** argv)
185 bool isLearning =
false;
186 std::string dataFile(
"./dataFern");
187 bool opt_click_allowed =
true;
188 bool opt_display =
true;
189 std::string objectName(
"object");
190 bool displayPoints =
false;
191 std::string opt_ipath;
193 std::string env_ipath;
195 std::string filename;
201 if (! env_ipath.empty()){
206 if (getOptions(argc, argv,
207 isLearning, dataFile, opt_click_allowed, opt_display, displayPoints, opt_ipath) ==
false) {
212 if (!opt_ipath.empty()){
218 if (!opt_ipath.empty() && !env_ipath.empty()) {
219 if (ipath != env_ipath) {
220 std::cout << std::endl
221 <<
"WARNING: " << std::endl;
222 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
223 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
224 <<
" we skip the environment variable." << std::endl;
229 if (opt_ipath.empty() && env_ipath.empty()){
230 usage(argv[0], NULL);
231 std::cerr << std::endl
232 <<
"ERROR:" << std::endl;
233 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
235 <<
" environment variable to specify the location of the " << std::endl
236 <<
" image path where test images are located." << std::endl << std::endl;
251 std::ostringstream s;
252 s.setf(std::ios::right, std::ios::adjustfield);
253 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
".pgm";
263 std::cout <<
"Load: " << filename << std::endl;
273 std::cerr << std::endl
274 <<
"ERROR:" << std::endl;
275 std::cerr <<
" Cannot read " << filename << std::endl;
276 std::cerr <<
" Check your -i " << ipath <<
" option " << std::endl
277 <<
" or VISP_INPUT_IMAGE_PATH environment variable."
282 #if defined VISP_HAVE_X11
284 #elif defined VISP_HAVE_GTK
286 #elif defined VISP_HAVE_GDI
290 #if defined VISP_HAVE_X11
292 #elif defined VISP_HAVE_GTK
294 #elif defined VISP_HAVE_GDI
303 displayRef.
init(Iref, 100, 100,
"Reference image") ;
308 if (opt_display && opt_click_allowed){
309 std::cout <<
"Click on the top left and the bottom right corners to define the reference plane" << std::endl;
310 for (
int i=0 ; i < 2 ; i++){
312 std::cout << corners[i] << std::endl;
326 if (opt_click_allowed){
327 std::cout <<
"Click on the image to continue" << std::endl;
331 vpRect roi(corners[0], corners[1]);
333 std::cout <<
"> train the classifier on the selected plane. (may take up to several minutes)." << std::endl;
346 std::cout <<
"unknown error, line " << __LINE__ << std::endl;
349 fern.
record(objectName, dataFile);
355 std::cout <<
"unknown error, line " << __LINE__ << std::endl;
357 std::cout << __LINE__ << std::endl;
361 vpERROR_TRACE(
"cannot load the database with the specified name. Has the object been learned with the -l option? ");
366 fern.
load(dataFile, objectName);
369 vpERROR_TRACE(
"cannot load the database with the specified name. Has the object been learned with the -l option? ");
376 display.
init(I, 110 + (
int)Iref.
getWidth(), 100,
"Current image") ;
381 if (opt_display && opt_click_allowed){
382 std::cout <<
"Click on the current image to continue" << std::endl;
384 "Click on the current image to continue",
vpColor::red);
396 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
".pgm";
418 std::cout <<
"unknown error line " << __LINE__ << std::endl;
421 std::cout <<
"matching " << nbpts <<
" points : " <<
vpTime::measureTimeMs () - t0 <<
" ms" << std::endl;
437 std::cout <<
"Catch an exception: " << e << std::endl;
446 #if ( ! (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI)) )
447 vpERROR_TRACE(
"You do not have X11, GTK or GDI display functionalities...");
449 vpERROR_TRACE(
"You do not have OpenCV-2.0.0 or a more recent release...");
const char * getMessage(void)
unsigned int getWidth() const
virtual unsigned int matchPoint(const vpImage< unsigned char > &I)
Display for windows using GDI (available on any windows 32 platform).
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Define the X11 console to display images.
error that can be emited by ViSP classes.
static double measureTimeMs()
static const vpColor green
static void flush(const vpImage< unsigned char > &I)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
virtual unsigned int buildReference(const vpImage< unsigned char > &I)
static void display(const vpImage< unsigned char > &I)
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
virtual void displayRectangle(const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)=0
void record(const std::string &_objectName, const std::string &_dataFile)
record the Ferns classifier in the text file
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
unsigned int getHeight() const
Defines a rectangle in the plane.
virtual bool getClick(bool blocking=true)=0
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
void load(const std::string &_dataFile, const std::string &)
load the Fern classifier
virtual void display(const vpImage< unsigned char > &Iref, const vpImage< unsigned char > &Icurrent, unsigned int size=3)
static void read(vpImage< unsigned char > &I, const char *filename)
void set_ij(const double ii, const double jj)
Class that implements the Fern classifier and the YAPE detector thanks to the OpenCV library...