1 #include "test_precomp.hpp"
\r
7 #define sign(a) a > 0 ? 1 : a == 0 ? 0 : -1
\r
9 const int FLOAT_TYPE [2] = {CV_32F, CV_64F};
\r
10 const int INT_TYPE [5] = {CV_8U, CV_8S, CV_16U, CV_16S, CV_32S};
\r
12 #define MAX_CHANNELS 4
\r
13 #define MAX_WIDTH 1e+2
\r
14 #define MAX_HEIGHT 1e+2
\r
16 class CV_CountNonZeroTest: public cvtest::BaseTest
\r
19 CV_CountNonZeroTest();
\r
20 ~CV_CountNonZeroTest();
\r
26 float eps_32; double eps_64; Mat src;
\r
28 void generate_src_data(cv::Size size, int type, int channels);
\r
29 void generate_src_data(cv::Size size, int type, int channels, int count_non_zero);
\r
30 void generate_src_float_data(cv::Size size, int type, int channels, int distribution);
\r
32 void checking_function_work();
\r
33 void checking_function_work(int count_non_zero);
\r
36 CV_CountNonZeroTest::CV_CountNonZeroTest(): eps_32(1e-2), eps_64(1e-4), src(Mat()) {}
\r
37 CV_CountNonZeroTest::~CV_CountNonZeroTest() {}
\r
39 void CV_CountNonZeroTest::generate_src_data(cv::Size size, int type, int channels)
\r
41 src.create(size, CV_MAKETYPE(type, channels));
\r
43 for (size_t i = 0; i < size.width; ++i)
\r
44 for (size_t j = 0; j < size.height; ++j)
\r
46 if (type == CV_8U) switch (channels)
\r
48 case 1: {src.at<uchar>(j, i) = cv::randu<uchar>(); break;}
\r
49 case 2: {src.at<Vec2b>(j, i) = Vec2b(cv::randu<uchar>(), cv::randu<uchar>()); break;}
\r
50 case 3: {src.at<Vec3b>(j, i) = Vec3b(cv::randu<uchar>(), cv::randu<uchar>(), cv::randu<uchar>()); break;}
\r
51 case 4: {src.at<Vec4b>(j, i) = Vec4b(cv::randu<uchar>(), cv::randu<uchar>(), cv::randu<uchar>(), cv::randu<uchar>()); break;}
\r
56 else if (type == CV_8S) switch (channels)
\r
58 case 1: {src.at<char>(j,i) = cv::randu<uchar>()-128; break; }
\r
59 case 2: {src.at< Vec<char, 2> >(j, i) = Vec<char, 2>(cv::randu<uchar>()-128, cv::randu<uchar>()-128); break;}
\r
60 case 3: {src.at< Vec<char, 3> >(j, i) = Vec<char, 3>(cv::randu<uchar>()-128, cv::randu<uchar>()-128, cv::randu<uchar>()-128); break;}
\r
61 case 4: {src.at< Vec<char, 4> >(j, i) = Vec<char, 4>(cv::randu<uchar>()-128, cv::randu<uchar>()-128, cv::randu<uchar>()-128, cv::randu<uchar>()-128); break;}
\r
65 else if (type == CV_16U) switch (channels)
\r
67 case 1: {src.at<ushort>(j, i) = cv::randu<ushort>(); break;}
\r
68 case 2: {src.at< Vec<ushort, 2> >(j, i) = Vec<ushort, 2>(cv::randu<ushort>(), cv::randu<ushort>()); break;}
\r
69 case 3: {src.at< Vec<ushort, 3> >(j, i) = Vec<ushort, 3>(cv::randu<ushort>(), cv::randu<ushort>(), cv::randu<ushort>()); break;}
\r
70 case 4: {src.at< Vec<ushort, 4> >(j, i) = Vec<ushort, 4>(cv::randu<ushort>(), cv::randu<ushort>(), cv::randu<ushort>(), cv::randu<ushort>()); break;}
\r
74 else if (type == CV_16S) switch (channels)
\r
76 case 1: {src.at<short>(j, i) = cv::randu<short>(); break;}
\r
77 case 2: {src.at<Vec2s>(j, i) = Vec2s(cv::randu<short>(), cv::randu<short>()); break;}
\r
78 case 3: {src.at<Vec3s>(j, i) = Vec3s(cv::randu<short>(), cv::randu<short>(), cv::randu<short>()); break;}
\r
79 case 4: {src.at<Vec4s>(j, i) = Vec4s(cv::randu<short>(), cv::randu<short>(), cv::randu<short>(), cv::randu<short>()); break;}
\r
83 else if (type == CV_32S) switch (channels)
\r
85 case 1: {src.at<int>(j, i) = cv::randu<int>(); break;}
\r
86 case 2: {src.at<Vec2i>(j, i) = Vec2i(cv::randu<int>(), cv::randu<int>()); break;}
\r
87 case 3: {src.at<Vec3i>(j, i) = Vec3i(cv::randu<int>(), cv::randu<int>(), cv::randu<int>()); break;}
\r
88 case 4: {src.at<Vec4i>(j, i) = Vec4i(cv::randu<int>(), cv::randu<int>(), cv::randu<int>(), cv::randu<int>()); break;}
\r
92 else if (type == CV_32F) switch (channels)
\r
94 case 1: {src.at<float>(j, i) = cv::randu<float>(); break;}
\r
95 case 2: {src.at<Vec2f>(j, i) = Vec2i(cv::randu<float>(), cv::randu<float>()); break;}
\r
96 case 3: {src.at<Vec3f>(j, i) = Vec3i(cv::randu<float>(), cv::randu<float>(), cv::randu<float>()); break;}
\r
97 case 4: {src.at<Vec4f>(j, i) = Vec4i(cv::randu<float>(), cv::randu<float>(), cv::randu<float>(), cv::randu<float>()); break;}
\r
101 else if (type == CV_64F) switch (channels)
\r
103 case 1: {src.at<double>(j, i) = cv::randu<double>(); break;}
\r
104 case 2: {src.at<Vec2d>(j, i) = Vec2d(cv::randu<double>(), cv::randu<double>()); break;}
\r
105 case 3: {src.at<Vec3d>(j, i) = Vec3d(cv::randu<double>(), cv::randu<double>(), cv::randu<double>()); break;}
\r
106 case 4: {src.at<Vec4d>(j, i) = Vec4d(cv::randu<double>(), cv::randu<double>(), cv::randu<double>(), cv::randu<double>()); break;}
\r
112 void CV_CountNonZeroTest::generate_src_data(cv::Size size, int type, int channels, int count_non_zero)
\r
114 src = Mat::zeros(size, CV_MAKETYPE(type, channels));
\r
118 while (n < count_non_zero)
\r
120 RNG& rng = ts->get_rng();
\r
122 size_t i = rng.next()%size.height, j = rng.next()%size.width;
\r
128 if (channels == 1)
\r
130 uchar value = cv::randu<uchar>();
\r
131 if (value != 0) {src.at<uchar>(i, j) = value; n++;}
\r
134 else if (channels == 2)
\r
136 Vec2b value(cv::randu<uchar>(), cv::randu<uchar>());
\r
137 if (value != Vec2b(0, 0)) {src.at<Vec2b>(i, j) = value; n++;}
\r
140 else if (channels == 3)
\r
142 Vec3b value(cv::randu<uchar>(), cv::randu<uchar>(), cv::randu<uchar>());
\r
143 if (value != Vec3b(0, 0, 0)) {src.at<Vec3b>(i, j) = value; n++;}
\r
149 Vec4b value(cv::randu<uchar>(), cv::randu<uchar>(), cv::randu<uchar>(), cv::randu<uchar>());
\r
150 if (value != Vec4b(0, 0, 0, 0)) {src.at<Vec4b>(i, j) = value; n++;}
\r
158 if (channels == 1)
\r
160 char value = cv::randu<uchar>()-128;
\r
161 if (value != 0) {src.at<char>(i, j) = value; n++;}
\r
164 else if (channels == 2)
\r
166 Vec<char, 2> value(cv::randu<uchar>()-128, cv::randu<uchar>()-128);
\r
167 if (value != Vec<char, 2>(0, 0)) {src.at< Vec<char, 2 > >(i, j) = value; n++;}
\r
170 else if (channels == 3)
\r
172 Vec<char, 3> value(cv::randu<uchar>()-128, cv::randu<uchar>()-128, cv::randu<uchar>()-128);
\r
173 if (value != Vec<char, 3>(0, 0, 0)) {src.at< Vec<char, 3> >(i, j) = value; n++;}
\r
179 Vec<char, 4> value(cv::randu<uchar>()-128, cv::randu<uchar>()-128, cv::randu<uchar>()-128, cv::randu<uchar>()-128);
\r
180 if (value != Vec<char, 4>(0, 0, 0, 0)) {src.at< Vec<char, 4> >(i, j) = value; n++;}
\r
188 if (channels == 1)
\r
190 ushort value = cv::randu<ushort>();
\r
191 n += abs(sign(value));
\r
192 src.at<ushort>(i, j) = value;
\r
195 else if (channels == 2)
\r
197 Vec<ushort, 2> value(cv::randu<ushort>(), cv::randu<ushort>());
\r
198 if (value != Vec<ushort, 2>(0, 0)) {src.at< Vec<ushort, 2> >(i, j) = value; n++;}
\r
201 else if (channels == 3)
\r
203 Vec<ushort, 3> value(cv::randu<ushort>(), cv::randu<ushort>(), cv::randu<ushort>());
\r
204 if (value != Vec<ushort, 3>(0, 0, 0)) {src.at< Vec<ushort, 3> >(i, j) = value; n++;}
\r
210 Vec<ushort, 4> value(cv::randu<ushort>(), cv::randu<ushort>(), cv::randu<ushort>(), cv::randu<ushort>());
\r
211 if (value != Vec<ushort, 4>(0, 0, 0, 0)) {src.at< Vec<ushort, 4> >(i, j) = value; n++;}
\r
219 if (channels == 1)
\r
221 short value = cv::randu<short>();
\r
222 n += abs(sign(value));
\r
223 src.at<short>(i, j) = value;
\r
226 else if (channels == 2)
\r
228 Vec2s value(cv::randu<short>(), cv::randu<short>());
\r
229 if (value != Vec2s(0, 0)) {src.at<Vec2s>(i, j) = value; n++;}
\r
232 else if (channels == 3)
\r
234 Vec3s value(cv::randu<short>(), cv::randu<short>(), cv::randu<short>());
\r
235 if (value != Vec3s(0, 0, 0)) {src.at<Vec3s>(i, j) = value; n++;}
\r
241 Vec4s value(cv::randu<short>(), cv::randu<short>(), cv::randu<short>(), cv::randu<short>());
\r
242 if (value != Vec4s(0, 0, 0, 0)) {src.at<Vec4s>(i, j) = value; n++;}
\r
250 if (channels == 1)
\r
252 int value = cv::randu<int>();
\r
253 n += abs(sign(value));
\r
254 src.at<int>(i, j) = value;
\r
257 else if (channels == 2)
\r
259 Vec2i value(cv::randu<int>(), cv::randu<int>());
\r
260 if (value != Vec2i(0, 0)) {src.at<Vec2i>(i, j) = value; n++;}
\r
263 else if (channels == 3)
\r
265 Vec3i value(cv::randu<int>(), cv::randu<int>(), cv::randu<int>());
\r
266 if (value != Vec3i(0, 0, 0)) {src.at<Vec3i>(i, j) = value; n++;}
\r
272 Vec4i value(cv::randu<int>(), cv::randu<int>(), cv::randu<int>(), cv::randu<int>());
\r
273 if (value != Vec4i(0, 0, 0, 0)) {src.at<Vec4i>(i, j) = value; n++;}
\r
284 float value = cv::randu<float>();
\r
285 n += sign(fabs(value) > eps_32);
\r
286 src.at<float>(i, j) = value;
\r
293 Vec2f value(cv::randu<float>(), cv::randu<float>());
\r
294 n += sign(cv::norm(value) > eps_32);
\r
295 src.at<Vec2f>(i, j) = value;
\r
302 Vec3f value(cv::randu<float>(), cv::randu<float>(), cv::randu<float>());
\r
303 n += sign(cv::norm(value) > eps_32);
\r
304 src.at<Vec3f>(i, j) = value;
\r
310 Vec4f value(cv::randu<float>(), cv::randu<float>(), cv::randu<float>(), cv::randu<float>());
\r
311 n += sign(cv::norm(value) > eps_32);
\r
312 src.at<Vec4f>(i, j) = value;
\r
322 double value = cv::randu<double>();
\r
323 n += sign(fabs(value) > eps_64);
\r
324 src.at<double>(i, j) = value;
\r
331 Vec2d value(cv::randu<double>(), cv::randu<double>());
\r
332 n += sign(cv::norm(value) > eps_64);
\r
333 src.at<Vec2d>(i, j) = value;
\r
340 Vec3d value(cv::randu<double>(), cv::randu<double>(), cv::randu<double>());
\r
341 n += sign(cv::norm(value) > eps_64);
\r
342 src.at<Vec3d>(i, j) = value;
\r
348 Vec4d value(cv::randu<double>(), cv::randu<double>(), cv::randu<double>(), cv::randu<double>());
\r
349 n += sign(cv::norm(value) > eps_64);
\r
350 src.at<Vec4d>(i, j) = value;
\r
362 void CV_CountNonZeroTest::generate_src_float_data(cv::Size size, int type, int channels, int distribution)
\r
364 src.create(size, CV_MAKETYPE(type, channels));
\r
366 double mean = 0.0, sigma = 1.0;
\r
367 double left = -1.0, right = 1.0;
\r
369 RNG& rng = ts->get_rng();
\r
371 if (distribution == RNG::NORMAL)
\r
372 rng.fill(src, RNG::NORMAL, Scalar::all(mean), Scalar::all(sigma));
\r
373 else if (distribution == RNG::UNIFORM)
\r
374 rng.fill(src, RNG::UNIFORM, Scalar::all(left), Scalar::all(right));
\r
377 void CV_CountNonZeroTest::run(int)
\r
382 TEST (Core_CountNonZero, accuracy) { CV_CountNonZeroTest test; test.safe_run(); }