Added ocl_matchTemplate( without dft)
This commit is contained in:
@@ -40,6 +40,365 @@
|
||||
//M*/
|
||||
|
||||
#include "precomp.hpp"
|
||||
#include "opencl_kernels.hpp"
|
||||
|
||||
//////////////////////////////////////////////////matchTemplate//////////////////////////////////////////////////////////
|
||||
namespace cv
|
||||
{
|
||||
struct MatchTemplateBuf
|
||||
{
|
||||
Size user_block_size;
|
||||
UMat imagef, templf;
|
||||
UMat image_sums;
|
||||
UMat image_sqsums;
|
||||
};
|
||||
|
||||
static bool matchTemplate_CCORR(const UMat &image, const UMat &templ, UMat &result, MatchTemplateBuf &buf);
|
||||
static bool matchTemplate_CCORR_NORMED(const UMat &image, const UMat &templ, UMat &result, MatchTemplateBuf &buf);
|
||||
|
||||
static bool matchTemplate_SQDIFF(const UMat &image, const UMat &templ, UMat &result, MatchTemplateBuf &buf);
|
||||
static bool matchTemplate_SQDIFF_NORMED (const UMat &image, const UMat &templ, UMat &result, MatchTemplateBuf &buf);
|
||||
|
||||
static bool matchTemplate_CCOEFF(const UMat &image, const UMat &templ, UMat &result, MatchTemplateBuf &buf);
|
||||
static bool matchTemplate_CCOEFF_NORMED(const UMat &image, const UMat &templ, UMat &result, MatchTemplateBuf &buf);
|
||||
|
||||
static bool matchTemplateNaive_CCORR (const UMat &image, const UMat &templ, UMat &result, int cn);
|
||||
static bool matchTemplateNaive_SQDIFF(const UMat &image, const UMat &templ, UMat &result, int cn);
|
||||
|
||||
static bool useNaive(int method, int depth, Size size)
|
||||
{
|
||||
#ifdef HAVE_CLAMDFFT
|
||||
if (method == TM_SQDIFF && depth == CV_32F)
|
||||
return true;
|
||||
else if(method == TM_CCORR || (method == TM_SQDIFF && depth == CV_8U))
|
||||
return size.height < 18 && size.width < 18;
|
||||
else
|
||||
return false;
|
||||
#else
|
||||
#define UNUSED(x) (void)(x);
|
||||
UNUSED(method) UNUSED(depth) UNUSED(size)
|
||||
#undef UNUSED
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////CCORR//////////////////////////////////////////////////////////////
|
||||
|
||||
static bool extractFirstChannel_32F(const UMat &image, UMat &result)
|
||||
{
|
||||
const char * kernelName = "extractFirstChannel";
|
||||
int type = image.type();
|
||||
int depth = CV_MAT_DEPTH(type);
|
||||
int cn = CV_MAT_CN(type);
|
||||
|
||||
ocl::Kernel k (kernelName, ocl::imgproc::match_template_oclsrc, format("-D type=%s -D elem_type=%s -D cn=%d",ocl::typeToStr(type), ocl::typeToStr(depth), cn));
|
||||
if (k.empty())
|
||||
return false;
|
||||
|
||||
size_t globalsize[2] = {result.cols, result.rows};
|
||||
size_t localsize[2] = {16, 16};
|
||||
|
||||
return k.args(ocl::KernelArg::ReadOnlyNoSize(image), ocl::KernelArg::WriteOnly(result)).run(2,globalsize,localsize,true);
|
||||
}
|
||||
|
||||
static bool matchTemplate_CCORR(const UMat &image, const UMat &templ, UMat &result, MatchTemplateBuf &buf)
|
||||
{
|
||||
if (useNaive(TM_CCORR, image.depth(), templ.size()) )
|
||||
return matchTemplateNaive_CCORR(image, templ, result, image.channels());
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool matchTemplateNaive_CCORR (const UMat &image, const UMat &templ, UMat &result, int cn)
|
||||
{
|
||||
int type = image.type();
|
||||
int depth = CV_MAT_DEPTH(type);
|
||||
|
||||
CV_Assert(result.channels() == 1);
|
||||
|
||||
const char * kernelName = "matchTemplate_Naive_CCORR";
|
||||
|
||||
ocl::Kernel k (kernelName, ocl::imgproc::match_template_oclsrc, format("-D type=%s -D elem_type=%s -D cn=%d",ocl::typeToStr(type), ocl::typeToStr(depth), cn));
|
||||
if (k.empty())
|
||||
return false;
|
||||
|
||||
size_t globalsize[2] = {result.cols, result.rows};
|
||||
size_t localsize[2] = {16, 16};
|
||||
|
||||
return k.args(ocl::KernelArg::ReadOnlyNoSize(image), ocl::KernelArg::ReadOnly(templ), ocl::KernelArg::WriteOnly(result)).run(2,globalsize,localsize,true);
|
||||
}
|
||||
|
||||
static bool matchTemplate_CCORR_NORMED(const UMat &image, const UMat &templ, UMat &result, MatchTemplateBuf &buf)
|
||||
{
|
||||
if (!matchTemplate_CCORR(image, templ, result, buf))
|
||||
return false;
|
||||
|
||||
int type = image.type();
|
||||
int depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
|
||||
|
||||
const char * kernelName = "matchTemplate_CCORR_NORMED";
|
||||
|
||||
ocl::Kernel k(kernelName, ocl::imgproc::match_template_oclsrc, format("-D type=%s -D elem_type=%s -D cn=%d",ocl::typeToStr(type), ocl::typeToStr(depth), cn));
|
||||
if (k.empty())
|
||||
return false;
|
||||
|
||||
UMat temp;
|
||||
integral(image.reshape(1), buf.image_sums, temp);
|
||||
|
||||
if(temp.depth() == CV_64F)
|
||||
temp.convertTo(buf.image_sqsums, CV_32F);
|
||||
else
|
||||
buf.image_sqsums = temp;
|
||||
|
||||
UMat templ_resh;
|
||||
templ.reshape(1).convertTo(templ_resh, CV_32F);
|
||||
|
||||
multiply(templ_resh, templ_resh, temp);
|
||||
unsigned long long templ_sqsum = (unsigned long long)sum(temp)[0];
|
||||
|
||||
size_t globalsize[2] = {result.cols, result.rows};
|
||||
size_t localsize[2] = {16, 16};
|
||||
|
||||
return k.args(ocl::KernelArg::ReadOnlyNoSize(buf.image_sqsums), ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols, templ_sqsum).run(2,globalsize,localsize,true);
|
||||
}
|
||||
|
||||
//////////////////////////////////////SQDIFF//////////////////////////////////////////////////////////////
|
||||
|
||||
static bool matchTemplate_SQDIFF(const UMat &image, const UMat &templ, UMat &result, MatchTemplateBuf &buf)
|
||||
{
|
||||
if (useNaive(TM_SQDIFF, image.depth(), templ.size()))
|
||||
{
|
||||
return matchTemplateNaive_SQDIFF(image, templ, result, image.channels());;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool matchTemplateNaive_SQDIFF(const UMat &image, const UMat &templ, UMat &result, int cn)
|
||||
{
|
||||
int type = image.type();
|
||||
int depth = CV_MAT_DEPTH(type);
|
||||
|
||||
CV_Assert(result.channels() == 1);
|
||||
|
||||
const char * kernelName = "matchTemplate_Naive_SQDIFF";
|
||||
|
||||
ocl::Kernel k (kernelName, ocl::imgproc::match_template_oclsrc, format("-D type=%s -D elem_type=%s -D cn=%d",ocl::typeToStr(type), ocl::typeToStr(depth), cn));
|
||||
if (k.empty())
|
||||
return false;
|
||||
|
||||
size_t globalsize[2] = {result.cols, result.rows};
|
||||
size_t localsize[2] = {16, 16};
|
||||
|
||||
return k.args(ocl::KernelArg::ReadOnlyNoSize(image), ocl::KernelArg::ReadOnly(templ), ocl::KernelArg::WriteOnly(result)).run(2,globalsize,localsize,true);
|
||||
}
|
||||
|
||||
static bool matchTemplate_SQDIFF_NORMED (const UMat &image, const UMat &templ, UMat &result, MatchTemplateBuf &buf)
|
||||
{
|
||||
if (!matchTemplate_CCORR(image, templ, result, buf))
|
||||
return false;
|
||||
|
||||
int type = image.type();
|
||||
int depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
|
||||
|
||||
const char * kernelName = "matchTemplate_SQDIFF_NORMED";
|
||||
|
||||
ocl::Kernel k(kernelName, ocl::imgproc::match_template_oclsrc, format("-D type=%s -D elem_type=%s -D cn=%d",ocl::typeToStr(type), ocl::typeToStr(depth), cn));
|
||||
if (k.empty())
|
||||
return false;
|
||||
|
||||
UMat temp;
|
||||
integral(image.reshape(1), buf.image_sums, temp);
|
||||
|
||||
if(temp.depth() == CV_64F)
|
||||
temp.convertTo(buf.image_sqsums, CV_32F);
|
||||
else
|
||||
buf.image_sqsums = temp;
|
||||
|
||||
UMat templ_resh;
|
||||
templ.reshape(1).convertTo(templ_resh, CV_32F);
|
||||
|
||||
multiply(templ_resh, templ_resh, temp);
|
||||
unsigned long long templ_sqsum = (unsigned long long)sum(temp)[0];
|
||||
|
||||
size_t globalsize[2] = {result.cols, result.rows};
|
||||
size_t localsize[2] = {16, 16};
|
||||
|
||||
return k.args(ocl::KernelArg::ReadOnlyNoSize(buf.image_sqsums), ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols, templ_sqsum).run(2,globalsize,localsize,true);
|
||||
}
|
||||
|
||||
/////////////////////////////////////CCOEFF/////////////////////////////////////////////////////////////////
|
||||
|
||||
static bool matchTemplate_CCOEFF(const UMat &image, const UMat &templ, UMat &result, MatchTemplateBuf &buf)
|
||||
{
|
||||
if (!matchTemplate_CCORR(image, templ, result, buf))
|
||||
return false;
|
||||
|
||||
integral(image, buf.image_sums);
|
||||
|
||||
int type = buf.image_sums.type();
|
||||
int depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
|
||||
|
||||
const char * kernelName;
|
||||
|
||||
if (cn==1)
|
||||
kernelName = "matchTemplate_Prepared_CCOEFF_C1";
|
||||
else if (cn==2)
|
||||
kernelName = "matchTemplate_Prepared_CCOEFF_C2";
|
||||
else
|
||||
kernelName = "matchTemplate_Prepared_CCOEFF_C4";
|
||||
|
||||
ocl::Kernel k(kernelName, ocl::imgproc::match_template_oclsrc, format("-D type=%s -D elem_type=%s -D cn=%d",ocl::typeToStr(type), ocl::typeToStr(depth), cn));
|
||||
if (k.empty())
|
||||
return false;
|
||||
|
||||
size_t globalsize[2] = {result.cols, result.rows};
|
||||
size_t localsize[2] = {16, 16};
|
||||
|
||||
if (cn==1)
|
||||
{
|
||||
float templ_sum = (float)sum(templ)[0]/ templ.size().area();
|
||||
return k.args(ocl::KernelArg::ReadOnlyNoSize(buf.image_sums), ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols, templ_sum).run(2,globalsize,localsize,true);
|
||||
}
|
||||
else
|
||||
{
|
||||
Vec4f templ_sum = Vec4f::all(0);
|
||||
templ_sum = sum(templ)/ templ.size().area();
|
||||
if (cn==2)
|
||||
return k.args(ocl::KernelArg::ReadOnlyNoSize(buf.image_sums), ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols,
|
||||
templ_sum[0],templ_sum[1]).run(2,globalsize,localsize,true);
|
||||
|
||||
return k.args(ocl::KernelArg::ReadOnlyNoSize(buf.image_sums), ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols,
|
||||
templ_sum[0],templ_sum[1],templ_sum[2],templ_sum[3]).run(2,globalsize,localsize,true);
|
||||
}
|
||||
}
|
||||
|
||||
static bool matchTemplate_CCOEFF_NORMED(const UMat &image, const UMat &templ, UMat &result, MatchTemplateBuf &buf)
|
||||
{
|
||||
image.convertTo(buf.imagef, CV_32F);
|
||||
templ.convertTo(buf.templf, CV_32F);
|
||||
|
||||
if(!matchTemplate_CCORR(buf.imagef, buf.templf, result, buf))
|
||||
return false;
|
||||
|
||||
const char * kernelName;
|
||||
|
||||
UMat temp;
|
||||
integral(image, buf.image_sums, temp);
|
||||
|
||||
int type = buf.image_sums.type();
|
||||
int depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
|
||||
|
||||
if (cn== 1)
|
||||
kernelName = "matchTemplate_CCOEFF_NORMED_C1";
|
||||
else if (cn==2)
|
||||
kernelName = "matchTemplate_CCOEFF_NORMED_C2";
|
||||
else
|
||||
kernelName = "matchTemplate_CCOEFF_NORMED_C4";
|
||||
|
||||
ocl::Kernel k(kernelName, ocl::imgproc::match_template_oclsrc,
|
||||
format("-D type=%s -D elem_type=%s -D cn=%d", ocl::typeToStr(type), ocl::typeToStr(depth), cn));
|
||||
if (k.empty())
|
||||
return false;
|
||||
|
||||
if(temp.depth() == CV_64F)
|
||||
temp.convertTo(buf.image_sqsums, CV_32F);
|
||||
else
|
||||
buf.image_sqsums = temp;
|
||||
|
||||
size_t globalsize[2] = {result.cols, result.rows};
|
||||
size_t localsize[2] = {16, 16};
|
||||
|
||||
float scale = 1.f / templ.size().area();
|
||||
|
||||
if (cn==1)
|
||||
{
|
||||
float templ_sum = (float)sum(templ)[0];
|
||||
|
||||
multiply(buf.templf, buf.templf, temp);
|
||||
float templ_sqsum = (float)sum(temp)[0];
|
||||
|
||||
templ_sqsum -= scale * templ_sum * templ_sum;
|
||||
templ_sum *= scale;
|
||||
|
||||
if (templ_sqsum < DBL_EPSILON)
|
||||
{
|
||||
result = Scalar::all(1);
|
||||
return true;
|
||||
}
|
||||
|
||||
return k.args(ocl::KernelArg::ReadOnlyNoSize(buf.image_sums),ocl::KernelArg::ReadOnlyNoSize(buf.image_sqsums),
|
||||
ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols, scale, templ_sum, templ_sqsum)
|
||||
.run(2,globalsize,localsize,true);
|
||||
}
|
||||
else
|
||||
{
|
||||
Vec4f templ_sum = Vec4f::all(0);
|
||||
Vec4f templ_sqsum = Vec4f::all(0);
|
||||
|
||||
templ_sum = sum(templ);
|
||||
|
||||
multiply(buf.templf, buf.templf, temp);
|
||||
templ_sqsum = sum(temp);
|
||||
|
||||
float templ_sqsum_sum = 0;
|
||||
for(int i = 0; i < cn; i ++)
|
||||
{
|
||||
templ_sqsum_sum += templ_sqsum[i] - scale * templ_sum[i] * templ_sum[i];
|
||||
}
|
||||
|
||||
templ_sum *= scale;
|
||||
|
||||
if (templ_sqsum_sum < DBL_EPSILON)
|
||||
{
|
||||
result = Scalar::all(1);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (cn==2)
|
||||
return k.args(ocl::KernelArg::ReadOnlyNoSize(buf.image_sums), ocl::KernelArg::ReadOnlyNoSize(buf.image_sqsums),
|
||||
ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols, scale,
|
||||
templ_sum[0],templ_sum[1], templ_sqsum_sum)
|
||||
.run(2,globalsize,localsize,true);
|
||||
|
||||
return k.args(ocl::KernelArg::ReadOnlyNoSize(buf.image_sums), ocl::KernelArg::ReadOnlyNoSize(buf.image_sqsums),
|
||||
ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols, scale,
|
||||
templ_sum[0],templ_sum[1],templ_sum[2],templ_sum[3], templ_sqsum_sum)
|
||||
.run(2,globalsize,localsize,true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static bool ocl_matchTemplate( InputArray _img, InputArray _templ, OutputArray _result, int method)
|
||||
{
|
||||
int type = _img.type();
|
||||
int cn = CV_MAT_CN(type);
|
||||
|
||||
CV_Assert( cn == _templ.channels() && cn!=3 && cn<=4);
|
||||
|
||||
typedef bool (*Caller)(const UMat &, const UMat &, UMat &, MatchTemplateBuf &);
|
||||
|
||||
const Caller callers[] =
|
||||
{
|
||||
matchTemplate_SQDIFF, matchTemplate_SQDIFF_NORMED, matchTemplate_CCORR,
|
||||
matchTemplate_CCORR_NORMED, matchTemplate_CCOEFF, matchTemplate_CCOEFF_NORMED
|
||||
};
|
||||
|
||||
Caller caller;
|
||||
if (!(caller = callers[method]))
|
||||
return false;
|
||||
|
||||
MatchTemplateBuf buf;
|
||||
|
||||
UMat image = _img.getUMat();
|
||||
UMat templ = _templ.getUMat(), result;
|
||||
_result.create(image.rows - templ.rows + 1, image.cols - templ.cols + 1, CV_32F);
|
||||
result = _result.getUMat();
|
||||
return caller(image, templ, result, buf);
|
||||
}
|
||||
}
|
||||
|
||||
namespace cv
|
||||
{
|
||||
@@ -226,15 +585,24 @@ void crossCorr( const Mat& img, const Mat& _templ, Mat& corr,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*****************************************************************************************/
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void cv::matchTemplate( InputArray _img, InputArray _templ, OutputArray _result, int method )
|
||||
{
|
||||
CV_Assert( CV_TM_SQDIFF <= method && method <= CV_TM_CCOEFF_NORMED );
|
||||
|
||||
CV_Assert( (_img.depth() == CV_8U || _img.depth() == CV_32F) && _img.type() == _templ.type() );
|
||||
|
||||
CV_Assert(_img.size().height >= _templ.size().height && _img.size().width >= _templ.size().width);
|
||||
|
||||
CV_Assert(_img.dims() <= 2);
|
||||
|
||||
bool use_opencl = ocl::useOpenCL() && _result.isUMat();
|
||||
if ( use_opencl && ocl_matchTemplate(_img,_templ,_result,method))
|
||||
return;
|
||||
|
||||
int numType = method == CV_TM_CCORR || method == CV_TM_CCORR_NORMED ? 0 :
|
||||
method == CV_TM_CCOEFF || method == CV_TM_CCOEFF_NORMED ? 1 : 2;
|
||||
bool isNormed = method == CV_TM_CCORR_NORMED ||
|
||||
@@ -245,11 +613,6 @@ void cv::matchTemplate( InputArray _img, InputArray _templ, OutputArray _result,
|
||||
if( img.rows < templ.rows || img.cols < templ.cols )
|
||||
std::swap(img, templ);
|
||||
|
||||
CV_Assert( (img.depth() == CV_8U || img.depth() == CV_32F) &&
|
||||
img.type() == templ.type() );
|
||||
|
||||
CV_Assert( img.rows >= templ.rows && img.cols >= templ.cols);
|
||||
|
||||
Size corrSize(img.cols - templ.cols + 1, img.rows - templ.rows + 1);
|
||||
_result.create(corrSize, CV_32F);
|
||||
Mat result = _result.getMat();
|
||||
|
Reference in New Issue
Block a user