1 #include "test_precomp.hpp"
\r
7 #define CORE_COUNTNONZERO_ERROR_COUNT 1
\r
9 #define MESSAGE_ERROR_COUNT "Count non zero elements returned by OpenCV function is incorrect."
\r
11 #define sign(a) a > 0 ? 1 : a == 0 ? 0 : -1
\r
13 const int FLOAT_TYPE [2] = {CV_32F, CV_64F};
\r
14 const int INT_TYPE [5] = {CV_8U, CV_8S, CV_16U, CV_16S, CV_32S};
\r
16 #define MAX_WIDTH 100
\r
17 #define MAX_HEIGHT 100
\r
19 class CV_CountNonZeroTest: public cvtest::BaseTest
\r
22 CV_CountNonZeroTest();
\r
23 ~CV_CountNonZeroTest();
\r
34 void generate_src_data(cv::Size size, int type);
\r
35 void generate_src_data(cv::Size size, int type, int count_non_zero);
\r
36 void generate_src_stat_data(cv::Size size, int type, int distribution);
\r
38 int get_count_non_zero();
\r
40 void print_information(int right, int result);
\r
43 CV_CountNonZeroTest::CV_CountNonZeroTest(): eps_32(1e-8), eps_64(1e-16), src(Mat()), current_type(-1) {}
\r
44 CV_CountNonZeroTest::~CV_CountNonZeroTest() {}
\r
46 void CV_CountNonZeroTest::generate_src_data(cv::Size size, int type)
\r
48 src.create(size, CV_MAKETYPE(type, 1));
\r
50 for (size_t j = 0; j < size.width; ++j)
\r
51 for (size_t i = 0; i < size.height; ++i)
\r
54 case CV_8U: { src.at<uchar>(i, j) = cv::randu<uchar>(); break; }
\r
55 case CV_8S: { src.at<char>(i, j) = cv::randu<uchar>() - 128; break; }
\r
56 case CV_16U: { src.at<ushort>(i, j) = cv::randu<ushort>(); break; }
\r
57 case CV_16S: { src.at<short>(i, j) = cv::randu<short>(); break; }
\r
58 case CV_32S: { src.at<int>(i, j) = cv::randu<int>(); break; }
\r
59 case CV_32F: { src.at<float>(i, j) = cv::randu<float>(); break; }
\r
60 case CV_64F: { src.at<double>(i, j) = cv::randu<double>(); break; }
\r
65 void CV_CountNonZeroTest::generate_src_data(cv::Size size, int type, int count_non_zero)
\r
67 src = Mat::zeros(size, CV_MAKETYPE(type, 1));
\r
69 int n = 0; RNG& rng = ts->get_rng();
\r
71 while (n < count_non_zero)
\r
73 size_t i = rng.next()%size.height, j = rng.next()%size.width;
\r
77 case CV_8U: { if (!src.at<uchar>(i, j)) {src.at<uchar>(i, j) = cv::randu<uchar>(); n += abs(sign(src.at<uchar>(i, j)));} break; }
\r
78 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
79 case CV_16U: { if (!src.at<ushort>(i, j)) {src.at<ushort>(i, j) = cv::randu<ushort>(); n += abs(sign(src.at<ushort>(i, j)));} break; }
\r
80 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
81 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
82 case CV_32F: { if (fabs(src.at<float>(i, j)) <= eps_32) {src.at<float>(i, j) = cv::randu<float>(); n += sign(fabs(src.at<float>(i, j)) > eps_32);} break; }
\r
83 case CV_64F: { if (fabs(src.at<double>(i, j)) <= eps_64) {src.at<double>(i, j) = cv::randu<double>(); n += sign(fabs(src.at<double>(i, j)) > eps_64);} break; }
\r
91 void CV_CountNonZeroTest::generate_src_stat_data(cv::Size size, int type, int distribution)
\r
93 src.create(size, CV_MAKETYPE(type, 1));
\r
95 double mean = 0.0, sigma = 1.0;
\r
96 double left = -1.0, right = 1.0;
\r
98 RNG& rng = ts->get_rng();
\r
100 if (distribution == RNG::NORMAL)
\r
101 rng.fill(src, RNG::NORMAL, Scalar::all(mean), Scalar::all(sigma));
\r
102 else if (distribution == RNG::UNIFORM)
\r
103 rng.fill(src, RNG::UNIFORM, Scalar::all(left), Scalar::all(right));
\r
106 int CV_CountNonZeroTest::get_count_non_zero()
\r
108 int result = 0, channels = src.channels();
\r
110 for (size_t i = 0; i < src.rows; ++i)
\r
111 for (size_t j = 0; j < src.cols; ++j)
\r
113 if (current_type == CV_8U) result += abs(sign(src.at<uchar>(i, j)));
\r
115 else if (current_type == CV_8S) result += abs(sign(src.at<char>(i, j)));
\r
117 else if (current_type == CV_16U) result += abs(sign(src.at<ushort>(i, j)));
\r
119 else if (current_type == CV_16S) result += abs(sign(src.at<short>(i, j)));
\r
121 else if (current_type == CV_32S) result += abs(sign(src.at<int>(i, j)));
\r
123 else if (current_type == CV_32F) result += sign(fabs(src.at<float>(i, j)) > eps_32);
\r
125 else result += sign(fabs(src.at<double>(i, j)) > eps_64);
\r
130 void CV_CountNonZeroTest::print_information(int right, int result)
\r
132 cout << endl; cout << "Checking for the work of countNonZero function..." << endl; cout << endl;
\r
133 cout << "Type of Mat: ";
\r
134 switch (current_type)
\r
136 case 0: {cout << "CV_8U"; break;}
\r
137 case 1: {cout << "CV_8S"; break;}
\r
138 case 2: {cout << "CV_16U"; break;}
\r
139 case 3: {cout << "CV_16S"; break;}
\r
140 case 4: {cout << "CV_32S"; break;}
\r
141 case 5: {cout << "CV_32F"; break;}
\r
142 case 6: {cout << "CV_64F"; break;}
\r
146 cout << "Number of rows: " << src.rows << " Number of cols: " << src.cols << endl;
\r
147 cout << "True count non zero elements: " << right << " Result: " << result << endl;
\r
151 void CV_CountNonZeroTest::run(int)
\r
153 const size_t N = 1500;
\r
155 for (int k = 1; k <= 3; ++k)
\r
156 for (size_t i = 0; i < N; ++i)
\r
158 RNG& rng = ts->get_rng();
\r
160 int w = rng.next()%MAX_WIDTH + 1, h = rng.next()%MAX_HEIGHT + 1;
\r
162 current_type = rng.next()%7;
\r
167 generate_src_data(Size(w, h), current_type);
\r
168 int right = get_count_non_zero(), result = countNonZero(src);
\r
169 if (result != right)
\r
171 cout << "Number of experiment: " << i << endl;
\r
172 cout << "Method of data generation: RANDOM" << endl;
\r
173 print_information(right, result);
\r
174 CV_Error(CORE_COUNTNONZERO_ERROR_COUNT, MESSAGE_ERROR_COUNT);
\r
182 int count_non_zero = rng.next()%(w*h);
\r
183 generate_src_data(Size(w, h), current_type, count_non_zero);
\r
184 int result = countNonZero(src);
\r
185 if (result != count_non_zero)
\r
187 cout << "Number of experiment: " << i << endl;
\r
188 cout << "Method of data generation: HALF-RANDOM" << endl;
\r
189 print_information(count_non_zero, result);
\r
190 CV_Error(CORE_COUNTNONZERO_ERROR_COUNT, MESSAGE_ERROR_COUNT);
\r
198 int distribution = cv::randu<uchar>()%2;
\r
199 generate_src_stat_data(Size(w, h), current_type, distribution);
\r
200 int right = get_count_non_zero(), result = countNonZero(src);
\r
201 if (right != result)
\r
203 cout << "Number of experiment: " << i << endl;
\r
204 cout << "Method of data generation: STATISTIC" << endl;
\r
205 print_information(right, result);
\r
206 CV_Error(CORE_COUNTNONZERO_ERROR_COUNT, MESSAGE_ERROR_COUNT);
\r
218 // TEST (Core_CountNonZero, accuracy) { CV_CountNonZeroTest test; test.safe_run(); }