countNonZero & boundingRect tests - added first versions
[profile/ivi/opencv.git] / modules / core / test / test_countnonzero.cpp
1 #include "test_precomp.hpp"\r
2 #include <time.h>\r
3 \r
4 using namespace cv;\r
5 using namespace std;\r
6 \r
7 #define sign(a) a > 0 ? 1 : a == 0 ? 0 : -1\r
8 \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
11 \r
12 #define MAX_CHANNELS 4\r
13 #define MAX_WIDTH 1e+2\r
14 #define MAX_HEIGHT 1e+2\r
15 \r
16 class CV_CountNonZeroTest: public cvtest::BaseTest\r
17 {\r
18  public:\r
19         CV_CountNonZeroTest();\r
20         ~CV_CountNonZeroTest();\r
21 \r
22  protected:\r
23         void run (int);\r
24 \r
25  private:\r
26          float eps_32; double eps_64; Mat src;\r
27         \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
31 \r
32          void checking_function_work();  \r
33          void checking_function_work(int count_non_zero); \r
34 };\r
35 \r
36 CV_CountNonZeroTest::CV_CountNonZeroTest(): eps_32(1e-2), eps_64(1e-4), src(Mat()) {}\r
37 CV_CountNonZeroTest::~CV_CountNonZeroTest() {}\r
38 \r
39 void CV_CountNonZeroTest::generate_src_data(cv::Size size, int type, int channels)\r
40 {\r
41  src.create(size, CV_MAKETYPE(type, channels));\r
42 \r
43  for (size_t i = 0; i < size.width; ++i)\r
44         for (size_t j = 0; j < size.height; ++j)\r
45                 {\r
46                  if (type == CV_8U) switch (channels)\r
47                  {\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
52 \r
53                   default: break;\r
54                  }\r
55 \r
56                  else if (type == CV_8S) switch (channels)\r
57                  {\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
62                   default:break;\r
63                  }\r
64                         \r
65                  else if (type == CV_16U) switch (channels)\r
66                  {\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
71                   default: break;\r
72                  }\r
73 \r
74                  else if (type == CV_16S) switch (channels)\r
75                  {\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
80                   default: break;\r
81                  }\r
82 \r
83                  else if (type == CV_32S) switch (channels)\r
84                  {\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
89                   default: break;\r
90                  }\r
91 \r
92                  else if (type == CV_32F) switch (channels)\r
93                  {\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
98                   default: break;\r
99                  }\r
100 \r
101                  else if (type == CV_64F) switch (channels)\r
102                  {\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
107                   default: break;\r
108                  }\r
109                 }\r
110 }\r
111 \r
112 void CV_CountNonZeroTest::generate_src_data(cv::Size size, int type, int channels, int count_non_zero)\r
113 {\r
114  src = Mat::zeros(size, CV_MAKETYPE(type, channels));\r
115  \r
116  int n = -1;\r
117 \r
118  while (n < count_non_zero)\r
119  {\r
120          RNG& rng = ts->get_rng();\r
121 \r
122          size_t i = rng.next()%size.height, j = rng.next()%size.width;\r
123          \r
124          switch (type)\r
125          {\r
126           case CV_8U: \r
127                  {\r
128                    if (channels == 1) \r
129                    {\r
130                         uchar value = cv::randu<uchar>();\r
131                         if (value != 0) {src.at<uchar>(i, j) = value; n++;}\r
132                    }\r
133                          \r
134                    else if (channels == 2)\r
135                    {\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
138                    }\r
139 \r
140                    else if (channels == 3)\r
141                    {\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
144                    }\r
145 \r
146                    else\r
147 \r
148                    {\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
151                    }\r
152 \r
153                    break;\r
154                  }\r
155 \r
156           case CV_8S:\r
157                   {\r
158                    if (channels == 1) \r
159                    {\r
160                         char value = cv::randu<uchar>()-128;\r
161                         if (value != 0) {src.at<char>(i, j) = value; n++;}\r
162                    }\r
163                          \r
164                    else if (channels == 2)\r
165                    {\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
168                    }\r
169 \r
170                    else if (channels == 3)\r
171                    {\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
174                    }\r
175 \r
176                    else\r
177 \r
178                    {\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
181                    }\r
182 \r
183                    break;\r
184                   }\r
185 \r
186           case CV_16U:\r
187                   {\r
188                    if (channels == 1) \r
189                    {\r
190                         ushort value = cv::randu<ushort>();\r
191                         n += abs(sign(value));\r
192                         src.at<ushort>(i, j) = value;\r
193                    }\r
194                          \r
195                    else if (channels == 2)\r
196                    {\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
199                    }\r
200 \r
201                    else if (channels == 3)\r
202                    {\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
205                    }\r
206 \r
207                    else\r
208 \r
209                    {\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
212                    }\r
213 \r
214                    break;\r
215                   }\r
216 \r
217           case CV_16S:\r
218                   {\r
219                    if (channels == 1) \r
220                    {\r
221                         short value = cv::randu<short>();\r
222                         n += abs(sign(value));\r
223                         src.at<short>(i, j) = value;\r
224                    }\r
225                          \r
226                    else if (channels == 2)\r
227                    {\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
230                    }\r
231 \r
232                    else if (channels == 3)\r
233                    {\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
236                    }\r
237 \r
238                    else\r
239 \r
240                    {\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
243                    }\r
244 \r
245                    break;\r
246                   }\r
247 \r
248           case CV_32S:\r
249                   {\r
250                    if (channels == 1) \r
251                    {\r
252                         int value = cv::randu<int>();\r
253                         n += abs(sign(value));\r
254                         src.at<int>(i, j) = value;\r
255                    }\r
256                          \r
257                    else if (channels == 2)\r
258                    {\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
261                    }\r
262 \r
263                    else if (channels == 3)\r
264                    {\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
267                    }\r
268 \r
269                    else\r
270 \r
271                    {\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
274                    }\r
275 \r
276                    break;\r
277                   }\r
278 \r
279 \r
280           case CV_32F:\r
281                   {\r
282                     if (channels == 1)\r
283                         {\r
284                          float value = cv::randu<float>();\r
285                          n += sign(fabs(value) > eps_32);\r
286                          src.at<float>(i, j) = value;  \r
287                         }\r
288                         \r
289                         else \r
290 \r
291                         if (channels == 2)\r
292                         {\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
296                         }\r
297 \r
298                         else \r
299 \r
300                         if (channels == 3)\r
301                         {\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
305                         }\r
306                                 \r
307                         else\r
308                                 \r
309                         {\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
313                         }\r
314 \r
315                         break;\r
316                   }\r
317 \r
318           case CV_64F:\r
319                   {\r
320                    if (channels == 1)\r
321                         {\r
322                          double value = cv::randu<double>();\r
323                          n += sign(fabs(value) > eps_64);\r
324                          src.at<double>(i, j) = value;  \r
325                         }\r
326                         \r
327                         else \r
328 \r
329                         if (channels == 2)\r
330                         {\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
334                         }\r
335 \r
336                         else \r
337 \r
338                         if (channels == 3)\r
339                         {\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
343                         }\r
344                                 \r
345                         else\r
346                                 \r
347                         {\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
351                         }\r
352 \r
353                    break;\r
354                   }\r
355 \r
356           default: break;\r
357          }\r
358  }\r
359  \r
360 }\r
361 \r
362 void CV_CountNonZeroTest::generate_src_float_data(cv::Size size, int type, int channels, int distribution)\r
363 {\r
364  src.create(size, CV_MAKETYPE(type, channels));\r
365 \r
366  double mean = 0.0, sigma = 1.0;\r
367  double left = -1.0, right = 1.0;\r
368 \r
369  RNG& rng = ts->get_rng();\r
370 \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
375 }\r
376 \r
377 void CV_CountNonZeroTest::run(int)\r
378 {\r
379  \r
380 }\r
381 \r
382 TEST (Core_CountNonZero, accuracy) { CV_CountNonZeroTest test; test.safe_run(); }