【问题标题】:repeatability rate in OpenCVOpenCV 中的重复率
【发布时间】:2019-07-17 01:39:00
【问题描述】:

我使用以下代码在 Microsoft Visual Studio 2012 中使用 C++ 检测 OpenCV 2.4.10 中的描述和评估功能。

#include <iostream>
#include <fstream>
#include <vector>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/legacy/legacy.hpp>

using namespace cv;
using namespace std;

int main(int argc, char ** argv)
{
  Mat image1;
  image1 = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE); // Read the first file

  Mat image2;
  image2 = imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE);

  if ((!image1.data) || (!image2.data)) {
    std::cout << "ERROR: Cannot load images in\n" << argv[1] << "\n" << argv[2] << endl;
    return -1;
  }

  vector < KeyPoint > keypoints1, keypoints2;
  cv::Mat descriptors1, descriptors2;

  /** Construction of the feature detector
   */

  double ExTime = (double) cv::getTickCount();
  cv::SurfFeatureDetector surf(800);

  /** Detection of the features
   */
  surf.detect(image1, keypoints1);
  surf.detect(image2, keypoints2);

  cv::SurfDescriptorExtractor surfDesc;
  surfDesc.compute(image1, keypoints1, descriptors1);
  surfDesc.compute(image2, keypoints2, descriptors2);

  //Calculate the time needed for code execution
  ExTime = ((double) cv::getTickCount() - ExTime) / cv::getTickFrequency();

  /** Draw the keypoints
   */
  Mat ImageKP1, ImageKP2;

  drawKeypoints(image1, keypoints1, ImageKP1, cv::Scalar(255, 0, 255), cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
  drawKeypoints(image2, keypoints2, ImageKP2, cv::Scalar(255, 0, 255), cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);

  // Construction of the matcher 
  cv::Mat ImageMatch;
  cv::FlannBasedMatcher matcher;

  // Match the two image descriptors    
  std::vector < DMatch > matches;
  matcher.match(descriptors1, descriptors2, matches);

  double max_dist = 0;
  double min_dist = 100;

  //-- Quick calculation of max and min distances between keypoints
  for (int i = 0; i < descriptors1.rows; i++) {
    double dist = matches[i].distance;
    if (dist < min_dist) min_dist = dist;
    if (dist > max_dist) max_dist = dist;
  }

  //-- Draw only "good" matches (i.e. whose distance is less than 2*min_dist,
  //-- or a small arbitary value ( 0.02 ) in the event that min_dist is very
  //-- small)
  std::vector < DMatch > good_matches;

  for (int i = 0; i < descriptors1.rows; i++) {
    if (matches[i].distance <= 2 * min_dist) {
      good_matches.push_back(matches[i]);
    }
  }

  cout << "Number of good matches: " << good_matches.size() << endl;

  drawMatches(image1, keypoints1, image2, keypoints2, good_matches, ImageMatch, cv::Scalar(255, 0, 255), cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);

  /** Evaluation of detected points
   */
  std::cout << ">" << std::endl;
  cout << "Evaluating feature detector..." << endl;
  float repeatability;
  int corrCounter;
  cv::Mat Homog;

  std::vector < cv::Point2f > srcKey;
  std::vector < cv::Point2f > refKey;

  for (int i = 0; i < matches.size(); i++) {
    srcKey.push_back(keypoints1[matches[i].queryIdx].pt);
    refKey.push_back(keypoints2[matches[i].queryIdx].pt);
  }

  Homog = cv::findHomography(srcKey, refKey, CV_RANSAC, 1);

  cv::evaluateFeatureDetector(image1, image2, Homog, & keypoints1, & keypoints2, repeatability, corrCounter);

  std::cout << "repeatability = " << repeatability << std::endl;
  std::cout << "correspCount = " << corrCounter << std::endl;
  std::cout << ">" << std::endl;

  system("pause");

  return 0;
}

问题在于repeatability 的速率始终为 -1,correspCount 也是如此。 我使用我的图像,它们有很大的重叠并且检测到许多特征。我在OpenCV的官方网站上找不到关于该功能的教程

cv::EvaluateFeatureDetector

但只是像这样的网站中的一些教程。有一些类似的问题,但没有人总是以 -1 作为回报。可能有什么问题?

【问题讨论】:

  • 你检查过你的同形异义矩阵了吗?是否给出合理的结果?

标签: c++ opencv feature-detection


【解决方案1】:

如果您检查the source codeevaluateFeatureDetector 函数,它会检查关键点并调用calculateRepeatability 函数。在该函数中,第 436 行:

correspondencesCount = -1;
repeatability = -1.f;
if( overlaps.empty() )
    return;

这些值设置为 -1,如果两个图像中过滤的关键点之间没有重叠,则返回这些值。在这种情况下,由于您的实际关键点向量不是空的,我会说您的单应性不正确。这可能是由于图像不够相似,或者单应性计算不正确。既然你保证第一个不是这种情况,我建议你怀疑得到的单应矩阵。您可以使用this tutorial 通过生成的单应性在透视变换下绘制图像,这样如果图像意外失真,您将了解上述功能如何/为何失败。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-21
    • 2010-12-15
    • 1970-01-01
    • 2019-07-22
    • 2016-10-21
    • 2018-02-23
    相关资源
    最近更新 更多