|
|
|
@ -49,7 +49,7 @@ namespace cv |
|
|
|
|
|
|
|
|
|
#ifdef HAVE_OPENCL |
|
|
|
|
|
|
|
|
|
static bool useNaive(int method, int depth, Size size) |
|
|
|
|
static bool useNaive(int method, int depth, const Size & size) |
|
|
|
|
{ |
|
|
|
|
#ifdef HAVE_CLAMDFFT |
|
|
|
|
if (method == TM_SQDIFF && depth == CV_32F) |
|
|
|
@ -59,135 +59,128 @@ static bool useNaive(int method, int depth, Size size) |
|
|
|
|
else |
|
|
|
|
return false; |
|
|
|
|
#else |
|
|
|
|
#define UNUSED(x) (void)(x); |
|
|
|
|
UNUSED(method) UNUSED(depth) UNUSED(size) |
|
|
|
|
#undef UNUSED |
|
|
|
|
return true; |
|
|
|
|
(void)(method); |
|
|
|
|
(void)(depth); |
|
|
|
|
(void)(size); |
|
|
|
|
return true; |
|
|
|
|
#endif |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////// CCORR //////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
static bool matchTemplateNaive_CCORR (InputArray _image, InputArray _templ, OutputArray _result, int cn) |
|
|
|
|
static bool matchTemplateNaive_CCORR(InputArray _image, InputArray _templ, OutputArray _result) |
|
|
|
|
{ |
|
|
|
|
int type = _image.type(); |
|
|
|
|
int depth = CV_MAT_DEPTH(type); |
|
|
|
|
|
|
|
|
|
const char * kernelName = "matchTemplate_Naive_CCORR"; |
|
|
|
|
int type = _image.type(), 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)); |
|
|
|
|
ocl::Kernel k("matchTemplate_Naive_CCORR", 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; |
|
|
|
|
UMat image = _image.getUMat(), templ = _templ.getUMat(); |
|
|
|
|
_result.create(image.rows - templ.rows + 1, image.cols - templ.cols + 1, CV_32F); |
|
|
|
|
result = _result.getUMat(); |
|
|
|
|
UMat result = _result.getUMat(); |
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
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) |
|
|
|
|
{ |
|
|
|
|
matchTemplate(_image, _templ, _result, CV_TM_CCORR); |
|
|
|
|
|
|
|
|
|
int type = _image.type(); |
|
|
|
|
int depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type); |
|
|
|
|
|
|
|
|
|
const char * kernelName = "matchTemplate_CCORR_NORMED"; |
|
|
|
|
int type = _image.type(), 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)); |
|
|
|
|
ocl::Kernel k("matchTemplate_CCORR_NORMED", 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; |
|
|
|
|
UMat image = _image.getUMat(), templ = _templ.getUMat(); |
|
|
|
|
_result.create(image.rows - templ.rows + 1, image.cols - templ.cols + 1, CV_32F); |
|
|
|
|
result = _result.getUMat(); |
|
|
|
|
UMat result = _result.getUMat(); |
|
|
|
|
|
|
|
|
|
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 temp; |
|
|
|
|
multiply(templ, templ, temp, 1, CV_32F); |
|
|
|
|
Scalar s = sum(temp); |
|
|
|
|
float templ_sqsum = 0; |
|
|
|
|
for (int i = 0; i < cn; ++i) |
|
|
|
|
templ_sqsum += static_cast<float>(s[i]); |
|
|
|
|
|
|
|
|
|
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}; |
|
|
|
|
|
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sqsums), ocl::KernelArg::WriteOnly(result), templ.rows, templ.cols, templ_sqsum).run(2,globalsize,NULL,false); |
|
|
|
|
size_t globalsize[2] = { result.cols, result.rows }; |
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sqsums), ocl::KernelArg::ReadWrite(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()); |
|
|
|
|
return matchTemplateNaive_CCORR(_image, _templ, _result); |
|
|
|
|
else |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
////////////////////////////////////// SQDIFF //////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
static bool matchTemplateNaive_SQDIFF(InputArray _image, InputArray _templ, OutputArray _result, int cn) |
|
|
|
|
static bool matchTemplateNaive_SQDIFF(InputArray _image, InputArray _templ, OutputArray _result) |
|
|
|
|
{ |
|
|
|
|
int type = _image.type(); |
|
|
|
|
int depth = CV_MAT_DEPTH(type); |
|
|
|
|
int type = _image.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type); |
|
|
|
|
|
|
|
|
|
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)); |
|
|
|
|
ocl::Kernel k("matchTemplate_Naive_SQDIFF", 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; |
|
|
|
|
UMat image = _image.getUMat(), templ = _templ.getUMat(); |
|
|
|
|
_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}; |
|
|
|
|
UMat result = _result.getUMat(); |
|
|
|
|
|
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image), ocl::KernelArg::ReadOnly(templ), ocl::KernelArg::WriteOnly(result)).run(2,globalsize,NULL,false); |
|
|
|
|
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(); |
|
|
|
|
int depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type); |
|
|
|
|
int type = _image.type(), 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)); |
|
|
|
|
ocl::Kernel k("matchTemplate_SQDIFF_NORMED", 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; |
|
|
|
|
UMat image = _image.getUMat(), templ = _templ.getUMat(); |
|
|
|
|
_result.create(image.rows - templ.rows + 1, image.cols - templ.cols + 1, CV_32F); |
|
|
|
|
result = _result.getUMat(); |
|
|
|
|
UMat result = _result.getUMat(); |
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
|
|
|
|
|
multiply(templ_resh, templ_resh, temp); |
|
|
|
|
unsigned long long templ_sqsum = (unsigned long long)sum(temp)[0]; |
|
|
|
|
UMat temp; |
|
|
|
|
multiply(templ, templ, temp, 1, CV_32F); |
|
|
|
|
Scalar s = sum(temp); |
|
|
|
|
float templ_sqsum = 0; |
|
|
|
|
for (int i = 0; i < cn; ++i) |
|
|
|
|
templ_sqsum += (float)s[i]; |
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
size_t globalsize[2] = { result.cols, result.rows }; |
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sqsums), ocl::KernelArg::ReadWrite(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()); |
|
|
|
|
return matchTemplateNaive_SQDIFF(_image, _templ, _result); |
|
|
|
|
else |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
@ -201,91 +194,66 @@ static bool matchTemplate_CCOEFF(InputArray _image, InputArray _templ, OutputArr |
|
|
|
|
UMat image_sums; |
|
|
|
|
integral(_image, image_sums); |
|
|
|
|
|
|
|
|
|
int type = image_sums.type(); |
|
|
|
|
int depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type); |
|
|
|
|
|
|
|
|
|
const char * kernelName; |
|
|
|
|
int type = image_sums.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type); |
|
|
|
|
|
|
|
|
|
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)); |
|
|
|
|
ocl::Kernel k(cv::format("matchTemplate_Prepared_CCOEFF_C%d", cn).c_str(), 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(); |
|
|
|
|
UMat templ = _templ.getUMat(); |
|
|
|
|
Size size = _image.size(), tsize = templ.size(); |
|
|
|
|
_result.create(size.height - templ.rows + 1, size.width - templ.cols + 1, CV_32F); |
|
|
|
|
result = _result.getUMat(); |
|
|
|
|
UMat result = _result.getUMat(); |
|
|
|
|
|
|
|
|
|
size_t globalsize[2] = {result.cols, result.rows}; |
|
|
|
|
size_t globalsize[2] = { result.cols, result.rows }; |
|
|
|
|
|
|
|
|
|
if (cn==1) |
|
|
|
|
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); |
|
|
|
|
float templ_sum = static_cast<float>(sum(_templ)[0]) / tsize.area(); |
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadWrite(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); |
|
|
|
|
templ_sum = sum(templ) / tsize.area(); |
|
|
|
|
if (cn == 2) |
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadWrite(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); |
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadWrite(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) |
|
|
|
|
{ |
|
|
|
|
UMat imagef, templf; |
|
|
|
|
|
|
|
|
|
_image.getUMat().convertTo(imagef, CV_32F); |
|
|
|
|
_templ.getUMat().convertTo(templf, CV_32F); |
|
|
|
|
|
|
|
|
|
matchTemplate(imagef, templf, _result, CV_TM_CCORR); |
|
|
|
|
|
|
|
|
|
const char * kernelName; |
|
|
|
|
matchTemplate(_image, _templ, _result, CV_TM_CCORR); |
|
|
|
|
|
|
|
|
|
UMat temp, image_sums, image_sqsums; |
|
|
|
|
integral(_image,image_sums, image_sqsums, CV_32F, CV_32F); |
|
|
|
|
integral(_image, image_sums, image_sqsums, CV_32F, CV_32F); |
|
|
|
|
|
|
|
|
|
int type = 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"; |
|
|
|
|
int type = image_sums.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type); |
|
|
|
|
|
|
|
|
|
ocl::Kernel k(kernelName, ocl::imgproc::match_template_oclsrc, |
|
|
|
|
ocl::Kernel k(format("matchTemplate_CCOEFF_NORMED_C%d", cn).c_str(), 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; |
|
|
|
|
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(); |
|
|
|
|
|
|
|
|
|
size_t globalsize[2] = {result.cols, result.rows}; |
|
|
|
|
UMat templ = _templ.getUMat(); |
|
|
|
|
Size size = _image.size(), tsize = templ.size(); |
|
|
|
|
_result.create(size.height - templ.rows + 1, size.width - templ.cols + 1, CV_32F); |
|
|
|
|
UMat result = _result.getUMat(); |
|
|
|
|
|
|
|
|
|
float scale = 1.f / templ.size().area(); |
|
|
|
|
size_t globalsize[2] = { result.cols, result.rows }; |
|
|
|
|
float scale = 1.f / tsize.area(); |
|
|
|
|
|
|
|
|
|
if (cn==1) |
|
|
|
|
if (cn == 1) |
|
|
|
|
{ |
|
|
|
|
float templ_sum = (float)sum(templ)[0]; |
|
|
|
|
|
|
|
|
|
multiply(templf, templf, temp); |
|
|
|
|
multiply(templ, templ, temp, 1, CV_32F); |
|
|
|
|
float templ_sqsum = (float)sum(temp)[0]; |
|
|
|
|
|
|
|
|
|
templ_sqsum -= scale * templ_sum * templ_sum; |
|
|
|
@ -297,27 +265,23 @@ static bool matchTemplate_CCOEFF_NORMED(InputArray _image, InputArray _templ, Ou |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadOnlyNoSize(image_sqsums), |
|
|
|
|
ocl::KernelArg::ReadWrite(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); |
|
|
|
|
|
|
|
|
|
Vec4f templ_sum = Vec4f::all(0), templ_sqsum = Vec4f::all(0); |
|
|
|
|
templ_sum = sum(templ); |
|
|
|
|
|
|
|
|
|
multiply(templf, templf, temp); |
|
|
|
|
multiply(templ, templ, temp, 1, CV_32F); |
|
|
|
|
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]; |
|
|
|
|
} |
|
|
|
|
for (int i = 0; i < cn; i ++) |
|
|
|
|
templ_sqsum_sum += templ_sqsum[i] - scale * templ_sum[i] * templ_sum[i]; |
|
|
|
|
|
|
|
|
|
templ_sum *= scale; |
|
|
|
|
templ_sum *= scale; |
|
|
|
|
|
|
|
|
|
if (templ_sqsum_sum < DBL_EPSILON) |
|
|
|
|
{ |
|
|
|
@ -325,38 +289,35 @@ static bool matchTemplate_CCOEFF_NORMED(InputArray _image, InputArray _templ, Ou |
|
|
|
|
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); |
|
|
|
|
if (cn == 2) |
|
|
|
|
return k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadOnlyNoSize(image_sqsums), |
|
|
|
|
ocl::KernelArg::ReadWrite(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); |
|
|
|
|
ocl::KernelArg::ReadWrite(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()); |
|
|
|
|
int cn = _img.channels(); |
|
|
|
|
|
|
|
|
|
if (cn == 3 || cn > 4) |
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
typedef bool (*Caller)(InputArray _img, InputArray _templ, OutputArray _result); |
|
|
|
|
|
|
|
|
|
const Caller callers[] = |
|
|
|
|
static const Caller callers[] = |
|
|
|
|
{ |
|
|
|
|
matchTemplate_SQDIFF, matchTemplate_SQDIFF_NORMED, matchTemplate_CCORR, |
|
|
|
|
matchTemplate_CCORR_NORMED, matchTemplate_CCOEFF, matchTemplate_CCOEFF_NORMED |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Caller caller = callers[method]; |
|
|
|
|
const Caller caller = callers[method]; |
|
|
|
|
|
|
|
|
|
return caller(_img, _templ, _result); |
|
|
|
|
} |
|
|
|
@ -552,17 +513,16 @@ 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); |
|
|
|
|
CV_Assert( (_img.depth() == CV_8U || _img.depth() == CV_32F) && _img.type() == _templ.type() && _img.dims() <= 2 ); |
|
|
|
|
|
|
|
|
|
bool swapNotNeed = (_img.size().height >= _templ.size().height && _img.size().width >= _templ.size().width); |
|
|
|
|
if (!swapNotNeed) |
|
|
|
|
bool needswap = _img.size().height < _templ.size().height || _img.size().width < _templ.size().width; |
|
|
|
|
if (needswap) |
|
|
|
|
{ |
|
|
|
|
CV_Assert(_img.size().height <= _templ.size().height && _img.size().width <= _templ.size().width); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
CV_OCL_RUN(_img.dims() <= 2 && _result.isUMat(), |
|
|
|
|
(swapNotNeed ? ocl_matchTemplate(_img,_templ,_result,method) : ocl_matchTemplate(_templ,_img,_result,method))) |
|
|
|
|
(!needswap ? 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; |
|
|
|
@ -571,7 +531,7 @@ void cv::matchTemplate( InputArray _img, InputArray _templ, OutputArray _result, |
|
|
|
|
method == CV_TM_CCOEFF_NORMED; |
|
|
|
|
|
|
|
|
|
Mat img = _img.getMat(), templ = _templ.getMat(); |
|
|
|
|
if(!swapNotNeed ) |
|
|
|
|
if (needswap) |
|
|
|
|
std::swap(img, templ); |
|
|
|
|
|
|
|
|
|
Size corrSize(img.cols - templ.cols + 1, img.rows - templ.rows + 1); |
|
|
|
|