From b5bbce5b54572e7797c79dc7fc81ea7c4f00475c Mon Sep 17 00:00:00 2001 From: Alexander Reshetnikov Date: Thu, 19 Jan 2012 16:30:35 +0000 Subject: [PATCH] Completed countNonZero test (found that it's already exist, so new implementation isn't used now). --- modules/core/test/test_countnonzero.cpp | 464 ++++++++++------------------- modules/imgproc/test/test_boundingrect.cpp | 10 + 2 files changed, 160 insertions(+), 314 deletions(-) diff --git a/modules/core/test/test_countnonzero.cpp b/modules/core/test/test_countnonzero.cpp index 2ab12e3..4d76fd6 100644 --- a/modules/core/test/test_countnonzero.cpp +++ b/modules/core/test/test_countnonzero.cpp @@ -4,14 +4,17 @@ using namespace cv; using namespace std; +#define CORE_COUNTNONZERO_ERROR_COUNT 1 + +#define MESSAGE_ERROR_COUNT "Count non zero elements returned by OpenCV function is incorrect." + #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 +#define MAX_WIDTH 100 +#define MAX_HEIGHT 100 class CV_CountNonZeroTest: public cvtest::BaseTest { @@ -23,360 +26,193 @@ class CV_CountNonZeroTest: public cvtest::BaseTest void run (int); private: - float eps_32; double eps_64; Mat src; + float eps_32; + double eps_64; + Mat src; + int current_type; - 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 generate_src_data(cv::Size size, int type); + void generate_src_data(cv::Size size, int type, int count_non_zero); + void generate_src_stat_data(cv::Size size, int type, int distribution); + + int get_count_non_zero(); - void checking_function_work(); - void checking_function_work(int count_non_zero); + void print_information(int right, int result); }; -CV_CountNonZeroTest::CV_CountNonZeroTest(): eps_32(1e-2), eps_64(1e-4), src(Mat()) {} +CV_CountNonZeroTest::CV_CountNonZeroTest(): eps_32(1e-8), eps_64(1e-16), src(Mat()), current_type(-1) {} CV_CountNonZeroTest::~CV_CountNonZeroTest() {} -void CV_CountNonZeroTest::generate_src_data(cv::Size size, int type, int channels) +void CV_CountNonZeroTest::generate_src_data(cv::Size size, int type) { - src.create(size, CV_MAKETYPE(type, channels)); + src.create(size, CV_MAKETYPE(type, 1)); - for (size_t i = 0; i < size.width; ++i) - for (size_t j = 0; j < size.height; ++j) + for (size_t j = 0; j < size.width; ++j) + for (size_t i = 0; i < size.height; ++i) + switch (type) { - 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; - } + case CV_8U: { src.at(i, j) = cv::randu(); break; } + case CV_8S: { src.at(i, j) = cv::randu() - 128; break; } + case CV_16U: { src.at(i, j) = cv::randu(); break; } + case CV_16S: { src.at(i, j) = cv::randu(); break; } + case CV_32S: { src.at(i, j) = cv::randu(); break; } + case CV_32F: { src.at(i, j) = cv::randu(); break; } + case CV_64F: { src.at(i, j) = cv::randu(); break; } + default: break; } } -void CV_CountNonZeroTest::generate_src_data(cv::Size size, int type, int channels, int count_non_zero) +void CV_CountNonZeroTest::generate_src_data(cv::Size size, int type, int count_non_zero) { - src = Mat::zeros(size, CV_MAKETYPE(type, channels)); + src = Mat::zeros(size, CV_MAKETYPE(type, 1)); - int n = -1; + int n = 0; RNG& rng = ts->get_rng(); while (n < count_non_zero) { - RNG& rng = ts->get_rng(); - - size_t i = rng.next()%size.height, j = rng.next()%size.width; + 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++;} - } + switch (type) + { + case CV_8U: { if (!src.at(i, j)) {src.at(i, j) = cv::randu(); n += abs(sign(src.at(i, j)));} break; } + case CV_8S: { if (!src.at(i, j)) {src.at(i, j) = cv::randu() - 128; n += abs(sign(src.at(i, j)));} break; } + case CV_16U: { if (!src.at(i, j)) {src.at(i, j) = cv::randu(); n += abs(sign(src.at(i, j)));} break; } + case CV_16S: { if (!src.at(i, j)) {src.at(i, j) = cv::randu(); n += abs(sign(src.at(i, j)));} break; } + case CV_32S: { if (!src.at(i, j)) {src.at(i, j) = cv::randu(); n += abs(sign(src.at(i, j)));} break; } + case CV_32F: { if (fabs(src.at(i, j)) <= eps_32) {src.at(i, j) = cv::randu(); n += sign(fabs(src.at(i, j)) > eps_32);} break; } + case CV_64F: { if (fabs(src.at(i, j)) <= eps_64) {src.at(i, j) = cv::randu(); n += sign(fabs(src.at(i, j)) > eps_64);} break; } + + default: break; + } + } + +} - break; - } +void CV_CountNonZeroTest::generate_src_stat_data(cv::Size size, int type, int distribution) +{ + src.create(size, CV_MAKETYPE(type, 1)); - 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++;} - } + double mean = 0.0, sigma = 1.0; + double left = -1.0, right = 1.0; - else if (channels == 3) - { - Vec3s value(cv::randu(), cv::randu(), cv::randu()); - if (value != Vec3s(0, 0, 0)) {src.at(i, j) = value; n++;} - } + RNG& rng = ts->get_rng(); - else + 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)); +} - { - Vec4s value(cv::randu(), cv::randu(), cv::randu(), cv::randu()); - if (value != Vec4s(0, 0, 0, 0)) {src.at(i, j) = value; n++;} - } +int CV_CountNonZeroTest::get_count_non_zero() +{ + int result = 0, channels = src.channels(); - break; - } + for (size_t i = 0; i < src.rows; ++i) + for (size_t j = 0; j < src.cols; ++j) - 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++;} - } + if (current_type == CV_8U) result += abs(sign(src.at(i, j))); + + else if (current_type == CV_8S) result += abs(sign(src.at(i, j))); - 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 if (current_type == CV_16U) result += abs(sign(src.at(i, j))); - else + else if (current_type == CV_16S) result += abs(sign(src.at(i, j))); - { - Vec4i value(cv::randu(), cv::randu(), cv::randu(), cv::randu()); - if (value != Vec4i(0, 0, 0, 0)) {src.at(i, j) = value; n++;} - } + else if (current_type == CV_32S) result += abs(sign(src.at(i, j))); - break; - } + else if (current_type == CV_32F) result += sign(fabs(src.at(i, j)) > eps_32); + else result += sign(fabs(src.at(i, j)) > eps_64); - case CV_32F: - { - if (channels == 1) - { - float value = cv::randu(); - n += sign(fabs(value) > eps_32); - src.at(i, j) = value; - } - - else + return result; +} - if (channels == 2) - { - Vec2f value(cv::randu(), cv::randu()); - n += sign(cv::norm(value) > eps_32); - src.at(i, j) = value; - } +void CV_CountNonZeroTest::print_information(int right, int result) +{ + cout << endl; cout << "Checking for the work of countNonZero function..." << endl; cout << endl; + cout << "Type of Mat: "; + switch (current_type) + { + case 0: {cout << "CV_8U"; break;} + case 1: {cout << "CV_8S"; break;} + case 2: {cout << "CV_16U"; break;} + case 3: {cout << "CV_16S"; break;} + case 4: {cout << "CV_32S"; break;} + case 5: {cout << "CV_32F"; break;} + case 6: {cout << "CV_64F"; break;} + default: break; + } + cout << endl; + cout << "Number of rows: " << src.rows << " Number of cols: " << src.cols << endl; + cout << "True count non zero elements: " << right << " Result: " << result << endl; + cout << endl; +} - else +void CV_CountNonZeroTest::run(int) +{ + const size_t N = 1500; - if (channels == 3) - { - Vec3f value(cv::randu(), cv::randu(), cv::randu()); - n += sign(cv::norm(value) > eps_32); - src.at(i, j) = value; - } - - else - + for (int k = 1; k <= 3; ++k) + for (size_t i = 0; i < N; ++i) + { + RNG& rng = ts->get_rng(); + + int w = rng.next()%MAX_WIDTH + 1, h = rng.next()%MAX_HEIGHT + 1; + + current_type = rng.next()%7; + + switch (k) + { + case 1: { + generate_src_data(Size(w, h), current_type); + int right = get_count_non_zero(), result = countNonZero(src); + if (result != right) + { + cout << "Number of experiment: " << i << endl; + cout << "Method of data generation: RANDOM" << endl; + print_information(right, result); + CV_Error(CORE_COUNTNONZERO_ERROR_COUNT, MESSAGE_ERROR_COUNT); + return; + } + + break; + } + + case 2: { + int count_non_zero = rng.next()%(w*h); + generate_src_data(Size(w, h), current_type, count_non_zero); + int result = countNonZero(src); + if (result != count_non_zero) { - Vec4f value(cv::randu(), cv::randu(), cv::randu(), cv::randu()); - n += sign(cv::norm(value) > eps_32); - src.at(i, j) = value; + cout << "Number of experiment: " << i << endl; + cout << "Method of data generation: HALF-RANDOM" << endl; + print_information(count_non_zero, result); + CV_Error(CORE_COUNTNONZERO_ERROR_COUNT, MESSAGE_ERROR_COUNT); + return; } 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 - + case 3: { + int distribution = cv::randu()%2; + generate_src_stat_data(Size(w, h), current_type, distribution); + int right = get_count_non_zero(), result = countNonZero(src); + if (right != result) { - Vec4d value(cv::randu(), cv::randu(), cv::randu(), cv::randu()); - n += sign(cv::norm(value) > eps_64); - src.at(i, j) = value; + cout << "Number of experiment: " << i << endl; + cout << "Method of data generation: STATISTIC" << endl; + print_information(right, result); + CV_Error(CORE_COUNTNONZERO_ERROR_COUNT, MESSAGE_ERROR_COUNT); + return; } - break; - } + break; + } - default: 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(); } \ No newline at end of file +// TEST (Core_CountNonZero, accuracy) { CV_CountNonZeroTest test; test.safe_run(); } \ No newline at end of file diff --git a/modules/imgproc/test/test_boundingrect.cpp b/modules/imgproc/test/test_boundingrect.cpp index 2321953..d150a89 100644 --- a/modules/imgproc/test/test_boundingrect.cpp +++ b/modules/imgproc/test/test_boundingrect.cpp @@ -22,7 +22,17 @@ CV_BoundingRectTest::~CV_BoundingRectTest() {} void CV_BoundingRectTest::run(int) { + const int MAX_WIDTH = 100; + const int MAX_HEIGHT = 100; + const int N = 100; + RNG& rng = ts->get_rng(); + + for (size_t i = 0; i < N; ++i) + { + int w = rng.next()%MAX_WIDTH + 1, h = rng.next()%MAX_HEIGHT + 1; + cv::Mat src(h, w, CV_8U); cv::randu(src, Scalar_::all(false), Scalar_::all(true)); + } } TEST (Imgproc_BoundingRect, accuracy) { CV_BoundingRectTest test; test.safe_run(); } \ No newline at end of file -- 2.7.4