title: Access violation reading in FeatureDetector OpenCV 2.4.5
body: <p>I tried the sample codes about matching to many images in <strong>OpenCV 2.4.5</strong> and I modified that code. I found the error code:</p>
  
  <pre><code>Unhandled exception at 0x585a7090 in testing.exe:
  0xC0000005: Access violation reading location 0x00000000.
  </code></pre>
  
  <p>Its fault is at <code>featureDetector-&gt;detect(queryImage, queryKeypoints)</code>.</p>
  
  <p>I can't find the solution from that problem.
  Please help me.</p>
  
  <pre><code>#include &lt;opencv2\highgui\highgui.hpp&gt;
  #include &lt;opencv2\features2d\features2d.hpp&gt;
  #include &lt;opencv2\contrib\contrib.hpp&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;conio.h&gt;
  #include &lt;string&gt;
  
  using namespace std;
  using namespace cv;
  
  static void readTrainFilenames(const string&amp; filename, string&amp; dirName, vector&lt;string&gt;&amp; trainFilenames);
  static bool readImages(const string&amp; queryImageName,  Mat&amp; queryImage);
  static bool readTrainImages(const string&amp; trainFilename, vector&lt;Mat&gt;&amp; trainImages, vector&lt;string&gt;&amp; trainImageNames);
  static void detectKeypoints(const Mat&amp; queryImage, vector&lt;KeyPoint&gt;&amp; queryKeypoints,
      const vector&lt;Mat&gt;&amp; trainImages, vector&lt;vector&lt;KeyPoint&gt;&gt;&amp; trainKeypoints, Ptr&lt;FeatureDetector&gt;&amp; featureDetector);
  
  
  static void readTrainFilenames(const string&amp; filename, string&amp; dirName, vector&lt;string&gt;&amp; trainFilenames)
  {
      trainFilenames.clear();
  
      ifstream file(filename.c_str());
  
      if(!file.is_open())
      {
          cout &lt;&lt; "File can't open" &lt;&lt; endl;
          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;
          trainFilenames.push_back(str);
      } // end while
  
      file.close();
  } // end void readTrainFilenames
  
  
  static bool readImages(const string&amp; queryImageName,  Mat&amp; queryImage)
  {
      cout &lt;&lt; "reading  images..." &lt;&lt; endl;
      queryImage = imread(queryImageName, CV_LOAD_IMAGE_GRAYSCALE);
      if(queryImage.empty())
      {
          cout &lt;&lt; "query image can not be read. \n";
          return false;
      } // end if
  
  
      return true;
  }
  
  static bool readTrainImages(const string&amp; trainFilename, vector&lt;Mat&gt;&amp; trainImages, vector&lt;string&gt;&amp; trainImageNames)
  {
      cout &lt;&lt; "reading training images..." &lt;&lt; endl;
      string trainDirName = "D:/matching_to_many_images/";
  
      readTrainFilenames(trainFilename, trainDirName, trainImageNames);
      if(trainImageNames.empty())
      {
          cout &lt;&lt; "Train image filenames can not be read." &lt;&lt; endl;
          return false;
      } // end if
  
      int readImageCount = 0;
      for(size_t i = 0; i &lt; trainImageNames.size(); i++)
      {
          string filename = trainDirName + trainImageNames[i];
          Mat img = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
          if(img.empty())
          {
              cout &lt;&lt; "Train image " &lt;&lt; filename &lt;&lt; " can not be read." &lt;&lt; endl;
          }
          else
          {
              readImageCount++;
          }// end if
  
          trainImages.push_back(img);
      } // end for
  
      if(!readImageCount)
      {
          cout &lt;&lt; "All train images can not be read." &lt;&lt; endl;
          return false;
      }
      else
      {
          cout &lt;&lt; readImageCount &lt;&lt; " train images were read." &lt;&lt; endl;
      }
  
      cout &lt;&lt; endl;
  
      return true;
  }
  
  static void detectKeypoints(const Mat&amp; queryImage, vector&lt;KeyPoint&gt;&amp; queryKeypoints,
                              const vector&lt;Mat&gt;&amp; trainImages, 
                              vector&lt;vector&lt;KeyPoint&gt;&gt;&amp; trainKeypoints, 
                              Ptr&lt;FeatureDetector&gt;&amp; featureDetector){
      cout &lt;&lt; endl &lt;&lt; "Extracting keypoints from images..." &lt;&lt; endl;
  
      try{
          featureDetector-&gt;detect(queryImage, queryKeypoints);
      }
      catch(Ptr&lt;FeatureDetector&gt; a)
      {
          cout &lt;&lt; "hmm" &lt;&lt; endl;
      }
  
      cout &lt;&lt; endl;
  } // end void detectKeypoints
  
  int main()
  {
      const string defaultDetectorType = "SURF";
      const string defaultDescriptorType = "SURF";
      const string defaultMatcherType = "FlannBased";
      const string defaultQueryImageName = "D:/matching_to_many_images/query.png";
      const string defaultFileWithTrainImages = "D:/matching_to_many_images/train/trainImages.txt";
      const string defaultDirToSaveResImages = "D:/matching_to_many_images/results";
  
      Ptr&lt;FeatureDetector&gt; featureDetector;
      Ptr&lt;DescriptorExtractor&gt; descriptorExtractor;
      Ptr&lt;DescriptorMatcher&gt; descriptorMatcher;
  
      Mat queryImages;
      vector&lt;Mat&gt; trainImages;
      vector&lt;string&gt; trainImagesNames;
  
      vector&lt;KeyPoint&gt; queryKeypoints;
      vector&lt;vector&lt;KeyPoint&gt;&gt; trainKeypoints;
  
      if(!readImages(defaultQueryImageName, queryImages))
      {
          _getch();
          return -1;
      } // end if
  
      if(!readTrainImages(defaultFileWithTrainImages, trainImages, trainImagesNames))
      {
          _getch();
          return -1;
      }
  
      detectKeypoints(queryImages, queryKeypoints, trainImages, trainKeypoints, featureDetector);
  
      cout &lt;&lt; "\n done \n";
      _getch();
      return 0;
  } // end main method
  </code></pre>
  
  <p><strong>Update:</strong></p>
  
  <pre><code>#include &lt;opencv2\highgui\highgui.hpp&gt;
  #include &lt;opencv2\features2d\features2d.hpp&gt;
  #include &lt;opencv2\contrib\contrib.hpp&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;conio.h&gt;
  #include &lt;string&gt;
  
  using namespace std;
  using namespace cv;
  
  static void readTrainFilenames(const string&amp; filename, string&amp; dirName, vector&lt;string&gt;&amp; trainFilenames);
  static bool readImages(const string&amp; queryImageName,  Mat&amp; queryImage);
  static bool readTrainImages(const string&amp; trainFilename, vector&lt;Mat&gt;&amp; trainImages, vector&lt;string&gt;&amp; trainImageNames);
  static void detectKeypoints(const Mat&amp; queryImage, vector&lt;KeyPoint&gt;&amp; queryKeypoints, Ptr&lt;FeatureDetector&gt;&amp; featureDetector);
  
  
  static void readTrainFilenames(const string&amp; filename, string&amp; dirName, vector&lt;string&gt;&amp; trainFilenames)
  {
      trainFilenames.clear();
  
      ifstream file(filename.c_str());
  
      if(!file.is_open())
      {
          cout &lt;&lt; "File can't open" &lt;&lt; endl;
          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;
          trainFilenames.push_back(str);
      } // end while
  
      file.close();
  } // end void readTrainFilenames
  
  
  static bool readImages(const string&amp; queryImageName,  Mat&amp; queryImage)
  {
      cout &lt;&lt; "reading  images..." &lt;&lt; endl;
      queryImage = imread(queryImageName, CV_LOAD_IMAGE_GRAYSCALE);
      if(queryImage.empty())
      {
          cout &lt;&lt; "query image can not be read. \n";
          return false;
      } // end if
  
  
      return true;
  }
  
  static bool readTrainImages(const string&amp; trainFilename, vector&lt;Mat&gt;&amp; trainImages, vector&lt;string&gt;&amp; trainImageNames)
  {
      cout &lt;&lt; "reading training images..." &lt;&lt; endl;
      string trainDirName = "D:/matching_to_many_images/";
  
      readTrainFilenames(trainFilename, trainDirName, trainImageNames);
      if(trainImageNames.empty())
      {
          cout &lt;&lt; "Train image filenames can not be read." &lt;&lt; endl;
          return false;
      } // end if
  
      int readImageCount = 0;
      for(size_t i = 0; i &lt; trainImageNames.size(); i++)
      {
          string filename = trainDirName + trainImageNames[i];
          Mat img = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
          if(img.empty())
          {
              cout &lt;&lt; "Train image " &lt;&lt; filename &lt;&lt; " can not be read." &lt;&lt; endl;
          }
          else
          {
              readImageCount++;
          }// end if
  
          trainImages.push_back(img);
      } // end for
  
      if(!readImageCount)
      {
          cout &lt;&lt; "All train images can not be read." &lt;&lt; endl;
          return false;
      }
      else
      {
          cout &lt;&lt; readImageCount &lt;&lt; " train images were read." &lt;&lt; endl;
      }
  
      cout &lt;&lt; endl;
  
      return true;
  }
  
  static void detectKeypoints(const Mat&amp; queryImage, vector&lt;KeyPoint&gt;&amp; queryKeypoints, Ptr&lt;FeatureDetector&gt;&amp; featureDetector){
      cout &lt;&lt; endl &lt;&lt; "Extracting keypoints from images..." &lt;&lt; endl;
  
      featureDetector-&gt;detect(queryImage, queryKeypoints);
  
  
      cout &lt;&lt; endl;
  } // end void detectKeypoints
  
  int main()
  {
      const string defaultDetectorType = "SURF";
      const string defaultDescriptorType = "SURF";
      const string defaultMatcherType = "FlannBased";
      const string defaultQueryImageName = "D:/matching_to_many_images/query.png";
      const string defaultFileWithTrainImages = "D:/matching_to_many_images/train/trainImages.txt";
      const string defaultDirToSaveResImages = "D:/matching_to_many_images/results";
  
      Ptr&lt;FeatureDetector&gt; featureDetector;
      Ptr&lt;DescriptorExtractor&gt; descriptorExtractor;
      Ptr&lt;DescriptorMatcher&gt; descriptorMatcher;
  
      Mat queryImages;
      vector&lt;Mat&gt; trainImages;
      vector&lt;string&gt; trainImagesNames;
  
      vector&lt;KeyPoint&gt; queryKeypoints;
      vector&lt;vector&lt;KeyPoint&gt;&gt; trainKeypoints;
  
      if(!readImages(defaultQueryImageName, queryImages))
      {
          _getch();
          return -1;
      } // end if
  
      if(!readTrainImages(defaultFileWithTrainImages, trainImages, trainImagesNames))
      {
          _getch();
          return -1;
      }
  
      detectKeypoints(queryImages, queryKeypoints, featureDetector);
  
      cout &lt;&lt; "\n done \n";
      _getch();
      return 0;
  } // end main method
  </code></pre>
  
  <p><strong>SOLVED PROBLEMS:</strong></p>
  
  <pre><code>#include &lt;opencv2\highgui\highgui.hpp&gt;
  #include &lt;opencv2\features2d\features2d.hpp&gt;
  #include &lt;opencv2\contrib\contrib.hpp&gt;
  #include &lt;opencv2\nonfree\nonfree.hpp&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;conio.h&gt;
  #include &lt;string&gt;
  
  using namespace std;
  using namespace cv;
  
  const string defaultDetectorType = "SURF";
  const string defaultDescriptorType = "SURF";
  const string defaultMatcherType = "FlannBased";
  const string defaultQueryImageName = "D:/matching_to_many_images/query.png";
  const string defaultFileWithTrainImages = "D:/matching_to_many_images/train/trainImages.txt";
  const string defaultDirToSaveResImages = "D:/matching_to_many_images/results";
  
  static void readTrainFilenames(const string&amp; filename, string&amp; dirName, vector&lt;string&gt;&amp; trainFilenames);
  static bool readImages(const string&amp; queryImageName,  Mat&amp; queryImage);
  static bool readTrainImages(const string&amp; trainFilename, vector&lt;Mat&gt;&amp; trainImages, vector&lt;string&gt;&amp; trainImageNames);
  static void detectKeypoints(const Mat&amp; queryImage, vector&lt;KeyPoint&gt;&amp; queryKeypoints, Ptr&lt;FeatureDetector&gt;&amp; featureDetector);
  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);
  
  static void readTrainFilenames(const string&amp; filename, string&amp; dirName, vector&lt;string&gt;&amp; trainFilenames)
  {
      trainFilenames.clear();
  
      ifstream file(filename.c_str());
  
      if(!file.is_open())
      {
          cout &lt;&lt; "File can't open" &lt;&lt; endl;
          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;
          trainFilenames.push_back(str);
      } // end while
  
      file.close();
  } // end void readTrainFilenames
  
  
  static bool readImages(const string&amp; queryImageName,  Mat&amp; queryImage)
  {
      cout &lt;&lt; "reading  images..." &lt;&lt; endl;
      queryImage = imread(queryImageName, CV_LOAD_IMAGE_GRAYSCALE);
      if(queryImage.empty())
      {
          cout &lt;&lt; "query image can not be read. \n";
          return false;
      } // end if
  
  
      return true;
  }
  
  static bool readTrainImages(const string&amp; trainFilename, vector&lt;Mat&gt;&amp; trainImages, vector&lt;string&gt;&amp; trainImageNames)
  {
      cout &lt;&lt; "reading training images..." &lt;&lt; endl;
      string trainDirName = "D:/matching_to_many_images/";
  
      readTrainFilenames(trainFilename, trainDirName, trainImageNames);
      if(trainImageNames.empty())
      {
          cout &lt;&lt; "Train image filenames can not be read." &lt;&lt; endl;
          return false;
      } // end if
  
      int readImageCount = 0;
      for(size_t i = 0; i &lt; trainImageNames.size(); i++)
      {
          string filename = trainDirName + trainImageNames[i];
          Mat img = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
          if(img.empty())
          {
              cout &lt;&lt; "Train image " &lt;&lt; filename &lt;&lt; " can not be read." &lt;&lt; endl;
          }
          else
          {
              readImageCount++;
          }// end if
  
          trainImages.push_back(img);
      } // end for
  
      if(!readImageCount)
      {
          cout &lt;&lt; "All train images can not be read." &lt;&lt; endl;
          return false;
      }
      else
      {
          cout &lt;&lt; readImageCount &lt;&lt; " train images were read." &lt;&lt; endl;
      }
  
      cout &lt;&lt; endl;
  
      return true;
  }
  
  static void detectKeypoints(const Mat&amp; queryImage, vector&lt;KeyPoint&gt;&amp; queryKeypoints, Ptr&lt;FeatureDetector&gt;&amp; featureDetector){
      cout &lt;&lt; endl &lt;&lt; "Extracting keypoints from images..." &lt;&lt; endl;
  
      if(queryImage.empty())
      {
          cout &lt;&lt; "Query Image EMPTY" &lt;&lt; endl;
      } 
      else{
          cout &lt;&lt; "Query Image FILLED" &lt;&lt; endl;
      }
  
      featureDetector-&gt;detect(queryImage, queryKeypoints);
  
  
      cout &lt;&lt; endl;
  } // end void detectKeypoints
  
  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; "Creating feature detector, descriptor extractor and descriptor matcher ... " &lt;&lt; endl;
      featureDetector = FeatureDetector::create(detectorType);
      descriptorExtractor = DescriptorExtractor::create(descriptorType);
      descriptorMatcher = DescriptorMatcher::create(matcherType);
      cout &lt;&lt; endl;
  
      if(featureDetector.empty())
      {
          cout &lt;&lt; "feature detector empty" &lt;&lt; endl;
      }
  
      if(descriptorExtractor.empty())
      {
          cout &lt;&lt; "descriptor extractor empty" &lt;&lt; endl;
      }
  
      if(descriptorMatcher.empty())
      {
          cout &lt;&lt; "descriptor matcher empty" &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;
      } // end if
  
      return isCreated;
  } // end void createDetectorDescriptorMatcher
  
  int main()
  {
      initModule_nonfree();
      string detectorType = defaultDetectorType;
      string descriptorType = defaultDetectorType;
      string matcherType = defaultMatcherType;
      string queryImageName = defaultQueryImageName;
      string fileWithTrainImages = defaultFileWithTrainImages;
      string dirToSaveResImages = defaultDirToSaveResImages;
  
      Ptr&lt;FeatureDetector&gt; featureDetector = FeatureDetector::create("SURF");
      Ptr&lt;DescriptorExtractor&gt; descriptorExtractor = DescriptorExtractor::create("SURF");
      Ptr&lt;DescriptorMatcher&gt; descriptorMatcher;
  
      if(!createDetectorDescriptorMatcher(detectorType, descriptorType, matcherType, featureDetector, descriptorExtractor, descriptorMatcher))
      {
  
          _getch();
          return -1;
      }
  
  
      Mat queryImages;
      vector&lt;Mat&gt; trainImages;
      vector&lt;string&gt; trainImagesNames;
  
      vector&lt;KeyPoint&gt; queryKeypoints;
      vector&lt;vector&lt;KeyPoint&gt;&gt; trainKeypoints;
  
      if(!readImages(defaultQueryImageName, queryImages))
      {
          _getch();
          return -1;
      } // end if
  
      if(!readTrainImages(defaultFileWithTrainImages, trainImages, trainImagesNames))
      {
          _getch();
          return -1;
      }
  
      detectKeypoints(queryImages, queryKeypoints, featureDetector);
  
      cout &lt;&lt; "\n done \n";
      _getch();
      return 0;
  } // end main method
  </code></pre>
  
  <p><strong>COMPLETE SAMPLE CODES MATCHING TO MANY IMAGES:</strong></p>
  
  <pre><code>#include &lt;opencv2\highgui\highgui.hpp&gt;
  #include &lt;opencv2\features2d\features2d.hpp&gt;
  #include &lt;opencv2\contrib\contrib.hpp&gt;
  #include &lt;opencv2\nonfree\nonfree.hpp&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;conio.h&gt;
  #include &lt;string&gt;
  
  using namespace std;
  using namespace cv;
  
  const string defaultDetectorType = "SURF";
  const string defaultDescriptorType = "SURF";
  const string defaultMatcherType = "FlannBased";
  const string defaultQueryImageName = "D:/matching_to_many_images/query.png";
  const string defaultFileWithTrainImages = "D:/matching_to_many_images/train/trainImages.txt";
  const string defaultDirToSaveResImages = "D:/matching_to_many_images/results";
  
  static void readTrainFilenames(const string&amp; filename, string&amp; dirName, vector&lt;string&gt;&amp; trainFilenames);
  static bool readImages(const string&amp; queryImageName,  Mat&amp; queryImage);
  static bool readTrainImages(const string&amp; trainFilename, vector&lt;Mat&gt;&amp; trainImages, vector&lt;string&gt;&amp; trainImageNames);
  static void detectKeypoints(const Mat&amp; queryImage, vector&lt;KeyPoint&gt;&amp; queryKeypoints, const vector&lt;Mat&gt;&amp; trainImages, vector&lt;vector&lt;KeyPoint&gt;&gt;&amp; trainKeypoints, Ptr&lt;FeatureDetector&gt;&amp; featureDetector);
  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);
  static void computeDescriptors(const Mat&amp; queryImage, vector&lt;KeyPoint&gt;&amp; queryKeypoints, Mat&amp; queryDescriptors, 
      const vector&lt;Mat&gt;&amp; trainImages, vector&lt;vector&lt;KeyPoint&gt;&gt;&amp; trainKeypoints, vector&lt;Mat&gt;&amp; trainDescriptors,
      Ptr&lt;DescriptorExtractor&gt;&amp; descriptorExtractor);
  static void matchDescriptors(const Mat&amp; queryDescriptors, const vector&lt;Mat&gt;&amp; trainDescriptors, vector&lt;DMatch&gt;&amp; matches, Ptr&lt;DescriptorMatcher&gt;&amp; descriptorMatcher);
  static void maskMatchesByTrainImgIdx(const vector&lt;DMatch&gt;&amp; matches, int trainImgIdx, vector&lt;char&gt;&amp; mask);
  
  static void readTrainFilenames(const string&amp; filename, string&amp; dirName, vector&lt;string&gt;&amp; trainFilenames)
  {
      trainFilenames.clear();
  
      ifstream file(filename.c_str());
  
      if(!file.is_open())
      {
          cout &lt;&lt; "File can't open" &lt;&lt; endl;
          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;
          trainFilenames.push_back(str);
      } // end while
  
      file.close();
  } // end void readTrainFilenames
  
  
  static bool readImages(const string&amp; queryImageName,  Mat&amp; queryImage)
  {
      cout &lt;&lt; "reading  images..." &lt;&lt; endl;
      queryImage = imread(queryImageName, CV_LOAD_IMAGE_GRAYSCALE);
      if(queryImage.empty())
      {
          cout &lt;&lt; "query image can not be read. \n";
          return false;
      } // end if
  
  
      return true;
  }
  
  static bool readTrainImages(const string&amp; trainFilename, vector&lt;Mat&gt;&amp; trainImages, vector&lt;string&gt;&amp; trainImageNames)
  {
      cout &lt;&lt; "reading training images..." &lt;&lt; endl;
      string trainDirName = "D:/matching_to_many_images/";
  
      readTrainFilenames(trainFilename, trainDirName, trainImageNames);
      if(trainImageNames.empty())
      {
          cout &lt;&lt; "Train image filenames can not be read." &lt;&lt; endl;
          return false;
      } // end if
  
      int readImageCount = 0;
      for(size_t i = 0; i &lt; trainImageNames.size(); i++)
      {
          string filename = trainDirName + trainImageNames[i];
          Mat img = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
          if(img.empty())
          {
              cout &lt;&lt; "Train image " &lt;&lt; filename &lt;&lt; " can not be read." &lt;&lt; endl;
          }
          else
          {
              readImageCount++;
          }// end if
  
          trainImages.push_back(img);
      } // end for
  
      if(!readImageCount)
      {
          cout &lt;&lt; "All train images can not be read." &lt;&lt; endl;
          return false;
      }
      else
      {
          cout &lt;&lt; readImageCount &lt;&lt; " train images were read." &lt;&lt; endl;
      }
  
      cout &lt;&lt; endl;
  
      return true;
  }
  
  static void detectKeypoints(const Mat&amp; queryImage, vector&lt;KeyPoint&gt;&amp; queryKeypoints, const vector&lt;Mat&gt;&amp; trainImages, vector&lt;vector&lt;KeyPoint&gt;&gt;&amp; trainKeypoints, Ptr&lt;FeatureDetector&gt;&amp; featureDetector){
      cout &lt;&lt; endl &lt;&lt; "Extracting keypoints from images..." &lt;&lt; endl;
  
      if(queryImage.empty())
      {
          cout &lt;&lt; "Query Image EMPTY" &lt;&lt; endl;
      } 
      else{
          cout &lt;&lt; "Query Image FILLED" &lt;&lt; endl;
      }
  
      featureDetector-&gt;detect(queryImage, queryKeypoints);
      featureDetector-&gt;detect(trainImages, trainKeypoints);
  
      cout &lt;&lt; endl;
  } // end void detectKeypoints
  
  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; "Creating feature detector, descriptor extractor and descriptor matcher ... " &lt;&lt; endl;
      featureDetector = FeatureDetector::create(detectorType);
      descriptorExtractor = DescriptorExtractor::create(descriptorType);
      descriptorMatcher = DescriptorMatcher::create(matcherType);
      cout &lt;&lt; endl;
  
      if(featureDetector.empty())
      {
          cout &lt;&lt; "feature detector empty" &lt;&lt; endl;
      }
  
      if(descriptorExtractor.empty())
      {
          cout &lt;&lt; "descriptor extractor empty" &lt;&lt; endl;
      }
  
      if(descriptorMatcher.empty())
      {
          cout &lt;&lt; "descriptor matcher empty" &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;
      } // end if
  
      return isCreated;
  } // end void createDetectorDescriptorMatcher
  
  static void computeDescriptors(const Mat&amp; queryImage, vector&lt;KeyPoint&gt;&amp; queryKeypoints, Mat&amp; queryDescriptors, 
      const vector&lt;Mat&gt;&amp; trainImages, vector&lt;vector&lt;KeyPoint&gt;&gt;&amp; trainKeypoints, vector&lt;Mat&gt;&amp; trainDescriptors,
      Ptr&lt;DescriptorExtractor&gt;&amp; descriptorExtractor)
  {
      cout &lt;&lt; "computing descriptors for keypoints..." &lt;&lt; endl;
      descriptorExtractor-&gt;compute(queryImage, queryKeypoints, queryDescriptors);
      descriptorExtractor-&gt;compute(trainImages, trainKeypoints, trainDescriptors);
  
      int totalTrainDesc = 0;
      for(vector&lt;Mat&gt;::const_iterator tdIter = trainDescriptors.begin(); tdIter != trainDescriptors.end(); tdIter++)
          totalTrainDesc += tdIter-&gt;rows;
  
      cout &lt;&lt; "Query descriptors count : " &lt;&lt; queryDescriptors.rows &lt;&lt; "; Total train descriptors count : " &lt;&lt; totalTrainDesc &lt;&lt; endl;
      cout &lt;&lt; endl;
  } // end void computeDescriptors
  
  static void matchDescriptors(const Mat&amp; queryDescriptors, const vector&lt;Mat&gt;&amp; trainDescriptors, vector&lt;DMatch&gt;&amp; matches, Ptr&lt;DescriptorMatcher&gt;&amp; descriptorMatcher)
  {
      cout &lt;&lt; "Set train descriptors collection in the matcher and match query descriptors to them..." &lt;&lt; endl;
      TickMeter tm;
      tm.start();
      descriptorMatcher-&gt;add(trainDescriptors);
      descriptorMatcher-&gt;train();
      tm.stop();
      double buildTime = tm.getTimeMilli();
  
      tm.start();
      descriptorMatcher-&gt;match(queryDescriptors, matches);
      tm.stop();
      double matchTime = tm.getTimeMilli();
  
      CV_Assert(queryDescriptors.rows == (int)matches.size() || matches.empty());
  
      cout &lt;&lt; "Number of matches: " &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; endl;
  } // end void matchDescriptors
  
  static void saveResultImages(const Mat&amp; queryImage, const vector&lt;KeyPoint&gt;&amp; queryKeypoints,
      const vector&lt;Mat&gt;&amp; trainImages, const vector&lt;vector&lt;KeyPoint&gt;&gt; &amp;trainKeypoints, const vector&lt;DMatch&gt;&amp; matches, 
      const vector&lt;string&gt;&amp; trainImageNames, const string&amp; resultDir)
  {
      cout &lt;&lt; "Save results..." &lt;&lt; endl;
      Mat drawImg;
      vector&lt;char&gt; mask;
      for(size_t i = 0; i &lt; trainImages.size(); i++)
      {
          if(!trainImages[i].empty())
          {
              maskMatchesByTrainImgIdx(matches, (int)i, mask);
              drawMatches(queryImage, queryKeypoints, trainImages[i], trainKeypoints[i], matches, drawImg, Scalar(255, 0, 0), Scalar(0, 255, 255), mask);
              string filename = resultDir + "/res_" + trainImageNames[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;
              } // end if
          } // end if
      }
  } // end void saveResultImages
  
  static void maskMatchesByTrainImgIdx(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;
          }
      }
  } // end void maskMatchesByTrainImgIdx
  
  int main()
  {
      initModule_nonfree();
      string detectorType = defaultDetectorType;
      string descriptorType = defaultDetectorType;
      string matcherType = defaultMatcherType;
      string queryImageName = defaultQueryImageName;
      string fileWithTrainImages = defaultFileWithTrainImages;
      string dirToSaveResImages = defaultDirToSaveResImages;
  
      Ptr&lt;FeatureDetector&gt; featureDetector = FeatureDetector::create("SURF");
      Ptr&lt;DescriptorExtractor&gt; descriptorExtractor = DescriptorExtractor::create("SURF");
      Ptr&lt;DescriptorMatcher&gt; descriptorMatcher;
  
      if(!createDetectorDescriptorMatcher(detectorType, descriptorType, matcherType, featureDetector, descriptorExtractor, descriptorMatcher))
      {
  
          _getch();
          return -1;
      }
  
      Mat queryImages;
      vector&lt;Mat&gt; trainImages;
      vector&lt;string&gt; trainImagesNames;
  
      vector&lt;KeyPoint&gt; queryKeypoints;
      vector&lt;vector&lt;KeyPoint&gt;&gt; trainKeypoints;
  
      if(!readImages(defaultQueryImageName, queryImages))
      {
          _getch();
          return -1;
      } // end if
  
      if(!readTrainImages(defaultFileWithTrainImages, trainImages, trainImagesNames))
      {
          _getch();
          return -1;
      }
  
      detectKeypoints(queryImages, queryKeypoints, trainImages, trainKeypoints, featureDetector);
  
      Mat queryDescriptors;
      vector&lt;Mat&gt; trainDescriptors;
  
      computeDescriptors(queryImages, queryKeypoints, queryDescriptors, trainImages, trainKeypoints, trainDescriptors, descriptorExtractor);
  
      vector&lt;DMatch&gt; matches;
      matchDescriptors(queryDescriptors, trainDescriptors, matches, descriptorMatcher);
  
      saveResultImages(queryImages, queryKeypoints, trainImages, trainKeypoints, matches, trainImagesNames, dirToSaveResImages);
  
      cout &lt;&lt; "\n done \n";
      _getch();
      return 0;
  } // end main method
  </code></pre>