1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
42 #include "precomp.hpp"
44 #include "opencv2/ts/ocl_test.hpp"
51 int test_loop_times = 1; // TODO Read from command line / environment
53 Mat TestUtils::readImage(const String &fileName, int flags)
55 return cv::imread(cvtest::TS::ptr()->get_data_path() + fileName, flags);
58 Mat TestUtils::readImageType(const String &fname, int type)
60 Mat src = readImage(fname, CV_MAT_CN(type) == 1 ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
61 if (CV_MAT_CN(type) == 4)
64 cv::cvtColor(src, temp, cv::COLOR_BGR2BGRA);
67 src.convertTo(src, CV_MAT_DEPTH(type));
71 double TestUtils::checkNorm1(InputArray m, InputArray mask)
73 return cvtest::norm(m.getMat(), NORM_INF, mask.getMat());
76 double TestUtils::checkNorm2(InputArray m1, InputArray m2, InputArray mask)
78 return cvtest::norm(m1.getMat(), m2.getMat(), NORM_INF, mask.getMat());
81 double TestUtils::checkSimilarity(InputArray m1, InputArray m2)
84 matchTemplate(m1.getMat(), m2.getMat(), diff, CV_TM_CCORR_NORMED);
85 return std::abs(diff.at<float>(0, 0) - 1.f);
88 double TestUtils::checkRectSimilarity(const Size & sz, std::vector<Rect>& ob1, std::vector<Rect>& ob2)
90 double final_test_result = 0.0;
91 size_t sz1 = ob1.size();
92 size_t sz2 = ob2.size();
95 return sz1 > sz2 ? (double)(sz1 - sz2) : (double)(sz2 - sz1);
98 if (sz1 == 0 && sz2 == 0)
100 cv::Mat cpu_result(sz, CV_8UC1);
103 for (vector<Rect>::const_iterator r = ob1.begin(); r != ob1.end(); ++r)
105 cv::Mat cpu_result_roi(cpu_result, *r);
106 cpu_result_roi.setTo(1);
107 cpu_result.copyTo(cpu_result);
109 int cpu_area = cv::countNonZero(cpu_result > 0);
111 cv::Mat gpu_result(sz, CV_8UC1);
113 for(vector<Rect>::const_iterator r2 = ob2.begin(); r2 != ob2.end(); ++r2)
115 cv::Mat gpu_result_roi(gpu_result, *r2);
116 gpu_result_roi.setTo(1);
117 gpu_result.copyTo(gpu_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;
128 return final_test_result;
131 void TestUtils::showDiff(InputArray _src, InputArray _gold, InputArray _actual, double eps, bool alwaysShow)
133 Mat src = _src.getMat(), actual = _actual.getMat(), gold = _gold.getMat();
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);
140 if (alwaysShow || cv::countNonZero(diff_thresh.reshape(1)) > 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;
148 namedWindow("src", WINDOW_NORMAL);
149 namedWindow("gold", WINDOW_NORMAL);
150 namedWindow("actual", WINDOW_NORMAL);
151 namedWindow("diff", WINDOW_NORMAL);
154 imshow("gold", gold);
155 imshow("actual", actual);
156 imshow("diff", diff);
162 } } // namespace cvtest::ocl