Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/core/test/test_countnonzero.cpp
16337 views
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) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
26
//
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
29
//
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
40
//
41
//M*/
42
43
#include "test_precomp.hpp"
44
45
namespace opencv_test { namespace {
46
47
#define CORE_COUNTNONZERO_ERROR_COUNT 1
48
49
#define MESSAGE_ERROR_COUNT "Count non zero elements returned by OpenCV function is incorrect."
50
51
#define sign(a) a > 0 ? 1 : a == 0 ? 0 : -1
52
53
#define MAX_WIDTH 100
54
#define MAX_HEIGHT 100
55
56
class CV_CountNonZeroTest: public cvtest::BaseTest
57
{
58
public:
59
CV_CountNonZeroTest();
60
~CV_CountNonZeroTest();
61
62
protected:
63
void run (int);
64
65
private:
66
float eps_32;
67
double eps_64;
68
Mat src;
69
int current_type;
70
71
void generate_src_data(cv::Size size, int type);
72
void generate_src_data(cv::Size size, int type, int count_non_zero);
73
void generate_src_stat_data(cv::Size size, int type, int distribution);
74
75
int get_count_non_zero();
76
77
void print_information(int right, int result);
78
};
79
80
CV_CountNonZeroTest::CV_CountNonZeroTest(): eps_32(std::numeric_limits<float>::min()), eps_64(std::numeric_limits<double>::min()), src(Mat()), current_type(-1) {}
81
CV_CountNonZeroTest::~CV_CountNonZeroTest() {}
82
83
void CV_CountNonZeroTest::generate_src_data(cv::Size size, int type)
84
{
85
src.create(size, CV_MAKETYPE(type, 1));
86
87
for (int j = 0; j < size.width; ++j)
88
for (int i = 0; i < size.height; ++i)
89
switch (type)
90
{
91
case CV_8U: { src.at<uchar>(i, j) = cv::randu<uchar>(); break; }
92
case CV_8S: { src.at<char>(i, j) = cv::randu<uchar>() - 128; break; }
93
case CV_16U: { src.at<ushort>(i, j) = cv::randu<ushort>(); break; }
94
case CV_16S: { src.at<short>(i, j) = cv::randu<short>(); break; }
95
case CV_32S: { src.at<int>(i, j) = cv::randu<int>(); break; }
96
case CV_32F: { src.at<float>(i, j) = cv::randu<float>(); break; }
97
case CV_64F: { src.at<double>(i, j) = cv::randu<double>(); break; }
98
default: break;
99
}
100
}
101
102
void CV_CountNonZeroTest::generate_src_data(cv::Size size, int type, int count_non_zero)
103
{
104
src = Mat::zeros(size, CV_MAKETYPE(type, 1));
105
106
int n = 0; RNG& rng = ts->get_rng();
107
108
while (n < count_non_zero)
109
{
110
int i = rng.next()%size.height, j = rng.next()%size.width;
111
112
switch (type)
113
{
114
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; }
115
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; }
116
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; }
117
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; }
118
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; }
119
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; }
120
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; }
121
122
default: break;
123
}
124
}
125
126
}
127
128
void CV_CountNonZeroTest::generate_src_stat_data(cv::Size size, int type, int distribution)
129
{
130
src.create(size, CV_MAKETYPE(type, 1));
131
132
double mean = 0.0, sigma = 1.0;
133
double left = -1.0, right = 1.0;
134
135
RNG& rng = ts->get_rng();
136
137
if (distribution == RNG::NORMAL)
138
rng.fill(src, RNG::NORMAL, Scalar::all(mean), Scalar::all(sigma));
139
else if (distribution == RNG::UNIFORM)
140
rng.fill(src, RNG::UNIFORM, Scalar::all(left), Scalar::all(right));
141
}
142
143
int CV_CountNonZeroTest::get_count_non_zero()
144
{
145
int result = 0;
146
147
for (int i = 0; i < src.rows; ++i)
148
for (int j = 0; j < src.cols; ++j)
149
{
150
if (current_type == CV_8U) result += (src.at<uchar>(i, j) > 0);
151
else if (current_type == CV_8S) result += abs(sign(src.at<char>(i, j)));
152
else if (current_type == CV_16U) result += (src.at<ushort>(i, j) > 0);
153
else if (current_type == CV_16S) result += abs(sign(src.at<short>(i, j)));
154
else if (current_type == CV_32S) result += abs(sign(src.at<int>(i, j)));
155
else if (current_type == CV_32F) result += (fabs(src.at<float>(i, j)) > eps_32);
156
else result += (fabs(src.at<double>(i, j)) > eps_64);
157
}
158
159
return result;
160
}
161
162
void CV_CountNonZeroTest::print_information(int right, int result)
163
{
164
cout << endl; cout << "Checking for the work of countNonZero function..." << endl; cout << endl;
165
cout << "Type of Mat: ";
166
switch (current_type)
167
{
168
case 0: {cout << "CV_8U"; break;}
169
case 1: {cout << "CV_8S"; break;}
170
case 2: {cout << "CV_16U"; break;}
171
case 3: {cout << "CV_16S"; break;}
172
case 4: {cout << "CV_32S"; break;}
173
case 5: {cout << "CV_32F"; break;}
174
case 6: {cout << "CV_64F"; break;}
175
default: break;
176
}
177
cout << endl;
178
cout << "Number of rows: " << src.rows << " Number of cols: " << src.cols << endl;
179
cout << "True count non zero elements: " << right << " Result: " << result << endl;
180
cout << endl;
181
}
182
183
void CV_CountNonZeroTest::run(int)
184
{
185
const size_t N = 1500;
186
187
for (int k = 1; k <= 3; ++k)
188
for (size_t i = 0; i < N; ++i)
189
{
190
RNG& rng = ts->get_rng();
191
192
int w = rng.next()%MAX_WIDTH + 1, h = rng.next()%MAX_HEIGHT + 1;
193
194
current_type = rng.next()%7;
195
196
switch (k)
197
{
198
case 1: {
199
generate_src_data(Size(w, h), current_type);
200
int right = get_count_non_zero(), result = countNonZero(src);
201
if (result != right)
202
{
203
cout << "Number of experiment: " << i << endl;
204
cout << "Method of data generation: RANDOM" << endl;
205
print_information(right, result);
206
CV_Error(CORE_COUNTNONZERO_ERROR_COUNT, MESSAGE_ERROR_COUNT);
207
return;
208
}
209
210
break;
211
}
212
213
case 2: {
214
int count_non_zero = rng.next()%(w*h);
215
generate_src_data(Size(w, h), current_type, count_non_zero);
216
int result = countNonZero(src);
217
if (result != count_non_zero)
218
{
219
cout << "Number of experiment: " << i << endl;
220
cout << "Method of data generation: HALF-RANDOM" << endl;
221
print_information(count_non_zero, result);
222
CV_Error(CORE_COUNTNONZERO_ERROR_COUNT, MESSAGE_ERROR_COUNT);
223
return;
224
}
225
226
break;
227
}
228
229
case 3: {
230
int distribution = cv::randu<uchar>()%2;
231
generate_src_stat_data(Size(w, h), current_type, distribution);
232
int right = get_count_non_zero(), result = countNonZero(src);
233
if (right != result)
234
{
235
cout << "Number of experiment: " << i << endl;
236
cout << "Method of data generation: STATISTIC" << endl;
237
print_information(right, result);
238
CV_Error(CORE_COUNTNONZERO_ERROR_COUNT, MESSAGE_ERROR_COUNT);
239
return;
240
}
241
242
break;
243
}
244
245
default: break;
246
}
247
}
248
}
249
250
TEST (Core_CountNonZero, accuracy) { CV_CountNonZeroTest test; test.safe_run(); }
251
252
253
typedef testing::TestWithParam<tuple<int, int> > CountNonZeroND;
254
255
TEST_P (CountNonZeroND, ndim)
256
{
257
const int dims = get<0>(GetParam());
258
const int type = get<1>(GetParam());
259
const int ONE_SIZE = 5;
260
261
vector<int> sizes(dims);
262
fill(sizes.begin(), sizes.end(), ONE_SIZE);
263
264
Mat data(sizes, CV_MAKETYPE(type, 1));
265
data = 0;
266
EXPECT_EQ(0, cv::countNonZero(data));
267
data = Scalar::all(1);
268
int expected = static_cast<int>(pow(static_cast<float>(ONE_SIZE), dims));
269
EXPECT_EQ(expected, cv::countNonZero(data));
270
}
271
272
INSTANTIATE_TEST_CASE_P(Core, CountNonZeroND,
273
testing::Combine(
274
testing::Range(2, 9),
275
testing::Values(CV_8U, CV_8S, CV_32F)
276
)
277
);
278
279
}} // namespace
280
281