ts: test case list is printed after cmd line parsing, refactored
[platform/upstream/opencv.git] / modules / ts / src / ocl_test.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-2013, Advanced Micro Devices, Inc., all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of the copyright holders may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the OpenCV Foundation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41
42 #include "precomp.hpp"
43
44 #include "opencv2/ts/ocl_test.hpp"
45
46 namespace cvtest {
47 namespace ocl {
48
49 using namespace cv;
50
51 int test_loop_times = 1; // TODO Read from command line / environment
52
53 Mat TestUtils::readImage(const String &fileName, int flags)
54 {
55     return cv::imread(cvtest::TS::ptr()->get_data_path() + fileName, flags);
56 }
57
58 Mat TestUtils::readImageType(const String &fname, int type)
59 {
60     Mat src = readImage(fname, CV_MAT_CN(type) == 1 ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
61     if (CV_MAT_CN(type) == 4)
62     {
63         Mat temp;
64         cv::cvtColor(src, temp, cv::COLOR_BGR2BGRA);
65         swap(src, temp);
66     }
67     src.convertTo(src, CV_MAT_DEPTH(type));
68     return src;
69 }
70
71 double TestUtils::checkNorm1(InputArray m, InputArray mask)
72 {
73     return cvtest::norm(m.getMat(), NORM_INF, mask.getMat());
74 }
75
76 double TestUtils::checkNorm2(InputArray m1, InputArray m2, InputArray mask)
77 {
78     return cvtest::norm(m1.getMat(), m2.getMat(), NORM_INF, mask.getMat());
79 }
80
81 double TestUtils::checkSimilarity(InputArray m1, InputArray m2)
82 {
83     Mat diff;
84     matchTemplate(m1.getMat(), m2.getMat(), diff, CV_TM_CCORR_NORMED);
85     return std::abs(diff.at<float>(0, 0) - 1.f);
86 }
87
88 double TestUtils::checkRectSimilarity(const Size & sz, std::vector<Rect>& ob1, std::vector<Rect>& ob2)
89 {
90     double final_test_result = 0.0;
91     size_t sz1 = ob1.size();
92     size_t sz2 = ob2.size();
93
94     if (sz1 != sz2)
95         return sz1 > sz2 ? (double)(sz1 - sz2) : (double)(sz2 - sz1);
96     else
97     {
98         if (sz1 == 0 && sz2 == 0)
99             return 0;
100         cv::Mat cpu_result(sz, CV_8UC1);
101         cpu_result.setTo(0);
102
103         for (vector<Rect>::const_iterator r = ob1.begin(); r != ob1.end(); ++r)
104         {
105             cv::Mat cpu_result_roi(cpu_result, *r);
106             cpu_result_roi.setTo(1);
107             cpu_result.copyTo(cpu_result);
108         }
109         int cpu_area = cv::countNonZero(cpu_result > 0);
110
111         cv::Mat gpu_result(sz, CV_8UC1);
112         gpu_result.setTo(0);
113         for(vector<Rect>::const_iterator r2 = ob2.begin(); r2 != ob2.end(); ++r2)
114         {
115             cv::Mat gpu_result_roi(gpu_result, *r2);
116             gpu_result_roi.setTo(1);
117             gpu_result.copyTo(gpu_result);
118         }
119
120         cv::Mat result_;
121         multiply(cpu_result, gpu_result, result_);
122         int result = cv::countNonZero(result_ > 0);
123         if (cpu_area!=0 && result!=0)
124             final_test_result = 1.0 - (double)result/(double)cpu_area;
125         else if(cpu_area==0 && result!=0)
126             final_test_result = -1;
127     }
128     return final_test_result;
129 }
130
131 void TestUtils::showDiff(InputArray _src, InputArray _gold, InputArray _actual, double eps, bool alwaysShow)
132 {
133     Mat src = _src.getMat(), actual = _actual.getMat(), gold = _gold.getMat();
134
135     Mat diff, diff_thresh;
136     absdiff(gold, actual, diff);
137     diff.convertTo(diff, CV_32F);
138     threshold(diff, diff_thresh, eps, 255.0, cv::THRESH_BINARY);
139
140     if (alwaysShow || cv::countNonZero(diff_thresh.reshape(1)) > 0)
141     {
142 #if 0
143         std::cout << "Source: " << std::endl << src << std::endl;
144         std::cout << "Expected: " << std::endl << gold << std::endl;
145         std::cout << "Actual: " << std::endl << actual << std::endl;
146 #endif
147
148         namedWindow("src", WINDOW_NORMAL);
149         namedWindow("gold", WINDOW_NORMAL);
150         namedWindow("actual", WINDOW_NORMAL);
151         namedWindow("diff", WINDOW_NORMAL);
152
153         imshow("src", src);
154         imshow("gold", gold);
155         imshow("actual", actual);
156         imshow("diff", diff);
157
158         cv::waitKey();
159     }
160 }
161
162 } } // namespace cvtest::ocl