opencv 暴力線性查找,kmeans查找,LSH查找

opencv 暴力線性查找,kmeans查找與LSH查找測試。

#include <stdio.h>
#include <sys/time.h>
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;

inline timeval cpu_time(){
    timeval t1;
    gettimeofday(&t1,NULL);
    return t1;
}
inline double cost_time(timeval t1,timeval t2){
    double t = 1000*(t2.tv_sec-t1.tv_sec)+(t2.tv_usec-t1.tv_usec)/1000;
    return t;
}

void generate_data(Mat& data){
    int DIM = 1024;
    int samples = 50000;
    RNG rng((unsigned int)time(NULL));
    //generate data randomly
    data.create(samples,DIM,CV_32FC1);
    Point center;
    center.x = rng.uniform(0,data.cols);
    center.y = rng.uniform(0, data.rows);
    rng.fill(data, RNG::NORMAL, Scalar(center.x,center.y), Scalar(data.cols*0.05,data.rows*0.05));
    //cout<<data<<endl;    
    //data.convertTo(data,CV_32FC1);
}

//lsh need data type:CV_8U
void lsh_search(const Mat& data,const Mat& point, Mat& indices, Mat& dists, const int k){
    timeval t1,t2;
    cout<<"Begin build LSHindex: "<<endl;
   // t1 = cpu_time();
    flann::Index flannIndex(data,flann::LshIndexParams(10,20,2),cvflann::FLANN_DIST_EUCLIDEAN);
    flannIndex.save("lshIndex.xml");
   // t2 = cpu_time();
    cout<<"Finish build LSHindex: \n";//<<cost_time(t1,t2)<<"ms"<<endl;

    t1 = cpu_time();

    flannIndex.knnSearch(point, indices, dists, k,flann::SearchParams(64));
    t2 = cpu_time();
    cout<<"Finish lshSearch: "<<cost_time(t1,t2)<<"ms"<<endl;
}
//linear need data type: CV_32FC1
void linear_search(const Mat& data,const Mat& point, Mat& indices, Mat& dists, const int k){
    //data.convertTo(data,CV_32FC1);
    timeval t1,t2;
    flann::Index flannIndex(data,flann::LinearIndexParams(),cvflann::FLANN_DIST_EUCLIDEAN);
    t1 = cpu_time();
    cout<<"Begin LinearSearch: "<<endl;
    flannIndex.knnSearch(point, indices, dists, k, flann::SearchParams(64));
    t2 = cpu_time();
    cout<<"Finish linearSearch: "<<cost_time(t1,t2)<<"ms"<<endl;
}
//kmeans need data type CV_32FC1
void kmeans_search(const Mat& data,const Mat& point, Mat& indices, Mat& dists, const int k){
    timeval t1,t2;
    cout<<"Begin build kmeansIndex "<<endl;
    flann::Index flannIndex(data,flann::KMeansIndexParams(64,-1),cvflann::FLANN_DIST_EUCLIDEAN);
    cout<<"Finish build kmeansIndex"<<endl;
    t1 = cpu_time();
    cout<<"Begin KmeansSearch: "<<endl;
    flannIndex.knnSearch(point, indices, dists, k, flann::SearchParams(64));
    t2 = cpu_time();
    cout<<"Finish KmeansSearch: "<<cost_time(t1,t2)<<"ms"<<endl;
}

int main(){

    //Mat data = generate_data();
    Mat data;
    generate_data(data);
    int k = 5;//knn
    Mat indices(1,k,CV_32SC1);
    Mat point = data.row(1).clone()+0.1;
    Mat dists(point.rows,k,CV_64FC1);

    linear_search(data,point,indices,dists,k);
    cout<<indices<<endl;
    cout<<dists<<endl;

    kmeans_search(data,point,indices,dists,k);
    cout<<indices<<endl;
    cout<<dists<<endl;

    data.convertTo(data,CV_8U);
    point.convertTo(point,CV_8U);
    lsh_search(data,point,indices,dists,k);
    cout<<indices<<endl;
    cout<<dists<<endl;

    return 0;
}
發佈了33 篇原創文章 · 獲贊 7 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章