|
|
|
@ -42,338 +42,326 @@ |
|
|
|
|
#include "precomp.hpp" |
|
|
|
|
#include "opencl_kernels.hpp" |
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////matchTemplate//////////////////////////////////////////////////////////
|
|
|
|
|
////////////////////////////////////////////////// matchTemplate //////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
namespace cv |
|
|
|
|
{ |
|
|
|
|
static bool matchTemplate_CCORR(InputArray _image, InputArray _templ, OutputArray _result); |
|
|
|
|
static bool matchTemplate_CCORR_NORMED(InputArray _image, InputArray _templ, OutputArray _result); |
|
|
|
|
|
|
|
|
|
static bool matchTemplate_SQDIFF(InputArray _image, InputArray _templ, OutputArray _result); |
|
|
|
|
static bool matchTemplate_SQDIFF_NORMED (InputArray _image, InputArray _templ, OutputArray _result); |
|
|
|
|
|
|
|
|
|
static bool matchTemplate_CCOEFF(InputArray _image, InputArray _templ, OutputArray _result); |
|
|
|
|
static bool matchTemplate_CCOEFF_NORMED(InputArray _image, InputArray _templ, OutputArray _result); |
|
|
|
|
|
|
|
|
|
static bool matchTemplateNaive_CCORR (InputArray _image, InputArray _templ, OutputArray _result, int cn); |
|
|
|
|
static bool matchTemplateNaive_SQDIFF(InputArray _image, InputArray _templ, OutputArray _result, int cn); |
|
|
|
|
#ifdef HAVE_OPENCL |
|
|
|
|
|
|
|
|
|
static bool useNaive(int method, int depth, Size size) |
|
|
|
|
{ |
|
|
|
|
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; |
|
|
|
|
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) |
|
|
|
|
UNUSED(method) UNUSED(depth) UNUSED(size) |
|
|
|
|
#undef UNUSED |
|
|
|
|
return true; |
|
|
|
|
return true; |
|
|
|
|
#endif |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////CCORR//////////////////////////////////////////////////////////////
|
|
|
|
|
/////////////////////////////////////////////////// CCORR //////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
static bool matchTemplate_CCORR(InputArray _image, InputArray _templ, OutputArray _result) |
|
|
|
|
{ |
|
|
|
|
if (useNaive(TM_CCORR, _image.depth(), _templ.size()) ) |
|
|
|
|
return matchTemplateNaive_CCORR(_image, _templ, _result, _image.channels()); |
|
|
|
|
else |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
static bool matchTemplateNaive_CCORR (InputArray _image, InputArray _templ, OutputArray _result, int cn) |
|
|
|
|
{ |
|
|
|
|
int type = _image.type(); |
|
|
|
|
int depth = CV_MAT_DEPTH(type); |
|
|
|
|
|
|
|
|
|
static bool matchTemplateNaive_CCORR (InputArray _image, InputArray _templ, OutputArray _result, int cn) |
|
|
|
|
{ |
|
|
|
|
int type = _image.type(); |
|
|
|
|
int depth = CV_MAT_DEPTH(type); |
|
|
|
|
const char * kernelName = "matchTemplate_Naive_CCORR"; |
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
|
|
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 image = _image.getUMat(); |
|
|
|
|
UMat templ = _templ.getUMat(), result; |
|
|
|
|
_result.create(image.rows - templ.rows + 1, image.cols - templ.cols + 1, CV_32F); |
|
|
|
|
result = _result.getUMat(); |
|
|
|
|
|
|
|
|
|
UMat image = _image.getUMat(); |
|
|
|
|
UMat templ = _templ.getUMat(), result; |
|
|
|
|
_result.create(image.rows - templ.rows + 1, image.cols - templ.cols + 1, CV_32F); |
|
|
|
|
result = _result.getUMat(); |
|
|
|
|
size_t globalsize[2] = {result.cols, result.rows}; |
|
|
|
|
|
|
|
|
|
size_t globalsize[2] = {result.cols, result.rows}; |
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image), ocl::KernelArg::ReadOnly(templ), ocl::KernelArg::WriteOnly(result)).run(2,globalsize,NULL,false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image), ocl::KernelArg::ReadOnly(templ), ocl::KernelArg::WriteOnly(result)).run(2,globalsize,NULL,false); |
|
|
|
|
} |
|
|
|
|
static bool matchTemplate_CCORR_NORMED(InputArray _image, InputArray _templ, OutputArray _result) |
|
|
|
|
{ |
|
|
|
|
matchTemplate(_image, _templ, _result, CV_TM_CCORR); |
|
|
|
|
|
|
|
|
|
static bool matchTemplate_CCORR_NORMED(InputArray _image, InputArray _templ, OutputArray _result) |
|
|
|
|
{ |
|
|
|
|
matchTemplate(_image, _templ, _result, CV_TM_CCORR); |
|
|
|
|
int type = _image.type(); |
|
|
|
|
int depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type); |
|
|
|
|
|
|
|
|
|
int type = _image.type(); |
|
|
|
|
int depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type); |
|
|
|
|
const char * kernelName = "matchTemplate_CCORR_NORMED"; |
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
|
|
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 image = _image.getUMat(); |
|
|
|
|
UMat templ = _templ.getUMat(), result; |
|
|
|
|
_result.create(image.rows - templ.rows + 1, image.cols - templ.cols + 1, CV_32F); |
|
|
|
|
result = _result.getUMat(); |
|
|
|
|
|
|
|
|
|
UMat image = _image.getUMat(); |
|
|
|
|
UMat templ = _templ.getUMat(), result; |
|
|
|
|
_result.create(image.rows - templ.rows + 1, image.cols - templ.cols + 1, CV_32F); |
|
|
|
|
result = _result.getUMat(); |
|
|
|
|
UMat image_sums, image_sqsums; |
|
|
|
|
integral(image.reshape(1), image_sums, image_sqsums, CV_32F, CV_32F); |
|
|
|
|
|
|
|
|
|
UMat image_sums, image_sqsums; |
|
|
|
|
integral(image.reshape(1), image_sums, image_sqsums, CV_32F, CV_32F); |
|
|
|
|
UMat templ_resh, temp; |
|
|
|
|
templ.reshape(1).convertTo(templ_resh, CV_32F); |
|
|
|
|
|
|
|
|
|
UMat templ_resh, temp; |
|
|
|
|
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]; |
|
|
|
|
|
|
|
|
|
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 globalsize[2] = {result.cols, result.rows}; |
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sqsums), ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols, templ_sqsum).run(2,globalsize,NULL,false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sqsums), ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols, templ_sqsum).run(2,globalsize,NULL,false); |
|
|
|
|
} |
|
|
|
|
static bool matchTemplate_CCORR(InputArray _image, InputArray _templ, OutputArray _result) |
|
|
|
|
{ |
|
|
|
|
if (useNaive(TM_CCORR, _image.depth(), _templ.size()) ) |
|
|
|
|
return matchTemplateNaive_CCORR(_image, _templ, _result, _image.channels()); |
|
|
|
|
else |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//////////////////////////////////////SQDIFF//////////////////////////////////////////////////////////////
|
|
|
|
|
////////////////////////////////////// SQDIFF //////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
static bool matchTemplate_SQDIFF(InputArray _image, InputArray _templ, OutputArray _result) |
|
|
|
|
{ |
|
|
|
|
if (useNaive(TM_SQDIFF, _image.depth(), _templ.size())) |
|
|
|
|
{ |
|
|
|
|
return matchTemplateNaive_SQDIFF(_image, _templ, _result, _image.channels());; |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
static bool matchTemplateNaive_SQDIFF(InputArray _image, InputArray _templ, OutputArray _result, int cn) |
|
|
|
|
{ |
|
|
|
|
int type = _image.type(); |
|
|
|
|
int depth = CV_MAT_DEPTH(type); |
|
|
|
|
|
|
|
|
|
static bool matchTemplateNaive_SQDIFF(InputArray _image, InputArray _templ, OutputArray _result, int cn) |
|
|
|
|
{ |
|
|
|
|
int type = _image.type(); |
|
|
|
|
int depth = CV_MAT_DEPTH(type); |
|
|
|
|
const char * kernelName = "matchTemplate_Naive_SQDIFF"; |
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
|
|
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 image = _image.getUMat(); |
|
|
|
|
UMat templ = _templ.getUMat(), result; |
|
|
|
|
_result.create(image.rows - templ.rows + 1, image.cols - templ.cols + 1, CV_32F); |
|
|
|
|
result = _result.getUMat(); |
|
|
|
|
|
|
|
|
|
UMat image = _image.getUMat(); |
|
|
|
|
UMat templ = _templ.getUMat(), result; |
|
|
|
|
_result.create(image.rows - templ.rows + 1, image.cols - templ.cols + 1, CV_32F); |
|
|
|
|
result = _result.getUMat(); |
|
|
|
|
size_t globalsize[2] = {result.cols, result.rows}; |
|
|
|
|
|
|
|
|
|
size_t globalsize[2] = {result.cols, result.rows}; |
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image), ocl::KernelArg::ReadOnly(templ), ocl::KernelArg::WriteOnly(result)).run(2,globalsize,NULL,false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image), ocl::KernelArg::ReadOnly(templ), ocl::KernelArg::WriteOnly(result)).run(2,globalsize,NULL,false); |
|
|
|
|
} |
|
|
|
|
static bool matchTemplate_SQDIFF_NORMED (InputArray _image, InputArray _templ, OutputArray _result) |
|
|
|
|
{ |
|
|
|
|
matchTemplate(_image, _templ, _result, CV_TM_CCORR); |
|
|
|
|
|
|
|
|
|
static bool matchTemplate_SQDIFF_NORMED (InputArray _image, InputArray _templ, OutputArray _result) |
|
|
|
|
{ |
|
|
|
|
matchTemplate(_image, _templ, _result, CV_TM_CCORR); |
|
|
|
|
int type = _image.type(); |
|
|
|
|
int depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type); |
|
|
|
|
|
|
|
|
|
int type = _image.type(); |
|
|
|
|
int depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type); |
|
|
|
|
const char * kernelName = "matchTemplate_SQDIFF_NORMED"; |
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
|
|
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 image = _image.getUMat(); |
|
|
|
|
UMat templ = _templ.getUMat(), result; |
|
|
|
|
_result.create(image.rows - templ.rows + 1, image.cols - templ.cols + 1, CV_32F); |
|
|
|
|
result = _result.getUMat(); |
|
|
|
|
|
|
|
|
|
UMat image = _image.getUMat(); |
|
|
|
|
UMat templ = _templ.getUMat(), result; |
|
|
|
|
_result.create(image.rows - templ.rows + 1, image.cols - templ.cols + 1, CV_32F); |
|
|
|
|
result = _result.getUMat(); |
|
|
|
|
UMat image_sums, image_sqsums; |
|
|
|
|
integral(image.reshape(1), image_sums, image_sqsums, CV_32F, CV_32F); |
|
|
|
|
|
|
|
|
|
UMat image_sums, image_sqsums; |
|
|
|
|
integral(image.reshape(1), image_sums, image_sqsums, CV_32F, CV_32F); |
|
|
|
|
UMat temp, templ_resh; |
|
|
|
|
templ.reshape(1).convertTo(templ_resh, CV_32F); |
|
|
|
|
|
|
|
|
|
UMat temp, 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]; |
|
|
|
|
|
|
|
|
|
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 globalsize[2] = {result.cols, result.rows}; |
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sqsums), ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols, templ_sqsum).run(2,globalsize,NULL,false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sqsums), ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols, templ_sqsum).run(2,globalsize,NULL,false); |
|
|
|
|
} |
|
|
|
|
static bool matchTemplate_SQDIFF(InputArray _image, InputArray _templ, OutputArray _result) |
|
|
|
|
{ |
|
|
|
|
if (useNaive(TM_SQDIFF, _image.depth(), _templ.size())) |
|
|
|
|
return matchTemplateNaive_SQDIFF(_image, _templ, _result, _image.channels()); |
|
|
|
|
else |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/////////////////////////////////////CCOEFF/////////////////////////////////////////////////////////////////
|
|
|
|
|
///////////////////////////////////// CCOEFF /////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
static bool matchTemplate_CCOEFF(InputArray _image, InputArray _templ, OutputArray _result) |
|
|
|
|
{ |
|
|
|
|
matchTemplate(_image, _templ, _result, CV_TM_CCORR); |
|
|
|
|
static bool matchTemplate_CCOEFF(InputArray _image, InputArray _templ, OutputArray _result) |
|
|
|
|
{ |
|
|
|
|
matchTemplate(_image, _templ, _result, CV_TM_CCORR); |
|
|
|
|
|
|
|
|
|
UMat image_sums; |
|
|
|
|
integral(_image, image_sums); |
|
|
|
|
UMat image_sums; |
|
|
|
|
integral(_image, image_sums); |
|
|
|
|
|
|
|
|
|
int type = image_sums.type(); |
|
|
|
|
int depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type); |
|
|
|
|
int type = image_sums.type(); |
|
|
|
|
int depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type); |
|
|
|
|
|
|
|
|
|
const char * kernelName; |
|
|
|
|
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"; |
|
|
|
|
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; |
|
|
|
|
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 templ = _templ.getUMat(), result; |
|
|
|
|
Size size = _image.size(); |
|
|
|
|
_result.create(size.height - templ.rows + 1, size.width - templ.cols + 1, CV_32F); |
|
|
|
|
result = _result.getUMat(); |
|
|
|
|
UMat templ = _templ.getUMat(), result; |
|
|
|
|
Size size = _image.size(); |
|
|
|
|
_result.create(size.height - templ.rows + 1, size.width - templ.cols + 1, CV_32F); |
|
|
|
|
result = _result.getUMat(); |
|
|
|
|
|
|
|
|
|
size_t globalsize[2] = {result.cols, result.rows}; |
|
|
|
|
size_t globalsize[2] = {result.cols, result.rows}; |
|
|
|
|
|
|
|
|
|
if (cn==1) |
|
|
|
|
{ |
|
|
|
|
float templ_sum = (float)sum(_templ)[0]/ _templ.size().area(); |
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols, templ_sum).run(2,globalsize,NULL,false); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
Vec4f templ_sum = Vec4f::all(0); |
|
|
|
|
templ_sum = sum(templ)/ templ.size().area(); |
|
|
|
|
if (cn==2) |
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols, |
|
|
|
|
templ_sum[0],templ_sum[1]).run(2,globalsize,NULL,false); |
|
|
|
|
|
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(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,NULL,false); |
|
|
|
|
} |
|
|
|
|
if (cn==1) |
|
|
|
|
{ |
|
|
|
|
float templ_sum = (float)sum(_templ)[0]/ _templ.size().area(); |
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols, templ_sum).run(2,globalsize,NULL,false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static bool matchTemplate_CCOEFF_NORMED(InputArray _image, InputArray _templ, OutputArray _result) |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
UMat imagef, templf; |
|
|
|
|
Vec4f templ_sum = Vec4f::all(0); |
|
|
|
|
templ_sum = sum(templ)/ templ.size().area(); |
|
|
|
|
if (cn==2) |
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols, |
|
|
|
|
templ_sum[0],templ_sum[1]).run(2,globalsize,NULL,false); |
|
|
|
|
|
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(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,NULL,false); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
_image.getUMat().convertTo(imagef, CV_32F); |
|
|
|
|
_templ.getUMat().convertTo(templf, CV_32F); |
|
|
|
|
static bool matchTemplate_CCOEFF_NORMED(InputArray _image, InputArray _templ, OutputArray _result) |
|
|
|
|
{ |
|
|
|
|
UMat imagef, templf; |
|
|
|
|
|
|
|
|
|
matchTemplate(imagef, templf, _result, CV_TM_CCORR); |
|
|
|
|
_image.getUMat().convertTo(imagef, CV_32F); |
|
|
|
|
_templ.getUMat().convertTo(templf, CV_32F); |
|
|
|
|
|
|
|
|
|
const char * kernelName; |
|
|
|
|
matchTemplate(imagef, templf, _result, CV_TM_CCORR); |
|
|
|
|
|
|
|
|
|
UMat temp, image_sums, image_sqsums; |
|
|
|
|
integral(_image,image_sums, image_sqsums, CV_32F, CV_32F); |
|
|
|
|
const char * kernelName; |
|
|
|
|
|
|
|
|
|
int type = image_sums.type(); |
|
|
|
|
int depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type); |
|
|
|
|
UMat temp, image_sums, image_sqsums; |
|
|
|
|
integral(_image,image_sums, image_sqsums, CV_32F, CV_32F); |
|
|
|
|
|
|
|
|
|
if (cn== 1) |
|
|
|
|
kernelName = "matchTemplate_CCOEFF_NORMED_C1"; |
|
|
|
|
else if (cn==2) |
|
|
|
|
kernelName = "matchTemplate_CCOEFF_NORMED_C2"; |
|
|
|
|
else |
|
|
|
|
kernelName = "matchTemplate_CCOEFF_NORMED_C4"; |
|
|
|
|
int type = image_sums.type(); |
|
|
|
|
int depth = CV_MAT_DEPTH(type), 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; |
|
|
|
|
if (cn== 1) |
|
|
|
|
kernelName = "matchTemplate_CCOEFF_NORMED_C1"; |
|
|
|
|
else if (cn==2) |
|
|
|
|
kernelName = "matchTemplate_CCOEFF_NORMED_C2"; |
|
|
|
|
else |
|
|
|
|
kernelName = "matchTemplate_CCOEFF_NORMED_C4"; |
|
|
|
|
|
|
|
|
|
UMat image = _image.getUMat(); |
|
|
|
|
UMat templ = _templ.getUMat(), result; |
|
|
|
|
int image_rows = _image.size().height, image_cols = _image.size().width; |
|
|
|
|
_result.create(image_rows - templ.rows + 1, image_cols - templ.cols + 1, CV_32F); |
|
|
|
|
result = _result.getUMat(); |
|
|
|
|
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}; |
|
|
|
|
UMat image = _image.getUMat(); |
|
|
|
|
UMat templ = _templ.getUMat(), result; |
|
|
|
|
int image_rows = _image.size().height, image_cols = _image.size().width; |
|
|
|
|
_result.create(image_rows - templ.rows + 1, image_cols - templ.cols + 1, CV_32F); |
|
|
|
|
result = _result.getUMat(); |
|
|
|
|
|
|
|
|
|
float scale = 1.f / templ.size().area(); |
|
|
|
|
size_t globalsize[2] = {result.cols, result.rows}; |
|
|
|
|
|
|
|
|
|
if (cn==1) |
|
|
|
|
{ |
|
|
|
|
float templ_sum = (float)sum(templ)[0]; |
|
|
|
|
float scale = 1.f / templ.size().area(); |
|
|
|
|
|
|
|
|
|
multiply(templf, templf, temp); |
|
|
|
|
float templ_sqsum = (float)sum(temp)[0]; |
|
|
|
|
if (cn==1) |
|
|
|
|
{ |
|
|
|
|
float templ_sum = (float)sum(templ)[0]; |
|
|
|
|
|
|
|
|
|
templ_sqsum -= scale * templ_sum * templ_sum; |
|
|
|
|
templ_sum *= scale; |
|
|
|
|
multiply(templf, templf, temp); |
|
|
|
|
float templ_sqsum = (float)sum(temp)[0]; |
|
|
|
|
|
|
|
|
|
if (templ_sqsum < DBL_EPSILON) |
|
|
|
|
{ |
|
|
|
|
result = Scalar::all(1); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
templ_sqsum -= scale * templ_sum * templ_sum; |
|
|
|
|
templ_sum *= scale; |
|
|
|
|
|
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums),ocl::KernelArg::ReadOnlyNoSize(image_sqsums), |
|
|
|
|
ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols, scale, templ_sum, templ_sqsum) |
|
|
|
|
.run(2,globalsize,NULL,false); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
if (templ_sqsum < DBL_EPSILON) |
|
|
|
|
{ |
|
|
|
|
Vec4f templ_sum = Vec4f::all(0); |
|
|
|
|
Vec4f templ_sqsum = Vec4f::all(0); |
|
|
|
|
|
|
|
|
|
templ_sum = sum(templ); |
|
|
|
|
result = Scalar::all(1); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
multiply(templf, templf, temp); |
|
|
|
|
templ_sqsum = sum(temp); |
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums),ocl::KernelArg::ReadOnlyNoSize(image_sqsums), |
|
|
|
|
ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols, scale, templ_sum, templ_sqsum) |
|
|
|
|
.run(2,globalsize,NULL,false); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
Vec4f templ_sum = Vec4f::all(0); |
|
|
|
|
Vec4f templ_sqsum = Vec4f::all(0); |
|
|
|
|
|
|
|
|
|
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 = sum(templ); |
|
|
|
|
|
|
|
|
|
templ_sum *= scale; |
|
|
|
|
multiply(templf, templf, temp); |
|
|
|
|
templ_sqsum = sum(temp); |
|
|
|
|
|
|
|
|
|
if (templ_sqsum_sum < DBL_EPSILON) |
|
|
|
|
float templ_sqsum_sum = 0; |
|
|
|
|
for(int i = 0; i < cn; i ++) |
|
|
|
|
{ |
|
|
|
|
result = Scalar::all(1); |
|
|
|
|
return true; |
|
|
|
|
templ_sqsum_sum += templ_sqsum[i] - scale * templ_sum[i] * templ_sum[i]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (cn==2) |
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadOnlyNoSize(image_sqsums), |
|
|
|
|
ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols, scale, |
|
|
|
|
templ_sum[0],templ_sum[1], templ_sqsum_sum) |
|
|
|
|
.run(2,globalsize,NULL,false); |
|
|
|
|
templ_sum *= scale; |
|
|
|
|
|
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadOnlyNoSize(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,NULL,false); |
|
|
|
|
if (templ_sqsum_sum < DBL_EPSILON) |
|
|
|
|
{ |
|
|
|
|
result = Scalar::all(1); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (cn==2) |
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadOnlyNoSize(image_sqsums), |
|
|
|
|
ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols, scale, |
|
|
|
|
templ_sum[0],templ_sum[1], templ_sqsum_sum) |
|
|
|
|
.run(2,globalsize,NULL,false); |
|
|
|
|
|
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadOnlyNoSize(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,NULL,false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
static bool ocl_matchTemplate( InputArray _img, InputArray _templ, OutputArray _result, int method) |
|
|
|
|
{ |
|
|
|
|
int cn = CV_MAT_CN(_img.type()); |
|
|
|
|
static bool ocl_matchTemplate( InputArray _img, InputArray _templ, OutputArray _result, int method) |
|
|
|
|
{ |
|
|
|
|
int cn = CV_MAT_CN(_img.type()); |
|
|
|
|
|
|
|
|
|
if (cn == 3 || cn > 4) |
|
|
|
|
return false; |
|
|
|
|
if (cn == 3 || cn > 4) |
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
typedef bool (*Caller)(InputArray _img, InputArray _templ, OutputArray _result); |
|
|
|
|
typedef bool (*Caller)(InputArray _img, InputArray _templ, OutputArray _result); |
|
|
|
|
|
|
|
|
|
const Caller callers[] = |
|
|
|
|
{ |
|
|
|
|
matchTemplate_SQDIFF, matchTemplate_SQDIFF_NORMED, matchTemplate_CCORR, |
|
|
|
|
matchTemplate_CCORR_NORMED, matchTemplate_CCOEFF, matchTemplate_CCOEFF_NORMED |
|
|
|
|
}; |
|
|
|
|
const Caller callers[] = |
|
|
|
|
{ |
|
|
|
|
matchTemplate_SQDIFF, matchTemplate_SQDIFF_NORMED, matchTemplate_CCORR, |
|
|
|
|
matchTemplate_CCORR_NORMED, matchTemplate_CCOEFF, matchTemplate_CCOEFF_NORMED |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Caller caller = callers[method]; |
|
|
|
|
Caller caller = callers[method]; |
|
|
|
|
|
|
|
|
|
return caller(_img, _templ, _result); |
|
|
|
|
} |
|
|
|
|
return caller(_img, _templ, _result); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
namespace cv |
|
|
|
|
{ |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
void crossCorr( const Mat& img, const Mat& _templ, Mat& corr, |
|
|
|
|
Size corrsize, int ctype, |
|
|
|
@ -564,9 +552,7 @@ 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.dims() <= 2); |
|
|
|
|
|
|
|
|
|
bool swapNotNeed = (_img.size().height >= _templ.size().height && _img.size().width >= _templ.size().width); |
|
|
|
@ -575,9 +561,8 @@ void cv::matchTemplate( InputArray _img, InputArray _templ, OutputArray _result, |
|
|
|
|
CV_Assert(_img.size().height <= _templ.size().height && _img.size().width <= _templ.size().width); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool use_opencl = ocl::useOpenCL() && _result.isUMat(); |
|
|
|
|
if ( use_opencl && (swapNotNeed ? ocl_matchTemplate(_img,_templ,_result,method) : ocl_matchTemplate(_templ,_img,_result,method))) |
|
|
|
|
return; |
|
|
|
|
CV_OCL_RUN(_img.dims() <= 2 && _result.isUMat(), |
|
|
|
|
(swapNotNeed ? ocl_matchTemplate(_img,_templ,_result,method) : ocl_matchTemplate(_templ,_img,_result,method))) |
|
|
|
|
|
|
|
|
|
int numType = method == CV_TM_CCORR || method == CV_TM_CCORR_NORMED ? 0 : |
|
|
|
|
method == CV_TM_CCOEFF || method == CV_TM_CCOEFF_NORMED ? 1 : 2; |
|
|
|
|