Implementation and analysis of Flann (Fast Library for approximate Nearest neighbors) Implementation of OpenCV k-d tree

Source: Internet
Author: User

The k-d tree searches for the nearest point, using the Flann algorithm in OpenCV, which contains:

1: Achievements 2. Enquiry

See the procedure below:

#include "kdtree.h"
#include <iostream>
#include <iomanip>
#include "Cv.h"
#include "highgui.h"
#include <fstream>
#include "Cv.h"
#include "highgui.h"
#include <vector>//<vector > STL header File
#include <stdio.h>
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include <iostream>


#define EXAMPLAR_NUM 700
#define Examplar_dim 3
The/**** system can search for the nearest point in 660 3-point clouds ****/
/************ Saving Cvmat matrix ****************************
Input parameters: &vecstring:n Point Cloud container
Output parameters: *matri: Convert to corresponding 3*n matrix
*************************************************************/
void Savecvmattotxt (const cvmat*matri, const char* filename)
{
Std::ofstream file (filename); Save File


for (int i = 0; i<matri->rows; i++)
{
for (int j = 0; j<matri->cols; j + +)
{
File << "" << cvmget (Matri, I, J);
}
File << Std::endl;
}
File.close ();
}
/*********** Read txt document save Cvmat matrix ****************************
Input parameters: &vecstring:n Point Cloud container
Output parameters: *matri: Convert to corresponding 3*n matrix
***************************************************************/
void Readtxttocvmat (Cvmat*matri, const char* filename)
{
Std::ifstream file (filename); Save File
Double temp;
for (int i = 0; i<matri->rows; i++)
{
for (int j = 0; j<matri->cols; j + +)
{
File >> temp;
Cvmset (Matri, I, j,temp);
}
}
File.close ();
}

int main (int argc, char *argv[])
{

Cv::mat Target (Ten, 3, cv_32f); Target Point Cloud (build k-d tree with it)

Readtxttocvmat (& (Cvmat) target, "data.txt");
std::cout << "target" <<target<< Std::endl;

1) Create Query tree: (Build k-d tree here)


Cv::flann::kdtreeindexparams Indexparams (4);//(this parameter is used to set the data structure of the build, where the k-d tree is selected)
Cv::flann::index Kdtree (target, indexparams);//Build k-d tree here with target


2) Find:
/*CV::P oint3f pt;
Std::cout << "Input Target point:" << Std::endl;
Std::cin >> pt.x >> pt.y >> pt.z;
std::vector<float> query;
Query.push_back (Pt.x);
Query.push_back (PT.Y);
Query.push_back (PT.Z); *//can be used to manually enter a single original point to be searched

Cv::mat source= Target; Original point
Cv::mat neibours (Source.rows, Source.cols, cv_32f);//Store The search to the point

int k = 1; Number of nearest neighbors

Cv::mat Indices (source.rows, K, cv_32f); The index of the corresponding point at which the search was loaded (that is, the number of rows of neibours in the target matrix)
Cv::mat dists (source.rows,k,cv_32f); Nearest nearest neighbor to search
Kdtree.knnsearch (source, indices, dists, K, Cv::flann::searchparams (32));


Std::cout << indices.at<int> (9, 0) << Std::endl;
Std::cout << dists.at<float> (9,0) << Std::endl;
for (int i = 0; i < neibours.rows; i++)
{
Neibours.row (i) = Target.Row (indices.at<int> (i, 0)) + 0;
}

Std::cout << "source=" << source << Std::endl;
Std::cout << "neibours" << neibours << Std::endl;
Std::cout << "indices=" << indices << Std::endl;
Std::cout << "dists=" << dists << Std::endl;


System ("PAUSE");
return 0;
}

Experimental results:


Implementation and analysis of Flann (Fast Library for approximate Nearest neighbors) Implementation of OpenCV k-d tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.