1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 //
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
20 //
21 //   * Redistribution's of source code must retain the above copyright notice,
22 //     this list of conditions and the following disclaimer.
23 //
24 //   * Redistribution's in binary form must reproduce the above copyright notice,
25 //     this list of conditions and the following disclaimer in the documentation
26 //     and/or other materials provided with the distribution.
27 //
28 //   * The name of the copyright holders may not be used to endorse or promote products
29 //     derived from this software without specific prior written permission.
30 //
31 // This software is provided by the copyright holders and contributors as is and
32 // any express or implied warranties, including, but not limited to, the implied
33 // warranties of merchantability and fitness for a particular purpose are disclaimed.
34 // In no event shall the Intel Corporation or contributors be liable for any direct,
35 // indirect, incidental, special, exemplary, or consequential damages
36 // (including, but not limited to, procurement of substitute goods or services;
37 // loss of use, data, or profits; or business interruption) however caused
38 // and on any theory of liability, whether in contract, strict liability,
39 // or tort (including negligence or otherwise) arising in any way out of
40 // the use of this software, even if advised of the possibility of such damage.
41 //
42 //M*/
43 
44 #include "../test_precomp.hpp"
45 #include "opencv2/ts/ocl_test.hpp"
46 #include "iostream"
47 #include "fstream"
48 
49 #ifdef HAVE_OPENCL
50 
51 namespace cvtest {
52 namespace ocl {
53 
54 ///////////////////////////////////////////// matchTemplate //////////////////////////////////////////////////////////
55 
CV_ENUM(MatchTemplType,CV_TM_CCORR,CV_TM_CCORR_NORMED,CV_TM_SQDIFF,CV_TM_SQDIFF_NORMED,CV_TM_CCOEFF,CV_TM_CCOEFF_NORMED)56 CV_ENUM(MatchTemplType, CV_TM_CCORR, CV_TM_CCORR_NORMED, CV_TM_SQDIFF, CV_TM_SQDIFF_NORMED, CV_TM_CCOEFF, CV_TM_CCOEFF_NORMED)
57 
58 PARAM_TEST_CASE(MatchTemplate, MatDepth, Channels, MatchTemplType, bool)
59 {
60     int type;
61     int depth;
62     int method;
63     bool use_roi;
64 
65     TEST_DECLARE_INPUT_PARAMETER(image);
66     TEST_DECLARE_INPUT_PARAMETER(templ);
67     TEST_DECLARE_OUTPUT_PARAMETER(result);
68 
69     virtual void SetUp()
70     {
71         type = CV_MAKE_TYPE(GET_PARAM(0), GET_PARAM(1));
72         depth = GET_PARAM(0);
73         method = GET_PARAM(2);
74         use_roi = GET_PARAM(3);
75     }
76 
77     virtual void generateTestData()
78     {
79         Size image_roiSize = randomSize(2, 100);
80         Size templ_roiSize = Size(randomInt(1, image_roiSize.width), randomInt(1, image_roiSize.height));
81         Size result_roiSize = Size(image_roiSize.width - templ_roiSize.width + 1,
82                                    image_roiSize.height - templ_roiSize.height + 1);
83 
84         const double upValue = 256;
85 
86         Border imageBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
87         randomSubMat(image, image_roi, image_roiSize, imageBorder, type, -upValue, upValue);
88 
89         Border templBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
90         randomSubMat(templ, templ_roi, templ_roiSize, templBorder, type, -upValue, upValue);
91 
92         Border resultBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
93         randomSubMat(result, result_roi, result_roiSize, resultBorder, CV_32FC1, -upValue, upValue);
94 
95         UMAT_UPLOAD_INPUT_PARAMETER(image);
96         UMAT_UPLOAD_INPUT_PARAMETER(templ);
97         UMAT_UPLOAD_OUTPUT_PARAMETER(result);
98     }
99 
100     void Near()
101     {
102         bool isNormed =
103         method == TM_CCORR_NORMED ||
104         method == TM_SQDIFF_NORMED ||
105         method == TM_CCOEFF_NORMED;
106 
107         if (isNormed)
108             OCL_EXPECT_MATS_NEAR(result, 3e-2);
109         else
110             OCL_EXPECT_MATS_NEAR_RELATIVE_SPARSE(result, 1.5e-2);
111     }
112 };
113 
OCL_TEST_P(MatchTemplate,Mat)114 OCL_TEST_P(MatchTemplate, Mat)
115 {
116     for (int j = 0; j < test_loop_times; j++)
117     {
118         generateTestData();
119 
120         OCL_OFF(cv::matchTemplate(image_roi, templ_roi, result_roi, method));
121         OCL_ON(cv::matchTemplate(uimage_roi, utempl_roi, uresult_roi, method));
122 
123         Near();
124     }
125 }
126 
127 OCL_INSTANTIATE_TEST_CASE_P(ImageProc, MatchTemplate, Combine(
128                                 Values(CV_8U, CV_32F),
129                                 Values(1, 2, 3, 4),
130                                 MatchTemplType::all(),
131                                 Bool())
132                            );
133 } } // namespace cvtest::ocl
134 
135 #endif
136