提交 07fa62f0 编写于 作者: A Alexander Reshetnikov

some design code changes in new tests

上级 ea5d0155
......@@ -18,26 +18,26 @@ const int INT_TYPE [5] = {CV_8U, CV_8S, CV_16U, CV_16S, CV_32S};
class CV_CountNonZeroTest: public cvtest::BaseTest
{
public:
public:
CV_CountNonZeroTest();
~CV_CountNonZeroTest();
protected:
protected:
void run (int);
private:
float eps_32;
double eps_64;
Mat src;
int current_type;
private:
float eps_32;
double eps_64;
Mat src;
int current_type;
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);
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();
int get_count_non_zero();
void print_information(int right, int result);
void print_information(int right, int result);
};
CV_CountNonZeroTest::CV_CountNonZeroTest(): eps_32(1e-8), eps_64(1e-16), src(Mat()), current_type(-1) {}
......@@ -45,174 +45,174 @@ CV_CountNonZeroTest::~CV_CountNonZeroTest() {}
void CV_CountNonZeroTest::generate_src_data(cv::Size size, int type)
{
src.create(size, CV_MAKETYPE(type, 1));
for (size_t j = 0; j < size.width; ++j)
for (size_t i = 0; i < size.height; ++i)
switch (type)
{
case CV_8U: { src.at<uchar>(i, j) = cv::randu<uchar>(); break; }
case CV_8S: { src.at<char>(i, j) = cv::randu<uchar>() - 128; break; }
case CV_16U: { src.at<ushort>(i, j) = cv::randu<ushort>(); break; }
case CV_16S: { src.at<short>(i, j) = cv::randu<short>(); break; }
case CV_32S: { src.at<int>(i, j) = cv::randu<int>(); break; }
case CV_32F: { src.at<float>(i, j) = cv::randu<float>(); break; }
case CV_64F: { src.at<double>(i, j) = cv::randu<double>(); break; }
default: break;
}
src.create(size, CV_MAKETYPE(type, 1));
for (int j = 0; j < size.width; ++j)
for (int i = 0; i < size.height; ++i)
switch (type)
{
case CV_8U: { src.at<uchar>(i, j) = cv::randu<uchar>(); break; }
case CV_8S: { src.at<char>(i, j) = cv::randu<uchar>() - 128; break; }
case CV_16U: { src.at<ushort>(i, j) = cv::randu<ushort>(); break; }
case CV_16S: { src.at<short>(i, j) = cv::randu<short>(); break; }
case CV_32S: { src.at<int>(i, j) = cv::randu<int>(); break; }
case CV_32F: { src.at<float>(i, j) = cv::randu<float>(); break; }
case CV_64F: { src.at<double>(i, j) = cv::randu<double>(); break; }
default: break;
}
}
void CV_CountNonZeroTest::generate_src_data(cv::Size size, int type, int count_non_zero)
{
src = Mat::zeros(size, CV_MAKETYPE(type, 1));
int n = 0; RNG& rng = ts->get_rng();
while (n < count_non_zero)
{
size_t i = rng.next()%size.height, j = rng.next()%size.width;
switch (type)
{
case CV_8U: { if (!src.at<uchar>(i, j)) {src.at<uchar>(i, j) = cv::randu<uchar>(); n += (src.at<uchar>(i, j) > 0);} break; }
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; }
case CV_16U: { if (!src.at<ushort>(i, j)) {src.at<ushort>(i, j) = cv::randu<ushort>(); n += (src.at<ushort>(i, j) > 0);} break; }
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; }
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; }
case 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; }
case 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; }
default: break;
}
}
src = Mat::zeros(size, CV_MAKETYPE(type, 1));
int n = 0; RNG& rng = ts->get_rng();
while (n < count_non_zero)
{
size_t i = rng.next()%size.height, j = rng.next()%size.width;
switch (type)
{
case CV_8U: { if (!src.at<uchar>(i, j)) {src.at<uchar>(i, j) = cv::randu<uchar>(); n += (src.at<uchar>(i, j) > 0);} break; }
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; }
case CV_16U: { if (!src.at<ushort>(i, j)) {src.at<ushort>(i, j) = cv::randu<ushort>(); n += (src.at<ushort>(i, j) > 0);} break; }
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; }
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; }
case 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; }
case 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; }
default: break;
}
}
}
void CV_CountNonZeroTest::generate_src_stat_data(cv::Size size, int type, int distribution)
{
src.create(size, CV_MAKETYPE(type, 1));
src.create(size, CV_MAKETYPE(type, 1));
double mean = 0.0, sigma = 1.0;
double left = -1.0, right = 1.0;
double mean = 0.0, sigma = 1.0;
double left = -1.0, right = 1.0;
RNG& rng = ts->get_rng();
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));
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));
}
int CV_CountNonZeroTest::get_count_non_zero()
{
int result = 0;
int result = 0;
for (int i = 0; i < src.rows; ++i)
for (int j = 0; j < src.cols; ++j)
for (size_t i = 0; i < src.rows; ++i)
for (size_t j = 0; j < src.cols; ++j)
if (current_type == CV_8U) result += (src.at<uchar>(i, j) > 0);
if (current_type == CV_8U) result += (src.at<uchar>(i, j) > 0);
else if (current_type == CV_8S) result += abs(sign(src.at<char>(i, j)));
else if (current_type == CV_8S) result += abs(sign(src.at<char>(i, j)));
else if (current_type == CV_16U) result += (src.at<ushort>(i, j) > 0);
else if (current_type == CV_16U) result += (src.at<ushort>(i, j) > 0);
else if (current_type == CV_16S) result += abs(sign(src.at<short>(i, j)));
else if (current_type == CV_16S) result += abs(sign(src.at<short>(i, j)));
else if (current_type == CV_32S) result += abs(sign(src.at<int>(i, j)));
else if (current_type == CV_32S) result += abs(sign(src.at<int>(i, j)));
else if (current_type == CV_32F) result += (fabs(src.at<float>(i, j)) > eps_32);
else if (current_type == CV_32F) result += (fabs(src.at<float>(i, j)) > eps_32);
else result += (fabs(src.at<double>(i, j)) > eps_64);
else result += (fabs(src.at<double>(i, j)) > eps_64);
return result;
return result;
}
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;
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;
}
void CV_CountNonZeroTest::run(int)
{
const size_t N = 1500;
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)
{
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 3: {
int distribution = cv::randu<uchar>()%2;
generate_src_stat_data(Size(w, h), current_type, distribution);
int right = get_count_non_zero(), result = countNonZero(src);
if (right != result)
{
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;
}
default: break;
}
}
const size_t N = 1500;
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)
{
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 3: {
int distribution = cv::randu<uchar>()%2;
generate_src_stat_data(Size(w, h), current_type, distribution);
int right = get_count_non_zero(), result = countNonZero(src);
if (right != result)
{
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;
}
default: break;
}
}
}
// 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(); }
此差异已折叠。
#include "test_precomp.hpp"
#include <time.h>
#include <iostream>
#define IMGPROC_BOUNDINGRECT_ERROR_DIFF 1
......@@ -11,94 +10,93 @@ using namespace std;
class CV_BoundingRectTest: public cvtest::ArrayTest
{
public:
public:
CV_BoundingRectTest();
~CV_BoundingRectTest();
protected:
protected:
void run (int);
private:
template <class T> void generate_src_points(vector <Point_<T> >& src, int n);
template <class T> cv::Rect get_bounding_rect(const vector <Point_<T> > src);
template <class T> bool checking_function_work(vector <Point_<T> >& src, int type);
private:
template <typename T> void generate_src_points(vector <Point_<T> >& src, int n);
template <typename T> cv::Rect get_bounding_rect(const vector <Point_<T> > src);
template <typename T> bool checking_function_work(vector <Point_<T> >& src, int type);
};
CV_BoundingRectTest::CV_BoundingRectTest() {}
CV_BoundingRectTest::~CV_BoundingRectTest() {}
template <class T> void CV_BoundingRectTest::generate_src_points(vector <Point_<T> >& src, int n)
template <typename T> void CV_BoundingRectTest::generate_src_points(vector <Point_<T> >& src, int n)
{
src.clear();
for (size_t i = 0; i < n; ++i)
src.push_back(Point_<T>(cv::randu<T>(), cv::randu<T>()));
src.clear();
for (int i = 0; i < n; ++i)
src.push_back(Point_<T>(cv::randu<T>(), cv::randu<T>()));
}
template <class T> cv::Rect CV_BoundingRectTest::get_bounding_rect(const vector <Point_<T> > src)
template <typename T> cv::Rect CV_BoundingRectTest::get_bounding_rect(const vector <Point_<T> > src)
{
int n = src.size();
T min_w = std::numeric_limits<T>::max(), max_w = std::numeric_limits<T>::min();
T min_h = min_w, max_h = max_w;
for (size_t i = 0; i < n; ++i)
{
min_w = std::min<T>(src.at(i).x, min_w);
max_w = std::max<T>(src.at(i).x, max_w);
min_h = std::min<T>(src.at(i).y, min_h);
max_h = std::max<T>(src.at(i).y, max_h);
}
return Rect((int)min_w, (int)min_h, (int)(floor(1.0*(max_w-min_w)) + 1), (int)(floor(1.0*(max_h-min_h)) + 1));
int n = src.size();
T min_w = std::numeric_limits<T>::max(), max_w = std::numeric_limits<T>::min();
T min_h = min_w, max_h = max_w;
for (int i = 0; i < n; ++i)
{
min_w = std::min<T>(src.at(i).x, min_w);
max_w = std::max<T>(src.at(i).x, max_w);
min_h = std::min<T>(src.at(i).y, min_h);
max_h = std::max<T>(src.at(i).y, max_h);
}
return Rect((int)min_w, (int)min_h, (int)(floor(1.0*(max_w-min_w)) + 1), (int)(floor(1.0*(max_h-min_h)) + 1));
}
template <class T> bool CV_BoundingRectTest::checking_function_work(vector <Point_<T> >& src, int type)
template <typename T> bool CV_BoundingRectTest::checking_function_work(vector <Point_<T> >& src, int type)
{
const int MAX_COUNT_OF_POINTS = 1000;
const int N = 10000;
for (int k = 0; k < N; ++k)
{
RNG& rng = ts->get_rng();
int n = rng.next()%MAX_COUNT_OF_POINTS + 1;
generate_src_points <T> (src, n);
cv::Rect right = get_bounding_rect <T> (src);
cv::Rect rect[2] = { boundingRect(src), boundingRect(Mat(src)) };
for (int i = 0; i < 2; ++i) if (rect[i] != right)
{
cout << endl; cout << "Checking for the work of boundingRect function..." << endl;
cout << "Type of src points: ";
switch (type)
{
case 0: {cout << "INT"; break;}
case 1: {cout << "FLOAT"; break;}
case 2: {cout << "DOUBLE"; break;}
default: break;
}
cout << endl;
cout << "Src points are stored as "; if (i == 0) cout << "VECTOR" << endl; else cout << "MAT" << endl;
cout << "Number of points: " << n << endl;
cout << "Right rect (x, y, w, h): [" << right.x << ", " << right.y << ", " << right.width << ", " << right.height << "]" << endl;
cout << "Result rect (x, y, w, h): [" << rect[i].x << ", " << rect[i].y << ", " << rect[i].width << ", " << rect[i].height << "]" << endl;
cout << endl;
CV_Error(IMGPROC_BOUNDINGRECT_ERROR_DIFF, MESSAGE_ERROR_DIFF);
return false;
}
}
return true;
const int MAX_COUNT_OF_POINTS = 1000;
const int N = 10000;
for (int k = 0; k < N; ++k)
{
RNG& rng = ts->get_rng();
int n = rng.next()%MAX_COUNT_OF_POINTS + 1;
generate_src_points <T> (src, n);
cv::Rect right = get_bounding_rect <T> (src);
cv::Rect rect[2] = { boundingRect(src), boundingRect(Mat(src)) };
for (int i = 0; i < 2; ++i) if (rect[i] != right)
{
cout << endl; cout << "Checking for the work of boundingRect function..." << endl;
cout << "Type of src points: ";
switch (type)
{
case 0: {cout << "INT"; break;}
case 1: {cout << "FLOAT"; break;}
default: break;
}
cout << endl;
cout << "Src points are stored as "; if (i == 0) cout << "VECTOR" << endl; else cout << "MAT" << endl;
cout << "Number of points: " << n << endl;
cout << "Right rect (x, y, w, h): [" << right.x << ", " << right.y << ", " << right.width << ", " << right.height << "]" << endl;
cout << "Result rect (x, y, w, h): [" << rect[i].x << ", " << rect[i].y << ", " << rect[i].width << ", " << rect[i].height << "]" << endl;
cout << endl;
CV_Error(IMGPROC_BOUNDINGRECT_ERROR_DIFF, MESSAGE_ERROR_DIFF);
return false;
}
}
return true;
}
void CV_BoundingRectTest::run(int)
{
vector <Point> src_veci; if (!checking_function_work(src_veci, 0)) return;
vector <Point2f> src_vecf; checking_function_work(src_vecf, 1);
vector <Point> src_veci; if (!checking_function_work(src_veci, 0)) return;
vector <Point2f> src_vecf; checking_function_work(src_vecf, 1);
}
TEST (Imgproc_BoundingRect, accuracy) { CV_BoundingRectTest test; test.safe_run(); }
\ No newline at end of file
TEST (Imgproc_BoundingRect, accuracy) { CV_BoundingRectTest test; test.safe_run(); }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册