49 #include <visp3/core/vpConfig.h>
50 #include <visp3/core/vpDebug.h>
52 #if ((defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI)) && (VISP_HAVE_OPENCV_VERSION >= 0x020000) && (VISP_HAVE_OPENCV_VERSION < 0x030000))
56 #include <visp3/vision/vpFernClassifier.h>
57 #include <visp3/io/vpParseArgv.h>
58 #include <visp3/core/vpConfig.h>
59 #include <visp3/core/vpImage.h>
60 #include <visp3/gui/vpDisplayX.h>
61 #include <visp3/gui/vpDisplayGTK.h>
62 #include <visp3/gui/vpDisplayGDI.h>
63 #include <visp3/vision/vpHomography.h>
64 #include <visp3/io/vpImageIo.h>
65 #include <visp3/core/vpIoTools.h>
66 #include <visp3/core/vpTime.h>
69 #define GETOPTARGS "hlcdb:i:p"
71 void usage(
const char *name,
const char *badparam);
72 bool getOptions(
int argc,
const char **argv,
bool &isLearning, std::string& dataFile,
bool& click_allowed,
73 bool& display,
bool& displayPoints, std::string& ipath);
83 void usage(
const char *name,
const char *badparam)
86 Detection of points of interests and matching using the Ferns classifier. The \
87 object needs first to be learned (-l option). This learning process will create\
88 a file used to detect the object.\n\
91 %s [-l] [-h] [-b] [-c] [-d] [-p] [-i]\n", name);
98 -i <input image path> \n\
99 Set image input path.\n\
100 From this path read \"ViSP-images/line/image.%%04d.pgm\"\n\
102 Setting the VISP_INPUT_IMAGE_PATH environment\n\
103 variable produces the same behaviour than using\n\
107 database filename to use (default is ./dataFern).\n\
110 Disable the mouse click. Useful to automaze the \n\
111 execution of this program without humain intervention.\n\
114 Turn off the display.\n\
117 display points of interest.\n\
120 Print this help.\n");
123 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
143 bool getOptions(
int argc,
const char **argv,
bool &isLearning, std::string& dataFile,
bool& click_allowed,
144 bool& display,
bool& displayPoints, std::string& ipath)
151 case 'c': click_allowed =
false;
break;
152 case 'd': display =
false;
break;
153 case 'l': isLearning =
true;
break;
154 case 'h': usage(argv[0], NULL);
return false;
break;
155 case 'b': dataFile = optarg_;
break;
156 case 'p': displayPoints =
true;
break;
157 case 'i': ipath = optarg_;
break;
159 usage(argv[0], optarg_);
164 if ((c == 1) || (c == -1)) {
166 usage(argv[0], NULL);
167 std::cerr <<
"ERROR: " << std::endl;
168 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
178 main(
int argc,
const char** argv)
181 bool isLearning =
false;
182 std::string dataFile(
"./dataFern");
183 bool opt_click_allowed =
true;
184 bool opt_display =
true;
185 std::string objectName(
"object");
186 bool displayPoints =
false;
187 std::string opt_ipath;
189 std::string env_ipath;
191 std::string filename;
197 if (! env_ipath.empty()){
202 if (getOptions(argc, argv,
203 isLearning, dataFile, opt_click_allowed, opt_display, displayPoints, opt_ipath) ==
false) {
208 if (!opt_ipath.empty()){
214 if (!opt_ipath.empty() && !env_ipath.empty()) {
215 if (ipath != env_ipath) {
216 std::cout << std::endl
217 <<
"WARNING: " << std::endl;
218 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
219 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
220 <<
" we skip the environment variable." << std::endl;
225 if (opt_ipath.empty() && env_ipath.empty()){
226 usage(argv[0], NULL);
227 std::cerr << std::endl
228 <<
"ERROR:" << std::endl;
229 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
231 <<
" environment variable to specify the location of the " << std::endl
232 <<
" image path where test images are located." << std::endl << std::endl;
247 std::ostringstream s;
248 s.setf(std::ios::right, std::ios::adjustfield);
249 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
".pgm";
259 std::cout <<
"Load: " << filename << std::endl;
269 std::cerr << std::endl
270 <<
"ERROR:" << std::endl;
271 std::cerr <<
" Cannot read " << filename << std::endl;
272 std::cerr <<
" Check your -i " << ipath <<
" option " << std::endl
273 <<
" or VISP_INPUT_IMAGE_PATH environment variable."
278 #if defined VISP_HAVE_X11
280 #elif defined VISP_HAVE_GTK
282 #elif defined VISP_HAVE_GDI
286 #if defined VISP_HAVE_X11
288 #elif defined VISP_HAVE_GTK
290 #elif defined VISP_HAVE_GDI
299 displayRef.
init(Iref, 100, 100,
"Reference image") ;
304 if (opt_display && opt_click_allowed){
305 std::cout <<
"Click on the top left and the bottom right corners to define the reference plane" << std::endl;
306 for (
int i=0 ; i < 2 ; i++){
308 std::cout << corners[i] << std::endl;
322 if (opt_click_allowed){
323 std::cout <<
"Click on the image to continue" << std::endl;
327 vpRect roi(corners[0], corners[1]);
329 std::cout <<
"> train the classifier on the selected plane. (may take up to several minutes)." << std::endl;
342 std::cout <<
"unknown error, line " << __LINE__ << std::endl;
345 fern.
record(objectName, dataFile);
351 std::cout <<
"unknown error, line " << __LINE__ << std::endl;
353 std::cout << __LINE__ << std::endl;
357 vpERROR_TRACE(
"cannot load the database with the specified name. Has the object been learned with the -l option? ");
362 fern.
load(dataFile, objectName);
365 vpERROR_TRACE(
"cannot load the database with the specified name. Has the object been learned with the -l option? ");
372 display.
init(I, 110 + (
int)Iref.
getWidth(), 100,
"Current image") ;
377 if (opt_display && opt_click_allowed){
378 std::cout <<
"Click on the current image to continue" << std::endl;
380 "Click on the current image to continue",
vpColor::red);
392 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
".pgm";
414 std::cout <<
"unknown error line " << __LINE__ << std::endl;
417 std::cout <<
"matching " << nbpts <<
" points : " <<
vpTime::measureTimeMs () - t0 <<
" ms" << std::endl;
433 std::cout <<
"Catch an exception: " << e << std::endl;
442 #if ( ! (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI)) )
443 vpERROR_TRACE(
"You do not have X11, GTK or GDI display functionalities...");
445 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 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)
VISP_EXPORT double measureTimeMs()
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...