Path: blob/master/modules/core/test/test_countnonzero.cpp
16337 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#include "test_precomp.hpp"4344namespace opencv_test { namespace {4546#define CORE_COUNTNONZERO_ERROR_COUNT 14748#define MESSAGE_ERROR_COUNT "Count non zero elements returned by OpenCV function is incorrect."4950#define sign(a) a > 0 ? 1 : a == 0 ? 0 : -15152#define MAX_WIDTH 10053#define MAX_HEIGHT 1005455class CV_CountNonZeroTest: public cvtest::BaseTest56{57public:58CV_CountNonZeroTest();59~CV_CountNonZeroTest();6061protected:62void run (int);6364private:65float eps_32;66double eps_64;67Mat src;68int current_type;6970void generate_src_data(cv::Size size, int type);71void generate_src_data(cv::Size size, int type, int count_non_zero);72void generate_src_stat_data(cv::Size size, int type, int distribution);7374int get_count_non_zero();7576void print_information(int right, int result);77};7879CV_CountNonZeroTest::CV_CountNonZeroTest(): eps_32(std::numeric_limits<float>::min()), eps_64(std::numeric_limits<double>::min()), src(Mat()), current_type(-1) {}80CV_CountNonZeroTest::~CV_CountNonZeroTest() {}8182void CV_CountNonZeroTest::generate_src_data(cv::Size size, int type)83{84src.create(size, CV_MAKETYPE(type, 1));8586for (int j = 0; j < size.width; ++j)87for (int i = 0; i < size.height; ++i)88switch (type)89{90case CV_8U: { src.at<uchar>(i, j) = cv::randu<uchar>(); break; }91case CV_8S: { src.at<char>(i, j) = cv::randu<uchar>() - 128; break; }92case CV_16U: { src.at<ushort>(i, j) = cv::randu<ushort>(); break; }93case CV_16S: { src.at<short>(i, j) = cv::randu<short>(); break; }94case CV_32S: { src.at<int>(i, j) = cv::randu<int>(); break; }95case CV_32F: { src.at<float>(i, j) = cv::randu<float>(); break; }96case CV_64F: { src.at<double>(i, j) = cv::randu<double>(); break; }97default: break;98}99}100101void CV_CountNonZeroTest::generate_src_data(cv::Size size, int type, int count_non_zero)102{103src = Mat::zeros(size, CV_MAKETYPE(type, 1));104105int n = 0; RNG& rng = ts->get_rng();106107while (n < count_non_zero)108{109int i = rng.next()%size.height, j = rng.next()%size.width;110111switch (type)112{113case CV_8U: { if (!src.at<uchar>(i, j)) {src.at<uchar>(i, j) = cv::randu<uchar>(); n += (src.at<uchar>(i, j) > 0);} break; }114case 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; }115case CV_16U: { if (!src.at<ushort>(i, j)) {src.at<ushort>(i, j) = cv::randu<ushort>(); n += (src.at<ushort>(i, j) > 0);} break; }116case 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; }117case 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; }118case CV_32F: { if (fabs(src.at<float>(i, j)) <= eps_32) {src.at<float>(i, j) = cv::randu<float>(); n += (fabs(src.at<float>(i, j)) > eps_32);} break; }119case CV_64F: { if (fabs(src.at<double>(i, j)) <= eps_64) {src.at<double>(i, j) = cv::randu<double>(); n += (fabs(src.at<double>(i, j)) > eps_64);} break; }120121default: break;122}123}124125}126127void CV_CountNonZeroTest::generate_src_stat_data(cv::Size size, int type, int distribution)128{129src.create(size, CV_MAKETYPE(type, 1));130131double mean = 0.0, sigma = 1.0;132double left = -1.0, right = 1.0;133134RNG& rng = ts->get_rng();135136if (distribution == RNG::NORMAL)137rng.fill(src, RNG::NORMAL, Scalar::all(mean), Scalar::all(sigma));138else if (distribution == RNG::UNIFORM)139rng.fill(src, RNG::UNIFORM, Scalar::all(left), Scalar::all(right));140}141142int CV_CountNonZeroTest::get_count_non_zero()143{144int result = 0;145146for (int i = 0; i < src.rows; ++i)147for (int j = 0; j < src.cols; ++j)148{149if (current_type == CV_8U) result += (src.at<uchar>(i, j) > 0);150else if (current_type == CV_8S) result += abs(sign(src.at<char>(i, j)));151else if (current_type == CV_16U) result += (src.at<ushort>(i, j) > 0);152else if (current_type == CV_16S) result += abs(sign(src.at<short>(i, j)));153else if (current_type == CV_32S) result += abs(sign(src.at<int>(i, j)));154else if (current_type == CV_32F) result += (fabs(src.at<float>(i, j)) > eps_32);155else result += (fabs(src.at<double>(i, j)) > eps_64);156}157158return result;159}160161void CV_CountNonZeroTest::print_information(int right, int result)162{163cout << endl; cout << "Checking for the work of countNonZero function..." << endl; cout << endl;164cout << "Type of Mat: ";165switch (current_type)166{167case 0: {cout << "CV_8U"; break;}168case 1: {cout << "CV_8S"; break;}169case 2: {cout << "CV_16U"; break;}170case 3: {cout << "CV_16S"; break;}171case 4: {cout << "CV_32S"; break;}172case 5: {cout << "CV_32F"; break;}173case 6: {cout << "CV_64F"; break;}174default: break;175}176cout << endl;177cout << "Number of rows: " << src.rows << " Number of cols: " << src.cols << endl;178cout << "True count non zero elements: " << right << " Result: " << result << endl;179cout << endl;180}181182void CV_CountNonZeroTest::run(int)183{184const size_t N = 1500;185186for (int k = 1; k <= 3; ++k)187for (size_t i = 0; i < N; ++i)188{189RNG& rng = ts->get_rng();190191int w = rng.next()%MAX_WIDTH + 1, h = rng.next()%MAX_HEIGHT + 1;192193current_type = rng.next()%7;194195switch (k)196{197case 1: {198generate_src_data(Size(w, h), current_type);199int right = get_count_non_zero(), result = countNonZero(src);200if (result != right)201{202cout << "Number of experiment: " << i << endl;203cout << "Method of data generation: RANDOM" << endl;204print_information(right, result);205CV_Error(CORE_COUNTNONZERO_ERROR_COUNT, MESSAGE_ERROR_COUNT);206return;207}208209break;210}211212case 2: {213int count_non_zero = rng.next()%(w*h);214generate_src_data(Size(w, h), current_type, count_non_zero);215int result = countNonZero(src);216if (result != count_non_zero)217{218cout << "Number of experiment: " << i << endl;219cout << "Method of data generation: HALF-RANDOM" << endl;220print_information(count_non_zero, result);221CV_Error(CORE_COUNTNONZERO_ERROR_COUNT, MESSAGE_ERROR_COUNT);222return;223}224225break;226}227228case 3: {229int distribution = cv::randu<uchar>()%2;230generate_src_stat_data(Size(w, h), current_type, distribution);231int right = get_count_non_zero(), result = countNonZero(src);232if (right != result)233{234cout << "Number of experiment: " << i << endl;235cout << "Method of data generation: STATISTIC" << endl;236print_information(right, result);237CV_Error(CORE_COUNTNONZERO_ERROR_COUNT, MESSAGE_ERROR_COUNT);238return;239}240241break;242}243244default: break;245}246}247}248249TEST (Core_CountNonZero, accuracy) { CV_CountNonZeroTest test; test.safe_run(); }250251252typedef testing::TestWithParam<tuple<int, int> > CountNonZeroND;253254TEST_P (CountNonZeroND, ndim)255{256const int dims = get<0>(GetParam());257const int type = get<1>(GetParam());258const int ONE_SIZE = 5;259260vector<int> sizes(dims);261fill(sizes.begin(), sizes.end(), ONE_SIZE);262263Mat data(sizes, CV_MAKETYPE(type, 1));264data = 0;265EXPECT_EQ(0, cv::countNonZero(data));266data = Scalar::all(1);267int expected = static_cast<int>(pow(static_cast<float>(ONE_SIZE), dims));268EXPECT_EQ(expected, cv::countNonZero(data));269}270271INSTANTIATE_TEST_CASE_P(Core, CountNonZeroND,272testing::Combine(273testing::Range(2, 9),274testing::Values(CV_8U, CV_8S, CV_32F)275)276);277278}} // namespace279280281