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