1 /*M///////////////////////////////////////////////////////////////////////////////////////
\r
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
\r
5 // By downloading, copying, installing or using the software you agree to this license.
\r
6 // If you do not agree to this license, do not download, install,
\r
7 // copy or use the software.
\r
10 // License Agreement
\r
11 // For Open Source Computer Vision Library
\r
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
\r
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
\r
15 // Third party copyrights are property of their respective owners.
\r
17 // Redistribution and use in source and binary forms, with or without modification,
\r
18 // are permitted provided that the following conditions are met:
\r
20 // * Redistribution's of source code must retain the above copyright notice,
\r
21 // this list of conditions and the following disclaimer.
\r
23 // * Redistribution's in binary form must reproduce the above copyright notice,
\r
24 // this list of conditions and the following disclaimer in the documentation
\r
25 // and/or other materials provided with the distribution.
\r
27 // * The name of the copyright holders may not be used to endorse or promote products
\r
28 // derived from this software without specific prior written permission.
\r
30 // This software is provided by the copyright holders and contributors "as is" and
\r
31 // any express or implied warranties, including, but not limited to, the implied
\r
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
\r
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
\r
34 // indirect, incidental, special, exemplary, or consequential damages
\r
35 // (including, but not limited to, procurement of substitute goods or services;
\r
36 // loss of use, data, or profits; or business interruption) however caused
\r
37 // and on any theory of liability, whether in contract, strict liability,
\r
38 // or tort (including negligence or otherwise) arising in any way out of
\r
39 // the use of this software, even if advised of the possibility of such damage.
\r
43 #include "test_precomp.hpp"
\r
47 using namespace std;
\r
49 #define CORE_COUNTNONZERO_ERROR_COUNT 1
\r
51 #define MESSAGE_ERROR_COUNT "Count non zero elements returned by OpenCV function is incorrect."
\r
53 #define sign(a) a > 0 ? 1 : a == 0 ? 0 : -1
\r
55 const int FLOAT_TYPE [2] = {CV_32F, CV_64F};
\r
56 const int INT_TYPE [5] = {CV_8U, CV_8S, CV_16U, CV_16S, CV_32S};
\r
58 #define MAX_WIDTH 100
\r
59 #define MAX_HEIGHT 100
\r
61 class CV_CountNonZeroTest: public cvtest::BaseTest
\r
64 CV_CountNonZeroTest();
\r
65 ~CV_CountNonZeroTest();
\r
76 void generate_src_data(cv::Size size, int type);
\r
77 void generate_src_data(cv::Size size, int type, int count_non_zero);
\r
78 void generate_src_stat_data(cv::Size size, int type, int distribution);
\r
80 int get_count_non_zero();
\r
82 void print_information(int right, int result);
\r
85 CV_CountNonZeroTest::CV_CountNonZeroTest(): eps_32(1e-8), eps_64(1e-16), src(Mat()), current_type(-1) {}
\r
86 CV_CountNonZeroTest::~CV_CountNonZeroTest() {}
\r
88 void CV_CountNonZeroTest::generate_src_data(cv::Size size, int type)
\r
90 src.create(size, CV_MAKETYPE(type, 1));
\r
92 for (int j = 0; j < size.width; ++j)
\r
93 for (int i = 0; i < size.height; ++i)
\r
96 case CV_8U: { src.at<uchar>(i, j) = cv::randu<uchar>(); break; }
\r
97 case CV_8S: { src.at<char>(i, j) = cv::randu<uchar>() - 128; break; }
\r
98 case CV_16U: { src.at<ushort>(i, j) = cv::randu<ushort>(); break; }
\r
99 case CV_16S: { src.at<short>(i, j) = cv::randu<short>(); break; }
\r
100 case CV_32S: { src.at<int>(i, j) = cv::randu<int>(); break; }
\r
101 case CV_32F: { src.at<float>(i, j) = cv::randu<float>(); break; }
\r
102 case CV_64F: { src.at<double>(i, j) = cv::randu<double>(); break; }
\r
107 void CV_CountNonZeroTest::generate_src_data(cv::Size size, int type, int count_non_zero)
\r
109 src = Mat::zeros(size, CV_MAKETYPE(type, 1));
\r
111 int n = 0; RNG& rng = ts->get_rng();
\r
113 while (n < count_non_zero)
\r
115 size_t i = rng.next()%size.height, j = rng.next()%size.width;
\r
119 case CV_8U: { if (!src.at<uchar>(i, j)) {src.at<uchar>(i, j) = cv::randu<uchar>(); n += (src.at<uchar>(i, j) > 0);} break; }
\r
120 case CV_8S: { if (!src.at<char>(i, j)) {src.at<char>(i, j) = cv::randu<uchar>() - 128; n += abs(sign(src.at<char>(i, j)));} break; }
\r
121 case CV_16U: { if (!src.at<ushort>(i, j)) {src.at<ushort>(i, j) = cv::randu<ushort>(); n += (src.at<ushort>(i, j) > 0);} break; }
\r
122 case CV_16S: { if (!src.at<short>(i, j)) {src.at<short>(i, j) = cv::randu<short>(); n += abs(sign(src.at<short>(i, j)));} break; }
\r
123 case CV_32S: { if (!src.at<int>(i, j)) {src.at<int>(i, j) = cv::randu<int>(); n += abs(sign(src.at<int>(i, j)));} break; }
\r
124 case CV_32F: { if (fabs(src.at<float>(i, j)) <= eps_32) {src.at<float>(i, j) = cv::randu<float>(); n += (fabs(src.at<float>(i, j)) > eps_32);} break; }
\r
125 case CV_64F: { if (fabs(src.at<double>(i, j)) <= eps_64) {src.at<double>(i, j) = cv::randu<double>(); n += (fabs(src.at<double>(i, j)) > eps_64);} break; }
\r
133 void CV_CountNonZeroTest::generate_src_stat_data(cv::Size size, int type, int distribution)
\r
135 src.create(size, CV_MAKETYPE(type, 1));
\r
137 double mean = 0.0, sigma = 1.0;
\r
138 double left = -1.0, right = 1.0;
\r
140 RNG& rng = ts->get_rng();
\r
142 if (distribution == RNG::NORMAL)
\r
143 rng.fill(src, RNG::NORMAL, Scalar::all(mean), Scalar::all(sigma));
\r
144 else if (distribution == RNG::UNIFORM)
\r
145 rng.fill(src, RNG::UNIFORM, Scalar::all(left), Scalar::all(right));
\r
148 int CV_CountNonZeroTest::get_count_non_zero()
\r
152 for (int i = 0; i < src.rows; ++i)
\r
153 for (int j = 0; j < src.cols; ++j)
\r
155 if (current_type == CV_8U) result += (src.at<uchar>(i, j) > 0);
\r
157 else if (current_type == CV_8S) result += abs(sign(src.at<char>(i, j)));
\r
159 else if (current_type == CV_16U) result += (src.at<ushort>(i, j) > 0);
\r
161 else if (current_type == CV_16S) result += abs(sign(src.at<short>(i, j)));
\r
163 else if (current_type == CV_32S) result += abs(sign(src.at<int>(i, j)));
\r
165 else if (current_type == CV_32F) result += (fabs(src.at<float>(i, j)) > eps_32);
\r
167 else result += (fabs(src.at<double>(i, j)) > eps_64);
\r
172 void CV_CountNonZeroTest::print_information(int right, int result)
\r
174 cout << endl; cout << "Checking for the work of countNonZero function..." << endl; cout << endl;
\r
175 cout << "Type of Mat: ";
\r
176 switch (current_type)
\r
178 case 0: {cout << "CV_8U"; break;}
\r
179 case 1: {cout << "CV_8S"; break;}
\r
180 case 2: {cout << "CV_16U"; break;}
\r
181 case 3: {cout << "CV_16S"; break;}
\r
182 case 4: {cout << "CV_32S"; break;}
\r
183 case 5: {cout << "CV_32F"; break;}
\r
184 case 6: {cout << "CV_64F"; break;}
\r
188 cout << "Number of rows: " << src.rows << " Number of cols: " << src.cols << endl;
\r
189 cout << "True count non zero elements: " << right << " Result: " << result << endl;
\r
193 void CV_CountNonZeroTest::run(int)
\r
195 const size_t N = 1500;
\r
197 for (int k = 1; k <= 3; ++k)
\r
198 for (size_t i = 0; i < N; ++i)
\r
200 RNG& rng = ts->get_rng();
\r
202 int w = rng.next()%MAX_WIDTH + 1, h = rng.next()%MAX_HEIGHT + 1;
\r
204 current_type = rng.next()%7;
\r
209 generate_src_data(Size(w, h), current_type);
\r
210 int right = get_count_non_zero(), result = countNonZero(src);
\r
211 if (result != right)
\r
213 cout << "Number of experiment: " << i << endl;
\r
214 cout << "Method of data generation: RANDOM" << endl;
\r
215 print_information(right, result);
\r
216 CV_Error(CORE_COUNTNONZERO_ERROR_COUNT, MESSAGE_ERROR_COUNT);
\r
224 int count_non_zero = rng.next()%(w*h);
\r
225 generate_src_data(Size(w, h), current_type, count_non_zero);
\r
226 int result = countNonZero(src);
\r
227 if (result != count_non_zero)
\r
229 cout << "Number of experiment: " << i << endl;
\r
230 cout << "Method of data generation: HALF-RANDOM" << endl;
\r
231 print_information(count_non_zero, result);
\r
232 CV_Error(CORE_COUNTNONZERO_ERROR_COUNT, MESSAGE_ERROR_COUNT);
\r
240 int distribution = cv::randu<uchar>()%2;
\r
241 generate_src_stat_data(Size(w, h), current_type, distribution);
\r
242 int right = get_count_non_zero(), result = countNonZero(src);
\r
243 if (right != result)
\r
245 cout << "Number of experiment: " << i << endl;
\r
246 cout << "Method of data generation: STATISTIC" << endl;
\r
247 print_information(right, result);
\r
248 CV_Error(CORE_COUNTNONZERO_ERROR_COUNT, MESSAGE_ERROR_COUNT);
\r
260 // TEST (Core_CountNonZero, accuracy) { CV_CountNonZeroTest test; test.safe_run(); }
\r