OpenCV C++ 词袋

OpenCV C++ Bag Of Words

在网上的任何地方,您都可以找到关于 BOW 不同部分的小教程,但是(据我所知)没有关于您之后做什么的内容:

bowDE.setVocabulary(dictionary);

...

bowDE.compute(image, keypoints, descriptors);

一旦你使用了 BOWImgDescriptorExtractorcompute,你会做什么?

你怎么知道什么是好的匹配,什么不是?

然后你能利用这些信息吗?

如果是,怎么做?

如果您同时拥有描述符和提取器,则可以使用匹配器来查找匹配项。

这是一个示例函数:

void drawMatches(const Mat& Img1,const Mat& Img2,const vector<KeyPoint>& Keypoints1,
    const vector<KeyPoint>& Keypoints2,const Mat& Descriptors1,const Mat& Descriptors2)
{
    Ptr<DescriptorMatcher> descriptorMatcher = DescriptorMatcher::create( "BruteForce" ); //
    vector<DMatch> matches;
    descriptorMatcher->match( Descriptors1, Descriptors2, matches );
    Mat matchImg;
    drawMatches(Img1,Keypoints1,Img2,Keypoints2,matches,matchImg,Scalar::all(-1),CV_RGB(255,255,255),Mat(),4);
    imshow("match",show);
}

获得这些匹配后,您可以通过检查它们的最大距离、平均距离、总匹配大小等来确定哪些匹配是“好”的。

关于如何使用这些描述符和关键点进行匹配还有一个官方教程

Features2D + Homography to find a known object

虽然它使用的特征检测器与您的不同,但您仍然可以使用文章的匹配部分。

更新:

无法准确回答匹配是否“正确”匹配。但是你可以得到匹配对的值。

这是使用 SIFT 特征检测器和 BruteForce 匹配器的“错误”匹配和“正确”匹配的示例。

部分代码:

size_t matches_size = matches.size();
for( unsigned i = 0; i < matches_size; i++ )
{
    if( matches[i].distance < MY_GOOD_DISTANCE)//You can get the matching distance like this.
    {
        good_matches.push_back( matches[i]); 
    }
}

这是一个正确的匹配。

计算出匹配后,我列出了匹配的距离:

27.7669 43.715  45.2217 47.4552 53.1601 54.074  57.3672 58.2924 59.0593 63.3009 
63.6475 64.1093 64.8922 67.0075 70.9718 73.4507 74.0878 76.6225 76.6551 80.075  
81.2219 82.2192 83.6959 89.2412 90.7855 91.4604 95.3363 95.352  95.6033 98.209  
98.3362 98.3412 99.4082 101.035 104.024 109.567 110.095 110.345 112.858 118.339 
119.311 123.976 125.948 126.625 128.02  128.269 130.219 133.015 135.739 138.43  
144.499 146.055 146.492 147.054 152.925 160.044 161.165 168.899 170.871 179.881 
183.39  183.573 187.061 192.764 192.961 194.268 194.44  196.489 202.255 204.854 
230.643 230.92  231.961 233.238 235.253 236.023 244.225 246.337 253.829 260.384 
261.383 263.934 266.933 269.232 272.586 273.651 283.891 289.261 291.805 297.165 
297.22  297.627 304.132 307.633 307.695 314.798 325.294 334.74  335.272 344.17  
352.095 353.456 354.144 357.398 363.762 366.344 367.301 368.977 371.102 371.44  
371.863 372.459 372.85  373.17  376.082 378.844 382.372 389.01  389.704 397.028 
398.236 400.53  414.523 417.628 422.61  430.731 461.3   

Min value: 27.76
Max value: 461.3
Average: 210.2526882

这是一个错误的匹配:

336.161 437.132 310.587 376.245 368.683 449.708 334.148 354.79  333.981 399.794 368.889 
361.653 341.778 266.443 259.365 338.726 352.789 381.097 427.143 350.732 355.522 349.819 
358.569 373.139 348.201 341.923 383.188 378.233 399.844 294.16  505.107 347.978 314.021 
332.983 335.364 403.217 385.8   408.859 381.472 372.078 434.167 436.489 279.646 253.271 
268.522 376.303 418.071 373.3   369.004 272.145 254.448 408.185 326.351 351.886 333.981 
371.59  440.336 230.558 250.928 337.368 288.579 262.107 409.971 339.391 380.58  374.162 
361.96  392.59  345.936 328.691 383.586 398.986 336.283 365.768 492.984 392.379 377.042 
371.652 279.014 370.849 378.213 351.048 311.148 319.168 324.268 319.191 261.555 339.257 
298.572 241.622 406.977 286.068 438.586 

