--- /dev/null
+#include "test_precomp.hpp"\r
+#include <time.h>\r
+\r
+using namespace cv;\r
+using namespace std;\r
+\r
+#define sign(a) a > 0 ? 1 : a == 0 ? 0 : -1\r
+\r
+const int FLOAT_TYPE [2] = {CV_32F, CV_64F};\r
+const int INT_TYPE [5] = {CV_8U, CV_8S, CV_16U, CV_16S, CV_32S};\r
+\r
+#define MAX_CHANNELS 4\r
+#define MAX_WIDTH 1e+2\r
+#define MAX_HEIGHT 1e+2\r
+\r
+class CV_CountNonZeroTest: public cvtest::BaseTest\r
+{\r
+ public:\r
+ CV_CountNonZeroTest();\r
+ ~CV_CountNonZeroTest();\r
+\r
+ protected:\r
+ void run (int);\r
+\r
+ private:\r
+ float eps_32; double eps_64; Mat src;\r
+ \r
+ void generate_src_data(cv::Size size, int type, int channels);\r
+ void generate_src_data(cv::Size size, int type, int channels, int count_non_zero);\r
+ void generate_src_float_data(cv::Size size, int type, int channels, int distribution);\r
+\r
+ void checking_function_work(); \r
+ void checking_function_work(int count_non_zero); \r
+};\r
+\r
+CV_CountNonZeroTest::CV_CountNonZeroTest(): eps_32(1e-2), eps_64(1e-4), src(Mat()) {}\r
+CV_CountNonZeroTest::~CV_CountNonZeroTest() {}\r
+\r
+void CV_CountNonZeroTest::generate_src_data(cv::Size size, int type, int channels)\r
+{\r
+ src.create(size, CV_MAKETYPE(type, channels));\r
+\r
+ for (size_t i = 0; i < size.width; ++i)\r
+ for (size_t j = 0; j < size.height; ++j)\r
+ {\r
+ if (type == CV_8U) switch (channels)\r
+ {\r
+ case 1: {src.at<uchar>(j, i) = cv::randu<uchar>(); break;}\r
+ case 2: {src.at<Vec2b>(j, i) = Vec2b(cv::randu<uchar>(), cv::randu<uchar>()); break;}\r
+ case 3: {src.at<Vec3b>(j, i) = Vec3b(cv::randu<uchar>(), cv::randu<uchar>(), cv::randu<uchar>()); break;}\r
+ case 4: {src.at<Vec4b>(j, i) = Vec4b(cv::randu<uchar>(), cv::randu<uchar>(), cv::randu<uchar>(), cv::randu<uchar>()); break;}\r
+\r
+ default: break;\r
+ }\r
+\r
+ else if (type == CV_8S) switch (channels)\r
+ {\r
+ case 1: {src.at<char>(j,i) = cv::randu<uchar>()-128; break; }\r
+ case 2: {src.at<Vec<char, 2>>(j, i) = Vec<char, 2>(cv::randu<uchar>()-128, cv::randu<uchar>()-128); break;}\r
+ 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
+ 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
+ default:break;\r
+ }\r
+ \r
+ else if (type == CV_16U) switch (channels)\r
+ {\r
+ case 1: {src.at<ushort>(j, i) = cv::randu<ushort>(); break;}\r
+ case 2: {src.at<Vec<ushort, 2>>(j, i) = Vec<ushort, 2>(cv::randu<ushort>(), cv::randu<ushort>()); break;}\r
+ case 3: {src.at<Vec<ushort, 3>>(j, i) = Vec<ushort, 3>(cv::randu<ushort>(), cv::randu<ushort>(), cv::randu<ushort>()); break;}\r
+ 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
+ default: break;\r
+ }\r
+\r
+ else if (type == CV_16S) switch (channels)\r
+ {\r
+ case 1: {src.at<short>(j, i) = cv::randu<short>(); break;}\r
+ case 2: {src.at<Vec2s>(j, i) = Vec2s(cv::randu<short>(), cv::randu<short>()); break;}\r
+ case 3: {src.at<Vec3s>(j, i) = Vec3s(cv::randu<short>(), cv::randu<short>(), cv::randu<short>()); break;}\r
+ case 4: {src.at<Vec4s>(j, i) = Vec4s(cv::randu<short>(), cv::randu<short>(), cv::randu<short>(), cv::randu<short>()); break;}\r
+ default: break;\r
+ }\r
+\r
+ else if (type == CV_32S) switch (channels)\r
+ {\r
+ case 1: {src.at<int>(j, i) = cv::randu<int>(); break;}\r
+ case 2: {src.at<Vec2i>(j, i) = Vec2i(cv::randu<int>(), cv::randu<int>()); break;}\r
+ case 3: {src.at<Vec3i>(j, i) = Vec3i(cv::randu<int>(), cv::randu<int>(), cv::randu<int>()); break;}\r
+ case 4: {src.at<Vec4i>(j, i) = Vec4i(cv::randu<int>(), cv::randu<int>(), cv::randu<int>(), cv::randu<int>()); break;}\r
+ default: break;\r
+ }\r
+\r
+ else if (type == CV_32F) switch (channels)\r
+ {\r
+ case 1: {src.at<float>(j, i) = cv::randu<float>(); break;}\r
+ case 2: {src.at<Vec2f>(j, i) = Vec2i(cv::randu<float>(), cv::randu<float>()); break;}\r
+ case 3: {src.at<Vec3f>(j, i) = Vec3i(cv::randu<float>(), cv::randu<float>(), cv::randu<float>()); break;}\r
+ case 4: {src.at<Vec4f>(j, i) = Vec4i(cv::randu<float>(), cv::randu<float>(), cv::randu<float>(), cv::randu<float>()); break;}\r
+ default: break;\r
+ }\r
+\r
+ else if (type == CV_64F) switch (channels)\r
+ {\r
+ case 1: {src.at<double>(j, i) = cv::randu<double>(); break;}\r
+ case 2: {src.at<Vec2d>(j, i) = Vec2d(cv::randu<double>(), cv::randu<double>()); break;}\r
+ case 3: {src.at<Vec3d>(j, i) = Vec3d(cv::randu<double>(), cv::randu<double>(), cv::randu<double>()); break;}\r
+ case 4: {src.at<Vec4d>(j, i) = Vec4d(cv::randu<double>(), cv::randu<double>(), cv::randu<double>(), cv::randu<double>()); break;}\r
+ default: break;\r
+ }\r
+ }\r
+}\r
+\r
+void CV_CountNonZeroTest::generate_src_data(cv::Size size, int type, int channels, int count_non_zero)\r
+{\r
+ src = Mat::zeros(size, CV_MAKETYPE(type, channels));\r
+ \r
+ int n = -1;\r
+\r
+ while (n < count_non_zero)\r
+ {\r
+ RNG& rng = ts->get_rng();\r
+\r
+ size_t i = rng.next()%size.height, j = rng.next()%size.width;\r
+ \r
+ switch (type)\r
+ {\r
+ case CV_8U: \r
+ {\r
+ if (channels == 1) \r
+ {\r
+ uchar value = cv::randu<uchar>();\r
+ if (value != 0) {src.at<uchar>(i, j) = value; n++;}\r
+ }\r
+ \r
+ else if (channels == 2)\r
+ {\r
+ Vec2b value(cv::randu<uchar>(), cv::randu<uchar>());\r
+ if (value != Vec2b(0, 0)) {src.at<Vec2b>(i, j) = value; n++;}\r
+ }\r
+\r
+ else if (channels == 3)\r
+ {\r
+ Vec3b value(cv::randu<uchar>(), cv::randu<uchar>(), cv::randu<uchar>());\r
+ if (value != Vec3b(0, 0, 0)) {src.at<Vec3b>(i, j) = value; n++;}\r
+ }\r
+\r
+ else\r
+\r
+ {\r
+ Vec4b value(cv::randu<uchar>(), cv::randu<uchar>(), cv::randu<uchar>(), cv::randu<uchar>());\r
+ if (value != Vec4b(0, 0, 0, 0)) {src.at<Vec4b>(i, j) = value; n++;}\r
+ }\r
+\r
+ break;\r
+ }\r
+\r
+ case CV_8S:\r
+ {\r
+ if (channels == 1) \r
+ {\r
+ char value = cv::randu<uchar>()-128;\r
+ if (value != 0) {src.at<char>(i, j) = value; n++;}\r
+ }\r
+ \r
+ else if (channels == 2)\r
+ {\r
+ Vec<char, 2> value(cv::randu<uchar>()-128, cv::randu<uchar>()-128);\r
+ if (value != Vec<char, 2>(0, 0)) {src.at<Vec<char, 2>>(i, j) = value; n++;}\r
+ }\r
+\r
+ else if (channels == 3)\r
+ {\r
+ Vec<char, 3> value(cv::randu<uchar>()-128, cv::randu<uchar>()-128, cv::randu<uchar>()-128);\r
+ if (value != Vec<char, 3>(0, 0, 0)) {src.at<Vec<char, 3>>(i, j) = value; n++;}\r
+ }\r
+\r
+ else\r
+\r
+ {\r
+ Vec<char, 4> value(cv::randu<uchar>()-128, cv::randu<uchar>()-128, cv::randu<uchar>()-128, cv::randu<uchar>()-128);\r
+ if (value != Vec<char, 4>(0, 0, 0, 0)) {src.at<Vec<char, 4>>(i, j) = value; n++;}\r
+ }\r
+\r
+ break;\r
+ }\r
+\r
+ case CV_16U:\r
+ {\r
+ if (channels == 1) \r
+ {\r
+ ushort value = cv::randu<ushort>();\r
+ n += abs(sign(value));\r
+ src.at<ushort>(i, j) = value;\r
+ }\r
+ \r
+ else if (channels == 2)\r
+ {\r
+ Vec<ushort, 2> value(cv::randu<ushort>(), cv::randu<ushort>());\r
+ if (value != Vec<ushort, 2>(0, 0)) {src.at<Vec<ushort, 2>>(i, j) = value; n++;}\r
+ }\r
+\r
+ else if (channels == 3)\r
+ {\r
+ Vec<ushort, 3> value(cv::randu<ushort>(), cv::randu<ushort>(), cv::randu<ushort>());\r
+ if (value != Vec<ushort, 3>(0, 0, 0)) {src.at<Vec<ushort, 3>>(i, j) = value; n++;}\r
+ }\r
+\r
+ else\r
+\r
+ {\r
+ Vec<ushort, 4> value(cv::randu<ushort>(), cv::randu<ushort>(), cv::randu<ushort>(), cv::randu<ushort>());\r
+ if (value != Vec<ushort, 4>(0, 0, 0, 0)) {src.at<Vec<ushort, 4>>(i, j) = value; n++;}\r
+ }\r
+\r
+ break;\r
+ }\r
+\r
+ case CV_16S:\r
+ {\r
+ if (channels == 1) \r
+ {\r
+ short value = cv::randu<short>();\r
+ n += abs(sign(value));\r
+ src.at<short>(i, j) = value;\r
+ }\r
+ \r
+ else if (channels == 2)\r
+ {\r
+ Vec2s value(cv::randu<short>(), cv::randu<short>());\r
+ if (value != Vec2s(0, 0)) {src.at<Vec2s>(i, j) = value; n++;}\r
+ }\r
+\r
+ else if (channels == 3)\r
+ {\r
+ Vec3s value(cv::randu<short>(), cv::randu<short>(), cv::randu<short>());\r
+ if (value != Vec3s(0, 0, 0)) {src.at<Vec3s>(i, j) = value; n++;}\r
+ }\r
+\r
+ else\r
+\r
+ {\r
+ Vec4s value(cv::randu<short>(), cv::randu<short>(), cv::randu<short>(), cv::randu<short>());\r
+ if (value != Vec4s(0, 0, 0, 0)) {src.at<Vec4s>(i, j) = value; n++;}\r
+ }\r
+\r
+ break;\r
+ }\r
+\r
+ case CV_32S:\r
+ {\r
+ if (channels == 1) \r
+ {\r
+ int value = cv::randu<int>();\r
+ n += abs(sign(value));\r
+ src.at<int>(i, j) = value;\r
+ }\r
+ \r
+ else if (channels == 2)\r
+ {\r
+ Vec2i value(cv::randu<int>(), cv::randu<int>());\r
+ if (value != Vec2i(0, 0)) {src.at<Vec2i>(i, j) = value; n++;}\r
+ }\r
+\r
+ else if (channels == 3)\r
+ {\r
+ Vec3i value(cv::randu<int>(), cv::randu<int>(), cv::randu<int>());\r
+ if (value != Vec3i(0, 0, 0)) {src.at<Vec3i>(i, j) = value; n++;}\r
+ }\r
+\r
+ else\r
+\r
+ {\r
+ Vec4i value(cv::randu<int>(), cv::randu<int>(), cv::randu<int>(), cv::randu<int>());\r
+ if (value != Vec4i(0, 0, 0, 0)) {src.at<Vec4i>(i, j) = value; n++;}\r
+ }\r
+\r
+ break;\r
+ }\r
+\r
+\r
+ case CV_32F:\r
+ {\r
+ if (channels == 1)\r
+ {\r
+ float value = cv::randu<float>();\r
+ n += sign(fabs(value) > eps_32);\r
+ src.at<float>(i, j) = value; \r
+ }\r
+ \r
+ else \r
+\r
+ if (channels == 2)\r
+ {\r
+ Vec2f value(cv::randu<float>(), cv::randu<float>());\r
+ n += sign(cv::norm(value) > eps_32); \r
+ src.at<Vec2f>(i, j) = value; \r
+ }\r
+\r
+ else \r
+\r
+ if (channels == 3)\r
+ {\r
+ Vec3f value(cv::randu<float>(), cv::randu<float>(), cv::randu<float>());\r
+ n += sign(cv::norm(value) > eps_32); \r
+ src.at<Vec3f>(i, j) = value;\r
+ }\r
+ \r
+ else\r
+ \r
+ {\r
+ Vec4f value(cv::randu<float>(), cv::randu<float>(), cv::randu<float>(), cv::randu<float>());\r
+ n += sign(cv::norm(value) > eps_32);\r
+ src.at<Vec4f>(i, j) = value;\r
+ }\r
+\r
+ break;\r
+ }\r
+\r
+ case CV_64F:\r
+ {\r
+ if (channels == 1)\r
+ {\r
+ double value = cv::randu<double>();\r
+ n += sign(fabs(value) > eps_64);\r
+ src.at<double>(i, j) = value; \r
+ }\r
+ \r
+ else \r
+\r
+ if (channels == 2)\r
+ {\r
+ Vec2d value(cv::randu<double>(), cv::randu<double>());\r
+ n += sign(cv::norm(value) > eps_64); \r
+ src.at<Vec2d>(i, j) = value; \r
+ }\r
+\r
+ else \r
+\r
+ if (channels == 3)\r
+ {\r
+ Vec3d value(cv::randu<double>(), cv::randu<double>(), cv::randu<double>());\r
+ n += sign(cv::norm(value) > eps_64); \r
+ src.at<Vec3d>(i, j) = value;\r
+ }\r
+ \r
+ else\r
+ \r
+ {\r
+ Vec4d value(cv::randu<double>(), cv::randu<double>(), cv::randu<double>(), cv::randu<double>());\r
+ n += sign(cv::norm(value) > eps_64);\r
+ src.at<Vec4d>(i, j) = value;\r
+ }\r
+\r
+ break;\r
+ }\r
+\r
+ default: break;\r
+ }\r
+ }\r
+ \r
+}\r
+\r
+void CV_CountNonZeroTest::generate_src_float_data(cv::Size size, int type, int channels, int distribution)\r
+{\r
+ src.create(size, CV_MAKETYPE(type, channels));\r
+\r
+ double mean = 0.0, sigma = 1.0;\r
+ double left = -1.0, right = 1.0;\r
+\r
+ RNG& rng = ts->get_rng();\r
+\r
+ if (distribution == RNG::NORMAL) \r
+ rng.fill(src, RNG::NORMAL, Scalar::all(mean), Scalar::all(sigma));\r
+ else if (distribution == RNG::UNIFORM)\r
+ rng.fill(src, RNG::UNIFORM, Scalar::all(left), Scalar::all(right));\r
+}\r
+\r
+void CV_CountNonZeroTest::run(int)\r
+{\r
+ \r
+}\r
+\r
+TEST (Core_CountNonZero, accuracy) { CV_CountNonZeroTest test; test.safe_run(); }
\ No newline at end of file