catching OpenCL double not supported exceptions
[profile/ivi/opencv.git] / modules / ocl / test / test_kalman.cpp
1 ///////////////////////////////////////////////////////////////////////////////////////
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 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
13 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // @Authors
17 //    Jin Ma, jin@multicorewareinc.com
18 //
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 #include "test_precomp.hpp"
46
47 #ifdef HAVE_OPENCL
48
49 #ifdef HAVE_CLAMDBLAS
50
51 using namespace cv;
52 using namespace cv::ocl;
53 using namespace cvtest;
54 using namespace testing;
55 using namespace std;
56
57 //////////////////////////////////////////////////////////////////////////
58
59 PARAM_TEST_CASE(Kalman, int, int)
60 {
61     int size_;
62     int iteration;
63     virtual void SetUp()
64     {
65         size_ = GET_PARAM(0);
66         iteration = GET_PARAM(1);
67     }
68 };
69
70 OCL_TEST_P(Kalman, Accuracy)
71 {
72     const int Dim = size_;
73     const int Steps = iteration;
74     const double max_init = 1;
75     const double max_noise = 0.1;
76
77     Mat sample_mat(Dim, 1, CV_32F), temp_mat;
78     oclMat Sample(Dim, 1, CV_32F);
79     oclMat Temp(Dim, 1, CV_32F);
80     Mat Temp_cpu(Dim, 1, CV_32F);
81
82     Size size(Sample.cols, Sample.rows);
83
84     sample_mat =  randomMat(size, Sample.type(), -max_init, max_init, false);
85     Sample.upload(sample_mat);
86
87     //ocl start
88     cv::ocl::KalmanFilter kalman_filter_ocl;
89     kalman_filter_ocl.init(Dim, Dim);
90
91     cv::ocl::setIdentity(kalman_filter_ocl.errorCovPre, 1);
92     cv::ocl::setIdentity(kalman_filter_ocl.measurementMatrix, 1);
93     cv::ocl::setIdentity(kalman_filter_ocl.errorCovPost, 1);
94
95     kalman_filter_ocl.measurementNoiseCov.setTo(Scalar::all(0));
96     kalman_filter_ocl.statePre.setTo(Scalar::all(0));
97     kalman_filter_ocl.statePost.setTo(Scalar::all(0));
98
99     kalman_filter_ocl.correct(Sample);
100     //ocl end
101
102     //cpu start
103     cv::KalmanFilter kalman_filter_cpu;
104
105     kalman_filter_cpu.init(Dim, Dim);
106
107     cv::setIdentity(kalman_filter_cpu.errorCovPre, 1);
108     cv::setIdentity(kalman_filter_cpu.measurementMatrix, 1);
109     cv::setIdentity(kalman_filter_cpu.errorCovPost, 1);
110
111     kalman_filter_cpu.measurementNoiseCov.setTo(Scalar::all(0));
112     kalman_filter_cpu.statePre.setTo(Scalar::all(0));
113     kalman_filter_cpu.statePost.setTo(Scalar::all(0));
114
115     kalman_filter_cpu.correct(sample_mat);
116     //cpu end
117     //test begin
118     for(int i = 0; i<Steps; i++)
119     {
120         kalman_filter_ocl.predict();
121         kalman_filter_cpu.predict();
122
123         cv::gemm(kalman_filter_cpu.transitionMatrix, sample_mat, 1, cv::Mat(), 0, Temp_cpu);
124
125         Size size1(Temp.cols, Temp.rows);
126         Mat temp = randomMat(size1, Temp.type(), 0, 0xffff, false);
127
128
129         cv::multiply(2, temp, temp);
130
131         cv::subtract(temp, 1, temp);
132
133         cv::multiply(max_noise, temp, temp);
134
135         cv::add(temp, Temp_cpu, Temp_cpu);
136
137         Temp.upload(Temp_cpu);
138         Temp.copyTo(Sample);
139         Temp_cpu.copyTo(sample_mat);
140
141         kalman_filter_ocl.correct(Temp);
142         kalman_filter_cpu.correct(Temp_cpu);
143     }
144     //test end
145     EXPECT_MAT_NEAR(kalman_filter_cpu.statePost, kalman_filter_ocl.statePost, 0);
146 }
147
148 INSTANTIATE_TEST_CASE_P(OCL_Video, Kalman, Combine(Values(3, 7), Values(30)));
149
150 #endif // HAVE_CLAMDBLAS
151
152 #endif // HAVE_OPENCL