Min value: 230
Max value: 505
Average: 352.6009711

获得所有匹配的距离后,您可以轻松看出什么是“好”匹配,什么是“坏”匹配。

这是计分部分。有点棘手,与数据高度相关。 MY_AVG_DISTANCE、MY_LEAST_DISTANCE、MY_MAX_DISTANCE 和 MY_GOOD_DISTANCE 是您应该仔细 select 的值。检查您自己的匹配距离,并 select 一些值。

int good_size = good_matches.size() > 30 ? 30 : good_matches.size(); //In case there are too many "good matches"
//...
//===========SCORE ============
double avg = 0;     //Calculates the average of some of the matches. 
int avgCount = 0;
int goodCount = 0 ;
for( unsigned i = 0; i < matches.size(); i++ )
{
    double dist = matches[i].distance;
    if( dist < MY_AVG_DISTANCE  && dist > MY_LEAST_DISTANCE )
    {
        avg += dist;
        avgCount++;
    }
    if(dist < MY_GOOD_DISTANCE && dist > MY_LEAST_DISTANCE ){
        goodCount++;
    }
}
if(avgCount > 6){
    avg /= avgCount;
    if(goodCount < 12){
        avg = avg + (12-goodCount) * 4;
    }
}else{
    avg = MY_MAX_DISTANCE;
}

avg = avg > MY_AVG_DISTANCE ? MY_AVG_DISTANCE : avg;
avg = avg < MY_MIN_DISTANCE ? MY_MIN_DISTANCE : avg;

double score_avg = (MY_AVG_DISTANCE - avg) / ( MY_AVG_DISTANCE - MY_MIN_DISTANCE ) * 100;
if(formsHomography){ //Some bonus...not related with your matching method, but you can adopt something like this
    score_avg += 40;
    score_avg = score_avg > 100 ? 100 : score_avg;
}else{
    score_avg -= 5;
    score_avg = score_avg < 0 ? 0 : score_avg;
}
return score_avg;

你可以找到一个simple implementation of bag of words in C++ here。所以你不需要OpenCV依赖。

class Statistics {
  std::unordered_map<std::string, int64_t> _counts;
  int64_t _totWords;

  void process(std::string& token);
public:
  explicit Statistics(const std::string& text);

  double Dist(const Statistics& fellow) const;

  bool IsEmpty() const { return _totWords == 0; }
};

namespace {
  const std::string gPunctStr = ".,;:!?";
  const std::unordered_set<char> gPunctSet(gPunctStr.begin(), gPunctStr.end());
}

Statistics::Statistics(const std::string& text) {
  std::string lastToken;
  for (size_t i = 0; i < text.size(); i++) {
    int ch = static_cast<uint8_t>(text[i]);
    if (!isspace(ch)) {
      lastToken.push_back(tolower(ch));
      continue;
    }
    process(lastToken);
  }
  process(lastToken);
}

void Statistics::process(std::string& token) {
  do {
    if (token.size() == 0) {
      break;
    }
    if (gPunctSet.find(token.back()) != gPunctSet.end()) {
      token.pop_back();
    }
  } while (false);
  if (token.size() != 0) {
    auto it = _counts.find(token);
    if (it == _counts.end()) {
      _counts.emplace(token, 1);
    }
    else {
      it->second++;
    }
    _totWords++;
    token.clear();
  }
}

double Statistics::Dist(const Statistics& fellow) const {
  double sum = 0;
  for (const auto& wordInfo : _counts) {
    const std::string wordText = wordInfo.first;
    const double freq = double(wordInfo.second) / _totWords;
    auto it = fellow._counts.find(wordText);
    double fellowFreq;
    if (it == fellow._counts.end()) {
      fellowFreq = 0;
    }
    else {
      fellowFreq = double(it->second) / fellow._totWords;
    }
    const double d = freq - fellowFreq;
    sum += d * d;
  }
  return std::sqrt(sum);
}