flann

install flann and lz4 from github , uninstall the default ones.

sudo apt-get remove liblz4-dev
sudo apt-get remove libflann-dev

CMakeList.txt

cmake_minimum_required(VERSION 2.8)

project(testFlann)

file(GLOB srcs *.cpp *.hpp *.cc *.h)

find_package(Flann REQUIRED)
#find_package(HDF5 REQUIRED)


include_directories(${FLANN_INCLUDE_DIRS})
#include_directories(${HDF5_INCLUDE_DIRS})

link_directories(${HDF5_LIBRARY_DIRS})

add_executable(${PROJECT_NAME} ${srcs})

target_link_libraries(${PROJECT_NAME} ${FLANN_LIBRARIES} lz4 )

# ${HDF5_LIBRARIES}

main.cpp

//============================================================================
// Name        : flann-knn-example.cpp
// Author      : Shohei Yokoyama
// Copyright   : Shizuoka University, Japan
// Description : 1st step of your FLANN application.
// Dependency  : FLANN http://people.cs.ubc.ca/~mariusm/index.php/FLANN/FLANN
//             : Boost http://www.boost.org/
//             : (If win) MinGW & MSYS http://www.mingw.org/
// License     : NYSL see http://www.kmonos.net/nysl/index.en.html
//============================================================================
#include <flann/flann.hpp>
#include <boost/random.hpp>
#include <iostream>
#include <iomanip>
using namespace std;
using namespace boost;

int main(int argc, char** argv) {
    const int numResult = 2;
    const int numInput = 100;
    const int numQuery = 2;
    const int dimension = 2;
    const int maxValue = 255;
    const int minValue = 0;

    mt19937 gen(static_cast<unsigned long>(time(0)));
    uniform_smallint<> dst(minValue,maxValue);
    variate_generator<mt19937&, uniform_smallint<> > rand(gen, dst);

    //Calculate the width of values
    ostringstream ss;
    ss << (numInput - 1);
    string s = ss.str();
    int lnWidth = s.length(); // for line number
    ostringstream sss;
    sss << maxValue;
    s = sss.str();
    int valWidth = s.length(); // for each values
    ostringstream ssss;
    ssss << numQuery;
    s = ssss.str();
    int qWidth = s.length(); // for query number

    cout << "Target Data:" << endl;
    int targetData[(numInput * dimension)];
    for (int i = 0; i < numInput; ++i) {
        cout.width(qWidth);
        cout.fill(' ');
        cout << ' ';
        cout << "#";
        cout.width(lnWidth);
        cout.fill('0');
        cout << i << "\t";
        for (int j = 0; j < dimension; ++j) {
            targetData[i * dimension + j] = rand();
            cout.width(valWidth);
            cout.fill(' ');
            cout << targetData[i * dimension + j] << "\t";
        }
        cout << endl;
    }
    flann::Matrix<int> dataset(targetData, numInput, dimension);
    int queryData[dimension];
    cout << endl << "Query:" << endl;
    for (int i = 0; i < numQuery; i++) {
        cout << "Q";
        cout.width(qWidth);
        cout.fill('0');
        cout << i;
        cout.width(lnWidth + 1);
        cout.fill(' ');
        cout << " " << "\t";
        for (int j = 0; j < dimension; j++) {
            queryData[(i * dimension) + j] = rand();
            cout.width(valWidth);
            cout.fill(' ');
            cout << queryData[(i * dimension) + j] << "\t";
        }
        cout << endl;
    }
    flann::Matrix<int> query(queryData, numQuery, dimension);
    flann::Matrix<int> indices(new int[query.rows * numResult], query.rows,
            numResult);
    flann::Matrix<float> dists(new float[query.rows * numResult], query.rows,
            numResult);
    // construct an randomized kd-tree index using 4 kd-trees
    flann::Index<flann::L2<int> > index(dataset, flann::KDTreeIndexParams(8));
    index.buildIndex();
    // do a knn search, using 128 checks
    index.knnSearch(query, indices, dists, numResult, flann::SearchParams(128));
    cout << endl;
    for (int q = 0; q < numQuery; q++) {
        for (int r = 0; r < numResult; r++) {
            cout << "Q";
            cout.width(qWidth);
            cout.fill('0');
            cout << q;
            cout << "#";
            cout.width(lnWidth);
            cout.fill('0');
            cout << indices[q][r] << "\t";
            for (int j = 0; j < dimension; ++j) {
                cout.width(valWidth);
                cout.fill(' ');
                cout << dataset[indices[q][r]][j] << "\t";
            }
            cout << "dist:" << dists[q][r] << endl;
        }
    }
    //delete[] dataset.ptr();
//    /delete[] query.ptr();
    delete[] indices.ptr();
    delete[] dists.ptr();
//    delete[] query.ptr();
//    delete[] dataset.ptr();
    return 0;
}
Target Data:
 #00    242  16 
 #01    210 243 
 #02    211 208 
 #03     65 180 
 #04    117 239 
 #05     52  70 
 #06    206 215 
 #07    176  45 
 #08     44 198 
 #09    170  11 
 #10    128 215 
 #11    205  59 
 #12     38 101 
 #13      6  80 
 #14    154 211 
 #15    111 225 
 #16    120  76 
 #17    244 130 
 #18    241 109 
 #19     41 182 
 #20      8 149 
 #21    207  34 
 #22     25 110 
 #23    142 102 
 #24     10 119 
 #25      0   5 
 #26    116  65 
 #27    239 134 
 #28     43 234 
 #29    225 103 
 #30     33 121 
 #31    130 130 
 #32    204 130 
 #33     53 132 
 #34    125 128 
 #35    231  35 
 #36     96 111 
 #37    199  56 
 #38    161 190 
 #39      5 224 
 #40    174 145 
 #41      6  29 
 #42     33 146 
 #43     65  41 
 #44    188 224 
 #45    207 139 
 #46    227  74 
 #47     20 245 
 #48     21 155 
 #49     36  68 
 #50    130 173 
 #51     74  93 
 #52    186 168 
 #53    102 129 
 #54     52 250 
 #55     21  49 
 #56     83 102 
 #57    255 186 
 #58    241 208 
 #59     78  29 
 #60     98 140 
 #61    185 173 
 #62    133   6 
 #63    183 195 
 #64     67  55 
 #65     27  19 
 #66    140  48 
 #67    250  88 
 #68    147 173 
 #69     62  26 
 #70     28 234 
 #71    136 107 
 #72    141 162 
 #73    203 226 
 #74    249 185 
 #75    165 162 
 #76     43 240 
 #77     54 173 
 #78     97  35 
 #79    225  39 
 #80    150  44 
 #81    134   1 
 #82     31 154 
 #83    132  40 
 #84     96 200 
 #85    234  38 
 #86     59 135 
 #87      0 141 
 #88    138 192 
 #89     34 177 
 #90    105  27 
 #91    127  81 
 #92     67 166 
 #93    162 131 
 #94    101 209 
 #95     23 171 
 #96    245 120 
 #97    226 201 
 #98    251 187 
 #99     22 225 

Query:
Q0      123 208 
Q1       22  15 

Q0#10   128 215 dist:74
Q0#15   111 225 dist:433
Q1#65    27  19 dist:41
Q1#41     6  29 dist:452
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值