51 #include <visp3/core/vpConfig.h> 52 #include <visp3/core/vpDebug.h> 54 #if ((defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI)) && \ 55 (VISP_HAVE_OPENCV_VERSION >= 0x020000) && (VISP_HAVE_OPENCV_VERSION < 0x030000)) 60 #include <visp3/core/vpImage.h> 61 #include <visp3/core/vpIoTools.h> 62 #include <visp3/core/vpTime.h> 63 #include <visp3/gui/vpDisplayGDI.h> 64 #include <visp3/gui/vpDisplayGTK.h> 65 #include <visp3/gui/vpDisplayX.h> 66 #include <visp3/io/vpImageIo.h> 67 #include <visp3/io/vpParseArgv.h> 68 #include <visp3/vision/vpFernClassifier.h> 69 #include <visp3/vision/vpHomography.h> 71 #define GETOPTARGS "hlcdb:i:p" 73 void usage(
const char *name,
const char *badparam);
74 bool getOptions(
int argc,
const char **argv,
bool &isLearning, std::string &dataFile,
bool &click_allowed,
75 bool &display,
bool &displayPoints, std::string &ipath);
85 void usage(
const char *name,
const char *badparam)
88 Detection of points of interests and matching using the Ferns classifier. The \ 89 object needs first to be learned (-l option). This learning process will create\ 90 a file used to detect the object.\n\ 93 %s [-l] [-h] [-b] [-c] [-d] [-p] [-i]\n", name);
100 -i <input image path> \n\ 101 Set image input path.\n\ 102 From this path read \"line/image.%%04d.pgm\"\n\ 104 Setting the VISP_INPUT_IMAGE_PATH environment\n\ 105 variable produces the same behaviour than using\n\ 109 database filename to use (default is ./dataFern).\n\ 112 Disable the mouse click. Useful to automaze the \n\ 113 execution of this program without humain intervention.\n\ 116 Turn off the display.\n\ 119 display points of interest.\n\ 122 Print this help.\n");
125 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
144 bool getOptions(
int argc,
const char **argv,
bool &isLearning, std::string &dataFile,
bool &click_allowed,
145 bool &display,
bool &displayPoints, std::string &ipath)
153 click_allowed =
false;
162 usage(argv[0], NULL);
169 displayPoints =
true;
175 usage(argv[0], optarg_);
181 if ((c == 1) || (c == -1)) {
183 usage(argv[0], NULL);
184 std::cerr <<
"ERROR: " << std::endl;
185 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
192 int main(
int argc,
const char **argv)
195 bool isLearning =
false;
196 std::string dataFile(
"./dataFern");
197 bool opt_click_allowed =
true;
198 bool opt_display =
true;
199 std::string objectName(
"object");
200 bool displayPoints =
false;
201 std::string opt_ipath;
203 std::string env_ipath;
205 std::string filename;
212 if (!env_ipath.empty()) {
217 if (getOptions(argc, argv, isLearning, dataFile, opt_click_allowed, opt_display, displayPoints, opt_ipath) ==
223 if (!opt_ipath.empty()) {
229 if (!opt_ipath.empty() && !env_ipath.empty()) {
230 if (ipath != env_ipath) {
231 std::cout << std::endl <<
"WARNING: " << std::endl;
232 std::cout <<
" Since -i <visp image path=" << ipath <<
"> " 233 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
234 <<
" we skip the environment variable." << std::endl;
239 if (opt_ipath.empty() && env_ipath.empty()) {
240 usage(argv[0], NULL);
241 std::cerr << std::endl <<
"ERROR:" << std::endl;
242 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
243 <<
" environment variable to specify the location of the " << std::endl
244 <<
" image path where test images are located." << std::endl
258 std::ostringstream s;
259 s.setf(std::ios::right, std::ios::adjustfield);
260 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
".pgm";
270 std::cout <<
"Load: " << filename << std::endl;
278 std::cerr << std::endl <<
"ERROR:" << std::endl;
279 std::cerr <<
" Cannot read " << filename << std::endl;
280 std::cerr <<
" Check your -i " << ipath <<
" option " << std::endl
281 <<
" or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
285 #if defined VISP_HAVE_X11 287 #elif defined VISP_HAVE_GTK 289 #elif defined VISP_HAVE_GDI 293 #if defined VISP_HAVE_X11 295 #elif defined VISP_HAVE_GTK 297 #elif defined VISP_HAVE_GDI 302 vpFernClassifier fern;
306 displayRef.
init(Iref, 100, 100,
"Reference image");
311 if (opt_display && opt_click_allowed) {
312 std::cout <<
"Click on the top left and the bottom right corners to " 313 "define the reference plane" 315 for (
int i = 0; i < 2; i++) {
317 std::cout << corners[i] << std::endl;
331 if (opt_click_allowed) {
332 std::cout <<
"Click on the image to continue" << std::endl;
336 vpRect roi(corners[0], corners[1]);
338 std::cout <<
"> train the classifier on the selected plane. (may take " 339 "up to several minutes)." 347 fern.buildReference(Iref, roi);
351 std::cout <<
"unknown error, line " << __LINE__ << std::endl;
354 fern.record(objectName, dataFile);
358 std::cout <<
"unknown error, line " << __LINE__ << std::endl;
360 std::cout << __LINE__ << std::endl;
363 vpERROR_TRACE(
"cannot load the database with the specified name. Has " 364 "the object been learned with the -l option? ");
369 fern.load(dataFile, objectName);
371 vpERROR_TRACE(
"cannot load the database with the specified name. Has " 372 "the object been learned with the -l option? ");
378 display.
init(I, 110 + (
int)Iref.
getWidth(), 100,
"Current image");
383 if (opt_display && opt_click_allowed) {
384 std::cout <<
"Click on the current image to continue" << std::endl;
397 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
".pgm";
412 nbpts = fern.matchPoint(I);
417 std::cout <<
"unknown error line " << __LINE__ << std::endl;
420 std::cout <<
"matching " << nbpts <<
" points : " <<
vpTime::measureTimeMs() - t0 <<
" ms" << std::endl;
423 fern.display(Iref, I, 7);
435 std::cout <<
"Catch an exception: " << e << std::endl;
443 #if (!(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))) 444 std::cout <<
"You do not have X11, or GTK, or GDI (Graphical Device Interface) functionalities to display images..." << std::endl;
445 std::cout <<
"Tip if you are on a unix-like system:" << std::endl;
446 std::cout <<
"- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
447 std::cout <<
"Tip if you are on a windows-like system:" << std::endl;
448 std::cout <<
"- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
450 std::cout <<
"You do not have OpenCV functionalities" << std::endl;
451 std::cout <<
"Tip:" << std::endl;
452 std::cout <<
"- Install OpenCV, configure again ViSP using cmake and build again this example" << std::endl;
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
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)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
error that can be emited by ViSP classes.
const char * getMessage() const
static const vpColor green
static void flush(const vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
static void display(const vpImage< unsigned char > &I)
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
static void displayRectangle(const vpImage< unsigned char > &I, const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)
void set_ij(double ii, double jj)
unsigned int getHeight() const
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Defines a rectangle in the plane.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
unsigned int getWidth() const