tools/similarity/main.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2011-2014, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 
31 // OpenCV stuff
32 #include <opencv2/core/core.hpp>
33 #include <opencv2/highgui/highgui.hpp>
34 #include <opencv2/features2d/features2d.hpp>
35 #include <opencv2/calib3d/calib3d.hpp> // for homography
36 #include <opencv2/opencv_modules.hpp>
37 
38 #ifdef HAVE_OPENCV_NONFREE
39  #if CV_MAJOR_VERSION == 2 && CV_MINOR_VERSION >=4
40  #include <opencv2/nonfree/gpu.hpp>
41  #include <opencv2/nonfree/features2d.hpp>
42  #endif
43 #endif
44 #ifdef HAVE_OPENCV_XFEATURES2D
45  #include <opencv2/xfeatures2d.hpp>
46  #include <opencv2/xfeatures2d/cuda.hpp>
47 #endif
48 
49 void showUsage()
50 {
51  printf(
52  "\n"
53  "Return similarity between two images (the number of similar features between the images).\n"
54  "Usage :\n"
55  " ./find_object-similarity [option] object.png scene.png\n"
56  "Options: \n"
57  " -inliers return inliers percentage : inliers / (inliers + outliers)\n"
58  " -quiet don't show messages\n");
59 
60  exit(-1);
61 }
62 
63 enum {mTotal, mInliers};
64 
65 int main(int argc, char * argv[])
66 {
67  bool quiet = false;
68  int method = mTotal; //total matches
69  if(argc<3)
70  {
71  printf("Two images required!\n");
72  showUsage();
73  }
74  else if(argc>3)
75  {
76  for(int i=1; i<argc-2; ++i)
77  {
78  if(std::string(argv[i]).compare("-inliers") == 0)
79  {
80  method = mInliers;
81  }
82  else if(std::string(argv[i]).compare("-quiet") == 0)
83  {
84  quiet = true;
85  }
86  else
87  {
88  printf("Option %s not recognized!", argv[1]);
89  showUsage();
90  }
91  }
92  }
93 
94 
95  //Load as grayscale
96  cv::Mat objectImg = cv::imread(argv[argc-2], cv::IMREAD_GRAYSCALE);
97  cv::Mat sceneImg = cv::imread(argv[argc-1], cv::IMREAD_GRAYSCALE);
98 
99  int value = 0;
100  if(!objectImg.empty() && !sceneImg.empty())
101  {
102  std::vector<cv::KeyPoint> objectKeypoints;
103  std::vector<cv::KeyPoint> sceneKeypoints;
104  cv::Mat objectDescriptors;
105  cv::Mat sceneDescriptors;
106 
107 #if CV_MAJOR_VERSION < 3
108  // EXTRACT KEYPOINTS
111  cv::SIFT sift;
112  sift.detect(objectImg, objectKeypoints);
113  sift.detect(sceneImg, sceneKeypoints);
114 
116  // EXTRACT DESCRIPTORS
118  sift.compute(objectImg, objectKeypoints, objectDescriptors);
119  sift.compute(sceneImg, sceneKeypoints, sceneDescriptors);
120 #else
121  // EXTRACT KEYPOINTS
124 #if (CV_MAJOR_VERSION == 4 && CV_MINOR_VERSION <= 3) || (CV_MAJOR_VERSION == 3 && (CV_MINOR_VERSION < 4 || (CV_MINOR_VERSION==4 && CV_SUBMINOR_VERSION<11)))
125  cv::Ptr<cv::xfeatures2d::SIFT> sift = cv::xfeatures2d::SIFT::create();
126 #else
127  cv::Ptr<cv::SIFT> sift = cv::SIFT::create();
128 #endif
129  sift->detect(objectImg, objectKeypoints);
130  sift->detect(sceneImg, sceneKeypoints);
131 
133  // EXTRACT DESCRIPTORS
135  sift->compute(objectImg, objectKeypoints, objectDescriptors);
136  sift->compute(sceneImg, sceneKeypoints, sceneDescriptors);
137 #endif
138  // NEAREST NEIGHBOR MATCHING USING FLANN LIBRARY (included in OpenCV)
141  cv::Mat results;
142  cv::Mat dists;
143  std::vector<std::vector<cv::DMatch> > matches;
144  int k=2; // find the 2 nearest neighbors
145 
146  // Create Flann KDTree index
147  cv::flann::Index flannIndex(sceneDescriptors, cv::flann::KDTreeIndexParams(), cvflann::FLANN_DIST_EUCLIDEAN);
148  results = cv::Mat(objectDescriptors.rows, k, CV_32SC1); // Results index
149  dists = cv::Mat(objectDescriptors.rows, k, CV_32FC1); // Distance results are CV_32FC1
150 
151  // search (nearest neighbor)
152  flannIndex.knnSearch(objectDescriptors, results, dists, k, cv::flann::SearchParams() );
153 
155  // PROCESS NEAREST NEIGHBOR RESULTS
157 
158  // Find correspondences by NNDR (Nearest Neighbor Distance Ratio)
159  float nndrRatio = 0.6f;
160  std::vector<cv::Point2f> mpts_1, mpts_2; // Used for homography
161  std::vector<int> indexes_1, indexes_2; // Used for homography
162  std::vector<uchar> outlier_mask; // Used for homography
163  // Check if this descriptor matches with those of the objects
164 
165  for(int i=0; i<objectDescriptors.rows; ++i)
166  {
167  // Apply NNDR
168  if(dists.at<float>(i,0) <= nndrRatio * dists.at<float>(i,1))
169  {
170  mpts_1.push_back(objectKeypoints.at(i).pt);
171  indexes_1.push_back(i);
172 
173  mpts_2.push_back(sceneKeypoints.at(results.at<int>(i,0)).pt);
174  indexes_2.push_back(results.at<int>(i,0));
175  }
176  }
177 
178  if(method == mInliers)
179  {
180  // FIND HOMOGRAPHY
181  unsigned int minInliers = 8;
182  if(mpts_1.size() >= minInliers)
183  {
184  cv::Mat H = findHomography(mpts_1,
185  mpts_2,
186  cv::RANSAC,
187  1.0,
188  outlier_mask);
189  int inliers=0, outliers=0;
190  for(unsigned int k=0; k<mpts_1.size();++k)
191  {
192  if(outlier_mask.at(k))
193  {
194  ++inliers;
195  }
196  else
197  {
198  ++outliers;
199  }
200  }
201  if(!quiet)
202  printf("Total=%d Inliers=%d Outliers=%d\n", (int)mpts_1.size(), inliers, outliers);
203  value = (inliers*100) / (inliers+outliers);
204  }
205  }
206  else
207  {
208  value = (int)mpts_1.size();
209  }
210  }
211  else
212  {
213  printf("Images are not valid!\n");
214  showUsage();
215  }
216  if(!quiet)
217  printf("Similarity = %d\n", value);
218  return value;
219 }
showUsage
void showUsage()
Definition: tools/similarity/main.cpp:49
mTotal
@ mTotal
Definition: tools/similarity/main.cpp:63
mInliers
@ mInliers
Definition: tools/similarity/main.cpp:63
main
int main(int argc, char *argv[])
Definition: tools/similarity/main.cpp:65


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Mon Dec 12 2022 03:43:35