簡體   English   中英

OpenCV C ++ Bag of Words

[英]OpenCV C++ Bag Of Words

在網上的任何地方,你都可以在BOW的不同部分找到很少的教程,但是(無論如何我發現)沒有關於你做什么之后的事情:

bowDE.setVocabulary(dictionary);

...

bowDE.compute(image, keypoints, descriptors);

一旦你使用BOWImgDescriptorExtractor進行compute ,你會怎么做?

你怎么知道什么是好的比賽,什么不是?

然后你可以利用這些信息嗎?

如果是這樣,怎么樣?

如果同時具有描述符和提取器,則可以使用匹配器來查找匹配項。

這是一個示例函數:

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找到一個已知對象

雖然它使用了與您不同的特征檢測器,但您仍然可以使用文章的匹配部分。

更新:

沒有辦法准確回答匹配是否是“正確”匹配。 但是你可以得到匹配對的值。

以下是使用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是您應該仔細選擇的值。 檢查您自己的匹配距離,並為它們選擇一些值。

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;

你可以在這里找到C ++中單詞包的簡單實現 所以你不需要依賴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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM