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  cv::Ptr<cv::xfeatures2d::SIFT> sift = cv::xfeatures2d::SIFT::create();
125  sift->detect(objectImg, objectKeypoints);
126  sift->detect(sceneImg, sceneKeypoints);
127 
129  // EXTRACT DESCRIPTORS
131  sift->compute(objectImg, objectKeypoints, objectDescriptors);
132  sift->compute(sceneImg, sceneKeypoints, sceneDescriptors);
133 #endif
134  // NEAREST NEIGHBOR MATCHING USING FLANN LIBRARY (included in OpenCV)
137  cv::Mat results;
138  cv::Mat dists;
139  std::vector<std::vector<cv::DMatch> > matches;
140  int k=2; // find the 2 nearest neighbors
141 
142  // Create Flann KDTree index
143  cv::flann::Index flannIndex(sceneDescriptors, cv::flann::KDTreeIndexParams(), cvflann::FLANN_DIST_EUCLIDEAN);
144  results = cv::Mat(objectDescriptors.rows, k, CV_32SC1); // Results index
145  dists = cv::Mat(objectDescriptors.rows, k, CV_32FC1); // Distance results are CV_32FC1
146 
147  // search (nearest neighbor)
148  flannIndex.knnSearch(objectDescriptors, results, dists, k, cv::flann::SearchParams() );
149 
151  // PROCESS NEAREST NEIGHBOR RESULTS
153 
154  // Find correspondences by NNDR (Nearest Neighbor Distance Ratio)
155  float nndrRatio = 0.6f;
156  std::vector<cv::Point2f> mpts_1, mpts_2; // Used for homography
157  std::vector<int> indexes_1, indexes_2; // Used for homography
158  std::vector<uchar> outlier_mask; // Used for homography
159  // Check if this descriptor matches with those of the objects
160 
161  for(int i=0; i<objectDescriptors.rows; ++i)
162  {
163  // Apply NNDR
164  if(dists.at<float>(i,0) <= nndrRatio * dists.at<float>(i,1))
165  {
166  mpts_1.push_back(objectKeypoints.at(i).pt);
167  indexes_1.push_back(i);
168 
169  mpts_2.push_back(sceneKeypoints.at(results.at<int>(i,0)).pt);
170  indexes_2.push_back(results.at<int>(i,0));
171  }
172  }
173 
174  if(method == mInliers)
175  {
176  // FIND HOMOGRAPHY
177  unsigned int minInliers = 8;
178  if(mpts_1.size() >= minInliers)
179  {
180  cv::Mat H = findHomography(mpts_1,
181  mpts_2,
182  cv::RANSAC,
183  1.0,
184  outlier_mask);
185  int inliers=0, outliers=0;
186  for(unsigned int k=0; k<mpts_1.size();++k)
187  {
188  if(outlier_mask.at(k))
189  {
190  ++inliers;
191  }
192  else
193  {
194  ++outliers;
195  }
196  }
197  if(!quiet)
198  printf("Total=%d Inliers=%d Outliers=%d\n", (int)mpts_1.size(), inliers, outliers);
199  value = (inliers*100) / (inliers+outliers);
200  }
201  }
202  else
203  {
204  value = (int)mpts_1.size();
205  }
206  }
207  else
208  {
209  printf("Images are not valid!\n");
210  showUsage();
211  }
212  if(!quiet)
213  printf("Similarity = %d\n", value);
214  return value;
215 }
void showUsage()
int main(int argc, char *argv[])


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 19:22:26