From b5f251c8159ccff82f5342c74eb1c313fa98c024 Mon Sep 17 00:00:00 2001 From: Elena Gvozdeva Date: Thu, 7 Aug 2014 16:00:25 +0400 Subject: [PATCH] fixed test ocl_MatchTemplate for sparse matrix --- modules/imgproc/src/templmatch.cpp | 5 ++-- modules/imgproc/test/ocl/test_match_template.cpp | 21 +++++++++-------- modules/ts/include/opencv2/ts/ocl_test.hpp | 29 ++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/modules/imgproc/src/templmatch.cpp b/modules/imgproc/src/templmatch.cpp index df2e19c..33c1e15 100644 --- a/modules/imgproc/src/templmatch.cpp +++ b/modules/imgproc/src/templmatch.cpp @@ -454,14 +454,15 @@ static bool matchTemplate_CCOEFF(InputArray _image, InputArray _templ, OutputArr if (cn==1) { - float templ_sum = static_cast(sum(_templ)[0]) / tsize.area(); + Scalar templMean = mean(templ); + float templ_sum = (float)templMean[0]; k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadWrite(result), templ.rows, templ.cols, templ_sum); } else { Vec4f templ_sum = Vec4f::all(0); - templ_sum = sum(templ) / tsize.area(); + templ_sum = (Vec4f)mean(templ); k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadWrite(result), templ.rows, templ.cols, templ_sum); } diff --git a/modules/imgproc/test/ocl/test_match_template.cpp b/modules/imgproc/test/ocl/test_match_template.cpp index 1d1352a..6cf0fe4 100644 --- a/modules/imgproc/test/ocl/test_match_template.cpp +++ b/modules/imgproc/test/ocl/test_match_template.cpp @@ -97,9 +97,17 @@ PARAM_TEST_CASE(MatchTemplate, MatDepth, Channels, MatchTemplType, bool) UMAT_UPLOAD_OUTPUT_PARAMETER(result); } - void Near(double threshold = 0.0) + void Near() { - OCL_EXPECT_MATS_NEAR(result, threshold); + bool isNormed = + method == TM_CCORR_NORMED || + method == TM_SQDIFF_NORMED || + method == TM_CCOEFF_NORMED; + + if (isNormed) + OCL_EXPECT_MATS_NEAR(result, 3e-2); + else + OCL_EXPECT_MATS_NEAR_RELATIVE_SPARSE(result, 1.5e-2); } }; @@ -112,14 +120,7 @@ OCL_TEST_P(MatchTemplate, Mat) OCL_OFF(cv::matchTemplate(image_roi, templ_roi, result_roi, method)); OCL_ON(cv::matchTemplate(uimage_roi, utempl_roi, uresult_roi, method)); - bool isNormed = - method == TM_CCORR_NORMED || - method == TM_SQDIFF_NORMED || - method == TM_CCOEFF_NORMED; - - double eps = isNormed ? 3e-2 : 255.0 * 255.0 * templ.total() * 2e-5; - - Near(eps); + Near(); } } diff --git a/modules/ts/include/opencv2/ts/ocl_test.hpp b/modules/ts/include/opencv2/ts/ocl_test.hpp index 3703b7b..559f4aa 100644 --- a/modules/ts/include/opencv2/ts/ocl_test.hpp +++ b/modules/ts/include/opencv2/ts/ocl_test.hpp @@ -159,6 +159,25 @@ do \ << "Size: " << name ## _roi.size() << std::endl; \ } while ((void)0, 0) +//for sparse matrix +#define OCL_EXPECT_MATS_NEAR_RELATIVE_SPARSE(name, eps) \ +do \ +{ \ + ASSERT_EQ(name ## _roi.type(), u ## name ## _roi.type()); \ + ASSERT_EQ(name ## _roi.size(), u ## name ## _roi.size()); \ + EXPECT_LE(TestUtils::checkNormRelativeSparse(name ## _roi, u ## name ## _roi), eps) \ + << "Size: " << name ## _roi.size() << std::endl; \ + Point _offset; \ + Size _wholeSize; \ + name ## _roi.locateROI(_wholeSize, _offset); \ + Mat _mask(name.size(), CV_8UC1, Scalar::all(255)); \ + _mask(Rect(_offset, name ## _roi.size())).setTo(Scalar::all(0)); \ + ASSERT_EQ(name.type(), u ## name.type()); \ + ASSERT_EQ(name.size(), u ## name.size()); \ + EXPECT_LE(TestUtils::checkNormRelativeSparse(name, u ## name, _mask), eps) \ + << "Size: " << name ## _roi.size() << std::endl; \ +} while ((void)0, 0) + #define EXPECT_MAT_SIMILAR(mat1, mat2, eps) \ do \ { \ @@ -274,6 +293,16 @@ struct CV_EXPORTS TestUtils std::max((double)std::numeric_limits::epsilon(), (double)std::max(cvtest::norm(m1.getMat(), cv::NORM_INF), cvtest::norm(m2.getMat(), cv::NORM_INF))); } + + static inline double checkNormRelativeSparse(InputArray m1, InputArray m2, InputArray mask = noArray()) + { + double norm_inf = cvtest::norm(m1.getMat(), m2.getMat(), cv::NORM_INF, mask); + double norm_rel = norm_inf / + std::max((double)std::numeric_limits::epsilon(), + (double)std::max(cvtest::norm(m1.getMat(), cv::NORM_INF), cvtest::norm(m2.getMat(), cv::NORM_INF))); + return std::min(norm_inf, norm_rel); + } + }; #define TEST_DECLARE_INPUT_PARAMETER(name) Mat name, name ## _roi; UMat u ## name, u ## name ## _roi -- 2.7.4