深入理解DBOW3算法(一)

导语: 最近在做视觉闭环相关的研究, 希望改进其中的部分问题, 因知”纸上得来终觉浅,绝知此事要躬行”之理, 没有对代码细节的研究就谈不上真正精通词带库算法, 因而笔者最近详细阅读了dbow3开源代码,对其中的算法细节有了较好的理解,汇总成文,著录于此,与大家分享.

词袋在笔者看来其本质的意义是事先通过kmeans聚类将图像的描述子进行聚类, 通过离线的无监督学习生成了数据库,之后每来一帧新图像,就可以将该图像与数据库进行比较.这里的关键是,将之前将描述子进行brouteforce穷举搜索的高计算复杂度通过对类的比较来减少计算复杂度,由于我们事先存储了词带数据库,所以该方法本质是一种以空间换时间的算法.那么该方法是如何进行的呢?

下面笔者将通过一个demo进行说明,首先下载DBOW3官方源码:

git clone https://github.com/rmsalinas/DBow3.git
mkdir build
cd build
cmake ..
make

编译完成后,输入以下命令,即对image0/1/2进行orb特征提取,并生成描述子,将描述子进行无监督学习生成了词带库”small_db.yml.gz”

../build/utils/demo_general orb ./images/image0.png images/image1.png images/image2.png

命令执行结果:

Extracting   features...
reading image: ./images/image0.png
extracting features
[ INFO:0] Initialize OpenCL runtime...
done detecting features
reading image: images/image1.png
extracting features
done detecting features
reading image: images/image2.png
extracting features
done detecting features
Creating a small 9^3 vocabulary...
feature.size: 1493
m_node.size: 763
m_word.size: 672
... done!
Vocabulary information: 
Vocabulary: k = 9, L = 3, Weighting = tf-idf, Scoring = L1-norm, Number of words = 672

Matching images against themselves (0 low, 1 high): 
Image 0 vs Image 0: 1
Image 0 vs Image 1: 0.0484061
Image 0 vs Image 2: 0.040641
Image 1 vs Image 0: 0.0484061
Image 1 vs Image 1: 1
Image 1 vs Image 2: 0.120282
Image 2 vs Image 0: 0.040641
Image 2 vs Image 1: 0.120282
Image 2 vs Image 2: 1

Saving vocabulary...
1 2 3 4 5 6 7 8 9 676 677 678 679 680 681 682 683 684 757 758 759 760 761 762 748 749 750 751 752 753 754 755 756 739 740 741 742 743 744 745 746 747 730 731 732 733 734 735 736 737 738 721 722 723 724 725 726 727 728 729 712 713 714 715 716 717 718 719 720 703 704 705 706 707 708 709 710 711 694 695 696 697 698 699 700 701 702 685 686 687 688 689 690 691 692 693 593 594 595 596 597 598 599 600 601 667 668 669 670 671 672 673 674 675 658 659 660 661 662 663 664 665 666 649 650 651 652 653 654 655 656 657 640 641 642 643 644 645 646 647 648 631 632 633 634 635 636 637 638 639 622 623 624 625 626 627 628 629 630 613 614 615 616 617 618 619 620 621 604 605 606 607 608 609 610 611 612 602 603 503 504 505 506 507 508 509 510 511 584 585 586 587 588 589 590 591 592 575 576 577 578 579 580 581 582 583 566 567 568 569 570 571 572 573 574 557 558 559 560 561 562 563 564 565 548 549 550 551 552 553 554 555 556 539 540 541 542 543 544 545 546 547 530 531 532 533 534 535 536 537 538 521 522 523 524 525 526 527 528 529 512 513 514 515 516 517 518 519 520 423 424 425 426 427 428 429 430 431 498 499 500 501 502 489 490 491 492 493 494 495 496 497 480 481 482 483 484 485 486 487 488 473 474 475 476 477 478 479 464 465 466 467 468 469 470 471 472 459 460 461 462 463 450 451 452 453 454 455 456 457 458 441 442 443 444 445 446 447 448 449 432 433 434 435 436 437 438 439 440 333 334 335 336 337 338 339 340 341 414 415 416 417 418 419 420 421 422 405 406 407 408 409 410 411 412 413 396 397 398 399 400 401 402 403 404 387 388 389 390 391 392 393 394 395 378 379 380 381 382 383 384 385 386 369 370 371 372 373 374 375 376 377 360 361 362 363 364 365 366 367 368 351 352 353 354 355 356 357 358 359 342 343 344 345 346 347 348 349 350 246 247 248 249 250 251 252 253 254 324 325 326 327 328 329 330 331 332 315 316 317 318 319 320 321 322 323 306 307 308 309 310 311 312 313 314 299 300 301 302 303 304 305 290 291 292 293 294 295 296 297 298 281 282 283 284 285 286 287 288 289 272 273 274 275 276 277 278 279 280 264 265 266 267 268 269 270 271 255 256 257 258 259 260 261 262 263 156 157 158 159 160 161 162 163 164 237 238 239 240 241 242 243 244 245 228 229 230 231 232 233 234 235 236 219 220 221 222 223 224 225 226 227 210 211 212 213 214 215 216 217 218 201 202 203 204 205 206 207 208 209 192 193 194 195 196 197 198 199 200 183 184 185 186 187 188 189 190 191 174 175 176 177 178 179 180 181 182 165 166 167 168 169 170 171 172 173 88 89 90 91 92 93 94 95 96 150 151 152 153 154 155 141 142 143 144 145 146 147 148 149 137 138 139 140 133 134 135 136 129 130 131 132 120 121 122 123 124 125 126 127 128 114 115 116 117 118 119 106 107 108 109 110 111 112 113 97 98 99 100 101 102 103 104 105 10 11 12 13 14 15 16 17 18 79 80 81 82 83 84 85 86 87 70 71 72 73 74 75 76 77 78 61 62 63 64 65 66 67 68 69 55 56 57 58 59 60 50 51 52 53 54 41 42 43 44 45 46 47 48 49 37 38 39 40 28 29 30 31 32 33 34 35 36 19 20 21 22 23 24 25 26 27 
Done

