title: What is the best way to find the closest match to a complex shape, using opencv and c++?
body: <p>Alright, here is my source code. This code will take an image in a file and compare it against a list of images in another file. In the file of images you must include a .txt file containing the names of all of the images in the file you are trying to compare. The problem i'm having is that these two images are very similar but are not exactly the same. I need a method to refine these matches further. Perhaps even an entire new way to compare these two shapes (in larger chunks, blobs, ect). One way I was considering is actually making an entire keypoint map, and only comparing keypionts if they are at or near a certain point that corespondes to both images. Ie: compare keypoints at point (12,200), +-10 pixels from (x, y) and see if there are similar keypoint on the other image.</p>
  
  <p>All I need is a way to get the best matches possible from: <a href="http://i.stack.imgur.com/TKxY1.png" rel="nofollow">ActualImplant</a> and <a href="http://i.stack.imgur.com/SuQaS.jpg" rel="nofollow">XrayOfThatSameImplantButASlightlyDifferentSize</a>. Please and thank you!</p>
  
  <p>PS: you will see commented out sections where I was experimenting with Sobel Derivatives and other such things. I ended up just adjusting contrast and brightness on the xray for the best outline. The same has to be done to the image of the implant before it is used to try to match anything. </p>
  
  <pre><code>#include "opencv2\highgui\highgui.hpp"
  #include "opencv2\features2d\features2d.hpp"
  #include "opencv2\imgproc.hpp"
  
  
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;ctime&gt;
  const string defaultDetector = "ORB";
  const string defaultDescriptor = "ORB";
  const string defaultMatcher = "BruteForce-Hamming";
  const string defaultXrayImagePath = "../../xray.png";
  const string defaultImplantImagesTextListPath = "../../implantImage.txt";
  const string defaultPathToResultsFolder = "../../results";
  
  static void printIntro(const string&amp; appName)
  {
      cout &lt;&lt; "/*                                                                                                       *\n"
          &lt;&lt; " * Created by: Alex Gatz. 1/11/12. Created for: Xray Implant Identification                               *\n"
          &lt;&lt; " * This code was created to scan a file full of images of differnt implants, generate keypoint maps       *\n"
          &lt;&lt; " * for each image, and identifywhich image most closely matches a chosen image in another folder          *\n"
          &lt;&lt; " */                                                                                                       *\n"
          &lt;&lt; endl;
  
      cout &lt;&lt; endl &lt;&lt; "Format:\n" &lt;&lt; endl;
      cout &lt;&lt; "./" &lt;&lt; appName &lt;&lt; " [detector] [descriptor] [matcher] [xrayImagePath] [implantImagesTextListPath] [pathToSaveResults]" &lt;&lt; endl;
      cout &lt;&lt; endl;
  
      cout &lt;&lt; "\nExample:" &lt;&lt; endl
          &lt;&lt; "./" &lt;&lt; appName &lt;&lt; " " &lt;&lt; defaultDetector &lt;&lt; " " &lt;&lt; defaultDescriptor &lt;&lt; " " &lt;&lt; defaultMatcher &lt;&lt; " "
          &lt;&lt; defaultXrayImagePath &lt;&lt; " " &lt;&lt; defaultImplantImagesTextListPath &lt;&lt; " " &lt;&lt; defaultPathToResultsFolder &lt;&lt; endl;
  }
  
  static void maskMatchesByImplantImgIdx(const vector&lt;DMatch&gt;&amp; matches, int trainImgIdx, vector&lt;char&gt;&amp; mask)
  {
      mask.resize(matches.size());
      fill(mask.begin(), mask.end(), 0);
      for (size_t i = 0; i &lt; matches.size(); i++)
      {
          if (matches[i].imgIdx == trainImgIdx)
              mask[i] = 1;
      }
  }
  
  static void readImplantFilenames(const string&amp; filename, string&amp; dirName, vector&lt;string&gt;&amp; implantFilenames)
  {
      implantFilenames.clear();
  
      ifstream file(filename.c_str());
      if (!file.is_open())
          return;
  
      size_t pos = filename.rfind('\\');
      char dlmtr = '\\';
      if (pos == String::npos)
      {
          pos = filename.rfind('/');
          dlmtr = '/';
      }
      dirName = pos == string::npos ? "" : filename.substr(0, pos) + dlmtr;
  
      while (!file.eof())
      {
          string str; getline(file, str);
          if (str.empty()) break;
          implantFilenames.push_back(str);
      }
      file.close();
  }
  
  static bool createDetectorDescriptorMatcher(const string&amp; detectorType, const string&amp; descriptorType, const string&amp; matcherType,
      Ptr&lt;FeatureDetector&gt;&amp; featureDetector,
      Ptr&lt;DescriptorExtractor&gt;&amp; descriptorExtractor,
      Ptr&lt;DescriptorMatcher&gt;&amp; descriptorMatcher)
  {
      cout &lt;&lt; "&lt; Creating feature detector, descriptor extractor and descriptor matcher ..." &lt;&lt; endl;
      featureDetector = ORB::create( //All of these are parameters that can be adjusted to effect match accuracy and process time.
          10000, //int nfeatures = Maxiumum number of features to retain; max vaulue unknown, higher number takes longer to process. Default: 500
          1.4f, //float scaleFactor= Pyramid decimation ratio; between 1.00 - 2.00.                                                 Default: 1.2f
          6,   //int nlevels = Number of pyramid levels used; more levels more time taken to process, but more accurate results.   Default: 8
          40,   //int edgeThreshold = Size of the border where the features are not detected. Should match patchSize roughly.       Default: 31
          0,    //int firstLevel = Should remain 0 for now.                                                                         Default: 0
          4,    //int WTA_K = Should remain 2.                                                                                      Default: 2
          ORB::HARRIS_SCORE,    //int scoreType = ORB::HARRIS_SCORE is the most accurate ranking possible for ORB.                                          Default: HARRIS_SCORE
          33    //int patchSize = size of patch used by the oriented BRIEF descriptor. Should match edgeThreashold.                 Default: 31
          ); 
      //featureDetector = ORB::create(); // &lt;-- Uncomment this and comment the featureDetector above for default detector-
      //OpenCV 3.1 got rid of the dynamic naming of detectors and extractors. 
  
      //These two are one in the same when using ORB, some detectors and extractors are separate
      // in which case you would set "descriptorExtractor = descriptorType::create();" or its equivilant.
      descriptorExtractor = featureDetector;
  
      descriptorMatcher = DescriptorMatcher::create(matcherType);
  
      cout &lt;&lt; "&gt;" &lt;&lt; endl;
  
      bool isCreated = !(featureDetector.empty() || descriptorExtractor.empty() || descriptorMatcher.empty());
      if (!isCreated)
          cout &lt;&lt; "Can not create feature detector or descriptor extractor or descriptor matcher of given types." &lt;&lt; endl &lt;&lt; "&gt;" &lt;&lt; endl;
  
      return isCreated;
  }
  
  static void manipulateImage(Mat&amp; image) //Manipulates images into only showing an outline!
  {
      //Sobel Dirivative edge finder
  
      //int scale = 1;
      //int delta = 0;
      //int ddepth = CV_16S;
      ////equalizeHist(image, image); //This will equilize the lighting levels in each image.
      //GaussianBlur(image, image, Size(3, 3), 0, 0, BORDER_DEFAULT);
  
      //Mat grad_x, grad_y;
      //Mat abs_grad_x, abs_grad_y;
      ////For x
      //Sobel(image, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT);
      //convertScaleAbs(grad_x, abs_grad_x);
      ////For y
      //Sobel(image, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT);
      //convertScaleAbs(grad_y, abs_grad_y);
  
      //addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, image);
  
      //Specific Level adjustment (very clean)
      double alpha = 20; //Best Result: 20
      int beta = -300;   //Best Result: -300
      image.convertTo(image, -1, alpha, beta);
  }
  
  static bool readImages(const string&amp; xrayImageName, const string&amp; implantFilename,
      Mat&amp; xrayImage, vector &lt;Mat&gt;&amp; implantImages, vector&lt;string&gt;&amp; implantImageNames)
  {
      //TODO: Add a funtion call to automatically adjust all images loaded to best settings for matching. 
      cout &lt;&lt; "&lt; Reading the images..." &lt;&lt; endl;
      xrayImage = imread(xrayImageName, CV_LOAD_IMAGE_GRAYSCALE); //Turns the image gray while loading.
      manipulateImage(xrayImage); //Runs image manipulations
  
      if (xrayImage.empty())
      {
          cout &lt;&lt; "Xray image can not be read." &lt;&lt; endl &lt;&lt; "&gt;" &lt;&lt; endl;
          return false;
      }
      string trainDirName;
      readImplantFilenames(implantFilename, trainDirName, implantImageNames);
      if (implantImageNames.empty())
      {
          cout &lt;&lt; "Implant image filenames can not be read." &lt;&lt; endl &lt;&lt; "&gt;" &lt;&lt; endl;
          return false;
      }
      int readImageCount = 0;
      for (size_t i = 0; i &lt; implantImageNames.size(); i++)
      {
          string filename = trainDirName + implantImageNames[i];
          Mat img = imread(filename, CV_LOAD_IMAGE_GRAYSCALE); //Turns imamges gray while loading.
          //manipulateImage(img); //Runs Sobel Dirivitage on implant image.
          if (img.empty())
          {
              cout &lt;&lt; "Implant image " &lt;&lt; filename &lt;&lt; " can not be read." &lt;&lt; endl;
          }
          else
          {
              readImageCount++;
          }
          implantImages.push_back(img);
      }
      if (!readImageCount)
      {
          cout &lt;&lt; "All implant images can not be read." &lt;&lt; endl &lt;&lt; "&gt;" &lt;&lt; endl;
          return false;
      }
      else
          cout &lt;&lt; readImageCount &lt;&lt; " implant images were read." &lt;&lt; endl;
      cout &lt;&lt; "&gt;" &lt;&lt; endl;
  
      return true;
  }
  
  static void detectKeypoints(const Mat&amp; xrayImage, vector&lt;KeyPoint&gt;&amp; xrayKeypoints,
      const vector&lt;Mat&gt;&amp; implantImages, vector&lt;vector&lt;KeyPoint&gt; &gt;&amp; implantKeypoints,
      Ptr&lt;FeatureDetector&gt;&amp; featureDetector)
  {
      cout &lt;&lt; endl &lt;&lt; "&lt; Extracting keypoints from images..." &lt;&lt; endl;
      featureDetector-&gt;detect(xrayImage, xrayKeypoints);
      featureDetector-&gt;detect(implantImages, implantKeypoints);
      cout &lt;&lt; "&gt;" &lt;&lt; endl;
  }
  
  static void computeDescriptors(const Mat&amp; xrayImage, vector&lt;KeyPoint&gt;&amp; implantKeypoints, Mat&amp; implantDescriptors,
      const vector&lt;Mat&gt;&amp; implantImages, vector&lt;vector&lt;KeyPoint&gt; &gt;&amp; implantImageKeypoints, vector&lt;Mat&gt;&amp; implantImageDescriptors,
      Ptr&lt;DescriptorExtractor&gt;&amp; descriptorExtractor)
  {
      cout &lt;&lt; "&lt; Computing descriptors for keypoints..." &lt;&lt; endl;
      descriptorExtractor-&gt;compute(xrayImage, implantKeypoints, implantDescriptors);
      descriptorExtractor-&gt;compute(implantImages, implantImageKeypoints, implantImageDescriptors);
  
      int totalTrainDesc = 0;
      for (vector&lt;Mat&gt;::const_iterator tdIter = implantImageDescriptors.begin(); tdIter != implantImageDescriptors.end(); tdIter++)
          totalTrainDesc += tdIter-&gt;rows;
  
      cout &lt;&lt; "Query descriptors count: " &lt;&lt; implantDescriptors.rows &lt;&lt; "; Total train descriptors count: " &lt;&lt; totalTrainDesc &lt;&lt; endl;
      cout &lt;&lt; "&gt;" &lt;&lt; endl;
  }
  
  static void matchDescriptors(const Mat&amp; xrayDescriptors, const vector&lt;Mat&gt;&amp; implantDescriptors,
      vector&lt;DMatch&gt;&amp; matches, Ptr&lt;DescriptorMatcher&gt;&amp; descriptorMatcher)
  {
      cout &lt;&lt; "&lt; Set implant image descriptors collection in the matcher and match xray descriptors to them..." &lt;&lt; endl;
      //time_t timerBegin, timerEnd;
  
      //time(&amp;timerBegin);
      descriptorMatcher-&gt;add(implantDescriptors);
      descriptorMatcher-&gt;train();
      //time(&amp;timerEnd);
      //double buildTime = difftime(timerEnd, timerBegin);
  
      //time(&amp;timerBegin);
      descriptorMatcher-&gt;match(xrayDescriptors, matches);
      //time(&amp;timerEnd);
      //double matchTime = difftime(timerEnd, timerBegin);
  
      CV_Assert(xrayDescriptors.rows == (int)matches.size() || matches.empty());
  
      cout &lt;&lt; "Number of imageMatches: " &lt;&lt; matches.size() &lt;&lt; endl;
      //cout &lt;&lt; "Build time: " &lt;&lt; buildTime &lt;&lt; " ms; Match time: " &lt;&lt; matchTime &lt;&lt; " ms" &lt;&lt; endl;
      cout &lt;&lt; "&gt;" &lt;&lt; endl;
  }
  
  static void saveResultImages(const Mat&amp; xrayImage, const vector&lt;KeyPoint&gt;&amp; xrayKeypoints,
      const vector&lt;Mat&gt;&amp; implantImage, const vector&lt;vector&lt;KeyPoint&gt; &gt;&amp; implantImageKeypoints,
      const vector&lt;DMatch&gt;&amp; matches, const vector&lt;string&gt;&amp; implantImagesName, const string&amp; resultDir)
  {
      cout &lt;&lt; "&lt; Save results..." &lt;&lt; endl;
      Mat drawImg;
      vector&lt;char&gt; mask;
      for (size_t i = 0; i &lt; implantImage.size(); i++)
      {
          if (!implantImage[i].empty())
          {
              maskMatchesByImplantImgIdx(matches, (int)i, mask);
              drawMatches(xrayImage, xrayKeypoints, implantImage[i], implantImageKeypoints[i],
                  matches, drawImg, Scalar::all(-1), Scalar(0, 0, 255), mask, 4);
              string filename = resultDir + "/result_" + implantImagesName[i];
              if (!imwrite(filename, drawImg))
                  cout &lt;&lt; "Image " &lt;&lt; filename &lt;&lt; " can not be saved (may be because directory " &lt;&lt; resultDir &lt;&lt; " does not exist)." &lt;&lt; endl;
          }
      }
      cout &lt;&lt; "&gt;" &lt;&lt; endl;
  
      //After all results have been saved, another function will scan and place the final result in a separate folder. 
      //For now this save process is required to manually access each result and determine if the current settings are working well. 
  }
  
  int main(int argc, char** argv)
  {
      //Intialize variables to global defaults.
      string detector = defaultDetector;
      string descriptor = defaultDescriptor;
      string matcher = defaultMatcher;
      string xrayImagePath = defaultXrayImagePath;
      string implantImagesTextListPath = defaultImplantImagesTextListPath;
      string pathToSaveResults = defaultPathToResultsFolder;
  
      //As long as you have 7 arguments, you can procede
      if (argc != 7 &amp;&amp; argc != 1)
      {
          //This will be called if the incorrect amount of commands are used to start the program.
          printIntro(argv[1]);
          system("PAUSE");
          return -1;
      }
  
      //As long as you still have 7 arguments, I will set the variables for this
      // to the arguments you decided on. 
      //If testing using XrayID --&gt; Properties --&gt; Debugging --&gt; Command Arguments, remember to start with [detector] as the first command
      // C++ includes the [appName] command as the first argument automantically. 
  
      if (argc != 1) //I suggest placing a break here and stepping through this to ensure the proper commands were sent in. With a
                     // GUI this would nto matter because the GUI would structure the input and use a default if no input was used. 
      {
          detector = argv[1]; 
          descriptor = argv[2]; 
          matcher = argv[3];
          xrayImagePath = argv[4]; 
          implantImagesTextListPath = argv[5];
          pathToSaveResults = argv[6];
      }
  
      //Set up cv::Ptr's for tools. 
      Ptr&lt;FeatureDetector&gt; featureDetector;
      Ptr&lt;DescriptorExtractor&gt; descriptorExtractor;
      Ptr&lt;DescriptorMatcher&gt; descriptorMatcher;
  
      //Check to see if tools are created, if not true print intro and close program.
      if (!createDetectorDescriptorMatcher(detector, descriptor, matcher, featureDetector, descriptorExtractor, descriptorMatcher))
      {
          printIntro(argv[0]);
          system("PAUSE");
          return -1;
      }
  
      Mat testImage;
      vector&lt;Mat&gt; implantImages;
      vector&lt;string&gt; implantImagesNames;
  
      //Check to see if readImages completes properly, if not true print intro and close program. 
      if (!readImages(xrayImagePath, implantImagesTextListPath, testImage, implantImages, implantImagesNames))
      {
          printIntro(argv[0]);
          system("PAUSE");
          return -1;
      }
  
      vector&lt;KeyPoint&gt; xrayKeypoints;
      vector&lt;vector&lt;KeyPoint&gt; &gt; implantKeypoints;
      detectKeypoints(testImage, xrayKeypoints, implantImages, implantKeypoints, featureDetector);
  
      Mat xrayDescriptors;
      vector&lt;Mat&gt; implantTestImageDescriptors;
      computeDescriptors(testImage, xrayKeypoints, xrayDescriptors, implantImages, implantKeypoints, implantTestImageDescriptors,
          descriptorExtractor);
  
      vector&lt;DMatch&gt; imageMatches;
      matchDescriptors(xrayDescriptors, implantTestImageDescriptors, imageMatches, descriptorMatcher);
      saveResultImages(testImage, xrayKeypoints, implantImages, implantKeypoints, imageMatches, implantImagesNames, pathToSaveResults);
  
      system("PAUSE");
      return 0;
  }
  </code></pre>