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/vpPlanarObjectDetector.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>
73 #include <visp/vpV4l2Grabber.h>
74 #include <visp/vp1394TwoGrabber.h>
76 #define GETOPTARGS "hlcdb:i:sp"
86 void usage(
const char *name,
const char *badparam)
89 Test of detection of planar surface using a Fern classifier. The object needs \
90 first to be learned (-l option). This learning process will create a file used\
91 to detect the object.\n\
94 %s [-l] [-h] [-b] [-c] [-d] [-p] [-i] [-s]\n", name);
101 -i <input image path> \n\
102 Set image input path.\n\
103 From this path read \"ViSP-images/line/image.%%04d.pgm\"\n\
105 Setting the VISP_INPUT_IMAGE_PATH environment\n\
106 variable produces the same behaviour than using\n\
110 database filename to use (default is ./dataPlanar).\n\
113 Disable the mouse click. Useful to automaze the \n\
114 execution of this program without humain intervention.\n\
117 Turn off the display.\n\
120 Turn off the use of the sequence and use a webcam.\n\
123 display points of interest.\n\
126 Print this help.\n");
129 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
150 bool getOptions(
int argc,
const char **argv,
151 bool &isLearning, std::string& dataFile,
bool& click_allowed,
bool& display,
bool& displayPoints,
bool& useSequence, std::string& ipath)
158 case 'c': click_allowed =
false;
break;
159 case 'd': display =
false;
break;
160 case 'l': isLearning =
true;
break;
161 case 'h': usage(argv[0], NULL);
return false;
break;
162 case 'b': dataFile = optarg;
break;
163 case 'p': displayPoints =
true;
break;
164 case 's': useSequence =
false;
break;
165 case 'i': ipath = optarg;
break;
167 usage(argv[0], optarg);
172 if ((c == 1) || (c == -1)) {
174 usage(argv[0], NULL);
175 std::cerr <<
"ERROR: " << std::endl;
176 std::cerr <<
" Bad argument " << optarg << std::endl << std::endl;
186 main(
int argc,
const char** argv)
188 bool isLearning =
false;
189 std::string dataFile(
"./dataPlanar");
190 bool opt_click_allowed =
true;
191 bool opt_display =
true;
192 std::string objectName(
"object");
193 bool displayPoints =
false;
194 bool useSequence =
true;
195 std::string opt_ipath;
197 std::string env_ipath;
199 std::string filename;
203 char *ptenv = getenv(
"VISP_INPUT_IMAGE_PATH");
209 if (! env_ipath.empty()){
214 if (getOptions(argc, argv,
215 isLearning, dataFile, opt_click_allowed, opt_display, displayPoints, useSequence, opt_ipath) ==
false) {
220 if (useSequence && !opt_ipath.empty()){
226 if (useSequence && !opt_ipath.empty() && !env_ipath.empty()) {
227 if (ipath != env_ipath) {
228 std::cout << std::endl
229 <<
"WARNING: " << std::endl;
230 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
231 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
232 <<
" we skip the environment variable." << std::endl;
237 if (useSequence && opt_ipath.empty() && env_ipath.empty()){
238 usage(argv[0], NULL);
239 std::cerr << std::endl
240 <<
"ERROR:" << std::endl;
241 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
243 <<
" environment variable to specify the location of the " << std::endl
244 <<
" image path where test images are located." << std::endl << std::endl;
259 std::ostringstream s;
260 s.setf(std::ios::right, std::ios::adjustfield);
261 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
".pgm";
262 filename = dirname + s.str();
272 vpCTRACE <<
"Load: " << filename << std::endl;
283 std::cerr << std::endl
284 <<
"ERROR:" << std::endl;
285 std::cerr <<
" Cannot read " << filename << std::endl;
286 std::cerr <<
" Check your -i " << ipath <<
" option " << std::endl
287 <<
" or VISP_INPUT_IMAGE_PATH environment variable."
300 std::cout <<
"problem to initialise the framegrabber" << std::endl;
310 #if defined VISP_HAVE_X11
312 #elif defined VISP_HAVE_GTK
314 #elif defined VISP_HAVE_GDI
318 #if defined VISP_HAVE_X11
320 #elif defined VISP_HAVE_GTK
322 #elif defined VISP_HAVE_GDI
332 displayRef.
init(Iref, 100, 100,
"Reference image") ;
336 if (opt_display && opt_click_allowed){
337 std::cout <<
"Click on the top left and the bottom right corners to define the reference plane" << std::endl;
338 for (
int i=0 ; i < 2 ; i++){
340 std::cout << corners[i] << std::endl;
354 if (opt_click_allowed){
355 std::cout <<
"Click on the image to continue" << std::endl;
359 vpRect roi(corners[0], corners[1]);
361 std::cout <<
"> train the classifier on the selected plane (may take up to several minutes)." << std::endl;
375 vpERROR_TRACE(
"cannot load the database with the specified name. Has the object been learned with the -l option? ");
380 planar.
load(dataFile, objectName);
383 vpERROR_TRACE(
"cannot load the database with the specified name. Has the object been learned with the -l option? ");
389 display.
init(I, 110 + (
int)Iref.
getWidth(), 100,
"Current image") ;
394 if (opt_display && opt_click_allowed){
395 std::cout <<
"Click on the reference image to continue" << std::endl;
397 (
char*)
"Click on the reference image to continue",
vpColor::red);
410 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
".pgm";
411 filename = dirname + s.str();
431 std::cout <<
" > computed homography:" << std::endl << H << std::endl;
436 planar.
display(Iref, I, displayPoints);
439 planar.
display(I, displayPoints);
444 std::cout <<
" > reference is not detected in the image" << std::endl;
461 #if ( ! (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI)) )
462 vpERROR_TRACE(
"You do not have X11, GTK or GDI display functionalities...");
464 vpERROR_TRACE(
"You do not have OpenCV-2.0.0 or a more recent release...");
unsigned int getWidth() const
void getHomography(vpHomography &_H) const
Display for windows using GDI (available on any windows 32 platform).
Define the X11 console to display images.
void load(const std::string &dataFilename, const std::string &objName)
Load the Fern classifier.
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)
bool matchPoint(const vpImage< unsigned char > &I)
This class aims to compute the homography wrt.two images.
void display(vpImage< unsigned char > &I, bool displayKpts=false)
void set_ij(const double i, const double j)
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
unsigned int buildReference(const vpImage< unsigned char > &I)
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
void recordDetector(const std::string &objectName, const std::string &dataFile)
Record the Ferns classifier in the text file.
virtual void displayCharString(const vpImagePoint &ip, const char *text, const vpColor &color=vpColor::green)=0
void acquire(vpImage< unsigned char > &I)
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 ...
Class used to detect a planar surface.
static void read(vpImage< unsigned char > &I, const char *filename)
Class for cameras video capture using OpenCV library.