OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
detect-test.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2  * *
3  * O S S I M *
4  * Open Source, Geospatial Image Processing Project *
5  * License: MIT, see LICENSE at the top-level directory *
6  * *
7  *****************************************************************************/
8 
9 // This test code was adapted from the opencv sample "matchmethod_orb_akaze_brisk"
10 
11 #include <opencv/cv.h>
12 #include <opencv2/imgproc.hpp>
13 #include <opencv2/highgui.hpp>
14 #include <opencv2/features2d.hpp>
15 #include <opencv2/objdetect.hpp>
16 #include <vector>
17 #include <iostream>
18 #include <string>
19 
20 using namespace std;
21 using namespace cv;
22 
23 static void help(const char* appname)
24 {
25  cout <<"\nThis program demonstrates how to detect and compute ORB BRISK and AKAZE descriptors \n"
26  "\nUsage: "<<appname<<" <image> \n"
27  "\nPress a key on active image window to proceed to the following descriptor.\n"
28  <<endl;
29 }
30 
31 int main(int argc, char *argv[])
32 {
33  vector<String> typeDesc;
34  String fileName;
35 
36  if (argc < 2)
37  {
38  help(argv[0]);
39  return 0;
40  }
41 
42  // This descriptor are going to be detect and compute
43  typeDesc.push_back("AKAZE-DESCRIPTOR_KAZE_UPRIGHT"); // see http://docs.opencv.org/trunk/d8/d30/classcv_1_1AKAZE.html
44  typeDesc.push_back("AKAZE"); // see http://docs.opencv.org/trunk/d8/d30/classcv_1_1AKAZE.html
45  typeDesc.push_back("ORB"); // see http://docs.opencv.org/trunk/de/dbf/classcv_1_1BRISK.html
46  typeDesc.push_back("BRISK"); // see http://docs.opencv.org/trunk/db/d95/classcv_1_1ORB.html
47  typeDesc.push_back("CORNER"); // see http://docs.opencv.org/trunk/db/d95/classcv_1_1ORB.html
48 
49  const String keys = "{@image1 | | Reference image } {help h | | }";
50  CommandLineParser parser(argc, argv, keys);
51  if (parser.has("help"))
52  {
53  help(argv[0]);
54  return 0;
55  }
56  fileName = parser.get<string>((int) 0);
57 
58  try
59  {
60  Mat img1 = imread(fileName, IMREAD_GRAYSCALE);
61  if (img1.rows * img1.cols <= 0)
62  {
63  cout << "Image " << fileName << " is empty or cannot be found\n";
64  return (0);
65  }
66 
67  vector<double> desMethCmp;
68  Ptr<Feature2D> b;
69 
70  // Descriptor loop
71  vector<String>::iterator itDesc;
72  for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); ++itDesc)
73  {
74  // keypoint for img1
75  vector<KeyPoint> keypoints;
76 
77  // Descriptor for img1
78  if (*itDesc == "AKAZE-DESCRIPTOR_KAZE_UPRIGHT")
79  b = AKAZE::create(AKAZE::DESCRIPTOR_KAZE_UPRIGHT);
80  else if (*itDesc == "AKAZE")
81  b = AKAZE::create();
82  else if (*itDesc == "ORB")
83  b = ORB::create();
84  else if (*itDesc == "BRISK")
85  b = BRISK::create();
86  else if (*itDesc == "CORNER")
87  b.release();
88  else
89  {
90  cout << "\nUnknown descriptor type requested: <"<<*itDesc<<">\n"<<endl;
91  return 0;
92  }
93 
94  Mat descImg1;
95  if (b)
96  {
97  // We can detect keypoint with detect method and then compute their descriptors:
98  b->detect(img1, keypoints);
99  b->compute(img1, keypoints, descImg1);
100 
101  }
102  else
103  {
104  // Next demo goodFaturesToTrack:
105  vector<Point2f> features;
106  int maxCorners = 1000;
107  double qualityLevel = 0.001;
108  double minDistance = 5.0;
109  goodFeaturesToTrack(img1, features, maxCorners, qualityLevel, minDistance);
110  KeyPoint::convert(features, keypoints, 10.0);
111  }
112 
113  // Draw:
114  drawKeypoints(img1, keypoints, descImg1, Scalar::all(-1),
115  DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
116  namedWindow(*itDesc, WINDOW_AUTOSIZE);
117  imshow(*itDesc, descImg1);
118  }
119  waitKey();
120  }
121  catch (Exception& e)
122  {
123  cout << "\nHit exception: "<<e.msg << endl;
124  return 0;
125  }
126  return 0;
127 }
int main(int argc, char *argv[])
Definition: detect-test.cpp:31