CLAHE Python bindings
[profile/ivi/opencv.git] / modules / ocl / test / test_match_template.cpp
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 // @Authors
18 //    Peng Xiao, pengxiao@multicorewareinc.com
19 // Redistribution and use in source and binary forms, with or without modification,
20 // are permitted provided that the following conditions are met:
21 //
22 //   * Redistribution's of source code must retain the above copyright notice,
23 //     this list of conditions and the following disclaimer.
24 //
25 //   * Redistribution's in binary form must reproduce the above copyright notice,
26 //     this list of conditions and the following disclaimer in the documentation
27 //     and/or other oclMaterials provided with the distribution.
28 //
29 //   * The name of the copyright holders may not be used to endorse or promote products
30 //     derived from this software without specific prior written permission.
31 //
32 // This software is provided by the copyright holders and contributors as is and
33 // any express or implied warranties, including, but not limited to, the implied
34 // warranties of merchantability and fitness for a particular purpose are disclaimed.
35 // In no event shall the Intel Corporation or contributors be liable for any direct,
36 // indirect, incidental, special, exemplary, or consequential damages
37 // (including, but not limited to, procurement of substitute goods or services;
38 // loss of use, data, or profits; or business interruption) however caused
39 // and on any theory of liability, whether in contract, strict liability,
40 // or tort (including negligence or otherwise) arising in any way out of
41 // the use of this software, even if advised of the possibility of such damage.
42 //
43 //M*/
44
45
46 #include "precomp.hpp"
47
48 #ifdef HAVE_OPENCL
49 ////////////////////////////////////////////////////////////////////////////////
50 // MatchTemplate
51 #define ALL_TEMPLATE_METHODS testing::Values(TemplateMethod(cv::TM_SQDIFF), TemplateMethod(cv::TM_CCORR), TemplateMethod(cv::TM_CCOEFF), TemplateMethod(cv::TM_SQDIFF_NORMED), TemplateMethod(cv::TM_CCORR_NORMED), TemplateMethod(cv::TM_CCOEFF_NORMED))
52
53 IMPLEMENT_PARAM_CLASS(TemplateSize, cv::Size);
54
55 const char *TEMPLATE_METHOD_NAMES[6] = {"TM_SQDIFF", "TM_SQDIFF_NORMED", "TM_CCORR", "TM_CCORR_NORMED", "TM_CCOEFF", "TM_CCOEFF_NORMED"};
56
57 #define MTEMP_SIZES testing::Values(cv::Size(128, 256), cv::Size(1024, 768))
58
59 PARAM_TEST_CASE(MatchTemplate8U, cv::Size, TemplateSize, Channels, TemplateMethod)
60 {
61     cv::Size size;
62     cv::Size templ_size;
63     int cn;
64     int method;
65
66     virtual void SetUp()
67     {
68         size = GET_PARAM(0);
69         templ_size = GET_PARAM(1);
70         cn = GET_PARAM(2);
71         method = GET_PARAM(3);
72     }
73 };
74
75 TEST_P(MatchTemplate8U, Accuracy)
76 {
77
78     std::cout << "Method: " << TEMPLATE_METHOD_NAMES[method] << std::endl;
79     std::cout << "Image Size: (" << size.width << ", " << size.height << ")" << std::endl;
80     std::cout << "Template Size: (" << templ_size.width << ", " << templ_size.height << ")" << std::endl;
81     std::cout << "Channels: " << cn << std::endl;
82
83     cv::Mat image = randomMat(size, CV_MAKETYPE(CV_8U, cn));
84     cv::Mat templ = randomMat(templ_size, CV_MAKETYPE(CV_8U, cn));
85
86     cv::ocl::oclMat dst, ocl_image(image), ocl_templ(templ);
87     cv::ocl::matchTemplate(ocl_image, ocl_templ, dst, method);
88
89     cv::Mat dst_gold;
90     cv::matchTemplate(image, templ, dst_gold, method);
91
92     cv::Mat mat_dst;
93     dst.download(mat_dst);
94
95     EXPECT_MAT_NEAR(dst_gold, mat_dst, templ_size.area() * 1e-1);
96 }
97
98 PARAM_TEST_CASE(MatchTemplate32F, cv::Size, TemplateSize, Channels, TemplateMethod)
99 {
100     cv::Size size;
101     cv::Size templ_size;
102     int cn;
103     int method;
104     //std::vector<cv::ocl::Info> oclinfo;
105
106     virtual void SetUp()
107     {
108         size = GET_PARAM(0);
109         templ_size = GET_PARAM(1);
110         cn = GET_PARAM(2);
111         method = GET_PARAM(3);
112     }
113 };
114
115 TEST_P(MatchTemplate32F, Accuracy)
116 {
117     cv::Mat image = randomMat(size, CV_MAKETYPE(CV_32F, cn));
118     cv::Mat templ = randomMat(templ_size, CV_MAKETYPE(CV_32F, cn));
119
120     cv::ocl::oclMat dst, ocl_image(image), ocl_templ(templ);
121     cv::ocl::matchTemplate(ocl_image, ocl_templ, dst, method);
122
123     cv::Mat dst_gold;
124     cv::matchTemplate(image, templ, dst_gold, method);
125
126     cv::Mat mat_dst;
127     dst.download(mat_dst);
128
129     EXPECT_MAT_NEAR(dst_gold, mat_dst, templ_size.area() * 1e-1);
130 }
131
132 INSTANTIATE_TEST_CASE_P(OCL_ImgProc, MatchTemplate8U,
133                         testing::Combine(
134                             MTEMP_SIZES,
135                             testing::Values(TemplateSize(cv::Size(5, 5)), TemplateSize(cv::Size(16, 16)), TemplateSize(cv::Size(30, 30))),
136                             testing::Values(Channels(1), Channels(3), Channels(4)),
137                             ALL_TEMPLATE_METHODS
138                         )
139                        );
140
141 INSTANTIATE_TEST_CASE_P(OCL_ImgProc, MatchTemplate32F, testing::Combine(
142                             MTEMP_SIZES,
143                             testing::Values(TemplateSize(cv::Size(5, 5)), TemplateSize(cv::Size(16, 16)), TemplateSize(cv::Size(30, 30))),
144                             testing::Values(Channels(1), Channels(3), Channels(4)),
145                             testing::Values(TemplateMethod(cv::TM_SQDIFF), TemplateMethod(cv::TM_CCORR))));
146 #endif