1+ #include " test_precomp.hpp"
2+ #include < time.h>
3+
4+ using namespace cv ;
5+ using namespace std ;
6+
7+ #define sign (a ) a > 0 ? 1 : a == 0 ? 0 : -1
8+
9+ const int FLOAT_TYPE [2 ] = {CV_32F, CV_64F};
10+ const int INT_TYPE [5 ] = {CV_8U, CV_8S, CV_16U, CV_16S, CV_32S};
11+
12+ #define MAX_CHANNELS 4
13+ #define MAX_WIDTH 1e+2
14+ #define MAX_HEIGHT 1e+2
15+
16+ class CV_CountNonZeroTest : public cvtest ::BaseTest
17+ {
18+ public:
19+ CV_CountNonZeroTest ();
20+ ~CV_CountNonZeroTest ();
21+
22+ protected:
23+ void run (int );
24+
25+ private:
26+ float eps_32; double eps_64; Mat src;
27+
28+ void generate_src_data (cv::Size size, int type, int channels);
29+ void generate_src_data (cv::Size size, int type, int channels, int count_non_zero);
30+ void generate_src_float_data (cv::Size size, int type, int channels, int distribution);
31+
32+ void checking_function_work ();
33+ void checking_function_work (int count_non_zero);
34+ };
35+
36+ CV_CountNonZeroTest::CV_CountNonZeroTest (): eps_32(1e-2 ), eps_64(1e-4 ), src(Mat()) {}
37+ CV_CountNonZeroTest::~CV_CountNonZeroTest () {}
38+
39+ void CV_CountNonZeroTest::generate_src_data (cv::Size size, int type, int channels)
40+ {
41+ src.create (size, CV_MAKETYPE (type, channels));
42+
43+ for (size_t i = 0 ; i < size.width ; ++i)
44+ for (size_t j = 0 ; j < size.height ; ++j)
45+ {
46+ if (type == CV_8U) switch (channels)
47+ {
48+ case 1 : {src.at <uchar>(j, i) = cv::randu<uchar>(); break ;}
49+ case 2 : {src.at <Vec2b>(j, i) = Vec2b (cv::randu<uchar>(), cv::randu<uchar>()); break ;}
50+ case 3 : {src.at <Vec3b>(j, i) = Vec3b (cv::randu<uchar>(), cv::randu<uchar>(), cv::randu<uchar>()); break ;}
51+ case 4 : {src.at <Vec4b>(j, i) = Vec4b (cv::randu<uchar>(), cv::randu<uchar>(), cv::randu<uchar>(), cv::randu<uchar>()); break ;}
52+
53+ default : break ;
54+ }
55+
56+ else if (type == CV_8S) switch (channels)
57+ {
58+ case 1 : {src.at <char >(j,i) = cv::randu<uchar>()-128 ; break ; }
59+ case 2 : {src.at <Vec<char , 2 >>(j, i) = Vec<char , 2 >(cv::randu<uchar>()-128 , cv::randu<uchar>()-128 ); break ;}
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 ;}
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 ;}
62+ default :break ;
63+ }
64+
65+ else if (type == CV_16U) switch (channels)
66+ {
67+ case 1 : {src.at <ushort>(j, i) = cv::randu<ushort>(); break ;}
68+ case 2 : {src.at <Vec<ushort, 2 >>(j, i) = Vec<ushort, 2 >(cv::randu<ushort>(), cv::randu<ushort>()); break ;}
69+ case 3 : {src.at <Vec<ushort, 3 >>(j, i) = Vec<ushort, 3 >(cv::randu<ushort>(), cv::randu<ushort>(), cv::randu<ushort>()); break ;}
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 ;}
71+ default : break ;
72+ }
73+
74+ else if (type == CV_16S) switch (channels)
75+ {
76+ case 1 : {src.at <short >(j, i) = cv::randu<short >(); break ;}
77+ case 2 : {src.at <Vec2s>(j, i) = Vec2s (cv::randu<short >(), cv::randu<short >()); break ;}
78+ case 3 : {src.at <Vec3s>(j, i) = Vec3s (cv::randu<short >(), cv::randu<short >(), cv::randu<short >()); break ;}
79+ case 4 : {src.at <Vec4s>(j, i) = Vec4s (cv::randu<short >(), cv::randu<short >(), cv::randu<short >(), cv::randu<short >()); break ;}
80+ default : break ;
81+ }
82+
83+ else if (type == CV_32S) switch (channels)
84+ {
85+ case 1 : {src.at <int >(j, i) = cv::randu<int >(); break ;}
86+ case 2 : {src.at <Vec2i>(j, i) = Vec2i (cv::randu<int >(), cv::randu<int >()); break ;}
87+ case 3 : {src.at <Vec3i>(j, i) = Vec3i (cv::randu<int >(), cv::randu<int >(), cv::randu<int >()); break ;}
88+ case 4 : {src.at <Vec4i>(j, i) = Vec4i (cv::randu<int >(), cv::randu<int >(), cv::randu<int >(), cv::randu<int >()); break ;}
89+ default : break ;
90+ }
91+
92+ else if (type == CV_32F) switch (channels)
93+ {
94+ case 1 : {src.at <float >(j, i) = cv::randu<float >(); break ;}
95+ case 2 : {src.at <Vec2f>(j, i) = Vec2i (cv::randu<float >(), cv::randu<float >()); break ;}
96+ case 3 : {src.at <Vec3f>(j, i) = Vec3i (cv::randu<float >(), cv::randu<float >(), cv::randu<float >()); break ;}
97+ case 4 : {src.at <Vec4f>(j, i) = Vec4i (cv::randu<float >(), cv::randu<float >(), cv::randu<float >(), cv::randu<float >()); break ;}
98+ default : break ;
99+ }
100+
101+ else if (type == CV_64F) switch (channels)
102+ {
103+ case 1 : {src.at <double >(j, i) = cv::randu<double >(); break ;}
104+ case 2 : {src.at <Vec2d>(j, i) = Vec2d (cv::randu<double >(), cv::randu<double >()); break ;}
105+ case 3 : {src.at <Vec3d>(j, i) = Vec3d (cv::randu<double >(), cv::randu<double >(), cv::randu<double >()); break ;}
106+ case 4 : {src.at <Vec4d>(j, i) = Vec4d (cv::randu<double >(), cv::randu<double >(), cv::randu<double >(), cv::randu<double >()); break ;}
107+ default : break ;
108+ }
109+ }
110+ }
111+
112+ void CV_CountNonZeroTest::generate_src_data (cv::Size size, int type, int channels, int count_non_zero)
113+ {
114+ src = Mat::zeros (size, CV_MAKETYPE (type, channels));
115+
116+ int n = -1 ;
117+
118+ while (n < count_non_zero)
119+ {
120+ RNG& rng = ts->get_rng ();
121+
122+ size_t i = rng.next ()%size.height , j = rng.next ()%size.width ;
123+
124+ switch (type)
125+ {
126+ case CV_8U:
127+ {
128+ if (channels == 1 )
129+ {
130+ uchar value = cv::randu<uchar>();
131+ if (value != 0 ) {src.at <uchar>(i, j) = value; n++;}
132+ }
133+
134+ else if (channels == 2 )
135+ {
136+ Vec2b value (cv::randu<uchar>(), cv::randu<uchar>());
137+ if (value != Vec2b (0 , 0 )) {src.at <Vec2b>(i, j) = value; n++;}
138+ }
139+
140+ else if (channels == 3 )
141+ {
142+ Vec3b value (cv::randu<uchar>(), cv::randu<uchar>(), cv::randu<uchar>());
143+ if (value != Vec3b (0 , 0 , 0 )) {src.at <Vec3b>(i, j) = value; n++;}
144+ }
145+
146+ else
147+
148+ {
149+ Vec4b value (cv::randu<uchar>(), cv::randu<uchar>(), cv::randu<uchar>(), cv::randu<uchar>());
150+ if (value != Vec4b (0 , 0 , 0 , 0 )) {src.at <Vec4b>(i, j) = value; n++;}
151+ }
152+
153+ break ;
154+ }
155+
156+ case CV_8S:
157+ {
158+ if (channels == 1 )
159+ {
160+ char value = cv::randu<uchar>()-128 ;
161+ if (value != 0 ) {src.at <char >(i, j) = value; n++;}
162+ }
163+
164+ else if (channels == 2 )
165+ {
166+ Vec<char , 2 > value (cv::randu<uchar>()-128 , cv::randu<uchar>()-128 );
167+ if (value != Vec<char , 2 >(0 , 0 )) {src.at <Vec<char , 2 >>(i, j) = value; n++;}
168+ }
169+
170+ else if (channels == 3 )
171+ {
172+ Vec<char , 3 > value (cv::randu<uchar>()-128 , cv::randu<uchar>()-128 , cv::randu<uchar>()-128 );
173+ if (value != Vec<char , 3 >(0 , 0 , 0 )) {src.at <Vec<char , 3 >>(i, j) = value; n++;}
174+ }
175+
176+ else
177+
178+ {
179+ Vec<char , 4 > value (cv::randu<uchar>()-128 , cv::randu<uchar>()-128 , cv::randu<uchar>()-128 , cv::randu<uchar>()-128 );
180+ if (value != Vec<char , 4 >(0 , 0 , 0 , 0 )) {src.at <Vec<char , 4 >>(i, j) = value; n++;}
181+ }
182+
183+ break ;
184+ }
185+
186+ case CV_16U:
187+ {
188+ if (channels == 1 )
189+ {
190+ ushort value = cv::randu<ushort>();
191+ n += abs (sign (value));
192+ src.at <ushort>(i, j) = value;
193+ }
194+
195+ else if (channels == 2 )
196+ {
197+ Vec<ushort, 2 > value (cv::randu<ushort>(), cv::randu<ushort>());
198+ if (value != Vec<ushort, 2 >(0 , 0 )) {src.at <Vec<ushort, 2 >>(i, j) = value; n++;}
199+ }
200+
201+ else if (channels == 3 )
202+ {
203+ Vec<ushort, 3 > value (cv::randu<ushort>(), cv::randu<ushort>(), cv::randu<ushort>());
204+ if (value != Vec<ushort, 3 >(0 , 0 , 0 )) {src.at <Vec<ushort, 3 >>(i, j) = value; n++;}
205+ }
206+
207+ else
208+
209+ {
210+ Vec<ushort, 4 > value (cv::randu<ushort>(), cv::randu<ushort>(), cv::randu<ushort>(), cv::randu<ushort>());
211+ if (value != Vec<ushort, 4 >(0 , 0 , 0 , 0 )) {src.at <Vec<ushort, 4 >>(i, j) = value; n++;}
212+ }
213+
214+ break ;
215+ }
216+
217+ case CV_16S:
218+ {
219+ if (channels == 1 )
220+ {
221+ short value = cv::randu<short >();
222+ n += abs (sign (value));
223+ src.at <short >(i, j) = value;
224+ }
225+
226+ else if (channels == 2 )
227+ {
228+ Vec2s value (cv::randu<short >(), cv::randu<short >());
229+ if (value != Vec2s (0 , 0 )) {src.at <Vec2s>(i, j) = value; n++;}
230+ }
231+
232+ else if (channels == 3 )
233+ {
234+ Vec3s value (cv::randu<short >(), cv::randu<short >(), cv::randu<short >());
235+ if (value != Vec3s (0 , 0 , 0 )) {src.at <Vec3s>(i, j) = value; n++;}
236+ }
237+
238+ else
239+
240+ {
241+ Vec4s value (cv::randu<short >(), cv::randu<short >(), cv::randu<short >(), cv::randu<short >());
242+ if (value != Vec4s (0 , 0 , 0 , 0 )) {src.at <Vec4s>(i, j) = value; n++;}
243+ }
244+
245+ break ;
246+ }
247+
248+ case CV_32S:
249+ {
250+ if (channels == 1 )
251+ {
252+ int value = cv::randu<int >();
253+ n += abs (sign (value));
254+ src.at <int >(i, j) = value;
255+ }
256+
257+ else if (channels == 2 )
258+ {
259+ Vec2i value (cv::randu<int >(), cv::randu<int >());
260+ if (value != Vec2i (0 , 0 )) {src.at <Vec2i>(i, j) = value; n++;}
261+ }
262+
263+ else if (channels == 3 )
264+ {
265+ Vec3i value (cv::randu<int >(), cv::randu<int >(), cv::randu<int >());
266+ if (value != Vec3i (0 , 0 , 0 )) {src.at <Vec3i>(i, j) = value; n++;}
267+ }
268+
269+ else
270+
271+ {
272+ Vec4i value (cv::randu<int >(), cv::randu<int >(), cv::randu<int >(), cv::randu<int >());
273+ if (value != Vec4i (0 , 0 , 0 , 0 )) {src.at <Vec4i>(i, j) = value; n++;}
274+ }
275+
276+ break ;
277+ }
278+
279+
280+ case CV_32F:
281+ {
282+ if (channels == 1 )
283+ {
284+ float value = cv::randu<float >();
285+ n += sign (fabs (value) > eps_32);
286+ src.at <float >(i, j) = value;
287+ }
288+
289+ else
290+
291+ if (channels == 2 )
292+ {
293+ Vec2f value (cv::randu<float >(), cv::randu<float >());
294+ n += sign (cv::norm (value) > eps_32);
295+ src.at <Vec2f>(i, j) = value;
296+ }
297+
298+ else
299+
300+ if (channels == 3 )
301+ {
302+ Vec3f value (cv::randu<float >(), cv::randu<float >(), cv::randu<float >());
303+ n += sign (cv::norm (value) > eps_32);
304+ src.at <Vec3f>(i, j) = value;
305+ }
306+
307+ else
308+
309+ {
310+ Vec4f value (cv::randu<float >(), cv::randu<float >(), cv::randu<float >(), cv::randu<float >());
311+ n += sign (cv::norm (value) > eps_32);
312+ src.at <Vec4f>(i, j) = value;
313+ }
314+
315+ break ;
316+ }
317+
318+ case CV_64F:
319+ {
320+ if (channels == 1 )
321+ {
322+ double value = cv::randu<double >();
323+ n += sign (fabs (value) > eps_64);
324+ src.at <double >(i, j) = value;
325+ }
326+
327+ else
328+
329+ if (channels == 2 )
330+ {
331+ Vec2d value (cv::randu<double >(), cv::randu<double >());
332+ n += sign (cv::norm (value) > eps_64);
333+ src.at <Vec2d>(i, j) = value;
334+ }
335+
336+ else
337+
338+ if (channels == 3 )
339+ {
340+ Vec3d value (cv::randu<double >(), cv::randu<double >(), cv::randu<double >());
341+ n += sign (cv::norm (value) > eps_64);
342+ src.at <Vec3d>(i, j) = value;
343+ }
344+
345+ else
346+
347+ {
348+ Vec4d value (cv::randu<double >(), cv::randu<double >(), cv::randu<double >(), cv::randu<double >());
349+ n += sign (cv::norm (value) > eps_64);
350+ src.at <Vec4d>(i, j) = value;
351+ }
352+
353+ break ;
354+ }
355+
356+ default : break ;
357+ }
358+ }
359+
360+ }
361+
362+ void CV_CountNonZeroTest::generate_src_float_data (cv::Size size, int type, int channels, int distribution)
363+ {
364+ src.create (size, CV_MAKETYPE (type, channels));
365+
366+ double mean = 0.0 , sigma = 1.0 ;
367+ double left = -1.0 , right = 1.0 ;
368+
369+ RNG& rng = ts->get_rng ();
370+
371+ if (distribution == RNG::NORMAL)
372+ rng.fill (src, RNG::NORMAL, Scalar::all (mean), Scalar::all (sigma));
373+ else if (distribution == RNG::UNIFORM)
374+ rng.fill (src, RNG::UNIFORM, Scalar::all (left), Scalar::all (right));
375+ }
376+
377+ void CV_CountNonZeroTest::run (int )
378+ {
379+
380+ }
381+
382+ TEST (Core_CountNonZero, accuracy) { CV_CountNonZeroTest test; test.safe_run (); }
0 commit comments