refactor CUDA BFMatcher algorithm:

use new abstract interface and hidden implementation
pull/3596/head
Vladislav Vinogradov 10 years ago
parent 764d55b81d
commit 8a178da1a4
  1. 459
      modules/cudafeatures2d/include/opencv2/cudafeatures2d.hpp
  2. 24
      modules/cudafeatures2d/perf/perf_features2d.cpp
  3. 1098
      modules/cudafeatures2d/src/brute_force_matcher.cpp
  4. 68
      modules/cudafeatures2d/test/test_features2d.cpp
  5. 11
      modules/stitching/src/matchers.cpp
  6. 18
      samples/gpu/performance/tests.cpp

@ -63,170 +63,315 @@ namespace cv { namespace cuda {
//! @addtogroup cudafeatures2d //! @addtogroup cudafeatures2d
//! @{ //! @{
//
// DescriptorMatcher
//
/** @brief Abstract base class for matching keypoint descriptors.
It has two groups of match methods: for matching descriptors of an image with another image or with
an image set.
*/
class CV_EXPORTS DescriptorMatcher : public cv::Algorithm
{
public:
//
// Factories
//
/** @brief Brute-force descriptor matcher. /** @brief Brute-force descriptor matcher.
For each descriptor in the first set, this matcher finds the closest descriptor in the second set For each descriptor in the first set, this matcher finds the closest descriptor in the second set
by trying each one. This descriptor matcher supports masking permissible matches between descriptor by trying each one. This descriptor matcher supports masking permissible matches of descriptor
sets. sets.
The class BFMatcher_CUDA has an interface similar to the class DescriptorMatcher. It has two groups @param normType One of NORM_L1, NORM_L2, NORM_HAMMING. L1 and L2 norms are
of match methods: for matching descriptors of one image with another image or with an image set. preferable choices for SIFT and SURF descriptors, NORM_HAMMING should be used with ORB, BRISK and
Also, all functions have an alternative to save results either to the GPU memory or to the CPU BRIEF).
memory. */
static Ptr<DescriptorMatcher> createBFMatcher(int norm = cv::NORM_L2);
@sa DescriptorMatcher, BFMatcher //
// Utility
//
/** @brief Returns true if the descriptor matcher supports masking permissible matches.
*/ */
class CV_EXPORTS BFMatcher_CUDA virtual bool isMaskSupported() const = 0;
{
public: //
explicit BFMatcher_CUDA(int norm = cv::NORM_L2); // Descriptor collection
//
//! Add descriptors to train descriptor collection
void add(const std::vector<GpuMat>& descCollection); /** @brief Adds descriptors to train a descriptor collection.
//! Get train descriptors collection If the collection is not empty, the new descriptors are added to existing train descriptors.
const std::vector<GpuMat>& getTrainDescriptors() const;
@param descriptors Descriptors to add. Each descriptors[i] is a set of descriptors from the same
//! Clear train descriptors collection train image.
void clear(); */
virtual void add(const std::vector<GpuMat>& descriptors) = 0;
//! Return true if there are not train descriptors in collection
bool empty() const; /** @brief Returns a constant link to the train descriptor collection.
*/
//! Return true if the matcher supports mask in match methods virtual const std::vector<GpuMat>& getTrainDescriptors() const = 0;
bool isMaskSupported() const;
/** @brief Clears the train descriptor collection.
//! Find one best match for each query descriptor */
void matchSingle(const GpuMat& query, const GpuMat& train, virtual void clear() = 0;
GpuMat& trainIdx, GpuMat& distance,
const GpuMat& mask = GpuMat(), Stream& stream = Stream::Null()); /** @brief Returns true if there are no train descriptors in the collection.
*/
//! Download trainIdx and distance and convert it to CPU vector with DMatch virtual bool empty() const = 0;
static void matchDownload(const GpuMat& trainIdx, const GpuMat& distance, std::vector<DMatch>& matches);
//! Convert trainIdx and distance to vector with DMatch /** @brief Trains a descriptor matcher.
static void matchConvert(const Mat& trainIdx, const Mat& distance, std::vector<DMatch>& matches);
Trains a descriptor matcher (for example, the flann index). In all methods to match, the method
//! Find one best match for each query descriptor train() is run every time before matching.
void match(const GpuMat& query, const GpuMat& train, std::vector<DMatch>& matches, const GpuMat& mask = GpuMat()); */
virtual void train() = 0;
//! Make gpu collection of trains and masks in suitable format for matchCollection function
void makeGpuCollection(GpuMat& trainCollection, GpuMat& maskCollection, const std::vector<GpuMat>& masks = std::vector<GpuMat>()); //
// 1 to 1 match
//! Find one best match from train collection for each query descriptor //
void matchCollection(const GpuMat& query, const GpuMat& trainCollection,
GpuMat& trainIdx, GpuMat& imgIdx, GpuMat& distance, /** @brief Finds the best match for each descriptor from a query set (blocking version).
const GpuMat& masks = GpuMat(), Stream& stream = Stream::Null());
@param queryDescriptors Query set of descriptors.
//! Download trainIdx, imgIdx and distance and convert it to vector with DMatch @param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
static void matchDownload(const GpuMat& trainIdx, const GpuMat& imgIdx, const GpuMat& distance, std::vector<DMatch>& matches); collection stored in the class object.
//! Convert trainIdx, imgIdx and distance to vector with DMatch @param matches Matches. If a query descriptor is masked out in mask , no match is added for this
static void matchConvert(const Mat& trainIdx, const Mat& imgIdx, const Mat& distance, std::vector<DMatch>& matches); descriptor. So, matches size may be smaller than the query descriptors count.
@param mask Mask specifying permissible matches between an input query and train matrices of
//! Find one best match from train collection for each query descriptor. descriptors.
void match(const GpuMat& query, std::vector<DMatch>& matches, const std::vector<GpuMat>& masks = std::vector<GpuMat>());
In the first variant of this method, the train descriptors are passed as an input argument. In the
//! Find k best matches for each query descriptor (in increasing order of distances) second variant of the method, train descriptors collection that was set by DescriptorMatcher::add is
void knnMatchSingle(const GpuMat& query, const GpuMat& train, used. Optional mask (or masks) can be passed to specify which query and training descriptors can be
GpuMat& trainIdx, GpuMat& distance, GpuMat& allDist, int k, matched. Namely, queryDescriptors[i] can be matched with trainDescriptors[j] only if
const GpuMat& mask = GpuMat(), Stream& stream = Stream::Null()); mask.at\<uchar\>(i,j) is non-zero.
*/
//! Download trainIdx and distance and convert it to vector with DMatch virtual void match(InputArray queryDescriptors, InputArray trainDescriptors,
//! compactResult is used when mask is not empty. If compactResult is false matches std::vector<DMatch>& matches,
//! vector will have the same size as queryDescriptors rows. If compactResult is true InputArray mask = noArray()) = 0;
//! matches vector will not contain matches for fully masked out query descriptors.
static void knnMatchDownload(const GpuMat& trainIdx, const GpuMat& distance, /** @overload
std::vector< std::vector<DMatch> >& matches, bool compactResult = false); */
//! Convert trainIdx and distance to vector with DMatch virtual void match(InputArray queryDescriptors,
static void knnMatchConvert(const Mat& trainIdx, const Mat& distance, std::vector<DMatch>& matches,
std::vector< std::vector<DMatch> >& matches, bool compactResult = false); const std::vector<GpuMat>& masks = std::vector<GpuMat>()) = 0;
//! Find k best matches for each query descriptor (in increasing order of distances). /** @brief Finds the best match for each descriptor from a query set (asynchronous version).
//! compactResult is used when mask is not empty. If compactResult is false matches
//! vector will have the same size as queryDescriptors rows. If compactResult is true @param queryDescriptors Query set of descriptors.
//! matches vector will not contain matches for fully masked out query descriptors. @param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
void knnMatch(const GpuMat& query, const GpuMat& train, collection stored in the class object.
std::vector< std::vector<DMatch> >& matches, int k, const GpuMat& mask = GpuMat(), @param matches Matches array stored in GPU memory. Internal representation is not defined.
bool compactResult = false); Use DescriptorMatcher::matchConvert method to retrieve results in standard representation.
@param mask Mask specifying permissible matches between an input query and train matrices of
//! Find k best matches from train collection for each query descriptor (in increasing order of distances) descriptors.
void knnMatch2Collection(const GpuMat& query, const GpuMat& trainCollection, @param stream CUDA stream.
GpuMat& trainIdx, GpuMat& imgIdx, GpuMat& distance,
const GpuMat& maskCollection = GpuMat(), Stream& stream = Stream::Null()); In the first variant of this method, the train descriptors are passed as an input argument. In the
second variant of the method, train descriptors collection that was set by DescriptorMatcher::add is
//! Download trainIdx and distance and convert it to vector with DMatch used. Optional mask (or masks) can be passed to specify which query and training descriptors can be
//! compactResult is used when mask is not empty. If compactResult is false matches matched. Namely, queryDescriptors[i] can be matched with trainDescriptors[j] only if
//! vector will have the same size as queryDescriptors rows. If compactResult is true mask.at\<uchar\>(i,j) is non-zero.
//! matches vector will not contain matches for fully masked out query descriptors. */
//! @see BFMatcher_CUDA::knnMatchDownload virtual void matchAsync(InputArray queryDescriptors, InputArray trainDescriptors,
static void knnMatch2Download(const GpuMat& trainIdx, const GpuMat& imgIdx, const GpuMat& distance, OutputArray matches,
std::vector< std::vector<DMatch> >& matches, bool compactResult = false); InputArray mask = noArray(),
//! Convert trainIdx and distance to vector with DMatch Stream& stream = Stream::Null()) = 0;
//! @see BFMatcher_CUDA::knnMatchConvert
static void knnMatch2Convert(const Mat& trainIdx, const Mat& imgIdx, const Mat& distance, /** @overload
std::vector< std::vector<DMatch> >& matches, bool compactResult = false); */
virtual void matchAsync(InputArray queryDescriptors,
//! Find k best matches for each query descriptor (in increasing order of distances). OutputArray matches,
//! compactResult is used when mask is not empty. If compactResult is false matches const std::vector<GpuMat>& masks = std::vector<GpuMat>(),
//! vector will have the same size as queryDescriptors rows. If compactResult is true Stream& stream = Stream::Null()) = 0;
//! matches vector will not contain matches for fully masked out query descriptors.
void knnMatch(const GpuMat& query, std::vector< std::vector<DMatch> >& matches, int k, /** @brief Converts matches array from internal representation to standard matches vector.
const std::vector<GpuMat>& masks = std::vector<GpuMat>(), bool compactResult = false);
The method is supposed to be used with DescriptorMatcher::matchAsync to get final result.
//! Find best matches for each query descriptor which have distance less than maxDistance. Call this method only after DescriptorMatcher::matchAsync is completed (ie. after synchronization).
//! nMatches.at<int>(0, queryIdx) will contain matches count for queryIdx.
//! carefully nMatches can be greater than trainIdx.cols - it means that matcher didn't find all matches, @param gpu_matches Matches, returned from DescriptorMatcher::matchAsync.
//! because it didn't have enough memory. @param matches Vector of DMatch objects.
//! If trainIdx is empty, then trainIdx and distance will be created with size nQuery x max((nTrain / 100), 10), */
//! otherwize user can pass own allocated trainIdx and distance with size nQuery x nMaxMatches virtual void matchConvert(InputArray gpu_matches,
//! Matches doesn't sorted. std::vector<DMatch>& matches) = 0;
void radiusMatchSingle(const GpuMat& query, const GpuMat& train,
GpuMat& trainIdx, GpuMat& distance, GpuMat& nMatches, float maxDistance, //
const GpuMat& mask = GpuMat(), Stream& stream = Stream::Null()); // knn match
//
//! Download trainIdx, nMatches and distance and convert it to vector with DMatch.
//! matches will be sorted in increasing order of distances. /** @brief Finds the k best matches for each descriptor from a query set (blocking version).
//! compactResult is used when mask is not empty. If compactResult is false matches
//! vector will have the same size as queryDescriptors rows. If compactResult is true @param queryDescriptors Query set of descriptors.
//! matches vector will not contain matches for fully masked out query descriptors. @param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
static void radiusMatchDownload(const GpuMat& trainIdx, const GpuMat& distance, const GpuMat& nMatches, collection stored in the class object.
std::vector< std::vector<DMatch> >& matches, bool compactResult = false); @param matches Matches. Each matches[i] is k or less matches for the same query descriptor.
//! Convert trainIdx, nMatches and distance to vector with DMatch. @param k Count of best matches found per each query descriptor or less if a query descriptor has
static void radiusMatchConvert(const Mat& trainIdx, const Mat& distance, const Mat& nMatches, less than k possible matches in total.
std::vector< std::vector<DMatch> >& matches, bool compactResult = false); @param mask Mask specifying permissible matches between an input query and train matrices of
descriptors.
//! Find best matches for each query descriptor which have distance less than maxDistance @param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
//! in increasing order of distances). false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
void radiusMatch(const GpuMat& query, const GpuMat& train, the matches vector does not contain matches for fully masked-out query descriptors.
std::vector< std::vector<DMatch> >& matches, float maxDistance,
const GpuMat& mask = GpuMat(), bool compactResult = false); These extended variants of DescriptorMatcher::match methods find several best matches for each query
descriptor. The matches are returned in the distance increasing order. See DescriptorMatcher::match
//! Find best matches for each query descriptor which have distance less than maxDistance. for the details about query and train descriptors.
//! If trainIdx is empty, then trainIdx and distance will be created with size nQuery x max((nQuery / 100), 10), */
//! otherwize user can pass own allocated trainIdx and distance with size nQuery x nMaxMatches virtual void knnMatch(InputArray queryDescriptors, InputArray trainDescriptors,
//! Matches doesn't sorted. std::vector<std::vector<DMatch> >& matches,
void radiusMatchCollection(const GpuMat& query, GpuMat& trainIdx, GpuMat& imgIdx, GpuMat& distance, GpuMat& nMatches, float maxDistance, int k,
const std::vector<GpuMat>& masks = std::vector<GpuMat>(), Stream& stream = Stream::Null()); InputArray mask = noArray(),
bool compactResult = false) = 0;
//! Download trainIdx, imgIdx, nMatches and distance and convert it to vector with DMatch.
//! matches will be sorted in increasing order of distances. /** @overload
//! compactResult is used when mask is not empty. If compactResult is false matches */
//! vector will have the same size as queryDescriptors rows. If compactResult is true virtual void knnMatch(InputArray queryDescriptors,
//! matches vector will not contain matches for fully masked out query descriptors. std::vector<std::vector<DMatch> >& matches,
static void radiusMatchDownload(const GpuMat& trainIdx, const GpuMat& imgIdx, const GpuMat& distance, const GpuMat& nMatches, int k,
std::vector< std::vector<DMatch> >& matches, bool compactResult = false); const std::vector<GpuMat>& masks = std::vector<GpuMat>(),
//! Convert trainIdx, nMatches and distance to vector with DMatch. bool compactResult = false) = 0;
static void radiusMatchConvert(const Mat& trainIdx, const Mat& imgIdx, const Mat& distance, const Mat& nMatches,
std::vector< std::vector<DMatch> >& matches, bool compactResult = false); /** @brief Finds the k best matches for each descriptor from a query set (asynchronous version).
//! Find best matches from train collection for each query descriptor which have distance less than @param queryDescriptors Query set of descriptors.
//! maxDistance (in increasing order of distances). @param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
void radiusMatch(const GpuMat& query, std::vector< std::vector<DMatch> >& matches, float maxDistance, collection stored in the class object.
const std::vector<GpuMat>& masks = std::vector<GpuMat>(), bool compactResult = false); @param matches Matches array stored in GPU memory. Internal representation is not defined.
Use DescriptorMatcher::knnMatchConvert method to retrieve results in standard representation.
int norm; @param k Count of best matches found per each query descriptor or less if a query descriptor has
less than k possible matches in total.
private: @param mask Mask specifying permissible matches between an input query and train matrices of
std::vector<GpuMat> trainDescCollection; descriptors.
@param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
the matches vector does not contain matches for fully masked-out query descriptors.
@param stream CUDA stream.
These extended variants of DescriptorMatcher::matchAsync methods find several best matches for each query
descriptor. The matches are returned in the distance increasing order. See DescriptorMatcher::matchAsync
for the details about query and train descriptors.
*/
virtual void knnMatchAsync(InputArray queryDescriptors, InputArray trainDescriptors,
OutputArray matches,
int k,
InputArray mask = noArray(),
Stream& stream = Stream::Null()) = 0;
/** @overload
*/
virtual void knnMatchAsync(InputArray queryDescriptors,
OutputArray matches,
int k,
const std::vector<GpuMat>& masks = std::vector<GpuMat>(),
Stream& stream = Stream::Null()) = 0;
/** @brief Converts matches array from internal representation to standard matches vector.
The method is supposed to be used with DescriptorMatcher::knnMatchAsync to get final result.
Call this method only after DescriptorMatcher::knnMatchAsync is completed (ie. after synchronization).
@param gpu_matches Matches, returned from DescriptorMatcher::knnMatchAsync.
@param matches Vector of DMatch objects.
@param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
the matches vector does not contain matches for fully masked-out query descriptors.
*/
virtual void knnMatchConvert(InputArray gpu_matches,
std::vector< std::vector<DMatch> >& matches,
bool compactResult = false) = 0;
//
// radius match
//
/** @brief For each query descriptor, finds the training descriptors not farther than the specified distance (blocking version).
@param queryDescriptors Query set of descriptors.
@param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
collection stored in the class object.
@param matches Found matches.
@param maxDistance Threshold for the distance between matched descriptors. Distance means here
metric distance (e.g. Hamming distance), not the distance between coordinates (which is measured
in Pixels)!
@param mask Mask specifying permissible matches between an input query and train matrices of
descriptors.
@param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
the matches vector does not contain matches for fully masked-out query descriptors.
For each query descriptor, the methods find such training descriptors that the distance between the
query descriptor and the training descriptor is equal or smaller than maxDistance. Found matches are
returned in the distance increasing order.
*/
virtual void radiusMatch(InputArray queryDescriptors, InputArray trainDescriptors,
std::vector<std::vector<DMatch> >& matches,
float maxDistance,
InputArray mask = noArray(),
bool compactResult = false) = 0;
/** @overload
*/
virtual void radiusMatch(InputArray queryDescriptors,
std::vector<std::vector<DMatch> >& matches,
float maxDistance,
const std::vector<GpuMat>& masks = std::vector<GpuMat>(),
bool compactResult = false) = 0;
/** @brief For each query descriptor, finds the training descriptors not farther than the specified distance (asynchronous version).
@param queryDescriptors Query set of descriptors.
@param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
collection stored in the class object.
@param matches Matches array stored in GPU memory. Internal representation is not defined.
Use DescriptorMatcher::radiusMatchConvert method to retrieve results in standard representation.
@param maxDistance Threshold for the distance between matched descriptors. Distance means here
metric distance (e.g. Hamming distance), not the distance between coordinates (which is measured
in Pixels)!
@param mask Mask specifying permissible matches between an input query and train matrices of
descriptors.
@param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
the matches vector does not contain matches for fully masked-out query descriptors.
@param stream CUDA stream.
For each query descriptor, the methods find such training descriptors that the distance between the
query descriptor and the training descriptor is equal or smaller than maxDistance. Found matches are
returned in the distance increasing order.
*/
virtual void radiusMatchAsync(InputArray queryDescriptors, InputArray trainDescriptors,
OutputArray matches,
float maxDistance,
InputArray mask = noArray(),
Stream& stream = Stream::Null()) = 0;
/** @overload
*/
virtual void radiusMatchAsync(InputArray queryDescriptors,
OutputArray matches,
float maxDistance,
const std::vector<GpuMat>& masks = std::vector<GpuMat>(),
Stream& stream = Stream::Null()) = 0;
/** @brief Converts matches array from internal representation to standard matches vector.
The method is supposed to be used with DescriptorMatcher::radiusMatchAsync to get final result.
Call this method only after DescriptorMatcher::radiusMatchAsync is completed (ie. after synchronization).
@param gpu_matches Matches, returned from DescriptorMatcher::radiusMatchAsync.
@param matches Vector of DMatch objects.
@param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
the matches vector does not contain matches for fully masked-out query descriptors.
*/
virtual void radiusMatchConvert(InputArray gpu_matches,
std::vector< std::vector<DMatch> >& matches,
bool compactResult = false) = 0;
}; };
// //

@ -167,16 +167,16 @@ PERF_TEST_P(DescSize_Norm, BFMatch,
if (PERF_RUN_CUDA()) if (PERF_RUN_CUDA())
{ {
cv::cuda::BFMatcher_CUDA d_matcher(normType); cv::Ptr<cv::cuda::DescriptorMatcher> d_matcher = cv::cuda::DescriptorMatcher::createBFMatcher(normType);
const cv::cuda::GpuMat d_query(query); const cv::cuda::GpuMat d_query(query);
const cv::cuda::GpuMat d_train(train); const cv::cuda::GpuMat d_train(train);
cv::cuda::GpuMat d_trainIdx, d_distance; cv::cuda::GpuMat d_matches;
TEST_CYCLE() d_matcher.matchSingle(d_query, d_train, d_trainIdx, d_distance); TEST_CYCLE() d_matcher->matchAsync(d_query, d_train, d_matches);
std::vector<cv::DMatch> gpu_matches; std::vector<cv::DMatch> gpu_matches;
d_matcher.matchDownload(d_trainIdx, d_distance, gpu_matches); d_matcher->matchConvert(d_matches, gpu_matches);
SANITY_CHECK_MATCHES(gpu_matches); SANITY_CHECK_MATCHES(gpu_matches);
} }
@ -226,16 +226,16 @@ PERF_TEST_P(DescSize_K_Norm, BFKnnMatch,
if (PERF_RUN_CUDA()) if (PERF_RUN_CUDA())
{ {
cv::cuda::BFMatcher_CUDA d_matcher(normType); cv::Ptr<cv::cuda::DescriptorMatcher> d_matcher = cv::cuda::DescriptorMatcher::createBFMatcher(normType);
const cv::cuda::GpuMat d_query(query); const cv::cuda::GpuMat d_query(query);
const cv::cuda::GpuMat d_train(train); const cv::cuda::GpuMat d_train(train);
cv::cuda::GpuMat d_trainIdx, d_distance, d_allDist; cv::cuda::GpuMat d_matches;
TEST_CYCLE() d_matcher.knnMatchSingle(d_query, d_train, d_trainIdx, d_distance, d_allDist, k); TEST_CYCLE() d_matcher->knnMatchAsync(d_query, d_train, d_matches, k);
std::vector< std::vector<cv::DMatch> > matchesTbl; std::vector< std::vector<cv::DMatch> > matchesTbl;
d_matcher.knnMatchDownload(d_trainIdx, d_distance, matchesTbl); d_matcher->knnMatchConvert(d_matches, matchesTbl);
std::vector<cv::DMatch> gpu_matches; std::vector<cv::DMatch> gpu_matches;
toOneRowMatches(matchesTbl, gpu_matches); toOneRowMatches(matchesTbl, gpu_matches);
@ -280,16 +280,16 @@ PERF_TEST_P(DescSize_Norm, BFRadiusMatch,
if (PERF_RUN_CUDA()) if (PERF_RUN_CUDA())
{ {
cv::cuda::BFMatcher_CUDA d_matcher(normType); cv::Ptr<cv::cuda::DescriptorMatcher> d_matcher = cv::cuda::DescriptorMatcher::createBFMatcher(normType);
const cv::cuda::GpuMat d_query(query); const cv::cuda::GpuMat d_query(query);
const cv::cuda::GpuMat d_train(train); const cv::cuda::GpuMat d_train(train);
cv::cuda::GpuMat d_trainIdx, d_nMatches, d_distance; cv::cuda::GpuMat d_matches;
TEST_CYCLE() d_matcher.radiusMatchSingle(d_query, d_train, d_trainIdx, d_distance, d_nMatches, maxDistance); TEST_CYCLE() d_matcher->radiusMatchAsync(d_query, d_train, d_matches, maxDistance);
std::vector< std::vector<cv::DMatch> > matchesTbl; std::vector< std::vector<cv::DMatch> > matchesTbl;
d_matcher.radiusMatchDownload(d_trainIdx, d_distance, d_nMatches, matchesTbl); d_matcher->radiusMatchConvert(d_matches, matchesTbl);
std::vector<cv::DMatch> gpu_matches; std::vector<cv::DMatch> gpu_matches;
toOneRowMatches(matchesTbl, gpu_matches); toOneRowMatches(matchesTbl, gpu_matches);

File diff suppressed because it is too large Load Diff

@ -285,7 +285,8 @@ PARAM_TEST_CASE(BruteForceMatcher, cv::cuda::DeviceInfo, NormCode, DescriptorSiz
CUDA_TEST_P(BruteForceMatcher, Match_Single) CUDA_TEST_P(BruteForceMatcher, Match_Single)
{ {
cv::cuda::BFMatcher_CUDA matcher(normCode); cv::Ptr<cv::cuda::DescriptorMatcher> matcher =
cv::cuda::DescriptorMatcher::createBFMatcher(normCode);
cv::cuda::GpuMat mask; cv::cuda::GpuMat mask;
if (useMask) if (useMask)
@ -295,7 +296,7 @@ CUDA_TEST_P(BruteForceMatcher, Match_Single)
} }
std::vector<cv::DMatch> matches; std::vector<cv::DMatch> matches;
matcher.match(loadMat(query), loadMat(train), matches, mask); matcher->match(loadMat(query), loadMat(train), matches, mask);
ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size()); ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());
@ -312,13 +313,14 @@ CUDA_TEST_P(BruteForceMatcher, Match_Single)
CUDA_TEST_P(BruteForceMatcher, Match_Collection) CUDA_TEST_P(BruteForceMatcher, Match_Collection)
{ {
cv::cuda::BFMatcher_CUDA matcher(normCode); cv::Ptr<cv::cuda::DescriptorMatcher> matcher =
cv::cuda::DescriptorMatcher::createBFMatcher(normCode);
cv::cuda::GpuMat d_train(train); cv::cuda::GpuMat d_train(train);
// make add() twice to test such case // make add() twice to test such case
matcher.add(std::vector<cv::cuda::GpuMat>(1, d_train.rowRange(0, train.rows / 2))); matcher->add(std::vector<cv::cuda::GpuMat>(1, d_train.rowRange(0, train.rows / 2)));
matcher.add(std::vector<cv::cuda::GpuMat>(1, d_train.rowRange(train.rows / 2, train.rows))); matcher->add(std::vector<cv::cuda::GpuMat>(1, d_train.rowRange(train.rows / 2, train.rows)));
// prepare masks (make first nearest match illegal) // prepare masks (make first nearest match illegal)
std::vector<cv::cuda::GpuMat> masks(2); std::vector<cv::cuda::GpuMat> masks(2);
@ -331,9 +333,9 @@ CUDA_TEST_P(BruteForceMatcher, Match_Collection)
std::vector<cv::DMatch> matches; std::vector<cv::DMatch> matches;
if (useMask) if (useMask)
matcher.match(cv::cuda::GpuMat(query), matches, masks); matcher->match(cv::cuda::GpuMat(query), matches, masks);
else else
matcher.match(cv::cuda::GpuMat(query), matches); matcher->match(cv::cuda::GpuMat(query), matches);
ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size()); ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());
@ -366,7 +368,8 @@ CUDA_TEST_P(BruteForceMatcher, Match_Collection)
CUDA_TEST_P(BruteForceMatcher, KnnMatch_2_Single) CUDA_TEST_P(BruteForceMatcher, KnnMatch_2_Single)
{ {
cv::cuda::BFMatcher_CUDA matcher(normCode); cv::Ptr<cv::cuda::DescriptorMatcher> matcher =
cv::cuda::DescriptorMatcher::createBFMatcher(normCode);
const int knn = 2; const int knn = 2;
@ -378,7 +381,7 @@ CUDA_TEST_P(BruteForceMatcher, KnnMatch_2_Single)
} }
std::vector< std::vector<cv::DMatch> > matches; std::vector< std::vector<cv::DMatch> > matches;
matcher.knnMatch(loadMat(query), loadMat(train), matches, knn, mask); matcher->knnMatch(loadMat(query), loadMat(train), matches, knn, mask);
ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size()); ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());
@ -405,7 +408,8 @@ CUDA_TEST_P(BruteForceMatcher, KnnMatch_2_Single)
CUDA_TEST_P(BruteForceMatcher, KnnMatch_3_Single) CUDA_TEST_P(BruteForceMatcher, KnnMatch_3_Single)
{ {
cv::cuda::BFMatcher_CUDA matcher(normCode); cv::Ptr<cv::cuda::DescriptorMatcher> matcher =
cv::cuda::DescriptorMatcher::createBFMatcher(normCode);
const int knn = 3; const int knn = 3;
@ -417,7 +421,7 @@ CUDA_TEST_P(BruteForceMatcher, KnnMatch_3_Single)
} }
std::vector< std::vector<cv::DMatch> > matches; std::vector< std::vector<cv::DMatch> > matches;
matcher.knnMatch(loadMat(query), loadMat(train), matches, knn, mask); matcher->knnMatch(loadMat(query), loadMat(train), matches, knn, mask);
ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size()); ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());
@ -444,15 +448,16 @@ CUDA_TEST_P(BruteForceMatcher, KnnMatch_3_Single)
CUDA_TEST_P(BruteForceMatcher, KnnMatch_2_Collection) CUDA_TEST_P(BruteForceMatcher, KnnMatch_2_Collection)
{ {
cv::cuda::BFMatcher_CUDA matcher(normCode); cv::Ptr<cv::cuda::DescriptorMatcher> matcher =
cv::cuda::DescriptorMatcher::createBFMatcher(normCode);
const int knn = 2; const int knn = 2;
cv::cuda::GpuMat d_train(train); cv::cuda::GpuMat d_train(train);
// make add() twice to test such case // make add() twice to test such case
matcher.add(std::vector<cv::cuda::GpuMat>(1, d_train.rowRange(0, train.rows / 2))); matcher->add(std::vector<cv::cuda::GpuMat>(1, d_train.rowRange(0, train.rows / 2)));
matcher.add(std::vector<cv::cuda::GpuMat>(1, d_train.rowRange(train.rows / 2, train.rows))); matcher->add(std::vector<cv::cuda::GpuMat>(1, d_train.rowRange(train.rows / 2, train.rows)));
// prepare masks (make first nearest match illegal) // prepare masks (make first nearest match illegal)
std::vector<cv::cuda::GpuMat> masks(2); std::vector<cv::cuda::GpuMat> masks(2);
@ -466,9 +471,9 @@ CUDA_TEST_P(BruteForceMatcher, KnnMatch_2_Collection)
std::vector< std::vector<cv::DMatch> > matches; std::vector< std::vector<cv::DMatch> > matches;
if (useMask) if (useMask)
matcher.knnMatch(cv::cuda::GpuMat(query), matches, knn, masks); matcher->knnMatch(cv::cuda::GpuMat(query), matches, knn, masks);
else else
matcher.knnMatch(cv::cuda::GpuMat(query), matches, knn); matcher->knnMatch(cv::cuda::GpuMat(query), matches, knn);
ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size()); ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());
@ -506,15 +511,16 @@ CUDA_TEST_P(BruteForceMatcher, KnnMatch_2_Collection)
CUDA_TEST_P(BruteForceMatcher, KnnMatch_3_Collection) CUDA_TEST_P(BruteForceMatcher, KnnMatch_3_Collection)
{ {
cv::cuda::BFMatcher_CUDA matcher(normCode); cv::Ptr<cv::cuda::DescriptorMatcher> matcher =
cv::cuda::DescriptorMatcher::createBFMatcher(normCode);
const int knn = 3; const int knn = 3;
cv::cuda::GpuMat d_train(train); cv::cuda::GpuMat d_train(train);
// make add() twice to test such case // make add() twice to test such case
matcher.add(std::vector<cv::cuda::GpuMat>(1, d_train.rowRange(0, train.rows / 2))); matcher->add(std::vector<cv::cuda::GpuMat>(1, d_train.rowRange(0, train.rows / 2)));
matcher.add(std::vector<cv::cuda::GpuMat>(1, d_train.rowRange(train.rows / 2, train.rows))); matcher->add(std::vector<cv::cuda::GpuMat>(1, d_train.rowRange(train.rows / 2, train.rows)));
// prepare masks (make first nearest match illegal) // prepare masks (make first nearest match illegal)
std::vector<cv::cuda::GpuMat> masks(2); std::vector<cv::cuda::GpuMat> masks(2);
@ -528,9 +534,9 @@ CUDA_TEST_P(BruteForceMatcher, KnnMatch_3_Collection)
std::vector< std::vector<cv::DMatch> > matches; std::vector< std::vector<cv::DMatch> > matches;
if (useMask) if (useMask)
matcher.knnMatch(cv::cuda::GpuMat(query), matches, knn, masks); matcher->knnMatch(cv::cuda::GpuMat(query), matches, knn, masks);
else else
matcher.knnMatch(cv::cuda::GpuMat(query), matches, knn); matcher->knnMatch(cv::cuda::GpuMat(query), matches, knn);
ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size()); ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());
@ -568,7 +574,8 @@ CUDA_TEST_P(BruteForceMatcher, KnnMatch_3_Collection)
CUDA_TEST_P(BruteForceMatcher, RadiusMatch_Single) CUDA_TEST_P(BruteForceMatcher, RadiusMatch_Single)
{ {
cv::cuda::BFMatcher_CUDA matcher(normCode); cv::Ptr<cv::cuda::DescriptorMatcher> matcher =
cv::cuda::DescriptorMatcher::createBFMatcher(normCode);
const float radius = 1.f / countFactor; const float radius = 1.f / countFactor;
@ -577,7 +584,7 @@ CUDA_TEST_P(BruteForceMatcher, RadiusMatch_Single)
try try
{ {
std::vector< std::vector<cv::DMatch> > matches; std::vector< std::vector<cv::DMatch> > matches;
matcher.radiusMatch(loadMat(query), loadMat(train), matches, radius); matcher->radiusMatch(loadMat(query), loadMat(train), matches, radius);
} }
catch (const cv::Exception& e) catch (const cv::Exception& e)
{ {
@ -594,7 +601,7 @@ CUDA_TEST_P(BruteForceMatcher, RadiusMatch_Single)
} }
std::vector< std::vector<cv::DMatch> > matches; std::vector< std::vector<cv::DMatch> > matches;
matcher.radiusMatch(loadMat(query), loadMat(train), matches, radius, mask); matcher->radiusMatch(loadMat(query), loadMat(train), matches, radius, mask);
ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size()); ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());
@ -617,7 +624,8 @@ CUDA_TEST_P(BruteForceMatcher, RadiusMatch_Single)
CUDA_TEST_P(BruteForceMatcher, RadiusMatch_Collection) CUDA_TEST_P(BruteForceMatcher, RadiusMatch_Collection)
{ {
cv::cuda::BFMatcher_CUDA matcher(normCode); cv::Ptr<cv::cuda::DescriptorMatcher> matcher =
cv::cuda::DescriptorMatcher::createBFMatcher(normCode);
const int n = 3; const int n = 3;
const float radius = 1.f / countFactor * n; const float radius = 1.f / countFactor * n;
@ -625,8 +633,8 @@ CUDA_TEST_P(BruteForceMatcher, RadiusMatch_Collection)
cv::cuda::GpuMat d_train(train); cv::cuda::GpuMat d_train(train);
// make add() twice to test such case // make add() twice to test such case
matcher.add(std::vector<cv::cuda::GpuMat>(1, d_train.rowRange(0, train.rows / 2))); matcher->add(std::vector<cv::cuda::GpuMat>(1, d_train.rowRange(0, train.rows / 2)));
matcher.add(std::vector<cv::cuda::GpuMat>(1, d_train.rowRange(train.rows / 2, train.rows))); matcher->add(std::vector<cv::cuda::GpuMat>(1, d_train.rowRange(train.rows / 2, train.rows)));
// prepare masks (make first nearest match illegal) // prepare masks (make first nearest match illegal)
std::vector<cv::cuda::GpuMat> masks(2); std::vector<cv::cuda::GpuMat> masks(2);
@ -642,7 +650,7 @@ CUDA_TEST_P(BruteForceMatcher, RadiusMatch_Collection)
try try
{ {
std::vector< std::vector<cv::DMatch> > matches; std::vector< std::vector<cv::DMatch> > matches;
matcher.radiusMatch(cv::cuda::GpuMat(query), matches, radius, masks); matcher->radiusMatch(cv::cuda::GpuMat(query), matches, radius, masks);
} }
catch (const cv::Exception& e) catch (const cv::Exception& e)
{ {
@ -654,9 +662,9 @@ CUDA_TEST_P(BruteForceMatcher, RadiusMatch_Collection)
std::vector< std::vector<cv::DMatch> > matches; std::vector< std::vector<cv::DMatch> > matches;
if (useMask) if (useMask)
matcher.radiusMatch(cv::cuda::GpuMat(query), matches, radius, masks); matcher->radiusMatch(cv::cuda::GpuMat(query), matches, radius, masks);
else else
matcher.radiusMatch(cv::cuda::GpuMat(query), matches, radius); matcher->radiusMatch(cv::cuda::GpuMat(query), matches, radius);
ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size()); ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());

@ -154,7 +154,7 @@ void CpuMatcher::match(const ImageFeatures &features1, const ImageFeatures &feat
matches_info.matches.clear(); matches_info.matches.clear();
Ptr<DescriptorMatcher> matcher; Ptr<cv::DescriptorMatcher> matcher;
#if 0 // TODO check this #if 0 // TODO check this
if (ocl::useOpenCL()) if (ocl::useOpenCL())
{ {
@ -220,13 +220,13 @@ void GpuMatcher::match(const ImageFeatures &features1, const ImageFeatures &feat
descriptors1_.upload(features1.descriptors); descriptors1_.upload(features1.descriptors);
descriptors2_.upload(features2.descriptors); descriptors2_.upload(features2.descriptors);
BFMatcher_CUDA matcher(NORM_L2); Ptr<cuda::DescriptorMatcher> matcher = cuda::DescriptorMatcher::createBFMatcher(NORM_L2);
MatchesSet matches; MatchesSet matches;
// Find 1->2 matches // Find 1->2 matches
pair_matches.clear(); pair_matches.clear();
matcher.knnMatchSingle(descriptors1_, descriptors2_, train_idx_, distance_, all_dist_, 2); matcher->knnMatch(descriptors1_, descriptors2_, pair_matches, 2);
matcher.knnMatchDownload(train_idx_, distance_, pair_matches);
for (size_t i = 0; i < pair_matches.size(); ++i) for (size_t i = 0; i < pair_matches.size(); ++i)
{ {
if (pair_matches[i].size() < 2) if (pair_matches[i].size() < 2)
@ -242,8 +242,7 @@ void GpuMatcher::match(const ImageFeatures &features1, const ImageFeatures &feat
// Find 2->1 matches // Find 2->1 matches
pair_matches.clear(); pair_matches.clear();
matcher.knnMatchSingle(descriptors2_, descriptors1_, train_idx_, distance_, all_dist_, 2); matcher->knnMatch(descriptors2_, descriptors1_, pair_matches, 2);
matcher.knnMatchDownload(train_idx_, distance_, pair_matches);
for (size_t i = 0; i < pair_matches.size(); ++i) for (size_t i = 0; i < pair_matches.size(); ++i)
{ {
if (pair_matches[i].size() < 2) if (pair_matches[i].size() < 2)

@ -379,14 +379,14 @@ TEST(BruteForceMatcher)
// Init CUDA matcher // Init CUDA matcher
cuda::BFMatcher_CUDA d_matcher(NORM_L2); Ptr<cuda::DescriptorMatcher> d_matcher = cuda::DescriptorMatcher::createBFMatcher(NORM_L2);
cuda::GpuMat d_query(query); cuda::GpuMat d_query(query);
cuda::GpuMat d_train(train); cuda::GpuMat d_train(train);
// Output // Output
vector< vector<DMatch> > matches(2); vector< vector<DMatch> > matches(2);
cuda::GpuMat d_trainIdx, d_distance, d_allDist, d_nMatches; cuda::GpuMat d_matches;
SUBTEST << "match"; SUBTEST << "match";
@ -396,10 +396,10 @@ TEST(BruteForceMatcher)
matcher.match(query, train, matches[0]); matcher.match(query, train, matches[0]);
CPU_OFF; CPU_OFF;
d_matcher.matchSingle(d_query, d_train, d_trainIdx, d_distance); d_matcher->matchAsync(d_query, d_train, d_matches);
CUDA_ON; CUDA_ON;
d_matcher.matchSingle(d_query, d_train, d_trainIdx, d_distance); d_matcher->matchAsync(d_query, d_train, d_matches);
CUDA_OFF; CUDA_OFF;
SUBTEST << "knnMatch"; SUBTEST << "knnMatch";
@ -410,10 +410,10 @@ TEST(BruteForceMatcher)
matcher.knnMatch(query, train, matches, 2); matcher.knnMatch(query, train, matches, 2);
CPU_OFF; CPU_OFF;
d_matcher.knnMatchSingle(d_query, d_train, d_trainIdx, d_distance, d_allDist, 2); d_matcher->knnMatchAsync(d_query, d_train, d_matches, 2);
CUDA_ON; CUDA_ON;
d_matcher.knnMatchSingle(d_query, d_train, d_trainIdx, d_distance, d_allDist, 2); d_matcher->knnMatchAsync(d_query, d_train, d_matches, 2);
CUDA_OFF; CUDA_OFF;
SUBTEST << "radiusMatch"; SUBTEST << "radiusMatch";
@ -426,12 +426,10 @@ TEST(BruteForceMatcher)
matcher.radiusMatch(query, train, matches, max_distance); matcher.radiusMatch(query, train, matches, max_distance);
CPU_OFF; CPU_OFF;
d_trainIdx.release(); d_matcher->radiusMatchAsync(d_query, d_train, d_matches, max_distance);
d_matcher.radiusMatchSingle(d_query, d_train, d_trainIdx, d_distance, d_nMatches, max_distance);
CUDA_ON; CUDA_ON;
d_matcher.radiusMatchSingle(d_query, d_train, d_trainIdx, d_distance, d_nMatches, max_distance); d_matcher->radiusMatchAsync(d_query, d_train, d_matches, max_distance);
CUDA_OFF; CUDA_OFF;
} }

Loading…
Cancel
Save