some fixes and improvements in cv::matchTemplate
authorIlya Lavrenov <ilya.lavrenov@itseez.com>
Sat, 1 Feb 2014 19:34:54 +0000 (23:34 +0400)
committerIlya Lavrenov <ilya.lavrenov@itseez.com>
Sun, 2 Feb 2014 15:20:37 +0000 (19:20 +0400)
modules/core/include/opencv2/core/opencl/ocl_defs.hpp
modules/imgproc/perf/opencl/perf_matchTemplate.cpp
modules/imgproc/src/opencl/match_template.cl
modules/imgproc/src/templmatch.cpp
modules/imgproc/test/ocl/test_match_template.cpp

index 76d4f84..abf2eaf 100644 (file)
@@ -5,8 +5,6 @@
 // Copyright (C) 2014, Advanced Micro Devices, Inc., all rights reserved.
 // Third party copyrights are property of their respective owners.
 
-#define CV_OPENCL_RUN_ASSERT
-
 #ifdef HAVE_OPENCL
 
 #ifdef CV_OPENCL_RUN_VERBOSE
index 9487747..ab906db 100644 (file)
@@ -54,4 +54,4 @@ namespace ocl {
 }
 }
 
-#endif // HAVE_OPENCL
\ No newline at end of file
+#endif // HAVE_OPENCL
index 451ec0c..123c1d6 100644 (file)
@@ -98,7 +98,7 @@ __kernel void matchTemplate_Naive_CCORR (__global const uchar * img,int img_step
 
 __kernel void matchTemplate_CCORR_NORMED ( __global const uchar * img_sqsums, int img_sqsums_step, int img_sqsums_offset,
                                            __global uchar * res, int res_step, int res_offset, int res_rows, int res_cols,
-                                           int tpl_rows, int tpl_cols, ulong tpl_sqsum)
+                                           int tpl_rows, int tpl_cols, float tpl_sqsum)
 {
     int gidx = get_global_id(0);
     int gidy = get_global_id(1);
@@ -157,7 +157,7 @@ __kernel void matchTemplate_Naive_SQDIFF(__global const uchar * img,int img_step
 
 __kernel void matchTemplate_SQDIFF_NORMED ( __global const uchar * img_sqsums, int img_sqsums_step, int img_sqsums_offset,
                                             __global uchar * res, int res_step, int res_offset, int res_rows, int res_cols,
-                                            int tpl_rows, int tpl_cols, ulong tpl_sqsum)
+                                            int tpl_rows, int tpl_cols, float tpl_sqsum)
 {
     int gidx = get_global_id(0);
     int gidy = get_global_id(1);
@@ -394,4 +394,4 @@ __kernel void matchTemplate_CCOEFF_NORMED_C4 (__global const uchar * img_sums, i
         __global float * result = (__global float *)(res+res_idx);
         *result = normAcc((*result) - num, denum);
     }
-}
\ No newline at end of file
+}
index 0242068..b5dbba9 100644 (file)
@@ -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);
index 782509c..507f0a7 100644 (file)
 namespace cvtest {
 namespace ocl {
 
-/////////////////////////////////////////////matchTemplate//////////////////////////////////////////////////////////
+///////////////////////////////////////////// matchTemplate //////////////////////////////////////////////////////////
 
-PARAM_TEST_CASE(MatchTemplate, MatDepth, Channels, int, bool)
+CV_ENUM(MatchTemplType, CV_TM_SQDIFF, CV_TM_SQDIFF_NORMED, CV_TM_CCORR,
+        CV_TM_CCORR_NORMED, CV_TM_CCOEFF, CV_TM_CCOEFF_NORMED)
+
+PARAM_TEST_CASE(MatchTemplate, MatDepth, Channels, MatchTemplType, bool)
 {
     int type;
     int depth;
@@ -88,7 +91,7 @@ PARAM_TEST_CASE(MatchTemplate, MatDepth, Channels, int, bool)
         randomSubMat(templ, templ_roi, templ_roiSize, templBorder, type, -upValue, upValue);
 
         Border resultBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
-        randomSubMat(result, result_roi, result_roiSize, resultBorder, CV_32F, -upValue, upValue);
+        randomSubMat(result, result_roi, result_roiSize, resultBorder, CV_32FC1, -upValue, upValue);
 
         UMAT_UPLOAD_INPUT_PARAMETER(image)
         UMAT_UPLOAD_INPUT_PARAMETER(templ)
@@ -97,7 +100,7 @@ PARAM_TEST_CASE(MatchTemplate, MatDepth, Channels, int, bool)
 
     void Near(double threshold = 0.0)
     {
-        OCL_EXPECT_MATS_NEAR(result,threshold);
+        OCL_EXPECT_MATS_NEAR_RELATIVE(result, threshold);
     }
 };
 
@@ -107,22 +110,19 @@ OCL_TEST_P(MatchTemplate, Mat)
     {
         generateTestData();
 
-        OCL_OFF(cv::matchTemplate(image_roi,templ_roi,result_roi, method));
-        OCL_ON(cv::matchTemplate(uimage_roi,utempl_roi,uresult_roi, method));
+        OCL_OFF(cv::matchTemplate(image_roi, templ_roi, result_roi, method));
+        OCL_ON(cv::matchTemplate(uimage_roi, utempl_roi, uresult_roi, method));
 
-       if (method == 0)
-           Near(10.0f);
-       else
-           Near(method % 2 == 1 ? 0.001f : 1.0f);
+        Near(1.5e-4);
     }
 }
 
 OCL_INSTANTIATE_TEST_CASE_P(ImageProc, MatchTemplate, Combine(
                                 Values(CV_8U, CV_32F),
                                 Values(1, 2, 4),
-                                Values(0,1,2,3,4,5),
+                                MatchTemplType::all(),
                                 Bool())
                            );
 } } // namespace cvtest::ocl
 
-#endif
\ No newline at end of file
+#endif