bazar  1.3.1
filedetect.cpp
Go to the documentation of this file.
1 /*
2 Copyright 2005, 2006 Computer Vision Lab,
3 Ecole Polytechnique Federale de Lausanne (EPFL), Switzerland.
4 All rights reserved.
5 
6 This file is part of BazAR.
7 
8 BazAR is free software; you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation; either version 2 of the License, or (at your option) any later
11 version.
12 
13 BazAR is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
15 PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License along with
18 BazAR; if not, write to the Free Software Foundation, Inc., 51 Franklin
19 Street, Fifth Floor, Boston, MA 02110-1301, USA
20 */
27 #include <iostream>
28 
29 // include everything garfeild needs
30 #include <garfeild.h>
31 
32 // image loading and saving with OpenCV
33 #include <highgui.h>
34 
35 using namespace std;
36 
37 int main(int argc, char *argv[])
38 {
39  // model.png is the default model image file name.
40  char *modelFile= "model.jpg";
41 
42  if (argc<2) {
43  cerr << "Usage: " << argv[0]
44  << " [-m <model image>] <image> [<image> ...]\n";
45  return -1;
46  }
47 
48  // search for model name
49  for (int i=1; i<argc-1; i++) {
50  if (strcmp(argv[i], "-m") ==0) {
51  modelFile = argv[i+1];
52  break;
53  }
54  }
55 
56  // Allocate the detector object
57  planar_object_recognizer detector;
58 
59  // A lower threshold will allow detection in harder conditions, but
60  // might lead to false positives.
61  detector.match_score_threshold=.03f;
62 
63  detector.ransac_dist_threshold = 5;
64  detector.max_ransac_iterations = 500;
65  detector.non_linear_refine_threshold = 1.5;
66  detector.min_view_rate=.2;
67  detector.views_number = 100;
68 
69  // Train or load classifier
70  if(!detector.build_with_cache(
71  string(modelFile), // mode image file name
72  400, // maximum number of keypoints on the model
73  32, // patch size in pixels
74  3, // yape radius. Use 3,5 or 7.
75  16, // number of trees for the classifier. Somewhere between 12-50
76  3 // number of levels in the gaussian pyramid
77  ))
78  {
79  cerr << modelFile << ": Error while loading model image or classifier!\n";
80  return -2;
81  }
82 
83  // The detector is now ready. Load input images.
84  for (int i=1; i<argc; ++i) {
85  if (argv[i][0] == '-') { ++i; continue; }
86  IplImage *im = cvLoadImage(argv[i],0);
87  if (!im) {
88  cerr << argv[i] << ": unable to load image.\n";
89  continue;
90  }
91  if (detector.detect(im)) {
92  cout << argv[i] << ": detection succeeded!\n";
93 
94  // save an image showing the result
95  IplImage *result = detector.create_result_image(im, false,true,false,true);
96  char fn[512]; sprintf(fn,"result_%s", argv[i]);
97  cvSaveImage(fn, result);
98  cvReleaseImage(&result);
99  } else {
100  cout << argv[i] << ": detection failed.\n";
101  }
102  }
103  return 0;
104 }