Open Source Computer Vision Library https://opencv.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

253 lines
12 KiB

\section{Fast Approximate Nearest Neighbor Search}
\ifCpp
This section documents OpenCV's interface to the FLANN\footnote{http://people.cs.ubc.ca/\~mariusm/flann} library. FLANN (Fast Library for Approximate Nearest Neighbors) is a library that
contains a collection of algorithms optimized for fast nearest neighbor search in large datasets and for high dimensional features. More
information about FLANN can be found in \cite{muja_flann_2009}.
\ifplastex
\cvclass{cv::flann::Index_}
\else
\subsection{cv::flann::Index\_}\label{cvflann.Index}
\fi
The FLANN nearest neighbor index class. This class is templated with the type of elements for which the index is built.
\begin{lstlisting}
namespace cv
{
namespace flann
{
template <typename T>
class Index_
{
public:
Index_(const Mat& features, const IndexParams& params);
~Index_();
void knnSearch(const vector<T>& query,
vector<int>& indices,
vector<float>& dists,
int knn,
const SearchParams& params);
void knnSearch(const Mat& queries,
Mat& indices,
Mat& dists,
int knn,
const SearchParams& params);
int radiusSearch(const vector<T>& query,
vector<int>& indices,
vector<float>& dists,
float radius,
const SearchParams& params);
int radiusSearch(const Mat& query,
Mat& indices,
Mat& dists,
float radius,
const SearchParams& params);
void save(std::string filename);
int veclen() const;
int size() const;
const IndexParams* getIndexParameters();
};
typedef Index_<float> Index;
} } // namespace cv::flann
\end{lstlisting}
\ifplastex
\cvCppFunc{cv::flann::Index_<T>::Index_}
\else
\subsection{cvflann::Index\_$<T>$::Index\_}\label{cvflann.Index.Index}
\fi
Constructs a nearest neighbor search index for a given dataset.
\cvdefCpp{Index\_<T>::Index\_(const Mat\& features, const IndexParams\& params);}
\begin{description}
\cvarg{features}{ Matrix of containing the features(points) to index. The size of the matrix is num\_features x feature\_dimensionality and
the data type of the elements in the matrix must coincide with the type of the index.}
\cvarg{params}{Structure containing the index parameters. The type of index that will be constructed depends on the type of this parameter.
The possible parameter types are:}
\begin{description}
\cvarg{LinearIndexParams}{When passing an object of this type, the index will perform a linear, brute-force search.}
\begin{lstlisting}
struct LinearIndexParams : public IndexParams
{
};
\end{lstlisting}
\cvarg{KDTreeIndexParams}{When passing an object of this type the index constructed will consist of a set of randomized kd-trees which will be searched in parallel.}
\begin{lstlisting}
struct KDTreeIndexParams : public IndexParams
{
KDTreeIndexParams( int trees = 4 );
};
\end{lstlisting}
\begin{description}
\cvarg{trees}{The number of parallel kd-trees to use. Good values are in the range [1..16]}
\end{description}
\cvarg{KMeansIndexParams}{When passing an object of this type the index constructed will be a hierarchical k-means tree.}
\begin{lstlisting}
struct KMeansIndexParams : public IndexParams
{
KMeansIndexParams(
int branching = 32,
int iterations = 11,
flann_centers_init_t centers_init = CENTERS_RANDOM,
float cb_index = 0.2 );
};
\end{lstlisting}
\begin{description}
\cvarg{branching}{ The branching factor to use for the hierarchical k-means tree }
\cvarg{iterations}{ The maximum number of iterations to use in the k-means clustering stage when building the k-means tree. A value of -1 used here means that the k-means clustering should be iterated until convergence}
\cvarg{centers\_init}{The algorithm to use for selecting the initial centers when performing a k-means clustering step. The possible values are \texttt{CENTERS\_RANDOM} (picks the initial cluster centers randomly), \texttt{CENTERS\_GONZALES} (picks the initial centers using Gonzales' algorithm) and \texttt{CENTERS\_KMEANSPP} (picks the initial centers using the algorithm suggested in \cite{arthur_kmeanspp_2007})}
\cvarg{cb\_index}{This parameter (cluster boundary index) influences the way exploration is performed in the hierarchical kmeans tree. When \texttt{cb\_index} is zero the next kmeans domain to be explored is choosen to be the one with the closest center. A value greater then zero also takes into account the size of the domain.}
\end{description}
\cvarg{CompositeIndexParams}{When using a parameters object of this type the index created combines the randomized kd-trees and the hierarchical k-means tree.}
\begin{lstlisting}
struct CompositeIndexParams : public IndexParams
{
CompositeIndexParams(
int trees = 4,
int branching = 32,
int iterations = 11,
flann_centers_init_t centers_init = CENTERS_RANDOM,
float cb_index = 0.2 );
};
\end{lstlisting}
\cvarg{AutotunedIndexParams}{When passing an object of this type the index created is automatically tuned to offer the best performance, by choosing the optimal index type (randomized kd-trees, hierarchical kmeans, linear) and parameters for the dataset provided.}
\begin{lstlisting}
struct AutotunedIndexParams : public IndexParams
{
AutotunedIndexParams(
float target_precision = 0.9,
float build_weight = 0.01,
float memory_weight = 0,
float sample_fraction = 0.1 );
};
\end{lstlisting}
\begin{description}
\cvarg{target\_precision}{ Is a number between 0 and 1 specifying the percentage of the approximate nearest-neighbor searches that return the exact nearest-neighbor. Using a higher value for this parameter gives more accurate results, but the search takes longer. The optimum value usually depends on the application. }
\cvarg{build\_weight}{ Specifies the importance of the index build time raported to the nearest-neighbor search time. In some applications it's acceptable for the index build step to take a long time if the subsequent searches in the index can be performed very fast. In other applications it's required that the index be build as fast as possible even if that leads to slightly longer search times.}
\cvarg{memory\_weight}{Is used to specify the tradeoff between time (index build time and search time) and memory used by the index. A value less than 1 gives more importance to the time spent and a value greater than 1 gives more importance to the memory usage.}
\cvarg{sample\_fraction}{Is a number between 0 and 1 indicating what fraction of the dataset to use in the automatic parameter configuration algorithm. Running the algorithm on the full dataset gives the most accurate results, but for very large datasets can take longer than desired. In such case using just a fraction of the data helps speeding up this algorithm while still giving good approximations of the optimum parameters.}
\end{description}
\cvarg{SavedIndexParams}{This object type is used for loading a previously saved index from the disk.}
\begin{lstlisting}
struct SavedIndexParams : public IndexParams
{
SavedIndexParams( std::string filename );
};
\end{lstlisting}
\begin{description}
\cvarg{filename}{ The filename in which the index was saved. }
\end{description}
\end{description}
\end{description}
\ifplastex
\cvCppFunc{cv::flann::Index_<T>::knnSearch}
\else
\subsection{cv::flann::Index\_$<T>$::knnSearch}\label{cvflann.Index.knnSearch}
\fi
Performs a K-nearest neighbor search for a given query point using the index.
\cvdefCpp{
void Index\_<T>::knnSearch(const vector<T>\& query, \par
vector<int>\& indices, \par
vector<float>\& dists, \par
int knn, \par
const SearchParams\& params);\newline
void Index\_<T>::knnSearch(const Mat\& queries,\par
Mat\& indices, Mat\& dists,\par
int knn, const SearchParams\& params);}
\begin{description}
\cvarg{query}{The query point}
\cvarg{indices}{Vector that will contain the indices of the K-nearest neighbors found. It must have at least knn size.}
\cvarg{dists}{Vector that will contain the distances to the K-nearest neighbors found. It must have at least knn size.}
\cvarg{knn}{Number of nearest neighbors to search for.}
\cvarg{params}{Search parameters}
\begin{lstlisting}
struct SearchParams {
SearchParams(int checks = 32);
};
\end{lstlisting}
\begin{description}
\cvarg{checks}{ The number of times the tree(s) in the index should be recursively traversed. A higher value for this parameter would give better search precision, but also take more time. If automatic configuration was used when the index was created, the number of checks required to achieve the specified precision was also computed, in which case this parameter is ignored.}
\end{description}
\end{description}
\ifplastex
\cvCppFunc{cv::flann::Index_<T>::radiusSearch}
\else
\subsection{cv::flann::Index\_$<T>$::radiusSearch}\label{cvflann.Index.radiusSearch}
\fi
Performs a radius nearest neighbor search for a given query point.
\cvdefCpp{
int Index\_<T>::radiusSearch(const vector<T>\& query, \par
vector<int>\& indices, \par
vector<float>\& dists, \par
float radius, \par
const SearchParams\& params);\newline
int Index\_<T>::radiusSearch(const Mat\& query, \par
Mat\& indices, \par
Mat\& dists, \par
float radius, \par
const SearchParams\& params);}
\begin{description}
\cvarg{query}{The query point}
\cvarg{indices}{Vector that will contain the indices of the points found within the search radius in decreasing order of the distance to the query point. If the number of neighbors in the search radius is bigger than the size of this vector, the ones that don't fit in the vector are ignored. }
\cvarg{dists}{Vector that will contain the distances to the points found within the search radius}
\cvarg{radius}{The search radius}
\cvarg{params}{Search parameters}
\end{description}
\ifplastex
\cvCppFunc{cv::flann::Index_<T>::save}
\else
\subsection{cv::flann::Index\_$<T>$::save}\label{cvflann.Index.save}
\fi
Saves the index to a file.
\cvdefCpp{void Index\_<T>::save(std::string filename);}
\begin{description}
\cvarg{filename}{The file to save the index to}
\end{description}
\ifplastex
\cvCppFunc{cv::flann::Index_<T>::getIndexParameters}
\else
\subsection{cv::flann::Index\_$<T>$::getIndexParameters}\label{cvflann.Index.getIndexParameters}
\fi
Returns the index paramreters. This is usefull in case of autotuned indices, when the parameters computed can be retrived using this method.
\cvdefCpp{const IndexParams* Index\_<T>::getIndexParameters();}
\section{Clustering}
\cvCppFunc{cv::flann::hierarchicalClustering<ET,DT>}
Clusters the given points by constructing a hierarchical k-means tree and choosing a cut in the tree that minimizes the cluster's variance.
\cvdefCpp{int hierarchicalClustering<ET,DT>(const Mat\& features, Mat\& centers,\par
const KMeansIndexParams\& params);}
\begin{description}
\cvarg{features}{The points to be clustered. The matrix must have elements of type ET.}
\cvarg{centers}{The centers of the clusters obtained. The matrix must have type DT. The number of rows in this matrix represents the number of clusters desired, however, because of the way the cut in the hierarchical tree is chosen, the number of clusters computed will be the highest number of the form \texttt{(branching-1)*k+1} that's lower than the number of clusters desired, where \texttt{branching} is the tree's branching factor (see description of the KMeansIndexParams).}
\cvarg{params}{Parameters used in the construction of the hierarchical k-means tree}
\end{description}
The function returns the number of clusters computed.
\fi