执行结果显示:

生成了9分支,深度为3的词袋树,对每个叶子节点使用TF-IDF权重,算分方式使用L1范数,叶子节点数为672,同时显示了每个word的ID

之后,对于img0/1/2,分别将图像与词袋树进行计算,生成了词袋向量,通过图像对于的词袋向量进行算分,可知,img0与自身的相似度为1,与img1相似度为0.0484061,与img2相似度为0.040641,以此类推.

上述命令的测试代码如下:

//输入参数为三张图像的描述子集合(vector,vector的每个元素为对应图像的描述子矩阵,矩阵的每一行为描述子向量256维)
void testVocCreation(const vector<cv::Mat> &features) {
    // branching factor and depth levels
    const int k = 9; //配置分支数为9
    const int L = 3; //配置深度为3
    const WeightingType weight = TF_IDF; //使用TF-IDF作为word权重
    const ScoringType score = L1_NORM; //使用L1范数计算word之间的分数差

    DBoW3::Vocabulary voc(k, L, weight, score);

    cout << "Creating a small " << k << "^" << L << " vocabulary..." << endl;
    voc.create(features); //该接口使用kmeans++生成了相应的词袋树
    cout << "... done!" << endl;

    cout << "Vocabulary information: " << endl
         << voc << endl << endl;

    // lets do something with this vocabulary
    cout << "Matching images against themselves (0 low, 1 high): " << endl;
    BowVector v1, v2; //词袋树生成之后,每进来一帧图像,都会将该图像与词袋树进行计算,生成bowvector,
                      //之后使用bowvecor来做图像相似度的计算
    for (size_t i = 0; i < features.size(); i++) {
        voc.transform(features[i], v1); 
        for (size_t j = 0; j < features.size(); j++) {
            voc.transform(features[j], v2);

            double score = voc.score(v1, v2); //这里才是真正计算图像相似度
                                              //score = {0~1} 数字越大表示越相似
            cout << "Image " << i << " vs Image " << j << ": " << score << endl;
        }
    }

    // save the vocabulary to disk
    cout << endl << "Saving vocabulary..." << endl;
    voc.save("small_voc.yml.gz");
    cout << "Done" << endl;
}

本篇主要介绍了词袋库使用,并引入了几个概念,下文将对概念做详细解释

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值