|
|
|
@ -42,23 +42,15 @@ |
|
|
|
|
#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); |
|
|
|
|
#ifdef HAVE_OPENCL |
|
|
|
|
|
|
|
|
|
static bool matchTemplateNaive_CCORR (InputArray _image, InputArray _templ, OutputArray _result, int cn); |
|
|
|
|
static bool matchTemplateNaive_SQDIFF(InputArray _image, InputArray _templ, OutputArray _result, int cn); |
|
|
|
|
|
|
|
|
|
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; |
|
|
|
@ -72,20 +64,12 @@ namespace cv |
|
|
|
|
#undef UNUSED |
|
|
|
|
return true; |
|
|
|
|
#endif |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////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; |
|
|
|
|
} |
|
|
|
|
/////////////////////////////////////////////////// CCORR //////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
static bool matchTemplateNaive_CCORR (InputArray _image, InputArray _templ, OutputArray _result, int cn) |
|
|
|
|
{ |
|
|
|
|
static bool matchTemplateNaive_CCORR (InputArray _image, InputArray _templ, OutputArray _result, int cn) |
|
|
|
|
{ |
|
|
|
|
int type = _image.type(); |
|
|
|
|
int depth = CV_MAT_DEPTH(type); |
|
|
|
|
|
|
|
|
@ -103,10 +87,10 @@ namespace cv |
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static bool matchTemplate_CCORR_NORMED(InputArray _image, InputArray _templ, OutputArray _result) |
|
|
|
|
{ |
|
|
|
|
static bool matchTemplate_CCORR_NORMED(InputArray _image, InputArray _templ, OutputArray _result) |
|
|
|
|
{ |
|
|
|
|
matchTemplate(_image, _templ, _result, CV_TM_CCORR); |
|
|
|
|
|
|
|
|
|
int type = _image.type(); |
|
|
|
@ -135,22 +119,20 @@ namespace cv |
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//////////////////////////////////////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());; |
|
|
|
|
} |
|
|
|
|
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_SQDIFF(InputArray _image, InputArray _templ, OutputArray _result, int cn) |
|
|
|
|
{ |
|
|
|
|
////////////////////////////////////// SQDIFF //////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
static bool matchTemplateNaive_SQDIFF(InputArray _image, InputArray _templ, OutputArray _result, int cn) |
|
|
|
|
{ |
|
|
|
|
int type = _image.type(); |
|
|
|
|
int depth = CV_MAT_DEPTH(type); |
|
|
|
|
|
|
|
|
@ -168,10 +150,10 @@ namespace cv |
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static bool matchTemplate_SQDIFF_NORMED (InputArray _image, InputArray _templ, OutputArray _result) |
|
|
|
|
{ |
|
|
|
|
static bool matchTemplate_SQDIFF_NORMED (InputArray _image, InputArray _templ, OutputArray _result) |
|
|
|
|
{ |
|
|
|
|
matchTemplate(_image, _templ, _result, CV_TM_CCORR); |
|
|
|
|
|
|
|
|
|
int type = _image.type(); |
|
|
|
@ -200,12 +182,20 @@ namespace cv |
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
{ |
|
|
|
|
static bool matchTemplate_CCOEFF(InputArray _image, InputArray _templ, OutputArray _result) |
|
|
|
|
{ |
|
|
|
|
matchTemplate(_image, _templ, _result, CV_TM_CCORR); |
|
|
|
|
|
|
|
|
|
UMat image_sums; |
|
|
|
@ -250,10 +240,10 @@ namespace cv |
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static bool matchTemplate_CCOEFF_NORMED(InputArray _image, InputArray _templ, OutputArray _result) |
|
|
|
|
{ |
|
|
|
|
static bool matchTemplate_CCOEFF_NORMED(InputArray _image, InputArray _templ, OutputArray _result) |
|
|
|
|
{ |
|
|
|
|
UMat imagef, templf; |
|
|
|
|
|
|
|
|
|
_image.getUMat().convertTo(imagef, CV_32F); |
|
|
|
@ -347,12 +337,12 @@ namespace cv |
|
|
|
|
.run(2,globalsize,NULL,false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
static bool ocl_matchTemplate( InputArray _img, InputArray _templ, OutputArray _result, int method) |
|
|
|
|
{ |
|
|
|
|
static bool ocl_matchTemplate( InputArray _img, InputArray _templ, OutputArray _result, int method) |
|
|
|
|
{ |
|
|
|
|
int cn = CV_MAT_CN(_img.type()); |
|
|
|
|
|
|
|
|
|
if (cn == 3 || cn > 4) |
|
|
|
@ -369,11 +359,9 @@ namespace cv |
|
|
|
|
Caller caller = callers[method]; |
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|