2012-09-12 11:40:13 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
|
|
|
|
// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// @Authors
|
|
|
|
// Nathan, liujun@multicorewareinc.com
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other oclMaterials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of the copyright holders may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
2013-03-26 13:41:13 +08:00
|
|
|
|
2013-01-24 15:45:29 +08:00
|
|
|
#include <functional>
|
2012-09-12 11:40:13 +08:00
|
|
|
#include <iterator>
|
|
|
|
#include <vector>
|
|
|
|
using namespace cv;
|
|
|
|
using namespace cv::ocl;
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
namespace cv
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
namespace ocl
|
|
|
|
{
|
2012-09-12 11:40:13 +08:00
|
|
|
////////////////////////////////////OpenCL kernel strings//////////////////////////
|
|
|
|
extern const char *brute_force_match;
|
2012-10-11 16:22:47 +08:00
|
|
|
}
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
template < int BLOCK_SIZE, int MAX_DESC_LEN/*, typename Mask*/ >
|
2013-02-28 14:31:08 +08:00
|
|
|
void matchUnrolledCached(const oclMat &query, const oclMat &train, const oclMat &/*mask*/,
|
2012-10-11 16:22:47 +08:00
|
|
|
const oclMat &trainIdx, const oclMat &distance, int distType)
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
2013-04-12 18:12:12 +08:00
|
|
|
CV_Assert(query.type() == CV_32F);
|
2012-10-11 16:22:47 +08:00
|
|
|
cv::ocl::Context *ctx = query.clCxt;
|
|
|
|
size_t globalSize[] = {(query.rows + BLOCK_SIZE - 1) / BLOCK_SIZE * BLOCK_SIZE, BLOCK_SIZE, 1};
|
|
|
|
size_t localSize[] = {BLOCK_SIZE, BLOCK_SIZE, 1};
|
|
|
|
const size_t smemSize = (BLOCK_SIZE * (MAX_DESC_LEN >= 2 * BLOCK_SIZE ? MAX_DESC_LEN : 2 * BLOCK_SIZE) + BLOCK_SIZE * BLOCK_SIZE) * sizeof(int);
|
|
|
|
int block_size = BLOCK_SIZE;
|
|
|
|
int m_size = MAX_DESC_LEN;
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector< std::pair<size_t, const void *> > args;
|
2012-10-11 16:22:47 +08:00
|
|
|
|
|
|
|
if(globalSize[0] != 0)
|
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&query.data ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&train.data ));
|
2013-03-04 23:29:40 +08:00
|
|
|
//args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mask.data ));
|
2013-02-25 00:14:01 +08:00
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&trainIdx.data ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&distance.data ));
|
|
|
|
args.push_back( std::make_pair( smemSize, (void *)NULL));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&block_size ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&m_size ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&query.rows ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&query.cols ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&train.rows ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&train.cols ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&query.step ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&distType ));
|
2012-10-11 16:22:47 +08:00
|
|
|
|
2013-03-23 00:37:49 +08:00
|
|
|
String kernelName = "BruteForceMatch_UnrollMatch";
|
2012-10-11 16:22:47 +08:00
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
openCLExecuteKernel(ctx, &brute_force_match, kernelName, globalSize, localSize, args, -1, query.depth());
|
2012-10-11 16:22:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
template < int BLOCK_SIZE, int MAX_DESC_LEN/*, typename Mask*/ >
|
2012-10-22 15:14:22 +08:00
|
|
|
void matchUnrolledCached(const oclMat /*query*/, const oclMat * /*trains*/, int /*n*/, const oclMat /*mask*/,
|
|
|
|
const oclMat &/*bestTrainIdx*/, const oclMat & /*bestImgIdx*/, const oclMat & /*bestDistance*/, int /*distType*/)
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
template < int BLOCK_SIZE/*, typename Mask*/ >
|
2013-02-28 14:31:08 +08:00
|
|
|
void match(const oclMat &query, const oclMat &train, const oclMat &/*mask*/,
|
2012-10-11 16:22:47 +08:00
|
|
|
const oclMat &trainIdx, const oclMat &distance, int distType)
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
2013-04-12 18:12:12 +08:00
|
|
|
CV_Assert(query.type() == CV_32F);
|
2012-10-11 16:22:47 +08:00
|
|
|
cv::ocl::Context *ctx = query.clCxt;
|
|
|
|
size_t globalSize[] = {(query.rows + BLOCK_SIZE - 1) / BLOCK_SIZE * BLOCK_SIZE, BLOCK_SIZE, 1};
|
|
|
|
size_t localSize[] = {BLOCK_SIZE, BLOCK_SIZE, 1};
|
|
|
|
const size_t smemSize = (2 * BLOCK_SIZE * BLOCK_SIZE) * sizeof(int);
|
|
|
|
int block_size = BLOCK_SIZE;
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector< std::pair<size_t, const void *> > args;
|
2012-09-12 11:40:13 +08:00
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
if(globalSize[0] != 0)
|
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&query.data ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&train.data ));
|
2013-03-04 23:29:40 +08:00
|
|
|
//args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mask.data ));
|
2013-02-25 00:14:01 +08:00
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&trainIdx.data ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&distance.data ));
|
|
|
|
args.push_back( std::make_pair( smemSize, (void *)NULL));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&block_size ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&query.rows ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&query.cols ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&train.rows ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&train.cols ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&query.step ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&distType ));
|
2012-09-12 11:40:13 +08:00
|
|
|
|
2013-03-23 00:37:49 +08:00
|
|
|
String kernelName = "BruteForceMatch_Match";
|
2012-09-12 11:40:13 +08:00
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
openCLExecuteKernel(ctx, &brute_force_match, kernelName, globalSize, localSize, args, -1, query.depth());
|
2012-10-11 16:22:47 +08:00
|
|
|
}
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
template < int BLOCK_SIZE/*, typename Mask*/ >
|
2012-10-22 15:14:22 +08:00
|
|
|
void match(const oclMat /*query*/, const oclMat * /*trains*/, int /*n*/, const oclMat /*mask*/,
|
|
|
|
const oclMat &/*bestTrainIdx*/, const oclMat & /*bestImgIdx*/, const oclMat & /*bestDistance*/, int /*distType*/)
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//radius_matchUnrolledCached
|
2013-03-26 13:41:13 +08:00
|
|
|
template < int BLOCK_SIZE, int MAX_DESC_LEN/*, typename Mask*/ >
|
2013-02-28 14:31:08 +08:00
|
|
|
void matchUnrolledCached(const oclMat &query, const oclMat &train, float maxDistance, const oclMat &/*mask*/,
|
2012-10-11 16:22:47 +08:00
|
|
|
const oclMat &trainIdx, const oclMat &distance, const oclMat &nMatches, int distType)
|
|
|
|
{
|
2013-04-12 18:12:12 +08:00
|
|
|
CV_Assert(query.type() == CV_32F);
|
2012-10-11 16:22:47 +08:00
|
|
|
cv::ocl::Context *ctx = query.clCxt;
|
|
|
|
size_t globalSize[] = {(train.rows + BLOCK_SIZE - 1) / BLOCK_SIZE * BLOCK_SIZE, (query.rows + BLOCK_SIZE - 1) / BLOCK_SIZE * BLOCK_SIZE, 1};
|
|
|
|
size_t localSize[] = {BLOCK_SIZE, BLOCK_SIZE, 1};
|
|
|
|
const size_t smemSize = (2 * BLOCK_SIZE * BLOCK_SIZE) * sizeof(int);
|
|
|
|
int block_size = BLOCK_SIZE;
|
|
|
|
int m_size = MAX_DESC_LEN;
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector< std::pair<size_t, const void *> > args;
|
2012-10-11 16:22:47 +08:00
|
|
|
|
|
|
|
if(globalSize[0] != 0)
|
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&query.data ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&train.data ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_float), (void *)&maxDistance ));
|
2013-03-04 23:29:40 +08:00
|
|
|
//args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mask.data ));
|
2013-02-25 00:14:01 +08:00
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&trainIdx.data ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&distance.data ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&nMatches.data ));
|
|
|
|
args.push_back( std::make_pair( smemSize, (void *)NULL));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&block_size ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&m_size ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&query.rows ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&query.cols ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&train.rows ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&train.cols ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&trainIdx.cols ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&query.step ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&trainIdx.step ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&distType ));
|
2012-10-11 16:22:47 +08:00
|
|
|
|
2013-03-23 00:37:49 +08:00
|
|
|
String kernelName = "BruteForceMatch_RadiusUnrollMatch";
|
2012-10-11 16:22:47 +08:00
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
openCLExecuteKernel(ctx, &brute_force_match, kernelName, globalSize, localSize, args, -1, query.depth());
|
2012-10-11 16:22:47 +08:00
|
|
|
}
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//radius_match
|
2013-03-26 13:41:13 +08:00
|
|
|
template < int BLOCK_SIZE/*, typename Mask*/ >
|
2013-02-28 14:31:08 +08:00
|
|
|
void radius_match(const oclMat &query, const oclMat &train, float maxDistance, const oclMat &/*mask*/,
|
2012-10-11 16:22:47 +08:00
|
|
|
const oclMat &trainIdx, const oclMat &distance, const oclMat &nMatches, int distType)
|
|
|
|
{
|
2013-04-12 18:12:12 +08:00
|
|
|
CV_Assert(query.type() == CV_32F);
|
2012-10-11 16:22:47 +08:00
|
|
|
cv::ocl::Context *ctx = query.clCxt;
|
|
|
|
size_t globalSize[] = {(train.rows + BLOCK_SIZE - 1) / BLOCK_SIZE * BLOCK_SIZE, (query.rows + BLOCK_SIZE - 1) / BLOCK_SIZE * BLOCK_SIZE, 1};
|
|
|
|
size_t localSize[] = {BLOCK_SIZE, BLOCK_SIZE, 1};
|
|
|
|
const size_t smemSize = (2 * BLOCK_SIZE * BLOCK_SIZE) * sizeof(int);
|
|
|
|
int block_size = BLOCK_SIZE;
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector< std::pair<size_t, const void *> > args;
|
2012-10-11 16:22:47 +08:00
|
|
|
|
|
|
|
if(globalSize[0] != 0)
|
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&query.data ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&train.data ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_float), (void *)&maxDistance ));
|
2013-03-04 23:29:40 +08:00
|
|
|
//args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mask.data ));
|
2013-02-25 00:14:01 +08:00
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&trainIdx.data ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&distance.data ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&nMatches.data ));
|
|
|
|
args.push_back( std::make_pair( smemSize, (void *)NULL));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&block_size ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&query.rows ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&query.cols ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&train.rows ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&train.cols ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&trainIdx.cols ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&query.step ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&trainIdx.step ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&distType ));
|
2012-10-11 16:22:47 +08:00
|
|
|
|
2013-03-23 00:37:49 +08:00
|
|
|
String kernelName = "BruteForceMatch_RadiusMatch";
|
2012-10-11 16:22:47 +08:00
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
openCLExecuteKernel(ctx, &brute_force_match, kernelName, globalSize, localSize, args, -1, query.depth());
|
2012-10-11 16:22:47 +08:00
|
|
|
}
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
static void matchDispatcher(const oclMat &query, const oclMat &train, const oclMat &mask,
|
2012-10-11 16:22:47 +08:00
|
|
|
const oclMat &trainIdx, const oclMat &distance, int distType)
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
2013-03-26 13:41:13 +08:00
|
|
|
const oclMat zeroMask;
|
|
|
|
const oclMat &tempMask = mask.data ? mask : zeroMask;
|
2012-09-12 11:40:13 +08:00
|
|
|
if (query.cols <= 64)
|
|
|
|
{
|
2013-03-26 13:41:13 +08:00
|
|
|
matchUnrolledCached<16, 64>(query, train, tempMask, trainIdx, distance, distType);
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
else if (query.cols <= 128)
|
|
|
|
{
|
2013-03-26 13:41:13 +08:00
|
|
|
matchUnrolledCached<16, 128>(query, train, tempMask, trainIdx, distance, distType);
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-03-26 13:41:13 +08:00
|
|
|
match<16>(query, train, tempMask, trainIdx, distance, distType);
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
static void matchDispatcher(const oclMat &query, const oclMat *trains, int n, const oclMat &mask,
|
2012-10-11 16:22:47 +08:00
|
|
|
const oclMat &trainIdx, const oclMat &imgIdx, const oclMat &distance, int distType)
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
2013-03-26 13:41:13 +08:00
|
|
|
const oclMat zeroMask;
|
|
|
|
const oclMat &tempMask = mask.data ? mask : zeroMask;
|
2012-09-12 11:40:13 +08:00
|
|
|
if (query.cols <= 64)
|
|
|
|
{
|
2013-03-26 13:41:13 +08:00
|
|
|
matchUnrolledCached<16, 64>(query, trains, n, tempMask, trainIdx, imgIdx, distance, distType);
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
else if (query.cols <= 128)
|
|
|
|
{
|
2013-03-26 13:41:13 +08:00
|
|
|
matchUnrolledCached<16, 128>(query, trains, n, tempMask, trainIdx, imgIdx, distance, distType);
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-03-26 13:41:13 +08:00
|
|
|
match<16>(query, trains, n, tempMask, trainIdx, imgIdx, distance, distType);
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//radius matchDispatcher
|
2013-03-26 13:41:13 +08:00
|
|
|
static void matchDispatcher(const oclMat &query, const oclMat &train, float maxDistance, const oclMat &mask,
|
2012-10-11 16:22:47 +08:00
|
|
|
const oclMat &trainIdx, const oclMat &distance, const oclMat &nMatches, int distType)
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
2013-03-26 13:41:13 +08:00
|
|
|
const oclMat zeroMask;
|
|
|
|
const oclMat &tempMask = mask.data ? mask : zeroMask;
|
2012-09-12 11:40:13 +08:00
|
|
|
if (query.cols <= 64)
|
|
|
|
{
|
2013-03-26 13:41:13 +08:00
|
|
|
matchUnrolledCached<16, 64>(query, train, maxDistance, tempMask, trainIdx, distance, nMatches, distType);
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
else if (query.cols <= 128)
|
|
|
|
{
|
2013-03-26 13:41:13 +08:00
|
|
|
matchUnrolledCached<16, 128>(query, train, maxDistance, tempMask, trainIdx, distance, nMatches, distType);
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-03-26 13:41:13 +08:00
|
|
|
radius_match<16>(query, train, maxDistance, tempMask, trainIdx, distance, nMatches, distType);
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//knn match Dispatcher
|
2013-03-26 13:41:13 +08:00
|
|
|
template < int BLOCK_SIZE, int MAX_DESC_LEN/*, typename Mask*/ >
|
2013-02-28 14:31:08 +08:00
|
|
|
void knn_matchUnrolledCached(const oclMat &query, const oclMat &train, const oclMat &/*mask*/,
|
2012-10-11 16:22:47 +08:00
|
|
|
const oclMat &trainIdx, const oclMat &distance, int distType)
|
|
|
|
{
|
|
|
|
cv::ocl::Context *ctx = query.clCxt;
|
|
|
|
size_t globalSize[] = {(query.rows + BLOCK_SIZE - 1) / BLOCK_SIZE * BLOCK_SIZE, BLOCK_SIZE, 1};
|
|
|
|
size_t localSize[] = {BLOCK_SIZE, BLOCK_SIZE, 1};
|
|
|
|
const size_t smemSize = (BLOCK_SIZE * (MAX_DESC_LEN >= BLOCK_SIZE ? MAX_DESC_LEN : BLOCK_SIZE) + BLOCK_SIZE * BLOCK_SIZE) * sizeof(int);
|
|
|
|
int block_size = BLOCK_SIZE;
|
|
|
|
int m_size = MAX_DESC_LEN;
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector< std::pair<size_t, const void *> > args;
|
2012-10-11 16:22:47 +08:00
|
|
|
|
|
|
|
if(globalSize[0] != 0)
|
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&query.data ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&train.data ));
|
2013-03-04 23:29:40 +08:00
|
|
|
//args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mask.data ));
|
2013-02-25 00:14:01 +08:00
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&trainIdx.data ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&distance.data ));
|
|
|
|
args.push_back( std::make_pair( smemSize, (void *)NULL));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&block_size ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&m_size ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&query.rows ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&query.cols ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&train.rows ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&train.cols ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&query.step ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&distType ));
|
2012-10-11 16:22:47 +08:00
|
|
|
|
2013-03-23 00:37:49 +08:00
|
|
|
String kernelName = "BruteForceMatch_knnUnrollMatch";
|
2012-10-11 16:22:47 +08:00
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
openCLExecuteKernel(ctx, &brute_force_match, kernelName, globalSize, localSize, args, -1, query.depth());
|
2012-10-11 16:22:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
template < int BLOCK_SIZE/*, typename Mask*/ >
|
2013-02-28 14:31:08 +08:00
|
|
|
void knn_match(const oclMat &query, const oclMat &train, const oclMat &/*mask*/,
|
2012-10-11 16:22:47 +08:00
|
|
|
const oclMat &trainIdx, const oclMat &distance, int distType)
|
|
|
|
{
|
|
|
|
cv::ocl::Context *ctx = query.clCxt;
|
|
|
|
size_t globalSize[] = {(query.rows + BLOCK_SIZE - 1) / BLOCK_SIZE * BLOCK_SIZE, BLOCK_SIZE, 1};
|
|
|
|
size_t localSize[] = {BLOCK_SIZE, BLOCK_SIZE, 1};
|
|
|
|
const size_t smemSize = (2 * BLOCK_SIZE * BLOCK_SIZE) * sizeof(int);
|
|
|
|
int block_size = BLOCK_SIZE;
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector< std::pair<size_t, const void *> > args;
|
2012-10-11 16:22:47 +08:00
|
|
|
|
|
|
|
if(globalSize[0] != 0)
|
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&query.data ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&train.data ));
|
2013-03-04 23:29:40 +08:00
|
|
|
//args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mask.data ));
|
2013-02-25 00:14:01 +08:00
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&trainIdx.data ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&distance.data ));
|
|
|
|
args.push_back( std::make_pair( smemSize, (void *)NULL));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&block_size ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&query.rows ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&query.cols ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&train.rows ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&train.cols ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&query.step ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&distType ));
|
2012-10-11 16:22:47 +08:00
|
|
|
|
2013-03-23 00:37:49 +08:00
|
|
|
String kernelName = "BruteForceMatch_knnMatch";
|
2012-10-11 16:22:47 +08:00
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
openCLExecuteKernel(ctx, &brute_force_match, kernelName, globalSize, localSize, args, -1, query.depth());
|
2012-10-11 16:22:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
template < int BLOCK_SIZE, int MAX_DESC_LEN/*, typename Mask*/ >
|
2013-02-28 14:31:08 +08:00
|
|
|
void calcDistanceUnrolled(const oclMat &query, const oclMat &train, const oclMat &/*mask*/, const oclMat &allDist, int distType)
|
2012-10-11 16:22:47 +08:00
|
|
|
{
|
|
|
|
cv::ocl::Context *ctx = query.clCxt;
|
|
|
|
size_t globalSize[] = {(query.rows + BLOCK_SIZE - 1) / BLOCK_SIZE * BLOCK_SIZE, BLOCK_SIZE, 1};
|
|
|
|
size_t localSize[] = {BLOCK_SIZE, BLOCK_SIZE, 1};
|
|
|
|
const size_t smemSize = (2 * BLOCK_SIZE * BLOCK_SIZE) * sizeof(int);
|
|
|
|
int block_size = BLOCK_SIZE;
|
|
|
|
int m_size = MAX_DESC_LEN;
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector< std::pair<size_t, const void *> > args;
|
2012-10-11 16:22:47 +08:00
|
|
|
|
|
|
|
if(globalSize[0] != 0)
|
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&query.data ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&train.data ));
|
2013-03-04 23:29:40 +08:00
|
|
|
//args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mask.data ));
|
2013-02-25 00:14:01 +08:00
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&allDist.data ));
|
|
|
|
args.push_back( std::make_pair( smemSize, (void *)NULL));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&block_size ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&m_size ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&query.rows ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&query.cols ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&train.rows ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&train.cols ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&query.step ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&distType ));
|
2012-10-11 16:22:47 +08:00
|
|
|
|
2013-03-23 00:37:49 +08:00
|
|
|
String kernelName = "BruteForceMatch_calcDistanceUnrolled";
|
2012-10-11 16:22:47 +08:00
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
openCLExecuteKernel(ctx, &brute_force_match, kernelName, globalSize, localSize, args, -1, query.depth());
|
2012-10-11 16:22:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
template < int BLOCK_SIZE/*, typename Mask*/ >
|
2013-02-28 14:31:08 +08:00
|
|
|
void calcDistance(const oclMat &query, const oclMat &train, const oclMat &/*mask*/, const oclMat &allDist, int distType)
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
|
|
|
cv::ocl::Context *ctx = query.clCxt;
|
2012-10-11 16:22:47 +08:00
|
|
|
size_t globalSize[] = {(query.rows + BLOCK_SIZE - 1) / BLOCK_SIZE * BLOCK_SIZE, BLOCK_SIZE, 1};
|
|
|
|
size_t localSize[] = {BLOCK_SIZE, BLOCK_SIZE, 1};
|
|
|
|
const size_t smemSize = (2 * BLOCK_SIZE * BLOCK_SIZE) * sizeof(int);
|
|
|
|
int block_size = BLOCK_SIZE;
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector< std::pair<size_t, const void *> > args;
|
2012-10-11 16:22:47 +08:00
|
|
|
|
|
|
|
if(globalSize[0] != 0)
|
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&query.data ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&train.data ));
|
2013-03-04 23:29:40 +08:00
|
|
|
//args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mask.data ));
|
2013-02-25 00:14:01 +08:00
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&allDist.data ));
|
|
|
|
args.push_back( std::make_pair( smemSize, (void *)NULL));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&block_size ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&query.rows ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&query.cols ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&train.rows ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&train.cols ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&query.step ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&distType ));
|
2012-10-11 16:22:47 +08:00
|
|
|
|
2013-03-23 00:37:49 +08:00
|
|
|
String kernelName = "BruteForceMatch_calcDistance";
|
2012-10-11 16:22:47 +08:00
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
openCLExecuteKernel(ctx, &brute_force_match, kernelName, globalSize, localSize, args, -1, query.depth());
|
2012-10-11 16:22:47 +08:00
|
|
|
}
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Calc Distance dispatcher
|
2013-03-26 13:41:13 +08:00
|
|
|
static void calcDistanceDispatcher(const oclMat &query, const oclMat &train, const oclMat &mask,
|
2012-10-11 16:22:47 +08:00
|
|
|
const oclMat &allDist, int distType)
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
|
|
|
if (query.cols <= 64)
|
|
|
|
{
|
2013-03-26 13:41:13 +08:00
|
|
|
calcDistanceUnrolled<16, 64>(query, train, mask, allDist, distType);
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
else if (query.cols <= 128)
|
|
|
|
{
|
2013-03-26 13:41:13 +08:00
|
|
|
calcDistanceUnrolled<16, 128>(query, train, mask, allDist, distType);
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-03-26 13:41:13 +08:00
|
|
|
calcDistance<16>(query, train, mask, allDist, distType);
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
static void match2Dispatcher(const oclMat &query, const oclMat &train, const oclMat &mask,
|
2012-10-11 16:22:47 +08:00
|
|
|
const oclMat &trainIdx, const oclMat &distance, int distType)
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
|
|
|
if (query.cols <= 64)
|
|
|
|
{
|
2013-03-26 13:41:13 +08:00
|
|
|
knn_matchUnrolledCached<16, 64>(query, train, mask, trainIdx, distance, distType);
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
else if (query.cols <= 128)
|
|
|
|
{
|
2013-03-26 13:41:13 +08:00
|
|
|
knn_matchUnrolledCached<16, 128>(query, train, mask, trainIdx, distance, distType);
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-03-26 13:41:13 +08:00
|
|
|
knn_match<16>(query, train, mask, trainIdx, distance, distType);
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <int BLOCK_SIZE>
|
2012-10-22 15:14:22 +08:00
|
|
|
void findKnnMatch(int k, const oclMat &trainIdx, const oclMat &distance, const oclMat &allDist, int /*distType*/)
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
cv::ocl::Context *ctx = trainIdx.clCxt;
|
|
|
|
size_t globalSize[] = {trainIdx.rows * BLOCK_SIZE, 1, 1};
|
|
|
|
size_t localSize[] = {BLOCK_SIZE, 1, 1};
|
|
|
|
int block_size = BLOCK_SIZE;
|
2013-03-23 00:37:49 +08:00
|
|
|
String kernelName = "BruteForceMatch_findBestMatch";
|
2012-09-12 11:40:13 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < k; ++i)
|
2012-10-11 16:22:47 +08:00
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector< std::pair<size_t, const void *> > args;
|
2012-09-12 11:40:13 +08:00
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&allDist.data ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&trainIdx.data ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&distance.data ));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_mem), (void *)&i));
|
|
|
|
args.push_back( std::make_pair( sizeof(cl_int), (void *)&block_size ));
|
|
|
|
//args.push_back( std::make_pair( sizeof(cl_int), (void *)&train.rows ));
|
|
|
|
//args.push_back( std::make_pair( sizeof(cl_int), (void *)&train.cols ));
|
|
|
|
//args.push_back( std::make_pair( sizeof(cl_int), (void *)&query.step ));
|
2012-09-12 11:40:13 +08:00
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
openCLExecuteKernel(ctx, &brute_force_match, kernelName, globalSize, localSize, args, trainIdx.depth(), -1);
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-25 18:31:34 +08:00
|
|
|
static void findKnnMatchDispatcher(int k, const oclMat &trainIdx, const oclMat &distance, const oclMat &allDist, int distType)
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
|
|
|
findKnnMatch<256>(k, trainIdx, distance, allDist, distType);
|
|
|
|
}
|
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
static void kmatchDispatcher(const oclMat &query, const oclMat &train, int k, const oclMat &mask,
|
2012-10-11 16:22:47 +08:00
|
|
|
const oclMat &trainIdx, const oclMat &distance, const oclMat &allDist, int distType)
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
2013-03-26 13:41:13 +08:00
|
|
|
const oclMat zeroMask;
|
|
|
|
const oclMat &tempMask = mask.data ? mask : zeroMask;
|
2012-09-12 11:40:13 +08:00
|
|
|
if (k == 2)
|
|
|
|
{
|
2013-03-26 13:41:13 +08:00
|
|
|
match2Dispatcher(query, train, tempMask, trainIdx, distance, distType);
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-03-26 13:41:13 +08:00
|
|
|
calcDistanceDispatcher(query, train, tempMask, allDist, distType);
|
2012-09-12 11:40:13 +08:00
|
|
|
findKnnMatchDispatcher(k, trainIdx, distance, allDist, distType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cv::ocl::BruteForceMatcher_OCL_base::BruteForceMatcher_OCL_base(DistType distType_) : distType(distType_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::add(const std::vector<oclMat> &descCollection)
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
trainDescCollection.insert(trainDescCollection.end(), descCollection.begin(), descCollection.end());
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
const std::vector<oclMat> &cv::ocl::BruteForceMatcher_OCL_base::getTrainDescriptors() const
|
2012-10-11 16:22:47 +08:00
|
|
|
{
|
|
|
|
return trainDescCollection;
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::clear()
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
trainDescCollection.clear();
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
bool cv::ocl::BruteForceMatcher_OCL_base::empty() const
|
|
|
|
{
|
|
|
|
return trainDescCollection.empty();
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
bool cv::ocl::BruteForceMatcher_OCL_base::isMaskSupported() const
|
|
|
|
{
|
|
|
|
return true;
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::matchSingle(const oclMat &query, const oclMat &train,
|
|
|
|
oclMat &trainIdx, oclMat &distance, const oclMat &mask)
|
|
|
|
{
|
|
|
|
if (query.empty() || train.empty())
|
2012-09-12 11:40:13 +08:00
|
|
|
return;
|
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
// match1 doesn't support signed char type, match2 only support float, hamming support uchar, ushort and int
|
|
|
|
int callType = query.depth();
|
|
|
|
if (callType != 5)
|
2013-04-12 18:12:12 +08:00
|
|
|
CV_Error(Error::StsUnsupportedFormat, "BruteForceMatch OpenCL only support float type query!\n");
|
2012-09-12 11:40:13 +08:00
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
if ((distType == 0 && callType == 1 ) || (distType == 1 && callType != 5) || (distType == 2 && (callType != 0
|
|
|
|
|| callType != 2 || callType != 4)))
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
2013-04-12 18:12:12 +08:00
|
|
|
CV_Error(Error::BadDepth, "BruteForceMatch OpenCL only support float type query!\n");
|
2013-03-26 13:41:13 +08:00
|
|
|
}
|
2012-09-12 11:40:13 +08:00
|
|
|
|
|
|
|
CV_Assert(query.channels() == 1 && query.depth() < CV_64F);
|
|
|
|
CV_Assert(train.cols == query.cols && train.type() == query.type());
|
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
trainIdx.create(1, query.rows, CV_32S);
|
|
|
|
distance.create(1, query.rows, CV_32F);
|
2012-09-12 11:40:13 +08:00
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
matchDispatcher(query, train, mask, trainIdx, distance, distType);
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::matchDownload(const oclMat &trainIdx, const oclMat &distance, std::vector<DMatch> &matches)
|
2012-10-11 16:22:47 +08:00
|
|
|
{
|
|
|
|
if (trainIdx.empty() || distance.empty())
|
2012-09-12 11:40:13 +08:00
|
|
|
return;
|
2012-10-11 16:22:47 +08:00
|
|
|
|
2012-09-12 11:40:13 +08:00
|
|
|
Mat trainIdxCPU(trainIdx);
|
|
|
|
Mat distanceCPU(distance);
|
|
|
|
|
|
|
|
matchConvert(trainIdxCPU, distanceCPU, matches);
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::matchConvert(const Mat &trainIdx, const Mat &distance, std::vector<DMatch> &matches)
|
2012-10-11 16:22:47 +08:00
|
|
|
{
|
|
|
|
if (trainIdx.empty() || distance.empty())
|
2012-09-12 11:40:13 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
CV_Assert(trainIdx.type() == CV_32SC1);
|
|
|
|
CV_Assert(distance.type() == CV_32FC1 && distance.cols == trainIdx.cols);
|
|
|
|
|
|
|
|
const int nQuery = trainIdx.cols;
|
|
|
|
|
|
|
|
matches.clear();
|
|
|
|
matches.reserve(nQuery);
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
const int *trainIdx_ptr = trainIdx.ptr<int>();
|
|
|
|
const float *distance_ptr = distance.ptr<float>();
|
2012-09-12 11:40:13 +08:00
|
|
|
for (int queryIdx = 0; queryIdx < nQuery; ++queryIdx, ++trainIdx_ptr, ++distance_ptr)
|
|
|
|
{
|
|
|
|
int trainIdx = *trainIdx_ptr;
|
|
|
|
|
|
|
|
if (trainIdx == -1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
float distance = *distance_ptr;
|
|
|
|
|
|
|
|
DMatch m(queryIdx, trainIdx, 0, distance);
|
|
|
|
|
|
|
|
matches.push_back(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::match(const oclMat &query, const oclMat &train, std::vector<DMatch> &matches, const oclMat &mask)
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
2013-04-12 18:12:12 +08:00
|
|
|
CV_Assert(mask.empty()); // mask is not supported at the moment
|
2012-10-11 16:22:47 +08:00
|
|
|
oclMat trainIdx, distance;
|
2012-09-12 11:40:13 +08:00
|
|
|
matchSingle(query, train, trainIdx, distance, mask);
|
|
|
|
matchDownload(trainIdx, distance, matches);
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::makeGpuCollection(oclMat &trainCollection, oclMat &maskCollection, const std::vector<oclMat> &masks)
|
2012-10-11 16:22:47 +08:00
|
|
|
{
|
2012-09-12 11:40:13 +08:00
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
if (empty())
|
2012-09-12 11:40:13 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (masks.empty())
|
|
|
|
{
|
|
|
|
Mat trainCollectionCPU(1, static_cast<int>(trainDescCollection.size()), CV_8UC(sizeof(oclMat)));
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
oclMat *trainCollectionCPU_ptr = trainCollectionCPU.ptr<oclMat>();
|
2012-09-12 11:40:13 +08:00
|
|
|
|
|
|
|
for (size_t i = 0, size = trainDescCollection.size(); i < size; ++i, ++trainCollectionCPU_ptr)
|
|
|
|
*trainCollectionCPU_ptr = trainDescCollection[i];
|
|
|
|
|
|
|
|
trainCollection.upload(trainCollectionCPU);
|
|
|
|
maskCollection.release();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CV_Assert(masks.size() == trainDescCollection.size());
|
|
|
|
|
|
|
|
Mat trainCollectionCPU(1, static_cast<int>(trainDescCollection.size()), CV_8UC(sizeof(oclMat)));
|
|
|
|
Mat maskCollectionCPU(1, static_cast<int>(trainDescCollection.size()), CV_8UC(sizeof(oclMat)));
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
oclMat *trainCollectionCPU_ptr = trainCollectionCPU.ptr<oclMat>();
|
|
|
|
oclMat *maskCollectionCPU_ptr = maskCollectionCPU.ptr<oclMat>();
|
2012-09-12 11:40:13 +08:00
|
|
|
|
|
|
|
for (size_t i = 0, size = trainDescCollection.size(); i < size; ++i, ++trainCollectionCPU_ptr, ++maskCollectionCPU_ptr)
|
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
const oclMat &train = trainDescCollection[i];
|
|
|
|
const oclMat &mask = masks[i];
|
2012-09-12 11:40:13 +08:00
|
|
|
|
|
|
|
CV_Assert(mask.empty() || (mask.type() == CV_8UC1 && mask.cols == train.rows));
|
|
|
|
|
|
|
|
*trainCollectionCPU_ptr = train;
|
|
|
|
*maskCollectionCPU_ptr = mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
trainCollection.upload(trainCollectionCPU);
|
|
|
|
maskCollection.upload(maskCollectionCPU);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::matchCollection(const oclMat &query, const oclMat &trainCollection, oclMat &trainIdx,
|
|
|
|
oclMat &imgIdx, oclMat &distance, const oclMat &masks)
|
|
|
|
{
|
|
|
|
if (query.empty() || trainCollection.empty())
|
2012-09-12 11:40:13 +08:00
|
|
|
return;
|
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
// match1 doesn't support signed char type, match2 only support float, hamming support uchar, ushort and int
|
|
|
|
int callType = query.depth();
|
|
|
|
if (callType != 5)
|
2013-04-12 18:12:12 +08:00
|
|
|
CV_Error(Error::StsUnsupportedFormat, "BruteForceMatch OpenCL only support float type query!\n");
|
2012-09-12 11:40:13 +08:00
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
if ((distType == 0 && callType == 1 ) || (distType == 1 && callType != 5) || (distType == 2 && (callType != 0
|
|
|
|
|| callType != 2 || callType != 4)))
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
2013-04-12 18:12:12 +08:00
|
|
|
CV_Error(Error::BadDepth, "BruteForceMatch OpenCL only support float type query!\n");
|
2013-03-26 13:41:13 +08:00
|
|
|
}
|
2012-09-12 11:40:13 +08:00
|
|
|
|
|
|
|
CV_Assert(query.channels() == 1 && query.depth() < CV_64F);
|
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
trainIdx.create(1, query.rows, CV_32S);
|
|
|
|
imgIdx.create(1, query.rows, CV_32S);
|
|
|
|
distance.create(1, query.rows, CV_32F);
|
2012-09-12 11:40:13 +08:00
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
matchDispatcher(query, (const oclMat *)trainCollection.ptr(), trainCollection.cols, masks, trainIdx, imgIdx, distance, distType);
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::matchDownload(const oclMat &trainIdx, const oclMat &imgIdx, const oclMat &distance, std::vector<DMatch> &matches)
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
if (trainIdx.empty() || imgIdx.empty() || distance.empty())
|
2012-09-12 11:40:13 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
Mat trainIdxCPU(trainIdx);
|
|
|
|
Mat imgIdxCPU(imgIdx);
|
|
|
|
Mat distanceCPU(distance);
|
|
|
|
|
|
|
|
matchConvert(trainIdxCPU, imgIdxCPU, distanceCPU, matches);
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::matchConvert(const Mat &trainIdx, const Mat &imgIdx, const Mat &distance, std::vector<DMatch> &matches)
|
2012-10-11 16:22:47 +08:00
|
|
|
{
|
|
|
|
if (trainIdx.empty() || imgIdx.empty() || distance.empty())
|
2012-09-12 11:40:13 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
CV_Assert(trainIdx.type() == CV_32SC1);
|
|
|
|
CV_Assert(imgIdx.type() == CV_32SC1 && imgIdx.cols == trainIdx.cols);
|
|
|
|
CV_Assert(distance.type() == CV_32FC1 && distance.cols == trainIdx.cols);
|
|
|
|
|
|
|
|
const int nQuery = trainIdx.cols;
|
|
|
|
|
|
|
|
matches.clear();
|
|
|
|
matches.reserve(nQuery);
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
const int *trainIdx_ptr = trainIdx.ptr<int>();
|
|
|
|
const int *imgIdx_ptr = imgIdx.ptr<int>();
|
|
|
|
const float *distance_ptr = distance.ptr<float>();
|
2012-09-12 11:40:13 +08:00
|
|
|
for (int queryIdx = 0; queryIdx < nQuery; ++queryIdx, ++trainIdx_ptr, ++imgIdx_ptr, ++distance_ptr)
|
|
|
|
{
|
|
|
|
int trainIdx = *trainIdx_ptr;
|
|
|
|
|
|
|
|
if (trainIdx == -1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int imgIdx = *imgIdx_ptr;
|
|
|
|
|
|
|
|
float distance = *distance_ptr;
|
|
|
|
|
|
|
|
DMatch m(queryIdx, trainIdx, imgIdx, distance);
|
|
|
|
|
|
|
|
matches.push_back(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::match(const oclMat &query, std::vector<DMatch> &matches, const std::vector<oclMat> &masks)
|
2012-10-11 16:22:47 +08:00
|
|
|
{
|
|
|
|
oclMat trainCollection;
|
2012-09-12 11:40:13 +08:00
|
|
|
oclMat maskCollection;
|
|
|
|
|
|
|
|
makeGpuCollection(trainCollection, maskCollection, masks);
|
|
|
|
|
|
|
|
oclMat trainIdx, imgIdx, distance;
|
|
|
|
|
|
|
|
matchCollection(query, trainCollection, trainIdx, imgIdx, distance, maskCollection);
|
|
|
|
matchDownload(trainIdx, imgIdx, distance, matches);
|
|
|
|
}
|
|
|
|
|
|
|
|
// knn match
|
2012-10-11 16:22:47 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::knnMatchSingle(const oclMat &query, const oclMat &train, oclMat &trainIdx,
|
|
|
|
oclMat &distance, oclMat &allDist, int k, const oclMat &mask)
|
|
|
|
{
|
|
|
|
if (query.empty() || train.empty())
|
2012-09-12 11:40:13 +08:00
|
|
|
return;
|
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
// match1 doesn't support signed char type, match2 only support float, hamming support uchar, ushort and int
|
|
|
|
int callType = query.depth();
|
2012-09-12 11:40:13 +08:00
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
if (callType != 5)
|
2013-04-12 18:12:12 +08:00
|
|
|
CV_Error(Error::StsUnsupportedFormat, "BruteForceMatch OpenCL only support float type query!\n");
|
2013-03-26 13:41:13 +08:00
|
|
|
|
|
|
|
if ((distType == 0 && callType == 1 ) || (distType == 1 && callType != 5) || (distType == 2 && (callType != 0
|
|
|
|
|| callType != 2 || callType != 4)))
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
2013-04-12 18:12:12 +08:00
|
|
|
CV_Error(Error::BadDepth, "BruteForceMatch OpenCL only support float type query!\n");
|
2013-03-26 13:41:13 +08:00
|
|
|
}
|
2012-09-12 11:40:13 +08:00
|
|
|
|
|
|
|
CV_Assert(query.channels() == 1 && query.depth() < CV_64F);
|
|
|
|
CV_Assert(train.type() == query.type() && train.cols == query.cols);
|
|
|
|
|
|
|
|
if (k == 2)
|
|
|
|
{
|
2013-03-26 13:41:13 +08:00
|
|
|
trainIdx.create(1, query.rows, CV_32SC2);
|
|
|
|
distance.create(1, query.rows, CV_32FC2);
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-03-26 13:41:13 +08:00
|
|
|
trainIdx.create(query.rows, k, CV_32S);
|
|
|
|
distance.create(query.rows, k, CV_32F);
|
|
|
|
allDist.create(query.rows, train.rows, CV_32FC1);
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
trainIdx.setTo(Scalar::all(-1));
|
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
kmatchDispatcher(query, train, k, mask, trainIdx, distance, allDist, distType);
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::knnMatchDownload(const oclMat &trainIdx, const oclMat &distance, std::vector< std::vector<DMatch> > &matches, bool compactResult)
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
if (trainIdx.empty() || distance.empty())
|
2012-09-12 11:40:13 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
Mat trainIdxCPU(trainIdx);
|
|
|
|
Mat distanceCPU(distance);
|
|
|
|
|
|
|
|
knnMatchConvert(trainIdxCPU, distanceCPU, matches, compactResult);
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::knnMatchConvert(const Mat &trainIdx, const Mat &distance, std::vector< std::vector<DMatch> > &matches, bool compactResult)
|
2012-10-11 16:22:47 +08:00
|
|
|
{
|
|
|
|
if (trainIdx.empty() || distance.empty())
|
2012-09-12 11:40:13 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
CV_Assert(trainIdx.type() == CV_32SC2 || trainIdx.type() == CV_32SC1);
|
|
|
|
CV_Assert(distance.type() == CV_32FC2 || distance.type() == CV_32FC1);
|
|
|
|
CV_Assert(distance.size() == trainIdx.size());
|
|
|
|
CV_Assert(trainIdx.isContinuous() && distance.isContinuous());
|
|
|
|
|
|
|
|
const int nQuery = trainIdx.type() == CV_32SC2 ? trainIdx.cols : trainIdx.rows;
|
2012-10-11 16:22:47 +08:00
|
|
|
const int k = trainIdx.type() == CV_32SC2 ? 2 : trainIdx.cols;
|
2012-09-12 11:40:13 +08:00
|
|
|
|
|
|
|
matches.clear();
|
|
|
|
matches.reserve(nQuery);
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
const int *trainIdx_ptr = trainIdx.ptr<int>();
|
|
|
|
const float *distance_ptr = distance.ptr<float>();
|
2012-09-12 11:40:13 +08:00
|
|
|
|
|
|
|
for (int queryIdx = 0; queryIdx < nQuery; ++queryIdx)
|
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
matches.push_back(std::vector<DMatch>());
|
|
|
|
std::vector<DMatch> &curMatches = matches.back();
|
2012-09-12 11:40:13 +08:00
|
|
|
curMatches.reserve(k);
|
|
|
|
|
|
|
|
for (int i = 0; i < k; ++i, ++trainIdx_ptr, ++distance_ptr)
|
|
|
|
{
|
|
|
|
int trainIdx = *trainIdx_ptr;
|
|
|
|
|
|
|
|
if (trainIdx != -1)
|
|
|
|
{
|
|
|
|
float distance = *distance_ptr;
|
|
|
|
|
|
|
|
DMatch m(queryIdx, trainIdx, 0, distance);
|
|
|
|
|
|
|
|
curMatches.push_back(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (compactResult && curMatches.empty())
|
|
|
|
matches.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::knnMatch(const oclMat &query, const oclMat &train, std::vector< std::vector<DMatch> > &matches
|
2012-10-11 16:22:47 +08:00
|
|
|
, int k, const oclMat &mask, bool compactResult)
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
oclMat trainIdx, distance, allDist;
|
2012-09-12 11:40:13 +08:00
|
|
|
knnMatchSingle(query, train, trainIdx, distance, allDist, k, mask);
|
|
|
|
knnMatchDownload(trainIdx, distance, matches, compactResult);
|
|
|
|
}
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::knnMatch2Collection(const oclMat &query, const oclMat &trainCollection,
|
2012-10-22 15:14:22 +08:00
|
|
|
oclMat &trainIdx, oclMat &imgIdx, oclMat &distance, const oclMat &/*maskCollection*/)
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
if (query.empty() || trainCollection.empty())
|
2012-09-12 11:40:13 +08:00
|
|
|
return;
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
typedef void (*caller_t)(const oclMat & query, const oclMat & trains, const oclMat & masks,
|
|
|
|
const oclMat & trainIdx, const oclMat & imgIdx, const oclMat & distance);
|
2012-09-12 11:40:13 +08:00
|
|
|
#if 0
|
|
|
|
static const caller_t callers[3][6] =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
ocl_match2L1_gpu<unsigned char>, 0/*match2L1_gpu<signed char>*/,
|
|
|
|
ocl_match2L1_gpu<unsigned short>, ocl_match2L1_gpu<short>,
|
|
|
|
ocl_match2L1_gpu<int>, ocl_match2L1_gpu<float>
|
|
|
|
},
|
|
|
|
{
|
|
|
|
0/*match2L2_gpu<unsigned char>*/, 0/*match2L2_gpu<signed char>*/,
|
|
|
|
0/*match2L2_gpu<unsigned short>*/, 0/*match2L2_gpu<short>*/,
|
|
|
|
0/*match2L2_gpu<int>*/, ocl_match2L2_gpu<float>
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ocl_match2Hamming_gpu<unsigned char>, 0/*match2Hamming_gpu<signed char>*/,
|
|
|
|
ocl_match2Hamming_gpu<unsigned short>, 0/*match2Hamming_gpu<short>*/,
|
|
|
|
ocl_match2Hamming_gpu<int>, 0/*match2Hamming_gpu<float>*/
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
CV_Assert(query.channels() == 1 && query.depth() < CV_64F);
|
|
|
|
|
|
|
|
const int nQuery = query.rows;
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
trainIdx.create(1, nQuery, CV_32SC2);
|
|
|
|
imgIdx.create(1, nQuery, CV_32SC2);
|
|
|
|
distance.create(1, nQuery, CV_32SC2);
|
2012-09-12 11:40:13 +08:00
|
|
|
|
|
|
|
trainIdx.setTo(Scalar::all(-1));
|
|
|
|
|
|
|
|
//caller_t func = callers[distType][query.depth()];
|
|
|
|
//CV_Assert(func != 0);
|
|
|
|
|
|
|
|
//func(query, trainCollection, maskCollection, trainIdx, imgIdx, distance, cc, StreamAccessor::getStream(stream));
|
|
|
|
}
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::knnMatch2Download(const oclMat &trainIdx, const oclMat &imgIdx,
|
2013-02-25 00:14:01 +08:00
|
|
|
const oclMat &distance, std::vector< std::vector<DMatch> > &matches, bool compactResult)
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
if (trainIdx.empty() || imgIdx.empty() || distance.empty())
|
2012-09-12 11:40:13 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
Mat trainIdxCPU(trainIdx);
|
|
|
|
Mat imgIdxCPU(imgIdx);
|
|
|
|
Mat distanceCPU(distance);
|
|
|
|
|
|
|
|
knnMatch2Convert(trainIdxCPU, imgIdxCPU, distanceCPU, matches, compactResult);
|
|
|
|
}
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::knnMatch2Convert(const Mat &trainIdx, const Mat &imgIdx, const Mat &distance,
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector< std::vector<DMatch> > &matches, bool compactResult)
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
if (trainIdx.empty() || imgIdx.empty() || distance.empty())
|
2012-09-12 11:40:13 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
CV_Assert(trainIdx.type() == CV_32SC2);
|
|
|
|
CV_Assert(imgIdx.type() == CV_32SC2 && imgIdx.cols == trainIdx.cols);
|
|
|
|
CV_Assert(distance.type() == CV_32FC2 && distance.cols == trainIdx.cols);
|
|
|
|
|
|
|
|
const int nQuery = trainIdx.cols;
|
|
|
|
|
|
|
|
matches.clear();
|
|
|
|
matches.reserve(nQuery);
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
const int *trainIdx_ptr = trainIdx.ptr<int>();
|
|
|
|
const int *imgIdx_ptr = imgIdx.ptr<int>();
|
|
|
|
const float *distance_ptr = distance.ptr<float>();
|
2012-09-12 11:40:13 +08:00
|
|
|
|
|
|
|
for (int queryIdx = 0; queryIdx < nQuery; ++queryIdx)
|
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
matches.push_back(std::vector<DMatch>());
|
|
|
|
std::vector<DMatch> &curMatches = matches.back();
|
2012-09-12 11:40:13 +08:00
|
|
|
curMatches.reserve(2);
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; ++i, ++trainIdx_ptr, ++imgIdx_ptr, ++distance_ptr)
|
|
|
|
{
|
|
|
|
int trainIdx = *trainIdx_ptr;
|
|
|
|
|
|
|
|
if (trainIdx != -1)
|
|
|
|
{
|
|
|
|
int imgIdx = *imgIdx_ptr;
|
|
|
|
|
|
|
|
float distance = *distance_ptr;
|
|
|
|
|
|
|
|
DMatch m(queryIdx, trainIdx, imgIdx, distance);
|
|
|
|
|
|
|
|
curMatches.push_back(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (compactResult && curMatches.empty())
|
|
|
|
matches.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
struct ImgIdxSetter
|
|
|
|
{
|
|
|
|
explicit inline ImgIdxSetter(int imgIdx_) : imgIdx(imgIdx_) {}
|
2012-10-11 16:22:47 +08:00
|
|
|
inline void operator()(DMatch &m) const
|
|
|
|
{
|
|
|
|
m.imgIdx = imgIdx;
|
|
|
|
}
|
2012-09-12 11:40:13 +08:00
|
|
|
int imgIdx;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::knnMatch(const oclMat &query, std::vector< std::vector<DMatch> > &matches, int k,
|
|
|
|
const std::vector<oclMat> &masks, bool compactResult)
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
if (k == 2)
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
|
|
|
oclMat trainCollection;
|
|
|
|
oclMat maskCollection;
|
|
|
|
|
|
|
|
makeGpuCollection(trainCollection, maskCollection, masks);
|
|
|
|
|
|
|
|
oclMat trainIdx, imgIdx, distance;
|
|
|
|
|
|
|
|
knnMatch2Collection(query, trainCollection, trainIdx, imgIdx, distance, maskCollection);
|
|
|
|
knnMatch2Download(trainIdx, imgIdx, distance, matches);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (query.empty() || empty())
|
|
|
|
return;
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector< std::vector<DMatch> > curMatches;
|
|
|
|
std::vector<DMatch> temp;
|
2012-09-12 11:40:13 +08:00
|
|
|
temp.reserve(2 * k);
|
|
|
|
|
|
|
|
matches.resize(query.rows);
|
2013-02-25 00:14:01 +08:00
|
|
|
std::for_each(matches.begin(), matches.end(), std::bind2nd(std::mem_fun_ref(&std::vector<DMatch>::reserve), k));
|
2012-09-12 11:40:13 +08:00
|
|
|
|
|
|
|
for (size_t imgIdx = 0, size = trainDescCollection.size(); imgIdx < size; ++imgIdx)
|
|
|
|
{
|
|
|
|
knnMatch(query, trainDescCollection[imgIdx], curMatches, k, masks.empty() ? oclMat() : masks[imgIdx]);
|
|
|
|
|
|
|
|
for (int queryIdx = 0; queryIdx < query.rows; ++queryIdx)
|
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<DMatch> &localMatch = curMatches[queryIdx];
|
|
|
|
std::vector<DMatch> &globalMatch = matches[queryIdx];
|
2012-09-12 11:40:13 +08:00
|
|
|
|
|
|
|
for_each(localMatch.begin(), localMatch.end(), ImgIdxSetter(static_cast<int>(imgIdx)));
|
|
|
|
|
|
|
|
temp.clear();
|
2012-10-11 16:22:47 +08:00
|
|
|
merge(globalMatch.begin(), globalMatch.end(), localMatch.begin(), localMatch.end(), back_inserter(temp));
|
2012-09-12 11:40:13 +08:00
|
|
|
|
|
|
|
globalMatch.clear();
|
|
|
|
const size_t count = std::min((size_t)k, temp.size());
|
|
|
|
copy(temp.begin(), temp.begin() + count, back_inserter(globalMatch));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (compactResult)
|
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector< std::vector<DMatch> >::iterator new_end = std::remove_if(matches.begin(), matches.end(), std::mem_fun_ref(&std::vector<DMatch>::empty));
|
2012-10-26 17:43:59 +08:00
|
|
|
matches.erase(new_end, matches.end());
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// radiusMatchSingle
|
2012-10-11 16:22:47 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::radiusMatchSingle(const oclMat &query, const oclMat &train,
|
2013-03-04 23:29:40 +08:00
|
|
|
oclMat &trainIdx, oclMat &distance, oclMat &nMatches, float maxDistance, const oclMat &mask)
|
2012-10-11 16:22:47 +08:00
|
|
|
{
|
|
|
|
if (query.empty() || train.empty())
|
2012-09-12 11:40:13 +08:00
|
|
|
return;
|
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
// match1 doesn't support signed char type, match2 only support float, hamming support uchar, ushort and int
|
|
|
|
int callType = query.depth();
|
|
|
|
if (callType != 5)
|
2013-04-12 18:12:12 +08:00
|
|
|
CV_Error(Error::StsUnsupportedFormat, "BruteForceMatch OpenCL only support float type query!\n");
|
2012-09-12 11:40:13 +08:00
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
if ((distType == 0 && callType == 1 ) || (distType == 1 && callType != 5) || (distType == 2 && (callType != 0
|
|
|
|
|| callType != 2 || callType != 4)))
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
2013-04-12 18:12:12 +08:00
|
|
|
CV_Error(Error::BadDepth, "BruteForceMatch OpenCL only support float type query!\n");
|
2013-03-26 13:41:13 +08:00
|
|
|
}
|
2012-09-12 11:40:13 +08:00
|
|
|
|
|
|
|
CV_Assert(query.channels() == 1 && query.depth() < CV_64F);
|
|
|
|
CV_Assert(train.type() == query.type() && train.cols == query.cols);
|
2013-03-26 13:41:13 +08:00
|
|
|
CV_Assert(trainIdx.empty() || (trainIdx.rows == query.rows && trainIdx.size() == distance.size()));
|
2012-09-12 11:40:13 +08:00
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
nMatches.create(1, query.rows, CV_32SC1);
|
2012-09-12 11:40:13 +08:00
|
|
|
if (trainIdx.empty())
|
|
|
|
{
|
2013-03-26 13:41:13 +08:00
|
|
|
trainIdx.create(query.rows, std::max((train.rows/ 100), 10), CV_32SC1);
|
|
|
|
distance.create(query.rows, std::max((train.rows/ 100), 10), CV_32FC1);
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
nMatches.setTo(Scalar::all(0));
|
|
|
|
|
2013-03-26 13:41:13 +08:00
|
|
|
matchDispatcher(query, train, maxDistance, mask, trainIdx, distance, nMatches, distType);
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::radiusMatchDownload(const oclMat &trainIdx, const oclMat &distance, const oclMat &nMatches,
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector< std::vector<DMatch> > &matches, bool compactResult)
|
2012-10-11 16:22:47 +08:00
|
|
|
{
|
|
|
|
if (trainIdx.empty() || distance.empty() || nMatches.empty())
|
2012-09-12 11:40:13 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
Mat trainIdxCPU(trainIdx);
|
|
|
|
Mat distanceCPU(distance);
|
|
|
|
Mat nMatchesCPU(nMatches);
|
|
|
|
|
|
|
|
radiusMatchConvert(trainIdxCPU, distanceCPU, nMatchesCPU, matches, compactResult);
|
|
|
|
}
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::radiusMatchConvert(const Mat &trainIdx, const Mat &distance, const Mat &nMatches,
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector< std::vector<DMatch> > &matches, bool compactResult)
|
2012-10-11 16:22:47 +08:00
|
|
|
{
|
|
|
|
if (trainIdx.empty() || distance.empty() || nMatches.empty())
|
2012-09-12 11:40:13 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
CV_Assert(trainIdx.type() == CV_32SC1);
|
|
|
|
CV_Assert(distance.type() == CV_32FC1 && distance.size() == trainIdx.size());
|
|
|
|
CV_Assert(nMatches.type() == CV_32SC1 && nMatches.cols == trainIdx.rows);
|
|
|
|
|
|
|
|
const int nQuery = trainIdx.rows;
|
|
|
|
|
|
|
|
matches.clear();
|
|
|
|
matches.reserve(nQuery);
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
const int *nMatches_ptr = nMatches.ptr<int>();
|
2012-09-12 11:40:13 +08:00
|
|
|
|
|
|
|
for (int queryIdx = 0; queryIdx < nQuery; ++queryIdx)
|
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
const int *trainIdx_ptr = trainIdx.ptr<int>(queryIdx);
|
|
|
|
const float *distance_ptr = distance.ptr<float>(queryIdx);
|
2012-09-12 11:40:13 +08:00
|
|
|
|
|
|
|
const int nMatches = std::min(nMatches_ptr[queryIdx], trainIdx.cols);
|
|
|
|
|
|
|
|
if (nMatches == 0)
|
|
|
|
{
|
|
|
|
if (!compactResult)
|
2013-02-25 00:14:01 +08:00
|
|
|
matches.push_back(std::vector<DMatch>());
|
2012-09-12 11:40:13 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
matches.push_back(std::vector<DMatch>(nMatches));
|
|
|
|
std::vector<DMatch> &curMatches = matches.back();
|
2012-09-12 11:40:13 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < nMatches; ++i, ++trainIdx_ptr, ++distance_ptr)
|
|
|
|
{
|
|
|
|
int trainIdx = *trainIdx_ptr;
|
|
|
|
|
|
|
|
float distance = *distance_ptr;
|
|
|
|
|
|
|
|
DMatch m(queryIdx, trainIdx, 0, distance);
|
|
|
|
|
|
|
|
curMatches[i] = m;
|
|
|
|
}
|
|
|
|
|
|
|
|
sort(curMatches.begin(), curMatches.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::radiusMatch(const oclMat &query, const oclMat &train, std::vector< std::vector<DMatch> > &matches,
|
2012-10-11 16:22:47 +08:00
|
|
|
float maxDistance, const oclMat &mask, bool compactResult)
|
|
|
|
{
|
|
|
|
oclMat trainIdx, distance, nMatches;
|
2012-09-12 11:40:13 +08:00
|
|
|
radiusMatchSingle(query, train, trainIdx, distance, nMatches, maxDistance, mask);
|
|
|
|
radiusMatchDownload(trainIdx, distance, nMatches, matches, compactResult);
|
|
|
|
}
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::radiusMatchCollection(const oclMat &query, oclMat &trainIdx, oclMat &imgIdx, oclMat &distance,
|
2013-02-25 00:14:01 +08:00
|
|
|
oclMat &nMatches, float /*maxDistance*/, const std::vector<oclMat> &masks)
|
2012-10-11 16:22:47 +08:00
|
|
|
{
|
|
|
|
if (query.empty() || empty())
|
2012-09-12 11:40:13 +08:00
|
|
|
return;
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
typedef void (*caller_t)(const oclMat & query, const oclMat * trains, int n, float maxDistance, const oclMat * masks,
|
|
|
|
const oclMat & trainIdx, const oclMat & imgIdx, const oclMat & distance, const oclMat & nMatches);
|
2012-09-12 11:40:13 +08:00
|
|
|
#if 0
|
|
|
|
static const caller_t callers[3][6] =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
ocl_matchL1_gpu<unsigned char>, 0/*matchL1_gpu<signed char>*/,
|
|
|
|
ocl_matchL1_gpu<unsigned short>, matchL1_gpu<short>,
|
|
|
|
ocl_matchL1_gpu<int>, matchL1_gpu<float>
|
|
|
|
},
|
|
|
|
{
|
|
|
|
0/*matchL2_gpu<unsigned char>*/, 0/*matchL2_gpu<signed char>*/,
|
|
|
|
0/*matchL2_gpu<unsigned short>*/, 0/*matchL2_gpu<short>*/,
|
|
|
|
0/*matchL2_gpu<int>*/, ocl_matchL2_gpu<float>
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ocl_matchHamming_gpu<unsigned char>, 0/*matchHamming_gpu<signed char>*/,
|
|
|
|
ocl_matchHamming_gpu<unsigned short>, 0/*matchHamming_gpu<short>*/,
|
|
|
|
ocl_matchHamming_gpu<int>, 0/*matchHamming_gpu<float>*/
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
const int nQuery = query.rows;
|
|
|
|
|
|
|
|
CV_Assert(query.channels() == 1 && query.depth() < CV_64F);
|
|
|
|
CV_Assert(trainIdx.empty() || (trainIdx.rows == nQuery && trainIdx.size() == distance.size() && trainIdx.size() == imgIdx.size()));
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
nMatches.create(1, nQuery, CV_32SC1);
|
2012-09-12 11:40:13 +08:00
|
|
|
if (trainIdx.empty())
|
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
trainIdx.create(nQuery, std::max((nQuery / 100), 10), CV_32SC1);
|
|
|
|
imgIdx.create(nQuery, std::max((nQuery / 100), 10), CV_32SC1);
|
|
|
|
distance.create(nQuery, std::max((nQuery / 100), 10), CV_32FC1);
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
nMatches.setTo(Scalar::all(0));
|
|
|
|
|
|
|
|
//caller_t func = callers[distType][query.depth()];
|
|
|
|
//CV_Assert(func != 0);
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<oclMat> trains_(trainDescCollection.begin(), trainDescCollection.end());
|
|
|
|
std::vector<oclMat> masks_(masks.begin(), masks.end());
|
2012-09-12 11:40:13 +08:00
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
/* func(query, &trains_[0], static_cast<int>(trains_.size()), maxDistance, masks_.size() == 0 ? 0 : &masks_[0],
|
|
|
|
trainIdx, imgIdx, distance, nMatches));*/
|
2012-09-12 11:40:13 +08:00
|
|
|
}
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::radiusMatchDownload(const oclMat &trainIdx, const oclMat &imgIdx, const oclMat &distance,
|
2013-02-25 00:14:01 +08:00
|
|
|
const oclMat &nMatches, std::vector< std::vector<DMatch> > &matches, bool compactResult)
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
if (trainIdx.empty() || imgIdx.empty() || distance.empty() || nMatches.empty())
|
2012-09-12 11:40:13 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
Mat trainIdxCPU(trainIdx);
|
|
|
|
Mat imgIdxCPU(imgIdx);
|
|
|
|
Mat distanceCPU(distance);
|
|
|
|
Mat nMatchesCPU(nMatches);
|
|
|
|
|
|
|
|
radiusMatchConvert(trainIdxCPU, imgIdxCPU, distanceCPU, nMatchesCPU, matches, compactResult);
|
|
|
|
}
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::radiusMatchConvert(const Mat &trainIdx, const Mat &imgIdx, const Mat &distance, const Mat &nMatches,
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector< std::vector<DMatch> > &matches, bool compactResult)
|
2012-10-11 16:22:47 +08:00
|
|
|
{
|
|
|
|
if (trainIdx.empty() || imgIdx.empty() || distance.empty() || nMatches.empty())
|
2012-09-12 11:40:13 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
CV_Assert(trainIdx.type() == CV_32SC1);
|
|
|
|
CV_Assert(imgIdx.type() == CV_32SC1 && imgIdx.size() == trainIdx.size());
|
|
|
|
CV_Assert(distance.type() == CV_32FC1 && distance.size() == trainIdx.size());
|
|
|
|
CV_Assert(nMatches.type() == CV_32SC1 && nMatches.cols == trainIdx.rows);
|
|
|
|
|
|
|
|
const int nQuery = trainIdx.rows;
|
|
|
|
|
|
|
|
matches.clear();
|
|
|
|
matches.reserve(nQuery);
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
const int *nMatches_ptr = nMatches.ptr<int>();
|
2012-09-12 11:40:13 +08:00
|
|
|
|
|
|
|
for (int queryIdx = 0; queryIdx < nQuery; ++queryIdx)
|
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
const int *trainIdx_ptr = trainIdx.ptr<int>(queryIdx);
|
|
|
|
const int *imgIdx_ptr = imgIdx.ptr<int>(queryIdx);
|
|
|
|
const float *distance_ptr = distance.ptr<float>(queryIdx);
|
2012-09-12 11:40:13 +08:00
|
|
|
|
|
|
|
const int nMatches = std::min(nMatches_ptr[queryIdx], trainIdx.cols);
|
|
|
|
|
|
|
|
if (nMatches == 0)
|
|
|
|
{
|
|
|
|
if (!compactResult)
|
2013-02-25 00:14:01 +08:00
|
|
|
matches.push_back(std::vector<DMatch>());
|
2012-09-12 11:40:13 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
matches.push_back(std::vector<DMatch>());
|
|
|
|
std::vector<DMatch> &curMatches = matches.back();
|
2012-09-12 11:40:13 +08:00
|
|
|
curMatches.reserve(nMatches);
|
|
|
|
|
|
|
|
for (int i = 0; i < nMatches; ++i, ++trainIdx_ptr, ++imgIdx_ptr, ++distance_ptr)
|
|
|
|
{
|
|
|
|
int trainIdx = *trainIdx_ptr;
|
|
|
|
int imgIdx = *imgIdx_ptr;
|
|
|
|
float distance = *distance_ptr;
|
|
|
|
|
|
|
|
DMatch m(queryIdx, trainIdx, imgIdx, distance);
|
|
|
|
|
|
|
|
curMatches.push_back(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
sort(curMatches.begin(), curMatches.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
void cv::ocl::BruteForceMatcher_OCL_base::radiusMatch(const oclMat &query, std::vector< std::vector<DMatch> > &matches, float maxDistance,
|
|
|
|
const std::vector<oclMat> &masks, bool compactResult)
|
2012-09-12 11:40:13 +08:00
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
oclMat trainIdx, imgIdx, distance, nMatches;
|
2012-09-12 11:40:13 +08:00
|
|
|
radiusMatchCollection(query, trainIdx, imgIdx, distance, nMatches, maxDistance, masks);
|
|
|
|
radiusMatchDownload(trainIdx, imgIdx, distance, nMatches, matches, compactResult);
|
2013-02-28 14:31:08 +08:00
|
|
|
}
|