1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
namespace opencv_test { namespace {
CV_ENUM(MatchTemplType, CV_TM_CCORR, CV_TM_CCORR_NORMED,
CV_TM_SQDIFF, CV_TM_SQDIFF_NORMED,
CV_TM_CCOEFF, CV_TM_CCOEFF_NORMED)
class Imgproc_MatchTemplateWithMask : public TestWithParam<std::tuple<MatType,MatType>>
{
protected:
// Member functions inherited from ::testing::Test
void SetUp() override;
// Matrices for test calculations (always CV_32)
Mat img_;
Mat templ_;
Mat mask_;
Mat templ_masked_;
Mat img_roi_masked_;
// Matrices for call to matchTemplate (have test type)
Mat img_testtype_;
Mat templ_testtype_;
Mat mask_testtype_;
Mat result_;
// Constants
static const Size IMG_SIZE;
static const Size TEMPL_SIZE;
static const Point TEST_POINT;
};
// Arbitraryly chosen test constants
const Size Imgproc_MatchTemplateWithMask::IMG_SIZE(160, 100);
const Size Imgproc_MatchTemplateWithMask::TEMPL_SIZE(21, 13);
const Point Imgproc_MatchTemplateWithMask::TEST_POINT(8, 9);
void Imgproc_MatchTemplateWithMask::SetUp()
{
int type = std::get<0>(GetParam());
int type_mask = std::get<1>(GetParam());
// Matrices are created with the depth to test (for the call to matchTemplate()), but are also
// converted to CV_32 for the test calculations, because matchTemplate() also only operates on
// and returns CV_32.
img_testtype_.create(IMG_SIZE, type);
templ_testtype_.create(TEMPL_SIZE, type);
mask_testtype_.create(TEMPL_SIZE, type_mask);
randu(img_testtype_, 0, 10);
randu(templ_testtype_, 0, 10);
randu(mask_testtype_, 0, 5);
img_testtype_.convertTo(img_, CV_32F);
templ_testtype_.convertTo(templ_, CV_32F);
mask_testtype_.convertTo(mask_, CV_32F);
if (CV_MAT_DEPTH(type_mask) == CV_8U)
{
// CV_8U masks are interpreted as binary masks
mask_.setTo(Scalar::all(1), mask_ != 0);
}
if (mask_.channels() != templ_.channels())
{
std::vector<Mat> mask_channels(templ_.channels(), mask_);
merge(mask_channels.data(), templ_.channels(), mask_);
}
Rect roi(TEST_POINT, TEMPL_SIZE);
img_roi_masked_ = img_(roi).mul(mask_);
templ_masked_ = templ_.mul(mask_);
}
TEST_P(Imgproc_MatchTemplateWithMask, CompareNaiveImplSQDIFF)
{
matchTemplate(img_testtype_, templ_testtype_, result_, CV_TM_SQDIFF, mask_testtype_);
// Naive implementation for one point
Mat temp = img_roi_masked_ - templ_masked_;
Scalar temp_s = sum(temp.mul(temp));
double val = temp_s[0] + temp_s[1] + temp_s[2] + temp_s[3];
EXPECT_NEAR(val, result_.at<float>(TEST_POINT), TEMPL_SIZE.area()*abs(val)*FLT_EPSILON);
}
TEST_P(Imgproc_MatchTemplateWithMask, CompareNaiveImplSQDIFF_NORMED)
{
matchTemplate(img_testtype_, templ_testtype_, result_, CV_TM_SQDIFF_NORMED, mask_testtype_);
// Naive implementation for one point
Mat temp = img_roi_masked_ - templ_masked_;
Scalar temp_s = sum(temp.mul(temp));
double val = temp_s[0] + temp_s[1] + temp_s[2] + temp_s[3];
// Normalization
temp_s = sum(templ_masked_.mul(templ_masked_));
double norm = temp_s[0] + temp_s[1] + temp_s[2] + temp_s[3];
temp_s = sum(img_roi_masked_.mul(img_roi_masked_));
norm *= temp_s[0] + temp_s[1] + temp_s[2] + temp_s[3];
norm = sqrt(norm);
val /= norm;
EXPECT_NEAR(val, result_.at<float>(TEST_POINT), TEMPL_SIZE.area()*abs(val)*FLT_EPSILON);
}
TEST_P(Imgproc_MatchTemplateWithMask, CompareNaiveImplCCORR)
{
matchTemplate(img_testtype_, templ_testtype_, result_, CV_TM_CCORR, mask_testtype_);
// Naive implementation for one point
Scalar temp_s = sum(templ_masked_.mul(img_roi_masked_));
double val = temp_s[0] + temp_s[1] + temp_s[2] + temp_s[3];
EXPECT_NEAR(val, result_.at<float>(TEST_POINT), TEMPL_SIZE.area()*abs(val)*FLT_EPSILON);
}
TEST_P(Imgproc_MatchTemplateWithMask, CompareNaiveImplCCORR_NORMED)
{
matchTemplate(img_testtype_, templ_testtype_, result_, CV_TM_CCORR_NORMED, mask_testtype_);
// Naive implementation for one point
Scalar temp_s = sum(templ_masked_.mul(img_roi_masked_));
double val = temp_s[0] + temp_s[1] + temp_s[2] + temp_s[3];
// Normalization
temp_s = sum(templ_masked_.mul(templ_masked_));
double norm = temp_s[0] + temp_s[1] + temp_s[2] + temp_s[3];
temp_s = sum(img_roi_masked_.mul(img_roi_masked_));
norm *= temp_s[0] + temp_s[1] + temp_s[2] + temp_s[3];
norm = sqrt(norm);
val /= norm;
EXPECT_NEAR(val, result_.at<float>(TEST_POINT), TEMPL_SIZE.area()*abs(val)*FLT_EPSILON);
}
TEST_P(Imgproc_MatchTemplateWithMask, CompareNaiveImplCCOEFF)
{
matchTemplate(img_testtype_, templ_testtype_, result_, CV_TM_CCOEFF, mask_testtype_);
// Naive implementation for one point
Scalar temp_s = sum(mask_);
for (int i = 0; i < 4; i++)
{
if (temp_s[i] != 0.0)
temp_s[i] = 1.0 / temp_s[i];
else
temp_s[i] = 1.0;
}
Mat temp = mask_.clone(); temp = temp_s; // Workaround to multiply Mat by Scalar
Mat temp2 = mask_.clone(); temp2 = sum(templ_masked_); // Workaround to multiply Mat by Scalar
Mat templx = templ_masked_ - mask_.mul(temp).mul(temp2);
temp2 = sum(img_roi_masked_); // Workaround to multiply Mat by Scalar
Mat imgx = img_roi_masked_ - mask_.mul(temp).mul(temp2);
temp_s = sum(templx.mul(imgx));
double val = temp_s[0] + temp_s[1] + temp_s[2] + temp_s[3];
EXPECT_NEAR(val, result_.at<float>(TEST_POINT), TEMPL_SIZE.area()*abs(val)*FLT_EPSILON);
}
TEST_P(Imgproc_MatchTemplateWithMask, CompareNaiveImplCCOEFF_NORMED)
{
matchTemplate(img_testtype_, templ_testtype_, result_, CV_TM_CCOEFF_NORMED, mask_testtype_);
// Naive implementation for one point
Scalar temp_s = sum(mask_);
for (int i = 0; i < 4; i++)
{
if (temp_s[i] != 0.0)
temp_s[i] = 1.0 / temp_s[i];
else
temp_s[i] = 1.0;
}
Mat temp = mask_.clone(); temp = temp_s; // Workaround to multiply Mat by Scalar
Mat temp2 = mask_.clone(); temp2 = sum(templ_masked_); // Workaround to multiply Mat by Scalar
Mat templx = templ_masked_ - mask_.mul(temp).mul(temp2);
temp2 = sum(img_roi_masked_); // Workaround to multiply Mat by Scalar
Mat imgx = img_roi_masked_ - mask_.mul(temp).mul(temp2);
temp_s = sum(templx.mul(imgx));
double val = temp_s[0] + temp_s[1] + temp_s[2] + temp_s[3];
// Normalization
temp_s = sum(templx.mul(templx));
double norm = temp_s[0] + temp_s[1] + temp_s[2] + temp_s[3];
temp_s = sum(imgx.mul(imgx));
norm *= temp_s[0] + temp_s[1] + temp_s[2] + temp_s[3];
norm = sqrt(norm);
val /= norm;
EXPECT_NEAR(val, result_.at<float>(TEST_POINT), TEMPL_SIZE.area()*abs(val)*FLT_EPSILON);
}
INSTANTIATE_TEST_CASE_P(SingleChannelMask, Imgproc_MatchTemplateWithMask,
Combine(
Values(CV_32FC1, CV_32FC3, CV_8UC1, CV_8UC3),
Values(CV_32FC1, CV_8UC1)));
INSTANTIATE_TEST_CASE_P(MultiChannelMask, Imgproc_MatchTemplateWithMask,
Combine(
Values(CV_32FC3, CV_8UC3),
Values(CV_32FC3, CV_8UC3)));
class Imgproc_MatchTemplateWithMask2 : public TestWithParam<std::tuple<MatType,MatType,
MatchTemplType>>
{
protected:
// Member functions inherited from ::testing::Test
void SetUp() override;
// Data members
Mat img_;
Mat templ_;
Mat mask_;
Mat result_withoutmask_;
Mat result_withmask_;
// Constants
static const Size IMG_SIZE;
static const Size TEMPL_SIZE;
};
// Arbitraryly chosen test constants
const Size Imgproc_MatchTemplateWithMask2::IMG_SIZE(160, 100);
const Size Imgproc_MatchTemplateWithMask2::TEMPL_SIZE(21, 13);
void Imgproc_MatchTemplateWithMask2::SetUp()
{
int type = std::get<0>(GetParam());
int type_mask = std::get<1>(GetParam());
img_.create(IMG_SIZE, type);
templ_.create(TEMPL_SIZE, type);
mask_.create(TEMPL_SIZE, type_mask);
randu(img_, 0, 100);
randu(templ_, 0, 100);
if (CV_MAT_DEPTH(type_mask) == CV_8U)
{
// CV_8U implies binary mask, so all nonzero values should work
randu(mask_, 1, 255);
}
else
{
mask_ = Scalar(1, 1, 1, 1);
}
}
TEST_P(Imgproc_MatchTemplateWithMask2, CompareWithAndWithoutMask)
{
int method = std::get<2>(GetParam());
matchTemplate(img_, templ_, result_withmask_, method, mask_);
matchTemplate(img_, templ_, result_withoutmask_, method);
// Get maximum result for relative error calculation
double min_val, max_val;
minMaxLoc(abs(result_withmask_), &min_val, &max_val);
// Get maximum of absolute diff for comparison
double mindiff, maxdiff;
minMaxLoc(abs(result_withmask_ - result_withoutmask_), &mindiff, &maxdiff);
EXPECT_LT(maxdiff, max_val*TEMPL_SIZE.area()*FLT_EPSILON);
}
INSTANTIATE_TEST_CASE_P(SingleChannelMask, Imgproc_MatchTemplateWithMask2,
Combine(
Values(CV_32FC1, CV_32FC3, CV_8UC1, CV_8UC3),
Values(CV_32FC1, CV_8UC1),
Values(CV_TM_SQDIFF, CV_TM_SQDIFF_NORMED, CV_TM_CCORR, CV_TM_CCORR_NORMED,
CV_TM_CCOEFF, CV_TM_CCOEFF_NORMED)));
INSTANTIATE_TEST_CASE_P(MultiChannelMask, Imgproc_MatchTemplateWithMask2,
Combine(
Values(CV_32FC3, CV_8UC3),
Values(CV_32FC3, CV_8UC3),
Values(CV_TM_SQDIFF, CV_TM_SQDIFF_NORMED, CV_TM_CCORR, CV_TM_CCORR_NORMED,
CV_TM_CCOEFF, CV_TM_CCOEFF_NORMED)));
}} // namespace
|